如何在DigitalOcean的Nginx服务器上为Django+MySQL配置phpMyAdmin
Hey there! Let me walk you through setting up phpMyAdmin to manage your MySQL database on your DigitalOcean Nginx server running Django as the backend. I’ll break this into clear, actionable steps:
First, update your server’s package list to ensure you’re pulling the latest software versions:
sudo apt update
Then install phpMyAdmin along with required PHP dependencies (Nginx uses PHP-FPM to handle PHP requests):
sudo apt install phpmyadmin php-fpm php-mysql
During installation, you’ll be prompted to choose a web server—skip selecting Apache (even if it’s listed). Just hit Tab then Enter; we’ll configure Nginx manually later.
Since your Django app likely uses the root (/) path, we’ll host phpMyAdmin at a subpath like /phpmyadmin to avoid conflicts.
First, create a symbolic link from the phpMyAdmin directory to your Nginx web root:
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
Open your Django site’s Nginx configuration file (usually located at /etc/nginx/sites-available/your-django-site.conf):
sudo nano /etc/nginx/sites-available/your-django-site.conf
Add this location block inside your server block (place it before your Django proxy pass config so Nginx handles phpMyAdmin requests first):
location /phpmyadmin { root /var/www/html; index index.php index.html index.htm; # Handle PHP file execution location ~ ^/phpmyadmin/(.+\.php)$ { root /var/www/html; fastcgi_pass unix:/run/php/php8.1-fpm.sock; # Adjust PHP version to match your install fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # Block access to hidden files for security location ~ /phpmyadmin/\.ht { deny all; } }
Note: Replace
php8.1-fpm.sockwith your actual PHP-FPM socket version. Check available versions withls /run/php/.
Save the file, test for Nginx syntax errors:
sudo nginx -t
If no errors appear, restart Nginx to apply changes:
sudo systemctl restart nginx
For security, avoid using the MySQL root user with phpMyAdmin. Instead, create a dedicated user with appropriate privileges:
Log into the MySQL shell:
sudo mysql -u root -p
Run these SQL commands (replace the username and password with your preferred values):
CREATE USER 'pma_admin'@'localhost' IDENTIFIED BY 'your_strong_password_here'; GRANT ALL PRIVILEGES ON *.* TO 'pma_admin'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES; EXIT;
You’ll use this user to log into phpMyAdmin.
Add an extra layer of protection with HTTP basic authentication:
First, install the tool to create password files:
sudo apt install apache2-utils
Create a password file and add an authentication user:
sudo htpasswd -c /etc/nginx/.htpasswd phpmyadmin_user
Enter and confirm a password when prompted.
Go back to your Nginx config and add these lines inside the /phpmyadmin location block:
auth_basic "Restricted phpMyAdmin Access"; auth_basic_user_file /etc/nginx/.htpasswd;
Save, test the config again with sudo nginx -t, then restart Nginx.
Open your browser and go to http://your-server-ip/phpmyadmin (or your domain name if configured). You’ll see the phpMyAdmin login page—use the MySQL user credentials you created to access and manage your databases.
Important: Double-check your Django URL configuration to ensure no route matches
/phpmyadmin—adjust either the Django route or phpMyAdmin subpath if there’s a conflict.
内容的提问来源于stack exchange,提问作者Abhimanyu Singh

