Install Nginx, MariaDB and PHP 7.2-FPM with phpMyAdmin on Ubuntu 16.04 | 18.04 | 18.10 (LEMP + phpMyAdmin)

This brief tutorial shows students and new users how to install LEMP + phpMyAdmin on Ubuntu 16.04 | 18.04 | 18.10 servers….

LEMP is an acronym for Linux (Ubuntu), Engine-X (Nginx) HTTP Server, MariaDB or MySQL Database Server and PHP-FPM Scripting Language… It is a group of open source software and building blocks of many of the web applications and majority of the content management systems (CMS) in use today…

phpMyAdmin is a web-based tool that allows you to interact with MariaDB databases, manage user accounts and privileges, execute SQL-statements, import and export data and much, much more.

If you’re going to be developing any PHP based applications or websites, then you’re probably going to be using the LEMP and phpMyAdmin allows you to easily manage your databases from your favorites web browsers…

This brief tutorial is going to show students and new users how to install Nginx, MariaDB, PHP -FPM with phpMyAdmin support on Ubuntu Linux 16.04 | 18.04 and 18.10 servers…

To get started with installing the LEMP Stack, follow the steps below:

Step 1: Prepare Ubuntu Linux

The LEMP stack includes the Linux machine… in this case, Ubuntu… To get LEMP you must first install a Linux machine… this post assumes you’ve already install Ubuntu server..

After installing Ubuntu server, run the commands below to update the server…

sudo apt update && sudo apt dist-upgrade && sudo apt autoremove

Step 2: Install Nginx HTTP Server

Nginx HTTP Server represents the E in the LEMP stack… It’s the probably the second most popular web server installed today… not far behind the most popular web server, Apache2…

To install Nginx HTTP on 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 service to always start up with the server boots.

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

To test Nginx setup, open your browser and browse to the server hostname or IP address and you should see Nginx default test page as shown below.. When you see that, then Nginx is working as expected..

ex.. http://localhost

nginx default home page test

Step 3: Install MariaDB Database Server

MariaDB stands for M in LEMP and it’s a great place to start when looking at open source database server… Although MySQL was originally the default database server among Linux systems, MariaDB has taken over.. To install it run the commands below.

sudo apt-get install mariadb-server mariadb-client

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

On Ubuntu 16.04 LTS

sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service

On Ubuntu 18.04 LTS and 18.10 

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

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

sudo mysql_secure_installation

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

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • 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

Restart MariaDB server

To test if MariaDB is installed, type the commands below to logon to MariaDB server

sudo mysql -u root -p

Then type the password you created above to sign on… if successful, you should see MariaDB welcome message

mariadb welcome

Step 4: Install PHP 7.2-FPM and Related Modules

The last component of the LEMP stack is PHP-FPM… It’s the P in the LEMP stack…

PHP 7.2-FPM may not be available in Ubuntu default repositories… in order to install it, you will have to get it from third-party repositories.

Run the commands below to add the below third-party repository to upgrade to PHP 7.2-FPM

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

Then update and upgrade to PHP 7.2-FPM

sudo apt update

Then run the commands below to install PHP 7.2-FPM and related modules…

sudo apt install php7.2-fpm php7.2-common php7.2-mbstring php7.2-xmlrpc php7.2-soap php7.2-gd php7.2-xml php7.2-intl php7.2-mysql php7.2-cli php7.2-zip php7.2-curl

After installing PHP, run the commands below to find the version installed on the server…

php -v

You should see an output like the one below:

PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies

The version number determines the location of PHP default configuration file… For PHP 7.2, the location is as shown below:

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

Replace the version number above with the version of PHP installed..,..

When the file opens, make the changes on the following lines below in the file and save. The value below is great settings to apply in your environments.

file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago

After making the change above, save the file and close out.

Step 5: Install phpMyAdmin

Now that LEMP is in place, run the commands below to install phpMyAdmin…

sudo apt install phpmyadmin

When prompted to choose the web server, don’t select anything… just skip and continue. We’re using Nginx web server and it’s not on the list, so just skip.

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

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

 +------------------------+ 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>                             |
 |                                                                           |
 +---------------------------------------------------------------------------+

You will be prompted to create a password for phpMyAdmin to register with the database… Please provide a password for phpmyadmin to register with the database…   After that, phpMyAdmin should be installed and ready to use..

Next, run the commands below to create phpMyAdmin code snippets…

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

Then copy and paste the code below in 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/php7.2-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…

Next, create your Nginx site configuration file for your domain… and include the phpMyAdmin configuration…

sudo nano /etc/nginx/sites-available/example.com.conf

Then copy and pastes the configurations below

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/example.com;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

     client_max_body_size 100M;

     autoindex off;
  
     location / {
        try_files $uri $uri/ /index.php?$query_string;
      }

    location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }

   include snippets/phpmyadmin.conf;
}

Save the file and exit

Now enable the site

sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
sudo systemctl restart nginx.service
sudo systemctl restart php7.2-fpm.service

Step 6: Connecting to phpMyAdmin

Now open your browser and browse to the hostname followed by phpmyadmin

http://example.com/phpmyadmin

phpMyAdmin Ubuntu setup

When you attempt to logon using MariaDB root account it will fail… That’s because MariaDB and MySQL have switch their authentication method to auth_socket

The auth_socket plugin authenticates users that connect from the localhost through the Unix socket file… which prevents users from connecting with password… So, you won’t be able to connecto via phpMyAdmin…

When you attempt to logon, you see the error “#1698 – Access denied for user ‘root’@’localhost’

To fix that, run the commands below:

sudo mysql -u root

That should get you into the database server. After that, run the commands below to disable plugin authentication for the root user

use mysql;
update user set plugin='' where User='root';
flush privileges;
exit

Restart and run the commands below to set a new password.

sudo systemctl restart mariadb.service

Now try again to logon… this time it should work!

phpMyAdmin Nginx

Congratulations! You’re successfully installed the LEMP Stack with phpMyadmin on Ubuntu 16.04 | 18.04 | 18.10…

Enjoy~

You may also like the post below:

Setup OrangeScrum Project Management Platform on Ubuntu with Nginx, MariaDB and PHP 7.2-FPM

3 Replies to “Install Nginx, MariaDB and PHP 7.2-FPM with phpMyAdmin on Ubuntu 16.04 | 18.04 | 18.10 (LEMP + phpMyAdmin)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: