Nginx部署Laravel后配置phpMyAdmin出现404 Not Found问题
Alright, let’s dig into why you’re getting that 404 when trying to access phpMyAdmin on your Laravel server. I’ve worked through similar issues before, so here’s a step-by-step breakdown of what’s likely going wrong and how to fix it:
1. First, verify your phpMyAdmin path is correct
You’ve set alias /usr/share/nginx/html/phpmyadmin, but it’s super common for phpMyAdmin to be installed at /usr/share/phpmyadmin by default (especially on Debian/Ubuntu systems). Let’s check if your symlink is pointing to a valid location:
ls -l /usr/share/nginx/html/phpmyadmin
If this returns "No such file or directory" or shows the symlink points to a non-existent path, adjust the alias line to match the actual install path. For most setups, that would be:
alias /usr/share/phpmyadmin;
2. Fix the try_files directive (you’re referencing an undefined location)
In your phpMyAdmin config, you have try_files $uri @phpmyadmin; but you never defined the @phpmyadmin named location. When Nginx can’t find the requested file/directory, it tries to redirect to this non-existent location, which throws a 404. Replace that line with:
try_files $uri $uri/ /phpmyadmin/index.php?$query_string;
This tells Nginx to fall back to the phpMyAdmin index file if the requested resource doesn’t exist, which is the correct behavior for PHP apps.
3. Align your PHP-FPM socket path
Your Laravel config uses fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;, but your phpMyAdmin config uses /run/php/php7.2-fpm.sock. While these might be symlinks on some systems, it’s safer to use the same path to avoid any unexpected issues. Update the phpMyAdmin location’s fastcgi_pass to match the Laravel one.
4. Check directory permissions
Nginx needs read access to the phpMyAdmin files. Run these commands to ensure permissions are set correctly:
# Replace the path if you adjusted the alias to /usr/share/phpmyadmin sudo chown -R www-data:www-data /usr/share/nginx/html/phpmyadmin sudo chmod -R 755 /usr/share/nginx/html/phpmyadmin
5. Reload Nginx (don’t forget this step!)
After making any config changes, you need to tell Nginx to pick them up:
sudo systemctl reload nginx
If you’re still stuck: Check the error log
If you still get a 404, look at Nginx’s error log to see exactly what’s failing:
sudo tail -f /var/log/nginx/error.log
This will show you the exact file path Nginx is trying to access, which can help you spot path mismatches or permission issues quickly.
Corrected full phpMyAdmin location block
Here’s the finalized config (assuming you’re using the default /usr/share/phpmyadmin path):
location /phpmyadmin { alias /usr/share/phpmyadmin; try_files $uri $uri/ /phpmyadmin/index.php?$query_string; allow 103.209.11.41; deny all; index index.php; location ~* \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } }
内容的提问来源于stack exchange,提问作者Ikhtiar Mahmud

