LEMP is a popular stack of software that is commonly used for hosting web applications. It stands for Linux, Nginx, MySQL, and PHP. In this article, we will guide you through the process of installing LEMP on Ubuntu 20.04.
Before we begin, make sure that you have a fresh installation of Ubuntu 20.04. You will also need to be logged in as a user with sudo privileges.
Install Nginx
The first step is to install Nginx, the web server that will host our web application. To do this, open a terminal and run the following command:
sudo apt-get update
sudo apt-get install nginx
This will download and install Nginx on your system. Once the installation is complete, you can verify that Nginx is running by visiting http://localhost
in your web browser. You should see the default Nginx web page.
Install MySQL
Next, we need to install MySQL, the database server that will store our web application’s data. To do this, run the following command in the terminal:
sudo apt-get install mysql-server
During the installation, you will be prompted to set a password for the MySQL root
user. Make sure to choose a strong password and remember it, as you will need it later.
Once the installation is complete, run the following command to secure your MySQL installation:
sudo mysql_secure_installation
You will be asked to enter the password for the MySQL root user that you set up earlier. After that, you will be presented with a series of questions. It is recommended to answer "yes"
to all of them, as this will enable some additional security features.
Install PHP
The last piece of the LEMP stack is PHP, the scripting language that will be used by our web application. To install PHP, run the following command in the terminal:
sudo apt-get install php-fpm php-mysql
This will install PHP and the necessary modules for it to work with Nginx and MySQL.
Configure Nginx to work with PHP
Next, we need to configure Nginx to work with PHP. To do this, open the Nginx configuration file using the following command:
sudo nano /etc/nginx/nginx.conf
In the file, find the line that says "location /"
and replace it with the following code:
location / {
try_files $uri $uri/ =404;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
This code tells Nginx to pass PHP requests to the PHP-FPM (FastCGI Process Manager) service, which will handle them. Save the file and exit the editor.
Test your LEMP stack
To verify that your LEMP stack is working properly, you can create a test PHP file. To do this, open a terminal and run the following commands:
sudo nano /var/www/html/info.php
This will open the Nano text editor and create a new file called info.php
in the Nginx web root directory