How to Install LEMP Stack on Ubuntu 20.04
9 min readThis article describes about How to Install LEMP Stack on Ubuntu 20.04. A LEMP Stack is a combination of open source software. This combination has Ubuntu 20.04 Linux operating system, stands for (L). Nginx (pronounced as Engine-X) Web Server, stands for (E). MySQL or MariaDB Database Server, stands from (M). And finally PHP a server side scripting language which is used for coding of dynamic websites, stands for (P). The LEMP stack is one of the most popular tools among the developers.
Link to the Official Website of LEMP Stack |
Nginx Web Server is one of the most powerful, high performance and open source web server. It is mostly deployed to host high traffic websites. It has the ability to handle many thousands of concurrent requests simultaneously. As far as database server is concerned, we have the choice between MySQL and MariaDB database Server. Here, we will choose MySQL database server for this tutorial. The final part of the software stack is PHP. PHP is a server side scripting language.
Prerequisites – For LEMP Stack Installation
- To install the LEMP Server on Ubuntu 20.04, firstly, we will need a machine with Ubuntu 20.04 Server / Desktop System installed on it.
- We will also need a system root user and password for performing administrative task on the server.
- For Installation of Ubuntu 20.04 Desktop / Server Guide you can follow our tutorial:
How to Install Ubuntu 20.04 - An active firewall should be there on the Ubuntu 20.04 Server / Desktop.
- A stable and fast Interment connection will be needed for downloading and installation of rest of the software and its dependencies.
Installation of Nginx Web Server
Update the Ubuntu 20.04 Desktop / Server System
To update the Ubuntu 20.04, open the terminal window and issue the following command as shown below:
sudo apt update
Installation of Nginx Web Server on Ubuntu 20.04
To display the web pages of our websites we will deploy Nginx Web Server, a high performance Web Server. It is mostly deployed to host high traffic websites. It has the ability to handle many thousands of concurrent requests simultaneously.
After the completion of the server update process. Start the Nginx Web Server Installation by issuing the following command in the terminal window:
sudo apt install nginx
This will start installing Nginx web Server. The other required libraries and dependencies will be downloaded and installed automatically along with the server installation process. After the installation of the web server, the Nginx server automatically starts the web service.
Verify the status of Nginx
To verify and check the status of the newly installed web server we have to issue the following command in the terminal windows.
sudo systemctl status nginx
As the web service is automatically started after the installation, Nginx web server should be up and running, the output of the above command should look like as shown below.
systemctl status nginx nginx.service – A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset:> Active: active (running) since Wed 2021-07-28 23:18:13 IST; 11min ago Docs: man:nginx(8) Main PID: 2305 (nginx) Tasks: 2 (limit: 4652) Memory: 3.2M CGroup: /system.slice/nginx.service ├─2305 nginx: master process /usr/sbin/nginx -g daemon on; master_> └─2306 nginx: worker process ……. |
Furthermore, we may like to configure Nginx web server to start automatically on OS boot. To achieve this, we must enable it by issuing the command in the terminal window as shown below.
sudo systemctl start nginx
sudo systemctl enable nginx
Furthermore, if ufw firewall is enabled, we have to allow connections to Nginx Web Server. At the time of installation Nginx registers various UFW application profiles. To check the enabled UFW profiles, issue the following command in the terminal window:
sudo ufw app list
The Output of the above command will be like the given below:
Available applications: CUPS Nginx Full Nginx HTTP Nginx HTTPS OpenSSH |
Keeping in mind the security of the server, it is recommended to enable the most restrictive profile which will allow the traffic we require. Because we have not configured SSL for our server, it is required to allow only normal HTTP traffic on port 80.
To enable this traffic in UFW issue the following command in the terminal windows as given below:
sudo ufw allow 'Nginx HTTP'
The output of the above command will be similar to given below output:
Rules updated Rules updated (v6) |
To verify the changes made, issue the following command in the terminal windows:
sudo ufw status
The output of the issued command will be similar to the output given below:
Status: active To Action From — —— —- Nginx HTTP ALLOW Anywhere 22/tcp ALLOW Anywhere Nginx HTTP (v6) ALLOW Anywhere (v6) 22/tcp (v6) ALLOW Anywhere (v6) |
Now to check and make sure whether the web server is running open the browser and type the server IP in the browser address bar or just type localhost in the browser address bar.
http://Server –IP or http://localhost
The output will be like the shown below.
Installation of MySQL on Ubuntu 20.04
Now we have our Nginx Web Server up and running. To be able to store and manage databases, we now require installing MySQL Database Server and Client. MySQL Database Management application is one of the most popular applications. It is mostly used with PHP as the server side scripting language.
By default MySQL Database Server and Client package is a part of Ubuntu package repository. Therefore MySQL can be directly installed with apt command of Ubuntu 20.04.
To install MySQL Server and Client on Ubuntu 20.04 issue the following command in the terminal window:
sudo apt install mysql-server mysql-client
The apt installers will prompt us to confirm the installation and on selecting ‘Y’ the installation will start. The necessary library files and dependencies will be automatically installed along with the MySQL installation.
Now enable and start the database server. To do this issue the following commands in the terminal window:
sudo systemctl start mysql
sudo systemctl enable mysql
After the installation of MySQL Database Server and Client is complete. Now, execute the security script which is preinstalled with MySQL. This security script enables us to assign new root password for login to database server, removes sample databases, guest users and all insecure default settings of the database server. This will secure our database server from unauthorised access.
To secure our database server issue the following command in the terminal windows and answer the prompts as given below:
sudo mysql_secure_installation
The output of the command will be similar to the given below.
Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: N |
Change the Password Authentication Method in MySQL Database
By default MySQL use the UNIX socket plugin to authenticate users. Now we will set the password authentication method in MySQL Server to native. Later when we will install phpMyAdmin, this will enable us to connect as root user. Login in to MySQL Server and issue following commands step by step.
sudo mysql -u root –p [Run the Command]
Enter password: [Enter root User Password] |
The output of the above command will be similar to shown below and we will get the MySQL command prompt.
mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement. |
mysql> update mysql.user set plugin = 'mysql_native_password' where User = 'root';
Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 |
mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec) |
mysql> quit
Bye |
Now, installation is complete. Quit the MySQL Server Command prompt.
Test the Newly Installed MySQL Database Server. Now test the MySQL installation. Run the following command.
sudo systemctl status mysql
The output of the command will be like given below.
mysql.service – MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2021-07-29 12:12:12 IST; 54min ago Main PID: 3259 (mysqld) Status: “Server is operational” Tasks: 38 (limit: 4652) Memory: 357.4M CGroup: /system.slice/mysql.service └─3259 /usr/sbin/mysqld …………….. |
How to Start, Stop and Restart MySQL Database Server
To start, stop and restart MySQL Database Server run the following command in the terminal as root user.
sudo systemctl start mysql sudo systemctl stop mysql sudo systemctl restart mysql
That it, we have successfully installed and configured the MySQL Database Server and Client on Ubuntu 20.04.
Installation of PHP on Ubuntu 20.04
We have installed Nginx Web Server to serve our content to our website users. To store and manage our data we will use MySQL Database Server. Now, to generate dynamic content for our web site we will be using PHP, a server side scripting language. To serve our website using Nginx, we will require php-fpm, which is an abbreviation for PHP FastCGI process manager. We will also need php-mysql, a php module that enables PHP to communicate with MySQL Database Server. All the supporting libraries and dependencies will be automatically installed with PHP installation. The PHP core packages will also be installed automatically.
To install php-fpm and php-mysql packages on Ubuntu 20.04, issue the following command in the terminal window as shown below:
sudo apt install php-fpm php-mysql
After the installation is complete the PHP-FPM should run automatically. Check and verify this issue the following command in the terminal window as shown below:
sudo systemctl status php7.4-fpm
The output of the above command will be similar to the given below:
php7.4-fpm.service – The PHP 7.4 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2021-07-29 14:35:16 IST; 2min 58s ago Docs: man:php-fpm7.4(8) Process: 12027 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74 (code=e> Main PID: 12024 (php-fpm7.4) Status: “Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec” Tasks: 3 (limit: 4652) Memory: 7.1M CGroup: /system.slice/php7.4-fpm.service ├─12024 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf) ├─12025 php-fpm: pool www └─12026 php-fpm: pool www ………………….. |
If the PHP process is not running we can start and enable it by issuing the following command:
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
Configuration of Nginx Web Server to work with PHP
The settings of Nginx Web Server are configured by default to work with Apache Web Server. Some tweaks will be needed so that Nginx can serve PHP content.
Open the default virtual host files in your favorite text editor. We are using nano text editor.
sudo nano /etc/nginx/sites-available/default
Now find the line that beginning with the word – ‘index’
Now insert – ‘index.php’ before – ‘index.html’
Also append the following lines:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
After modifications, finally the modified part of file should look like this.
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
Now, this is the time to test our Nginx configuration and ensure that everything is in order as shown below:
sudo nginx -t
If everything is ok, we should get the output as shown below.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful |
Now we have to restart the Nginx Web Server for the changes to take effect. Issue the following command in the terminal window to do this:
sudo systemctl restart nginx
Now again, verify if Nginx Web Server is working with PHP. To test our Nginx webserver whether it can serve PHP content or not, we will create an phpinfo.php file in the /var/www/html folder also know as web root folder as shown below:
sudo nano /var/www/html/phpinfo.php
Now add the following content in the file and save and exit the editor.
<?php
phpinfo();
?>
Now open your favorite browser and type the following line in the address bar of the browser as shown below:
http://server-ip/phpinfo.php or https://pentarock.in/phpinfo.php
If everything is ok, we should get the output as shown below:
Our Nginx web server is ready and now it can serve PHP files.
Our LEMP stack is also ready to host any dynamic website.