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
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
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
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
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
Select PhpMyAdmin Web Server
PhpMyAdmin Configuration
PhpMyAdmin Configuration

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

Set Password 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
Add Index File in Nginx Block

17. 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/

18. 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
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
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.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

17 thoughts on “Install Nginx, MariaDB, PHP and PhpMyAdmin in Ubuntu 18.04”

  1. Man, you deserve a medal!

    I had trouble with other PHP versions. php-gettext too, what came from php7.0-fpm, so I removed all versions of PHP and your dependencies and reinstall PHP version 7.2 and php7.2-fpm, works fine.

    TIPS TO REINSTALL ALL PHP AND PHPMYAMIN:

    # apt-get remove php*
    # apt-get autoremove php*
    # apt-get purge php*
    # apt-get autoremove --purge php*
    

    OK, now install php and php-fpm with version 7.2:

    # apt install php7.2 php7.2-fpm php-common php-mysql php-gd php-cli
    

    No need restart, but you can.

    NOTE: Don’t forget to modify version to 7.2 in archive : /etc/nginx/sites-available/default.

    Reply
  2. Hi Guys,

    The same problem with 502 Bad Gateway.

    I have changed to 7.4, so:

    $ sudo systemctl status php7.4-fpm
    $ sudo systemctl start php7.4-fpm
    

    Also, I have uncommented this section in default server block file:

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

    What I need to do? Many thanks!

    Reply
  3. You have forgotten to mention about creating nginx user/group which doesn’t create by apt so it will be much better if you add this command line to your nginx installation part of your document :

    sudo adduser --system --no-create-home --shell /bin/false --group --disabled-login nginx

    Reply
    • (note: my code example is trimmed by this forum, I mean when I write – – system it posts as – system, so if you need it correct spelling I may mail to you )

      Reply
  4. Thanks, man, really helpful and I have a small doubt, I have a site which is using apache2, MySQL can I migrate apache2 to Nginx and also MySQL to MariaDB without any data loss.

    Reply
    • @Subin,

      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!

      Reply
  5. Good tutorial, man!

    But, I needed to reload again at the end of everything, for phpmyadmin to work fine.
    (Using Kubuntu 18.04 LTS)

    Reply
  6. Good tutorial!

    When you create a link there is a slash missing…

    On my computer the nginx is running as www-data so the chown command had to be modified

    Reply
    • Same here on a Ubuntu 18.04 server, however even after running the chown as root:www-data I still get an access denied on phpmyadmin. No clue why..

      Reply
  7. It seems to be something wrong with this tutorial…

    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

    I’m stuck, help please?

    Reply
    • @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:

      location ~ \.php$ {
                  include snippets/fastcgi-php.conf;
              	fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;       #take note of this line, it should indicate PHP7.2
      }
      
      Reply

Leave a Reply to Olivia Cancel reply

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

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