Table of Contents
Verified and Tested 06/13/21
Introduction
This how-to will show you how to install LEMP on a Ubuntu 20.04 cloud server. LEMP is a web service stack that consists of a Linux operating system, Nginx (pronounced “engine-x”), MySQL, and PHP. The main difference between LAMP and LEMP is that LAMP uses Apache, and LEMP uses Nginx. LEMP has been gaining popularity in the last few years because it excels in speed and scalability.
NGINX Car by Walker Cahall
Installing LEMP
First, we want to make sure that your server is up to date by running the commands:
sudo apt update sudo apt upgrade
Note: Depending on your installation you may need to remove apache2. You can do that by running the commands:
sudo apt remove apache2*
sudo apt autoremove
Installing Nginx
To install Nginx, use the command:
sudo apt install nginx
When it asks “Do you want to continue?”, hit Enter.
Start the Nginx service with the following command:
sudo systemctl start nginx
We can now test Nginx by going to your hostname or IP address in your browser’s address bar.
In our example, 192.168.0.192 is the IP address. So in our browser, we would go to http://192.168.0.192
.
You should see a web page that looks like the image below.
This example is the default nginx web page on Ubuntu 20.04
Now that Nginx is installed, we can move on to installing MySQL.
Installing MySQL
Install MySQL with the command:
sudo apt install mysql-server
When it asks “Do you want to continue?”, hit Enter to finish the installation.
Now that MySQL is installed we need to do the MySQL secure installation by running the command:
sudo mysql_secure_installation
Enter your MySQL root password. When it asks “Change the root password?”, type “N” followed by Enter. The rest of the questions are up to you. For standard installations, you can hit Enter for the defaults. It will look similar to the code box below.
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MySQL to secure it, we'll need the current password for the root user. If you've just installed MySQL, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MySQL root user without the proper authorisation. You already have a root password set, so you can safely answer 'n'. Change the root password? [Y/n] n ... skipping. By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] ... Success! By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] - Dropping test database... ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist ... Failed! Not critical, keep moving... - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] ... Success! All done! If you've completed all of the above steps, your MySQL installation should now be secure. Thanks for using MySQL!
Now that MySQL is installed, we can now install PHP.
Installing PHP
Install PHP with the following command:
sudo apt install php php-fpm php-mysql
When it asks “Do you want to continue?”, hit Enter.
For Nginx to work with PHP correctly, we need to edit an Nginx configuration file. In this how-to, we are going to use a simple Nginx config file.
First, we need to move the original configuration file to a new file name. Run the command:
sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old
Using a text editor of your choice, we are going to make a file called default in /etc/nginx/sites-available. For nano use the command:
sudo nano /etc/nginx/sites-available/default
Copy the following into your text editor:
server { listen 80; server_name your_site_name.com; root /usr/share/nginx/html; index index.php index.html; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/html; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
In nano, to exit and save, hit Ctrl+x
, type “y”, and then Enter.
Since we made changes to the configuration file, we need to restart Nginx, by running the command:
sudo systemctl restart nginx
We are now going to make a simple PHP page to test.
Using a text editor of your choice, we are going to create a file called info.php
in /usr/share/nginx/html/
.
sudo nano /usr/share/nginx/html/info.php
Copy the following into your text editor.
<?php phpinfo(); ?>
In your browser, you can go to http://Your-Hostname/info.php
or http://Your-IP-Address/info.php
. As above, in this example, we would use http://192.168.0.192/info.php
.
You should see a web page similar to the one below.
Once you are done testing, it is a good idea to remove the info.php
file, since it may give a potential attacker information that can be used to craft a specific attack against your server. To do that run the command:
sudo rm /usr/share/nginx/html/info.php
Congratulations, you have installed LEMP on Ubuntu 20.04. Thank you for following this how-to. Please check back for more updates.