How to install Linux Nginx, MySQL, PHP (LEMP Stack) on Ubuntu 16.04 / 18.04 LTS / 19.10 / 20.04 LTS
LEMP is a variation of the ubiquitous LAMP stack used for developing and deploying web applications. Traditionally, LAMP consists of a Linux operating system , a Nginx (Pronounced as Engine-X) web server, MySQL database server, and PHP for dynamic data processing. Due to its modular nature, the components can easily be swapped out. With LEMP, Apache is replaced with the lightweight yet powerful Nginx.
Step 1: Installing the Stack
This guide demonstrates how to install a LEMP stack on an Ubuntu with individual packages. Let’s connect to the server via SSH. Simply open terminal & put below command. For windows, you can download PuTTY or WinSCP
ssh root@server_ip_address
Let’s begin by updating server’s package index
sudo apt-get update
Step 2: Installing NGINX
sudo apt-get install nginx
Once you have installed it, the Nginx service should start automatically and will be enabled to start at boot time, you can check if it’s up and running. You can also check http://ip_address in your web browser which means your installation is working or not!
sudo systemctl status nginx
Step 3: Installing PHP
PHP is a popular server side scripting language. You can install PHP, PHP-FPM and other modules.
To check the Ubuntu version, use the following command in terminal
lsb_release -a
Install PHP 7.4 on Ubuntu 20.04
sudo apt-get install php-fpm php-mysql php-mbstring
Confirm PHP version
php -v
Install PHP 7.4 on Ubuntu 18.04/19.04/16.04
We need to add Ondrej Sury’s repository which has the latest build packages of PHP
sudo apt -y install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
Now we are ready to install php7.4 with other extensions
sudo apt-get install php7.4 php7.4-fpm php7.4-mysql php7.4-mbstring
For nginx configuration, you need to stop and disable Apache service
sudo systemctl disable — now apache2
Confirm PHP version
php -v
For install all important php7.4 extensions [ Good to know ]
sudo apt-get install php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl
Step 4: Configuring PHP in NGINX
PHP-FPM is to serve & process PHP scripts or PHP based web applications in nginx. Setup nginx server to serve PHP.
sudo rm -rf /etc/nginx/sites-available/default
sudo rm -rf /etc/nginx/sites-enabled/default
Now deploy below command to write config file
sudo nano /etc/nginx/sites-available/myphpconfig
Paste the below code in config file
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
#Or write the above line as server_name [server_IP][domain_name];
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# I am using php7.4-fpm, you can use other versions too
}
location ~ /\.ht {
deny all;
}
}
Enable the file by linking to the sites-enabled dir
sudo ln -s /etc/nginx/sites-available/myphpconfig /etc/nginx/sites-enabled
Test nginx configuration
sudo nginx -t
If testing is okay then restart the nginx server
sudo systemctl restart nginx
Now you can write or git clone any php script in cd /var/www/html directory.
sudo nano /var/www/html/info.php<?php
phpinfo();
Step 5: Installing MySQL
sudo apt-get install mysql-server
Create a Database. Drive into mysql
mysql -u root -p
Press enter as there is no password for root now. Create a DB names dbOne
create Database dbOne
To check authentication method of MySQL user accounts use with the following command:
SELECT user, authentication_string, plugin, host FROM mysql.user;
Output be like -
As we can see, there’s no auth password for root account. To configure the root account to authenticate with a password, run the following command
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'set_your_password_here';
Then, run FLUSH PRIVILEGES;
FLUSH PRIVILEGES;
Check the authentication method again
SELECT user, authentication_string, plugin, host FROM mysql.user;
root MySQL account has now authenticates using a password. You can exit the MySQL shell by writing exit;
exit;
Step 6: Installing PHPMyAdmin
sudo apt-get install phpmyadmin
Now setting PHPMyAdmin for nginx
sudo ln -s /usr/share/phpmyadmin /var/www/html
Restart the nginx server
sudo systemctl restart nginx
Now phpmyadmin is available on your server. You can check on domain_name/phpmyadmin or ip/phpmyadmin
All packages are installed & configured successfully.
A LEMP stack is a very popular & powerful platform that will help you to process & serve any script or application from your server.
Have fun 🔥 💪
If you have any query, please put in below. Find me on Github.