Linux

How to Install Nginx with PHP and MySQL (LEMP Stack) on Ubuntu 20.04 LTS

How to Install Nginx with PHP and MySQL (LEMP Stack) on Ubuntu 20.04 LTS

Nginx (pronounced “engine x”) is a free, open source, high-performance HTTP server. Nginx is known for its high speed, stability, rich feature set, simple configuration, and low resource consumption. This tutorial shows how you can install Nginx on an Ubuntu 20.04 LTS server with PHP 7.4 support (via PHP-FPM) and MySQL support (LEMP = Linux + Nginx (pronounced “engine x”) + MySQL + PHP).

Precondition

  • Ubuntu 20.04 LTS Server
  • Rooting privileges

What we will do?

  • Install Nginx
  • Install MySQL
  • Install PHP-FPM
  • Configure Nginx and PHP-FPM
  • Install PhpMyAdmin
  • Configure PhpMyAdmin
  • Testing

Step 1 – Install Nginx

Nginx or Engine X is a high-performance HTTP and proxy server with low memory consumption. Most large-scale websites like Netflix, Pinterest, CloudFlare, GitHub use Nginx.

In this step, we will install the Nginx web server from the Ubuntu repository.

Update all repositories on the Ubuntu system and install the Nginx package using the apt command below.

sudo apt update
sudo apt install nginx

After the installation is complete, start the Nginx service and activate it to launch it every time the system boots.

systemctl start nginx
systemctl enable nginx

Now that the Nginx Web server installation is complete, check the Nginx service using the following command.

systemctl status nginx

Below are the results that you will get.

1

Also, check the HTTP port on the Ubuntu system and make sure it is used by the Nginx service.

ss -plt4

Below are the results that you will get.

2

As a result, the HTTP protocol is used by the Nginx service.

Next, we will secure our Nginx installation using a UFW firewall.

Add HTTP and HTTPS protocols to the UFW firewall configuration using the following command.

for fw in ssh http https
do
ufw allow $fw
done

Now activate the UFW firewall and check the status.

ufw enable
ufw status numbered

And the HTTP and HTTPS protocols have been added to the UFW firewall configuration.

3

Step 2 – Install MySQL

MySQL is the most popular open-source Relational Database Management System (RDBMS) created by Oracle Corporation. This is the main component of LEMP Stack, and we will install the latest MySQL version from the Ubuntu repository.

Install MySQL using the apt command below.

sudo apt install mysql-server mysql-client

After the MySQL installation is complete, start the MySQL service and activate it to launch each time the system boots.

systemctl start mysql
systemctl enable mysql

Now check the MySQL service using the command below.

systemctl status mysql

MySQL service is already up and running.

4

Next, we will set the root password for the MySQL server.

Run the ‘mysql_secure_installation’ command below.

mysql_secure_installation

Now you will be asked as the following configuration, and be sure to type your strong password for the MySQL root user.

Press y|Y for Yes, any other key for No: Press Enter

New password: 
Re-enter new password: 

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y

And the MySQL root password has been configured.

As a result, the installation and configuration of the MySQL Database on Ubuntu 20.04 has been completed.

Step 3 – Install and Configure PHP-FPM

PHP-FPM or FastCGI Process Manager is an alternative to old PHP FastCGI that provides additional features and increased speed. Suitable for small to large sites based on the PHP programming language.

In this step, we will install the default PHP7.4-FPM with some additional extensions needed by PHPMyAdmin.

Install PHP-FPM using the command below.

sudo apt install php-fpm php-cli php-curl php-mysql php-curl php-gd php-mbstring php-pear -y

After the PHP-FPM installation is complete, open the ‘/etc/php/7.4/fpm’ directory and edit the php.ini configuration using the vim editor.

cd /etc/php/7.4/fpm/
vim php.ini

Uncomment the following configuration and change the value to ‘0’.

cgi.fix_pathinfo = 0

Save and close.

Now start the PHP7.4-FPM service and activate it to launch every time the system boots

systemctl start php7.4-fpm
systemctl enable php7.4-fpm

The PHP-FPM service is up and running, check using the following command.

systemctl status php7.4-fpm

Below are the results that you will get.

5

By default, PHP-FPM runs under the sock file, check using the ss command below.

ss -pl | grep php

Below is the response you will get.

u_str   LISTEN   0     511   /run/php/php7.4-fpm.sock 67702   * 0        

And the installation of PHP-FPM 7.4 on Ubuntu 20.04 has been completed.

Step 4 – Configure Nginx and PHP-FPM

In this step, we will configure the Nginx and PHP-FPM web servers.

Open the ‘/ etc / nginx’ configuration directory, and edit the ‘nginx.conf’ file using vim or nano.

cd /etc/nginx/
vim nginx.conf

Cancel comments in the following lines to hide the version of Nginx.

server_tokens off;

Save and close.

Next, edit the ‘default’ virtualhost configuration in the ‘sites-available’ directory.

