How to Install the LEMP (Linux, Nginx, MySQL PHP) stack on Ubuntu 20.04

Installing and Configuring Nginx Server


How to install Nginx on Ubuntu Linux
phpMyAdmin needs a web server to function, and Nginx HTTP Server is a great open-source server you can use with phpMyAdmin.

To install Nginx on the Ubuntu server, run the commands below.

sudo apt update
sudo apt install nginx

After installing Nginx, the commands below can be used to stop, start and enable Nginx services to always start up with the server boots.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

To determine if Nginx is installed and running, simply open your web browser and type in the server’s IP or hostname.

http://localhost



If you see a similar page above, Nginx is installed and functioning.

How to install MariaDB on Ubuntu Linux
phpMyAdmin is a tool to manage database servers. For our database server, we’re going to install MariaDB. phpMyAdmin should also work with the MySQL database server, but we will install it here.

To install MariaDB run the commands below.

sudo apt install mariadb-server
sudo apt install mariadb-client

After installing MariaDB, the commands below can stop, start and enable the MariaDB service to start up when the server boots.

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

After that, run the commands below to secure the MariaDB server by creating a root password, disallowing remote root access removing anonymous, and more.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

If you've just installed MariaDB, and haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): PRESS ENTER

Switch to unix_socket authentication [Y/n] n

Change the root password? [Y/n] n

Remove anonymous users? [Y/n] y

Disallow root login remotely? [Y/n] y

Remove test database and access to it? [Y/n] y

Reload privilege tables now? [Y/n] y

All done!


To verify that MariaDB is installed and running, run the commands below.

How to create a phpMyAdmin user for MariaDB

The latest MariaDB servers have a root user set to use the auth_socket authentication method by default.

The auth_socket plugin authenticates users that connect from the local host through the Unix socket file. You can’t authenticate as a root by providing a password.

This can cause issues with some apps that need to connect to the database via root. You’ll need to change the default authentication mechanism from auth_socket to mysql_native_password to fix that.

However, doing so might introduce security risks since root users shouldn’t be used to connect remotely to the database. A recommended method is to create a dedicated user to connect remotely to your database servers.

Since you don’t want to connect to the MariaDB database server from phpMyAdmin as the root user, you should probably create a separate account instead of connecting with the root.

Run the commands below to log on to the MariaDB server.

sudo mysql -u root -p

Then run the SQL commands below to create a new user for phpMyAdmin to connect to the database.

CREATE USER 'phpmyadmin'@'localhost' IDENTIFIED BY 'very_strong_password_here';

Then grant the user full access to manage the database server.

GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost' WITH GRANT OPTION;

How to install PHP-FPM on Ubuntu Linux

PHP is required for phpMyAdmin. PHP packages are added to Ubuntu repositories. However, the versions of the repositories might not be the latest. If you need to install the latest versions, you’ll need to add a third-party PPA repository.

Run the commands below to a third-party repository with the latest versions of PHP.

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

At the time of this writing, the latest PHP version is 8.0.

sudo apt update

Next, run the commands below to install PHP 8.0 and related modules.

sudo apt install php8.0-fpm php8.0-common php8.0-mysql php8.0-gmp php8.0-curl php8.0-intl php8.0-mbstring php8.0-xmlrpc php8.0-gd php8.0-xml php8.0-cli php8.0-zip

How to install phpMyAdmin on Ubuntu Linux

Now that Nginx and PHP are installed, the final step is to install phpMyAdmin and configure. To do that, run the commands below.

sudo apt install phpmyadmin

When prompted to choose the web server that should be automatically configured to run phpMyAdmin, tap Ok and press ENTER.

+------------------------+ Configuring phpmyadmin +------------------------+
| Please choose the web server that should be automatically configured to   |  
| Web server to reconfigure automatically:                                  |
|                                                                           |
|    [ ] apache2                                                            |
|    [ ] lighttpd                                                           | 
|                                 <ok>                                      |
 +-------------------------------------------------------------------------+

When prompted again to allow debconfig-common to install a database, configure, select Yes, and press ENTER.

Then type and confirm a password.

 +------------------------+ Configuring phpmyadmin +-------------------------+
 |                                                                             | 
 | The phpmyadmin package must have a database installed and configured      |
 | before it can be used.  This can be optionally handled with               |
 | dbconfig-common.                                                          |
 |                                                                           |
 | If you are an advanced database administrator and know that you want to   |
 | perform this configuration manually, or if your database has already      |
 | been installed and configured, you should refuse this option.  Details    |
 | on what needs to be done should most likely be provided in                |
 | /usr/share/doc/phpmyadmin.                                                |
 |                                                                           |
 | Otherwise, you should probably choose this option.                        |
 |                                                                           |
 | Configure database for phpmyadmin with dbconfig-common?                   |
 |                                                                           |
 |                  <Yes>                  <No>                              |
 |                                                                           |
 +---------------------------------------------------------------------------+   

Configuring phpMyAdmin for Nginx support
There are several ways to configure phpMyAdmin to work with Nginx. One way is to use an existing domain server block and create a symbolic link to phpMyAdmin installation files.

To do that, run the commands below to create a phpMyAdmin snippet that can be used on an existing server block.

sudo nano /etc/nginx/snippets/phpmyadmin.conf

Then copy and paste the code below into the file and save.

location /phpmyadmin {
    root /usr/share/;
    index index.php index.html index.htm;
    location ~ ^/phpmyadmin/(.+\.php)$ {
        try_files $uri =404;
        root /usr/share/;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

    location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
        root /usr/share/;
    }
}

Save the file and exit.

Finally, include the configuration above in your Nginx server block.

include snippets/phpmyadmin.conf;

If you don’t have an existing domain, open the Nginx default server block by running the commands below.

sudo nano /etc/nginx/sites-available/default

Copy and paste the highlighted line when the file opens, then save.

# Default server configuration
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        include snippets/phpmyadmin.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;
        location / {
                try_files $uri $uri/ =404;

        }
        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/run/php/php8.0-fpm.sock;
        }
}
Restart Nginx

sudo systemctl restart nginx

After installing phpMyAdmin, open your web browser and browse to the server hostname or IP address followed by /phpmyadmin.

http://localhost/phpmyadmin