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.

Read Also: Install Apache, MariaDB, PHP and 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:

  1. Minimal Ubuntu 18.04 server Installation.
  2. Access to server via a SSH session.
  3. Root access or use sudo command to run all commands.


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.

$ sudo mysql_secure_installation

Then enter yes/y to the following security questions:

  • Set root password? [Y/n]: y
  • 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

Secure Mariadb Installation in Ubuntu 18.04

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.

$ sudo systemctl status php7.2-fpm

10. Next, configure PHP-FPM properly to serve PHP based web applications or sites, in the configuration file /etc/php/7.2/fpm/php.ini.

$ sudo vim /etc/php/7.2/fpm/php.ini

Search for the ;cgi.fix_pathinfo=1 and change it to the following.

cgi.fix_pathinfo=0

11. Then configure PHP-FPM to process PHP scripts in Nginx default server block configuration file (/etc/nginx/sites-available/default).

$ sudo vim /etc/nginx/sites-available/default 

Uncomment the configuration section below to pass PHP scripts to FastCGI server.

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

Configure Nginx Server Block

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/htmlphpmyadmin

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

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

$ sudo chmod 775 -R /usr/share/phpmyadmin/
$ sudo chown root:nginx -R /usr/share/phpmyadmin/

17. Now, from a web browser, type the following URL to access PhpMyAdmin.

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

Then authenticate in the phpMyAdmin using your MySQL/MariaDB root username and password, and enjoy.

PhpMyAdmin Login

Note: If the root login fails (because it requires sudo starting from MySQL 5.7), you may need to create a new admin user account to access the mariadb shell using the root account from a terminal.

$ sudo mysql -u root -p
MariaDB [(none)]> CREATE USER 'admin'@'localhost' IDENTIFIED BY '[email protected]!#254tecmint';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
MariaDB [(none)]> FLUSH PRIVILEGES;

Now use new credentials to login again into PhpMyAdmin to administer your MySQL databases.

PhpMyAdmin MySQL Database Administration

To secure your PhpMyAdmin web interface, check this article: 4 Useful Tips to Secure PhpMyAdmin Web Interface.

That’s it! In this article, we have explained how to setup LEMP stack with the latest PhpMyAdmin in Ubuntu 18.04. If you have any queries, let us know via the feedback form below.

Spread the love

Posted by News Monkey

blank