vim sites-available/default

Cancel comments on the PHP line shown below and change the socks file line.

location ~ \.php$ {
                include snippets/fastcgi-php.conf;
    #
    # # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    # # With php-cgi (or other tcp sockets):
    # fastcgi_pass 127.2.0.1:9000;
    }

Save and close.

Test the Nginx configuration and make sure there are no errors, then restart the service.

nginx -t
systemctl restart nginx

As a result, Nginx and PHP-FPM configurations have been completed.

6

Step 5 – Install phpMyAdmin

PhpMyAdmin is a PHP based application for managing MySQL or MariaDB databases from a web browser. In this step, we will install and configure PHPMyAdmin under the LEMP stack (Linux, Nginx, MySQL, and PHP-FPM).

Install PHPMyAdmin using the apt command below.

sudo apt install phpmyadmin

During installation, it will ask about the web server configuration for phpmyadmin.

Select no options and move the cursor to ‘OK’.
7

For phpmyadmin database and user configuration, select ‘Yes’.

8

And type in the new ‘STRONG’ admin PHPMyAdmin like ‘Hakaselabs001 @ #’.
9

Repeat password ‘Hakaselabs001 @ #’.

10

And the phpMyAdmin installation on Ubuntu 20.04 is complete.

Step 6 – Configure phpMyAdmin with Nginx and MySQL

After installing phpmyadmin, we need to configure phpmyadmin to run under the Nginx web server and configure MySQL user phpmyadmin access. To run PHPMyAdmin under the Nginx web server, we need to add a configuration to the virtual host configuration file.

Configure phpMyAdmin with Nginx Webserver

Now open the ‘/ etc / nginx’ configuration directory, and edit the default virtual host file.

cd /etc/nginx/sites-available/
vim default

Paste the following Nginx configuration for PHPMyAdmin inside the ‘server {…}’ bracket.

location /phpmyadmin {
    root /usr/share/;
    index index.php;
    try_files $uri $uri/ =404;

location ~ ^/phpmyadmin/(doc|sql|setup)/ {
    deny all;
    }

location ~ /phpmyadmin/(.+\.php)$ {
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
    }
}

Save and exit.

Test the nginx configuration and make sure there is no error, then restart the nginx service.

nginx -t
systemctl reload nginx

And we have added the Nginx configuration to our phpMyAdmin installation, which can be accessed via the path as ‘http: // server-ip / phpmyadmin’.

11

Allow phpMyAdmin users to access the Database

For this tutorial, we will use a non-root MySQL user for PHPMyAdmin. We will create new users and give all privileges from all databases on the server to users.

Log in to the MySQL shell using the following command.

mysql -u root -p

Now create a new MySQL user ‘hakase’ and grant all database privileges.

create user hakase@'localhost' identified by 'Hakaselabs001@#';
grant all privileges on *.* to hakase@'localhost';
flush privileges;

Now type ‘exit’ to exit the MySQL shell.

exit

As a result, we have created a new MySQL user for phpMyAdmin.

Step 7 – Testing

In this step, we will test the configuration of our LEMP Stack and phpMyAdmin.

Test PHP file

Open the ‘/ var / www / html’ directory and create a new script file ‘info.php’ using vim editor.

cd /var/www/html
vim info.php

Paste the following script into it.

<?php
phpinfo();
?>

Save and close.

Next, open your web browser and type your server’s IP address following the path of your ‘info.php’ script.

http: //server-ip/info.php

Below are the results that you will get.

12

As a result, we have detailed our PHP-FPM 7.4 configuration on the Ubuntu 20.04 server, and PHP scripts are running on our LEMP stack server.

Test phpMyAdmin

To test our phpMyAdmin installation, type your server’s IP address by following the ‘phpmyadmin’ path in the URL.

http: // server-ip / phpmyadmin /

And you will get the phpMyAdmin login page.

13

To log in to phpMyAdmin, type the user that we created above. The user is ‘hakase’ with the password ‘Hakaselabs001 @ #’, then click the ‘Go‘ button.

And you will get the phpMyAdmin dashboard as below.

14

PhpMyAdmin works under the LEMP Stack environment.

As a result, the installation and configuration of LEMP Stack on Ubuntu 20.04 has been completed successfully.

Related posts

How to Set Up a Firewall with UFW on Ubuntu 20.04

Linux

How to Install Vagrant on Ubuntu 20.04

Linux

How to Install VLC Media Player on Ubuntu 20.04

Linux

How to Install Docker Compose on Ubuntu 20.04

Linux

How to Install PostgreSQL and phpPgAdmin on Ubuntu 20.04 LTS

Linux

How to Install Nagios on Ubuntu 20.04

Linux

How to Run a Cron Job Every 5, 10 or 15 Minutes

Linux

How to Install PostgreSQL on Ubuntu 20.04

Linux

How to Set an SSH Key on Ubuntu 20.04

Linux