Step-by-Step Guide on Installing Apache/Nginx, MySQL/MariaDB, and PHP
If you’re looking to set up a web server on Ubuntu, you're in the right place! This post will guide you through the process of setting up a web server using the LAMP (Linux, Apache, MySQL, PHP) and LEMP (Linux, Nginx, MySQL, PHP) stacks. Whether you prefer Apache or Nginx as your web server, we'll cover both options in detail.
Let’s break it down into two main sections: LAMP and LEMP. But first, a brief overview of these stacks:
What is a LAMP Stack?
The LAMP stack is one of the most widely used open-source software stacks for web development. It consists of:
- Linux: The operating system (Ubuntu, in this case).
- Apache: The web server that serves web pages to clients.
- MySQL or MariaDB: The database system used to store website data.
- PHP: The programming language used for dynamic web pages and backend code.
What is a LEMP Stack?
The LEMP stack is similar to LAMP but uses Nginx as the web server instead of Apache. Nginx is lightweight and performs better under heavy load compared to Apache.
- Linux: The operating system (Ubuntu).
- Nginx: The web server.
- MySQL or MariaDB: The database system.
- PHP: The programming language.
1. Installing a LAMP Stack on Ubuntu
Step 1: Update the Package Index
Before installing the LAMP stack, update your package index to make sure you have the latest package listings.
sudo apt update
Step 2: Install Apache
Apache is the first component of the LAMP stack. It serves the web pages to users. Install Apache with the following command:
sudo apt install apache2
Step 3: Verify Apache Installation
Once Apache is installed, check that it's running by typing the following command:
sudo systemctl status apache2
You should see that the Apache service is running. You can also check if Apache is working by opening your browser and typing the server’s IP address (or localhost if you’re working locally). You should see the Apache default page.
Step 4: Install MySQL/MariaDB
Next, we’ll install MySQL (or MariaDB, a popular MySQL fork). This is the database management system that will store your website's data.
sudo apt install mysql-server
After installation, run the security script to secure your MySQL installation:
sudo mysql_secure_installation
This script will walk you through several questions like setting the root password, disabling remote root access, and removing test databases.
Step 5: Install PHP
PHP is the server-side language used for dynamic web pages and server-side scripting. Install PHP by running:
sudo apt install php libapache2-mod-php php-mysql
Once PHP is installed, we need to ensure that Apache serves PHP files by default. To do that, we need to edit the Apache configuration file.
sudo nano /etc/apache2/mods-enabled/dir.conf
In this file, look for the line that starts with DirectoryIndex and move index.php to the front of the line. It should look like this:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
Save the file and restart Apache:
sudo systemctl restart apache2
Step 6: Test PHP
To verify that PHP is working correctly, create a test PHP file in the Apache document root.
sudo nano /var/www/html/info.php
Add the following content:
<?php
phpinfo();
?>
Save the file, then navigate to http://your-server-ip/info.php in your browser. You should see the PHP information page.
Conclusion for LAMP Installation
Congratulations! You’ve successfully set up a LAMP stack on your Ubuntu server. Apache, MySQL, and PHP are now working together to serve dynamic websites.
2. Installing a LEMP Stack on Ubuntu
Step 1: Update the Package Index
Just like with LAMP, start by updating the package index:
sudo apt update
Step 2: Install Nginx
Nginx is the web server in the LEMP stack. Install Nginx using the following command:
sudo apt install nginx
Once installed, check the status of Nginx to ensure it's running:
sudo systemctl status nginx
To verify, open your browser and type your server’s IP address. You should see the Nginx default welcome page.
Step 3: Install MySQL/MariaDB
For the database, you can either install MySQL or MariaDB. The command is the same as in the LAMP installation:
sudo apt install mysql-server
Run the security script to secure the MySQL installation:
sudo mysql_secure_installation
Step 4: Install PHP
Install PHP and the necessary extensions for Nginx:
sudo apt install php-fpm php-mysql
Since Nginx doesn’t use mod_php like Apache, we need to configure Nginx to use php-fpm. Open the Nginx configuration file for your site (the default is default):
sudo nano /etc/nginx/sites-available/default
Look for the location block for PHP files, and make sure it looks like this:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
}
Save the file and restart Nginx:
sudo systemctl restart nginx
Step 5: Test PHP
Just like in the LAMP setup, create a info.php file to test PHP:
sudo nano /var/www/html/info.php
Add the following content:
<?php
phpinfo();
?>
Navigate to http://your-server-ip/info.php to confirm that PHP is working.
Conclusion for LEMP Installation
Well done! You’ve now set up a LEMP stack on Ubuntu with Nginx, MySQL, and PHP. This configuration is great for handling high-traffic websites.
Final Thoughts
Whether you choose to install the LAMP or LEMP stack, you’ve taken the first major step in setting up a web server on Ubuntu. Both stacks offer flexibility and power for hosting dynamic websites, applications, or even REST APIs. Depending on your project needs and performance requirements, either stack will work great.
You’re now ready to deploy web applications on your Ubuntu server!