How to Fix Nginx Appending URI Path to Root Directory in phpMyAdmin Configuration


2 views

When configuring phpMyAdmin with Nginx, a common issue occurs where Nginx incorrectly appends the URI path to the root directory. This results in failed requests like:

2023/05/15 10:22:31 [error] 21567#0: *12 "/usr/share/phpMyAdmin/pma/index.php" not found (2: No such file or directory)

The server looks for files in /usr/share/phpMyAdmin/pma/ when they actually exist in /usr/share/phpMyAdmin/.

The core issue stems from misunderstanding Nginx's root and alias directives:

  • root appends the URI to the specified path
  • alias replaces the matched path portion with the specified path

For phpMyAdmin, we should use alias instead of root:

location ^~ /pma {
    alias /usr/share/phpMyAdmin/;
    index index.php;
    
    location ~ \\.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

Here's a tested configuration that handles both HTTP and HTTPS:

server {
    listen 80;
    server_name pma.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name pma.example.com;
    
    ssl_certificate /etc/letsencrypt/live/pma.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/pma.example.com/privkey.pem;
    
    # phpMyAdmin configuration
    location /pma {
        alias /usr/share/phpMyAdmin/;
        index index.php;
        
        location ~ ^/pma(.+\\.php)$ {
            alias /usr/share/phpMyAdmin/;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/share/phpMyAdmin$1;
            include fastcgi_params;
        }
    }
    
    # Security headers
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
}
  • Missing trailing slashes in alias paths
  • Incorrect SCRIPT_FILENAME parameter
  • Permission issues with PHP-FPM pool
  • Cache interfering with configuration changes

After making changes:

  1. Test configuration: nginx -t
  2. Reload Nginx: systemctl reload nginx
  3. Check error logs: tail -f /var/log/nginx/error.log
  4. Verify PHP execution: Create test.php with <?php phpinfo(); ?>

When configuring phpMyAdmin with Nginx, a common issue arises where Nginx incorrectly appends the location path to the root directory. The error message clearly shows this behavior:

2012/08/14 13:59:49 [error] 10151#0: *2 "/usr/share/phpMyAdmin/pma/index.php" is not found

The core of the problem lies in misunderstanding between root and alias directives:

  • root appends the URI to the specified path
  • alias replaces the matched part of URI with the specified path

For phpMyAdmin installations, using alias is generally the better approach:

location ^~ /pma {
    alias /usr/share/phpMyAdmin/;
    try_files $uri $uri/ /index.php;
    
    location ~ \\.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
    }
}

Here's a full server block configuration that handles both HTTP to HTTPS redirect and proper phpMyAdmin routing:

server {
    listen 80;
    server_name domain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name domain.com;
    
    root /var/www/html;
    index index.php;
    
    # Main PHP handler
    location ~ \\.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    
    # phpMyAdmin configuration
    location ^~ /pma {
        alias /usr/share/phpMyAdmin/;
        
        location ~ ^/pma/(.+\\.php)$ {
            alias /usr/share/phpMyAdmin/$1;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php-fpm/www.sock;
        }
    }
}
  • Always use $request_filename instead of $document_root$fastcgi_script_name when using alias
  • The nested location block for PHP files ensures proper script execution
  • HTTPS configuration should include proper SSL certificates

If you're still facing issues, check these common points:

# Verify file permissions
ls -la /usr/share/phpMyAdmin/

# Check Nginx error logs
tail -f /var/log/nginx/error.log

# Test PHP-FPM connectivity
SCRIPT_NAME=/pma/index.php \
SCRIPT_FILENAME=/usr/share/phpMyAdmin/index.php \
cgi-fcgi -bind -connect /var/run/php-fpm/www.sock