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

0

Linux, Nginx, MariaDB, and PHP (famously known as LEMP stack). Nginx is a free, open-source, high-performance HTTP web server and is known for its stability, simple configuration, and low resource consumption.

Here, we will see how to install LEMP Stack on Ubuntu 18.04 with PHP support (via PHP-FPM) and database (MariaDB) support. PHP-FPM is an alternative PHP FastCGI implementation, and it has a lot of additional features useful for busier sites.

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.14.0-1, PHP version 7.2, MariaDB version 10.1.29) on Ubuntu 18.04.

Log in as the root or switch to the root user.

$ su

OR

$ sudo su -

Install MariaDB Server

Update the system repository index.

apt update

First, we will install the MariaDB server by issuing the following command.

apt -y install mariadb-server

MariaDB service should now be up and running. If not, start MariaDB server using the following command.

systemctl start mariadb

Auto-start the MariaDB service during every boot. Type the following on terminal and hit enter.

systemctl enable mariadb

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

Install Nginx

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.

apt-key add nginx_signing.key

Append Nginx repository to /etc/apt/sources.list file.

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

Install Nginx package using the following command.

apt update
apt -y install nginx

Start the Nginx service after the installation.

systemctl start nginx

Firewall

Issue the following commands to allow HTTP requests through the firewall.

ufw allow 80/tcp
ufw reload
ufw enable

Open up a web browser and visit the below URL. You should see the Nginx default page and this confirms you that the Nginx is successfully installed on the server.

http://your-ip-add-ress
Install Linux, Nginx, MySQL, PHP (LEMP Stack) in Ubuntu 18.04 - Nginx Default Page
Install Linux, Nginx, MySQL, PHP (LEMP Stack) in Ubuntu 18.04 – Nginx 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.

Auto-start Nginx at system startup.

systemctl enable nginx

Install PHP7-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. Install php-fpm by issuing the following command.

apt install php-fpm php-mysql php-cli

Edit the respective php.ini file according to the installed PHP version.

nano /etc/php/7.2/fpm/php.ini

set cgi.fix_pathinfo to 0.

cgi.fix_pathinfo=0

PHP-FPM listens on the socket /var/run/php7.2-fpm.sock by default. To make PHP-FPM use a TCP connection, open the file /etc/php/7.2/fpm/pool.d/www.conf and change the listen parameter.

FROM:

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

TO:

listen = 127.0.0.1:9000

Restart PHP-FPM service.

systemctl status php7.2-fpm

Enable PHP-FPM Support on Virtual Host

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

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

First, create a virtual host configuration file for our domain: /etc/nginx/conf.d/web.itzgeek.local.conf.

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 /usr/share/nginx/html/web.itzgeek.local$fastcgi_script_name;
   }
}

Test PHP-FPM support on the Virtual Host

Create the root directory for our virtual host.

mkdir /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.

nano /usr/share/nginx/html/web.itzgeek.local/index.php

Copy/Paste the following line into the index.php file:

<?php phpinfo(); ?>

Save and close the file. Use Ctrl+x and Y and then Enter for saving the file.

Update the permission of the file.

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

Restart the services.

systemctl restart nginx
systemctl restart php7.2-fpm
systemctl enable php7.2-fpm

End-user Desktop

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:

Install Linux, Nginx, MySQL, PHP (LEMP Stack) in Ubuntu 18.04 - PHP Information
Install Linux, Nginx, MySQL, PHP (LEMP Stack) in Ubuntu 18.04 – PHP Information

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.

Install Linux, Nginx, MySQL, PHP (LEMP Stack) in Ubuntu 18.04 - MariaDB Support
Install Linux, Nginx, MySQL, PHP (LEMP Stack) in Ubuntu 18.04 – MariaDB Support

That’s All.

Further Reading

You might also like
Shares