Install Nginx, MariaDB, PHP and PhpMyAdmin in Ubuntu 18.04

A LEMP stack is made up of Nginx (pronounced Engine X), MySQL/MariaDB and PHP/Python packages installed on a Linux system, and configured to work together as a system for hosting websites and applications and more. In this guide, we will show how to install LEMP and the latest phpMyAdmin in Ubuntu 18.04.

PhpMyAdmin is a free, open source, popular and intuitive web-based application for administering MySQL and MariaDB database, which supports a wide range of operations.

It has a multitude of features for simply managing your databases from a web interface. It allows you to import and export data in various formats, manage multiple servers, build complex queries using Query-by-example (QBE), create graphics of your database layout in various formats, and so much more.

Requirements:

In this article, we will explain how to install LEMP stack with PhpMyAdmin in Ubuntu 18.04.

Step 1: Install Nginx Web Server on Ubuntu 18.04

1. First start by updating your software packages and then install Nginx, an open source, fast and high-performance web server, load balancer as well as reverse proxy with an easy to understand configuration language.

$ sudo apt update
$ sudo apt install nginx

2. Once you have installed it, the Nginx service should start automatically and will be enabled to start at boot time, you can check if it’s up and running.

$ sudo systemctl status nginx

3. If you have a firewall enabled and running on your system, you should open the ports 80 (HTTP) and 443 (HTTPS) to allow client requests to Nginx web server, and reload the firewall rules.

$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp
$ sudo ufw reload

4. Next, test if the Nginx package was successfully installed and is working fine, type this URL in your web browser.

http://domain_name/
OR
http://SERVER_IP/

If you see the Nginx default web page, it means your installation is working fine.

Check Nginx Web Page

Step 2: Install MariaDB on Ubuntu 18.04

5. Next install MariaDB database management system.

$ sudo apt install mariadb-server mariadb-client

6. After MariaDB installation, the service should start automatically and you can verify it using following command.

$ sudo systemctl status mysql

7. Next, secure your MariaDB installation by running the security script that comes with the package.

Step 3: Install PHP on Ubuntu 18.04

8.PHP is a popular server side scripting language used to generate dynamic content on websites. You can install PHP, PHP-FPM and other modules for web development using following command (the default version in the Ubuntu repos is PHP 7.2).

$ sudo apt install php php-fpm php-common php-mysql php-gd php-cli

9. After PHP installation, the PHP7.2-FPM service should also start automatically, you can verify the service using the following command.

After making changes, restart the php7.2-fpm and nginx services to apply the recent changes.

$ sudo systemctl restart php7.2-fpm
$ sudo systemctl restart nginx

12. Now you can test your PHP setup by creating a simple info.php page in your web server document root, with this single command.

$ echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

13. Next, open a web browser, and enter any of the following URL to view the php information page.

http://domain_name/info.php
OR
http://SERVER_IP/info.php

Nginx PHP Info Page

Step 4: Install PhpMyAdmin on Ubuntu 18.04

14. At last install PhpMyAdmin for administrating MySQL/MariaDB databases from the comfort of a web browser.

$ sudo apt install phpmyadmin

During the package installation process, you will be asked to choose the web server that should be automatically configured to run phpMyAdmin. Nginx is not in the list of web servers, simply press TAB key and press Enter.

Select PhpMyAdmin Web Server

PhpMyAdmin Configuration

15. Next, enter the password for the MySQL to create a database for phpmyadmin.

Set Password for Phpmyadmin

16. At this point the phpmyadmin installation process should be complete. You can access the phpMyAdmin interface in your browser by creating the following symlink.

$ sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

Because the phpmyadmin index file is index.php, also ensure that you have added it to the list of index files, in your default server block configuration file /etc/nginx/sites-available/default, as shown in the following screenshot.

index index.php index.html index.htm index.nginx-debian.html;

Add Index File in Nginx Block

17. Next, set the appropriate permissions on the phpmyadmin root directory to prevent access denied errors.

I am Ravi Saive, creator of TecMint. A Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux. Follow Me: Twitter, Facebook and Google+

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

Yes, you can switch from Apache to Nginx and from MySQL to MariaDB without loosing any data.. Just remove the Apache, and install Nginx and point the Nginx DocumentRoot to your website directory. Similarly, take a dump of all databases from MySQL, remove it and install MariaDB and import all databases that’s it!

1. In Step 3 #8 installing php will pull in apache as a dependency and replace nginx with apache. I had to install only php-fpm for this to not happen.

2. In Step 3 #12 and #13 i get error “502 Bad Gateway” when opening info.php (also tried with apache, same error). I’m able to open http://SERVER_IP but I get the error when opening http://SERVER_IP/info.php

@Kent879
You encountering a “502 Bad Gateway” error because Nginx is proxying requests to a nonexistent upstream server or endpoint. Ensure that PHP-FPM service is running, check its status using following command.

$ sudo systemctl status php7.2-fpm

In case it is not up and running, start it.

$ sudo systemctl start php7.2-fpm

Also make sure that you have uncommented this section in your default server block file: