PlusMagi's Blog By Pitt Phunsanit Nginx,phpMyAdmin,Web Security Nginx Reverse Proxy for phpMyAdmin: Secure Setup Guide

Nginx Reverse Proxy for phpMyAdmin: Secure Setup Guide

phpMyAdmin is a widely used web-based tool for managing MySQL databases. To enhance security and improve performance, you can set up Nginx as a reverse proxy for phpMyAdmin. This guide will walk you through the process of configuring Nginx to act as a reverse proxy for phpMyAdmin, ensuring that your database management tool is both secure and accessible.

Prerequisites

Before you begin, ensure that you have the following:

  • Ubuntu server with Nginx installed.
  • phpMyAdmin installed and running on your server.
  • A domain name pointing to your server.

Step 1: Install Nginx

If Nginx is not already installed, you can install it using the following command:

SQL

sudo apt-get install nginx

Step 2: Create Nginx Configuration File

Create a new Nginx configuration file for your phpMyAdmin setup. For example, create a file named /etc/nginx/sites-available/yourdomain.com.phpmyadmin.conf.

SQL

server {<br>    access_log /var/log/nginx/yourdomain.com.phpmyadmin-access.log;<br>    add_header X-Content-Type-Options "nosniff";<br>    add_header X-Frame-Options "SAMEORIGIN";<br>    add_header X-XSS-Protection "1; mode=block";<br>    error_log /var/log/nginx/yourdomain.com.phpmyadmin-error.log error;<br>    http2 on;<br>    include /etc/letsencrypt/options-ssl-nginx.conf;<br>    listen 443 ssl;<br>    server_name yourdomain.com;<br><br>    location / {<br>        proxy_pass http://127.0.0.1:8080;<br>        proxy_set_header Host $host;<br>        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br>        proxy_set_header X-Forwarded-Proto $scheme;<br>        proxy_set_header X-Real-IP $remote_addr;<br>    }<br>}<br><br>server {<br>    listen 80;<br>    server_name yourdomain.com;<br><br>    location / {<br>        return 301 https://$host$request_uri;<br>    }<br><br>    location /.well-known/acme-challenge/ {<br>        root /var/www/html;<br>        try_files $uri $uri/ =404;<br>    }<br>}

Step 3: Update Configuration

Make the following changes to the configuration file:

  • Replace yourdomain.com with your actual domain name.
  • Ensure the port in proxy_pass matches the port where phpMyAdmin is running.

Step 4: Enable the Configuration

Create a symbolic link to enable the new configuration:

SQL

sudo ln -s /etc/nginx/sites-available/yourdomain.com.phpmyadmin.conf /etc/nginx/sites-enabled/

Step 5: Test Nginx Configuration

Before restarting Nginx, test the configuration for any syntax errors:

SQL

sudo nginx -t

Step 6: Restart Nginx

Restart Nginx to apply the changes:

SQL

sudo systemctl restart nginx

Conclusion

By setting up Nginx as a reverse proxy for phpMyAdmin, you enhance the security and performance of your database management tool. This setup ensures that your phpMyAdmin interface is accessible only via HTTPS and that it is protected by Nginx’s robust security features. Follow the steps outlined in this guide to secure your phpMyAdmin installation with Nginx.