How To Install Linux, Nginx, MySQL, PHP (LEMP Stack) in Ubuntu 18.04

0

ADVERTISEMENT

LEMP stack stands for Linux, Nginx, MariaDB, and PHP stack and is widely used for hosting websites/blogs.

Here, we will see how to install LEMP Stack on Ubuntu 18.04 with PHP support (via PHP-FPM) and database (MariaDB) support.

THIS DOCUMENT IS ALSO AVAILABLE FOR

Install LEMP Stack

Install Linux

Here is the tutorial on Step by Step installation of Ubuntu 18.04 and Upgrading Ubuntu 16.04 & Ubuntu 17.10 to Ubuntu 18.04.

Proceed to the installation of EMP (Nginx version 1.17.2, PHP version 7.2, MariaDB version 10.1.40) on Ubuntu 18.04.

Install Nginx

Nginx is a free, open-source, high-performance HTTP web server and is known for its stability, simple configuration, and low resource consumption.

Update repository index.

sudo apt update

Install below packages.

sudo apt install -y wget gnupg2 ca-certificates

Download the Nginx repository signing key from the official website.

wget http://nginx.org/keys/nginx_signing.key

Add the Nginx public key into the system.

sudo apt-key add nginx_signing.key

Add Nginx repository to your system.

echo "deb [arch=amd64] http://nginx.org/packages/mainline/ubuntu bionic nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

Install Nginx package using the following command.

sudo apt update

sudo apt install -y nginx

Start the Nginx service after the installation.

sudo systemctl start nginx

Open up a web browser and visit the below URL.

http://your-ip-add-ress

You should see the Nginx default page, and this confirms you that the Nginx is successfully installed on the server.

Nginx's Default Page
Nginx’s Default Page

Nginx’s default document root on Ubuntu 18.04 is /usr/share/nginx/html/ and the configuration files are found under /etc/nginx/ directory.

ADVERTISEMENT

Auto-start Nginx at system startup.

sudo systemctl enable nginx

Install MariaDB Server

Install the MariaDB server by issuing the following command.

By default, Ubuntu 18.04 ships MariaDB v10.1 which is a bit older than the version available in official MariaDB repository (v10.4). You can install MariaDB v10.4 by following the steps in the below URL.

How To Install MariaDB On Ubuntu 18.04

sudo apt install -y mariadb-server mariadb-client

Next, make the MariaDB server secure by using the mysql_secure_installation command.

mysql_secure_installation

Output:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

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):  << No root password. 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 MariaDB root password
New password:   << Enter 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 users
 ... 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  << Disallow root login remotely
 ... 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 PHP-FPM

Install PHP through PHP-FPM (PHP-FastCGI Process Manager), an alternative PHP FastCGI implementation. It provides additional features useful for sites bigger in size.

By default, Ubuntu 18.04 ships PHP-FPM v7.2. You can also install PHP-FPM v7.3 by following the steps in the below URL.

How To Install PHP 7.3 On Ubuntu 18.04

Install php-fpm by using the following command.

sudo apt install -y php-fpm php-mysql php-cli

PHP-FPM listens on the socket /run/php/php7.2-fpm.sock by default.

To make PHP-FPM to use TCP connection, edit the below file.

sudo nano /etc/php/7.2/fpm/pool.d/www.conf

Then, change the listen parameter.

FROM:

listen = /run/php/php7.2-fpm.sock

TO:

listen = 127.0.0.1:9000

Restart the PHP-FPM process and enable it to start automatically on system boot.

sudo systemctl restart php7.2-fpm

sudo systemctl enable php7.2-fpm

Test LEMP Stack

Let’s create name-based virtual host on Nginx server for the following details.

ADVERTISEMENT

Domain Name: web.itzgeek.local
Document Root: /usr/share/nginx/html/web.itzgeek.local

First, create a virtual host configuration file for our domain under /etc/nginx/conf.d/ directory.

sudo nano /etc/nginx/conf.d/web.itzgeek.local.conf

Add the following content.

server {
   server_name web.itzgeek.local;
   root /usr/share/nginx/html/web.itzgeek.local;

   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 $document_root$fastcgi_script_name;
   }
}

Create the root directory for our virtual host.

sudo mkdir -p /usr/share/nginx/html/web.itzgeek.local

For testing the PHP-FPM support, place a PHP file onto the document root of the created virtual host.

echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/web.itzgeek.local/index.php

Update the permission of the file.

sudo chown -R www-data:www-data /usr/share/nginx/html/web.itzgeek.local/

Restart the services.

sudo systemctl restart nginx

sudo systemctl restart php7.2-fpm

Create a host entry for your domain (web.itzgeek.local) in the /etc/hosts file in case your environment doesn’t have DNS server for name resolution.

nano /etc/hosts

Add a host entry something look like below.

192.168.1.10        web.itzgeek.local web

Open up your web browser and type your domain name in the web address.

http://web.itzgeek.local

The page will look like below:

PHP Informations
PHP Informations

From the above screenshot, PHP is working through FPM/FastCGI, as shown in the Server API line.

If you scroll the page further down, you would see the support of MariaDB.

MariaDB Extension Details
MariaDB Extension Details

Conclusion

That’s All. I hope you have learned how to install LEMP stack on Ubuntu 18.04. Consider installing Let’s Encrypt SSL certificate for you site to improve the security. Please share your feedback in the comments section.

You might also like