A Guide to Installing and Securing phpMyAdmin with Nginx on an Ubuntu 20.04 Server

Introduction

phpMyAdmin is a popular open-source web application that provides an intuitive graphical user interface (GUI) to manage MySQL or MariaDB databases. Nginx, on the other hand, is a high-performance web server that excels in serving static content and managing web applications efficiently. Combining these two tools can offer a robust solution for managing and securing your databases. In this guide, we will walk you through the process of installing and securing phpMyAdmin with Nginx on an Ubuntu 20.04 server.

Step 1: Update and Upgrade

Before proceeding, ensure your system packages are up to date. Open a terminal and execute the following commands:

sudo apt update
sudo apt upgrade

Step 2: Install LEMP Stack

LEMP stands for Linux, Nginx, MySQL (or MariaDB), and PHP. Since you’re focusing on phpMyAdmin and Nginx, we’ll install only the necessary components:

sudo apt install nginx mysql-server php-fpm php-mysql

Follow the prompts to set up a MySQL root password.

Step 3: Install phpMyAdmin

Now, install phpMyAdmin:

sudo apt install phpmyadmin

During the installation, you’ll be prompted to choose a web server. Select ‘nginx’ and allow the installer to configure the necessary settings.

Step 4: Configure Nginx for phpMyAdmin

Create a new server block configuration file for phpMyAdmin:

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

Add the following configuration, adjusting paths and server name as needed:

server {
    listen 80;
    server_name your_domain_or_ip;

    root /usr/share/phpmyadmin;
    index index.php;

    location / {
        try_files $uri $uri/ =404;
    }

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

Activate the configuration:

sudo ln -s /etc/nginx/sites-available/phpmyadmin /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Step 5: Secure phpMyAdmin

Securing phpMyAdmin is crucial to prevent unauthorized access:

  • Rename Directory: Change the directory’s name to a less predictable one:
  sudo mv /usr/share/phpmyadmin /usr/share/someothername
  • Restrict Access: Create an Nginx configuration to restrict access:
  sudo nano /etc/nginx/conf.d/phpmyadmin.conf

Add the following:

  location /phpmyadmin {
      allow 192.168.1.0/24;  # Adjust IP range
      deny all;
  }
  location /phpMyAdmin {
      allow 192.168.1.0/24;  # Adjust IP range
      deny all;
  }

Reload Nginx: sudo systemctl reload nginx

Step 6: Enable HTTPS (Optional but Recommended)

For added security, consider enabling HTTPS using Let’s Encrypt:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your_domain_or_ip

Follow the prompts to secure your domain with a free SSL certificate.

Conclusion

By following this guide, you’ve successfully installed and secured phpMyAdmin with Nginx on an Ubuntu 20.04 server. This powerful combination provides a user-friendly interface to manage your MySQL or MariaDB databases while ensuring that access is restricted and secure. Always keep your server updated and perform regular security audits to maintain a robust and safe environment for your data.


Leave a Reply

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