How To Install LEMP Stack on Ubuntu 18.04
LEMP Stack consists of Nginx web server, MariaDB / MySQL database, and PHP language, running on top of Linux operating system. It is the most widely used tech stack after LAMP Stack for hosting websites, blogs, etc.. used by small to enterprise organizations.
Nginx web server is a free, open-source, high-performance web server and is known for its stability, scalability, simple configuration, and low resource consumption.
LEMP is the acronym of
L – Linux Operating System
E – Nginx Web Server
M – MariaDB / MySQL Server
P – PHP language
Here, we will see the steps to install LEMP Stack on Ubuntu 18.04.
Install Linux
You can follow the Step by Step guide to install Ubuntu 18.04.
Once you have Ubuntu 18.04 (Linux) ready, proceed to install Nginx, MariaDB database, and PHP on it.
Install Nginx
Download the Nginx repository public signing key from the official website.
wget http://nginx.org/keys/nginx_signing.key
Add the Nginx public signing key to your system.
sudo apt-key add nginx_signing.key
Add Nginx repository to your system.
echo "deb [arch=amd64] http://nginx.org/packages/ubuntu/ bionic nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
echo "deb-src [arch=amd64] http://nginx.org/packages/ubuntu/ bionic nginx" | sudo tee -a /etc/apt/sources.list.d/nginx.list
Now, install Nginx package using the apt command.
sudo apt update
sudo apt install -y nginx
Start the Nginx web service after the installation.
sudo systemctl start nginx
Test Nginx Web Server
To make sure Nginx web server installed correctly, we will now test it to see if it is working correctly. Open up a web browser and then go to the following web address.
OR
You should see Nginx’s default page. This page confirms that the Nginx is successfully installed on the server.
Nginx’s default document root (where we place html or php files) is /usr/share/nginx/html/
and the configuration files can be found under /etc/nginx/
directory.
Install MariaDB Server
Update the system repository index.
sudo apt update
Install the MariaDB server and the client by issuing the following command.
sudo apt install -y mariadb-server mariadb-client
MariaDB server should now be up and running. If not, start MariaDB server manually using the following command.
sudo systemctl start mariadb
Next, secure MariaDB server using the mysql_secure_installation
command.
mysql_secure_installation
In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, 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): <== Just Press Enter OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.
Set root password? [Y/n] Y <== Set Root Password New password: <== Enter New Root Password Re-enter new password: <== Re Enter Root password Password updated successfully! Reloading privilege tables.. … Success!
By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB 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] Y <== Remove Anonymous User … 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] Y <== Disable remote root login … Success!
By default, MariaDB 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] Y <== Remove test database
- Dropping test database… … Success!
- 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] Y <== Reload Privilege … Success!
Cleaning up…
All done! If you've completed all of the above steps, your MariaDB installation should now be secure.
Thanks for using MariaDB!
Install PHP7-FPM
Install PHP-FPM (PHP-FastCGI Process Manager), an alternative PHP FastCGI daemon for PHP. It can handle high loads of requests by maintaining a pool of PHP worker processes.
Install php-fpm by running the following command.
sudo apt install -y php-fpm php-mysql php-cli
Edit php.ini file according to the installed PHP version.
sudo nano /etc/php/7.2/fpm/php.ini
set cgi.fix_pathinfo
value to 0
.
cgi.fix_pathinfo=0
By default, PHP-FPM listens on the socket. But, here, we will configure PHP-FPM use a TCP connection.
Open the file configuration file and change the parameter listen.
sudo nano /etc/php/7.2/fpm/pool.d/www.conf
FROM:
listen = /run/php/php7.2-fpm.sock
TO:
listen = 127.0.0.1:9000
Restart PHP-FPM service.
sudo systemctl restart php7.2-fpm
Create Virtual Host with PHP-FPM Support
Let’s create a name-based virtual host to test the PHP-FPM support for the following details.
Domain Name : web.linuxbees.local
Document Root : /usr/share/nginx/html/web.linuxbees.local
Create a virtual host configuration file for the domain.
sudo nano /etc/nginx/conf.d/web.linuxbees.local.conf
Add the following content.
location / { index index.html index.htm index.php; }
location ~ .php$ { include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/web.linuxbees.local$fastcgi_script_name; } }
Create the document root directory for the virtual host.
sudo mkdir /usr/share/nginx/html/web.linuxbees.local
Place a PHP file onto the document root to test the created virtual host.
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/web.linuxbees.local/index.php
Update the permission of the file.
sudo chown -R www-data:www-data /usr/share/nginx/html/web.linuxbees.local/
Restart the services.
sudo systemctl restart nginx
sudo systemctl restart php7.2-fpm
sudo systemctl enable php7.2-fpm
Test PHP-FPM support on the Virtual Host
Create a host entry for the domain (web.linuxbees.local) in the /etc/hosts
file or make an A record on DNS server for name resolution.
sudo nano /etc/hosts
A host will look something like below:
192.168.1.10 web.linuxbees.local web
Open a web browser and go to the website.
The page will look like below:
From the above screenshot, the PHP is working through FPM/FastCGI shown in the Server API line.
You can also check the support of MariaDB by scrolling the page further down.
Conclusion
You have successfully installed LEMP Stack on Ubuntu 18.04. Consider installing phpMyAdmin on Ubuntu 18.04 to manage MariaDB database over a web browser. Also, you can install Let’s Encrypt SSL certificate in Nginx on Ubuntu 18.04 for secure communication.