The LEMP stack configuration described in this guide offers an alternative to the popular LAMP Stack, which powers many well-known web applications. LAMP includes a Linux-based operating system, the Apache web server, the MySQL database server, and the PHP programming language. In contrast, the LEMP stack replaces Apache with Nginx (pronounced "engine x"), which accounts for the "E" in LEMP, providing better scalability for handling server demand.
Prerequisites
Install Ubuntu 16.04 in the Client Area:
Log in to your VPS using SSH.
Since Ubuntu 16.04 includes Apache2 by default, you’ll first need to remove it before proceeding with the LEMP installation.
To remove Apache2 and its utilities, run:
apt-get remove --purge apache2 apache2-utils
Next, delete the Apache2 directory:
rm -rf /etc/apache2
Now, you’re ready to install the LEMP software stack.
Installing Nginx
The necessary software can be installed directly from Ubuntu's default package repositories. Begin by updating your local package index:
apt-get update
Next, install Nginx by typing:
apt-get install nginx
After installation, remove the default Apache page. Navigate to the /var/www/html
directory and execute:
mv index.nginx-debian.html index.html
Now, open your web browser and type one of your server’s addresses. You should see the Nginx default landing page:
http://server_domain_or_IP
Installing MySQL
Now, let’s install MySQL, which will serve as the database for storing and managing information. To install MySQL, use the following command:
apt-get install mysql-server
You will be prompted to create a root (administrator) password for the MySQL system.
Even though MySQL is now installed, further configuration is required. To secure the installation, run the MySQL security script:
mysql_secure_installation
You’ll need to enter the root password you just set. Afterward, when asked whether to change the password, type "n" if you’re happy with the current one.
For the remaining prompts, simply hit "ENTER" to accept the default values. This will remove unnecessary users and databases, disable remote root logins, and apply these new settings immediately.
At this point, your database system is configured, and you’re ready to move on.
Installing PHP
PHP will allow you to serve dynamic web pages to your visitors. Start by installing the necessary components to enable PHP to communicate with both Nginx and MySQL:
apt-get install php-fpm php-mysql
Now that PHP is installed, you need to modify the configuration to enhance security. First, install a text editor:
apt install nano
Next, open the main php-fpm
configuration file for editing:
nano /etc/php/7.0/fpm/php.ini
Look for the line starting with ;cgi.fix_pathinfo
. In nano, you can search by pressing Ctrl+w
and typing cgi.fix_pathinfo
. You’ll need to uncomment this line by removing the semicolon and changing the setting to 0
. The result should look like this:
cgi.fix_pathinfo=0
Save and close the file. Then, restart the PHP processor to apply the change:
systemctl restart php7.0-fpm
Configuring Nginx to Use PHP
Nginx is configured by default to serve HTML files, so we need to modify its configuration to serve PHP files instead.
Open the default Nginx server block configuration file:
nano /etc/nginx/sites-available/default
Find the line that begins with index index.html index.htm index.nginx-debian.html;
. Modify it to include index.php
, so it looks like this:
index index.php index.html index.htm index.nginx-debian.html;
Next, uncomment the block of code that handles PHP processing by removing the #
symbols:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
Finally, restrict access to .htaccess
files, which Nginx doesn’t use. Change the following block of code by removing the #
symbols:
location ~ /\.ht {
deny all;
}
Save and close the file, then test your Nginx configuration:
nginx -t
If there are no errors, restart Nginx to apply the new settings:
systemctl restart nginx
Testing PHP
To test if PHP is working correctly, create a simple PHP script in the /var/www/html
directory. Create a new file called phpinfo.php
:
nano /var/www/html/phpinfo.php
Add the following line to the file:
<?php phpinfo(); ?>
Save and exit the file (Ctrl + O
, Ctrl + X
).
You can now test the script by visiting the following URL in your web browser:
http://ip_address/phpinfo.php
This page will display detailed information about your server’s PHP configuration, helping you verify that everything is working correctly.
At this stage, the installation is complete, and you can begin testing your webpages and scripts.