How to Install LEMP Stack (Linux, Nginx, MySQL, PHP) on Ubuntu: A Comprehensive Guide.
The LEMP stack is a popular combination of software used to host web applications. LEMP stands for Linux, Nginx (pronounced “engine X”), MySQL, and PHP. In this guide, we will walk through the process of installing the LEMP stack on Ubuntu.
Before proceeding with the installation, make sure you have a non-root user with sudo privileges on your Ubuntu 20.04 system. If you don’t have one, create a new user and add it to the sudo group.
Step 1: Update the Package Index and Upgrade the System
First, we need to update the package index and upgrade the system using the following command:
sudo apt update
sudo apt upgrade
This could take some time, so be patient!
Step 2: Install Nginx
Next, we will install Nginx using the following command:
sudo apt install nginx
After installation, you can start Nginx using the following command:
sudo systemctl start nginx
To check the status of Nginx, run:
sudo systemctl status nginx
You can check also in browser using the IP address of your Ubuntu machine.
Step 3: Install MySQL
We will now install MySQL to store and manage our web application data. We can install MySQL using the following command:
sudo apt install mysql-server
After installation, you can start MySQL using the following command:
sudo systemctl start mysql
To check the status of MySQL, run:
sudo systemctl status mysql
To secure the MySQL installation, run:
sudo mysql_secure_installation
This command will prompt you to set the root password, remove anonymous users, disallow remote root login, and remove test databases.
If you face the following error:
… Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.
you have to:
- Open the terminal application.
- Terminate the
mysql_secure_installation
from another terminal using thekillall command
:sudo killall -9 mysql_secure_installation
- Start the mysql client:
sudo mysql
- Run the following SQL query:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'SetRootPasswordHere';
exit
- Then run the following command to secure it:
sudo mysql_secure_installation
- When promoted for the password enter the
SetRootPasswordHere
(or whatever you set when you ran the above SQL query)
- That is all.
Step 4: Install PHP
Next, we need to install PHP to enable dynamic content on our web pages. We can install PHP and its extensions using the following command:
sudo apt install php-fpm php-mysql
After installation, we need to configure PHP to work with Nginx. We can do this by editing the /etc/php/7.4/fpm/php.ini file and modifying the following setting:
cgi.fix_pathinfo=0
Next, we need to create a new PHP configuration file in the Nginx configuration directory:
sudo nano /etc/nginx/sites-available/example.com
Replace “example.com” with your own domain name.
Copy and paste the following configuration code:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Save and close the file.
Next, we need to enable the PHP configuration file and disable the default Nginx configuration file using the following commands:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo unlink /etc/nginx/sites-enabled/default
Finally, we need to restart Nginx and PHP-FPM using the following commands:
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm
Step 5: Test the LEMP Stack
To test your LEMP stack, create a new PHP file called test.php
in /var/www/example.com/html
<?php
phpinfo();
and check it in the browser – http://example.com/test.php