16:09 - Wednesday, 23 April 2014

How To Setup PhpMyAdmin With Nginx?

#Topics: how to connect to nginx phpmyadmin,protect phpmyadmin page nginx

I created an entry in virtuals.conf for phpMyAdmin and restarted Nginx, but I can not get it to actually server phpMyAdmin. PHP is installed and configured with Nginx for fast-cgi, the virtuals.conf entry syntax checks out fine, and the mysql server is running.

Also FWIW, centos is the hostname of my CentOS VM I’m running.

Here is what my entry looks like:

    server {    listen 80;    server_name mysql.centos;    location / {        root /usr/share/phpMyAdmin;        index index.php;    }    location ~ .php$ {        fastcgi_pass;        fastcgi_index index.php;        fastcgi_param SCRIPT_FILENAME /usr/share/phpMyAdmin$fastcgi_script_name;        include fastcgi_params;    }}

See an example from my setup:

server {    listen 80;    server_name domain.com;    # Server root    root /home/user/domain.com/public_html;    index index.html index.php;    location /MySQLAdmin {        auth_basic "Restricted Access";        auth_basic_user_file htpasswd_file;        alias /usr/share/phpMyAdmin/;        try_files $uri $uri/ @fastcgi;    }    location ~ .+.php$ {        # Return '400 Bad Request' for malformed URLs        location ~ ..*/.*.php$ {            return 400;        }        # Map 418 error code (no bady uses) to fastcgi        error_page 418 = @fastcgi;        return 418;    }    location @fastcgi {        fastcgi_pass;        ...    }    location / {        try_files $uri $uri/ @fastcgi;    }}

Requests to “MySQLAdmin” will be handled by that location which is aliased to phpMyAdmin folder. Nginx will return static files directly and send PHP to @fastcgi.
Requests to “.php” will be sent to @fastcgi.

Nginx tries to serves everything else and sends those not found to @fastcgi. Basically, static files will be served by Nginx. Index.php requests not explicit written out will not be found and passed to @fastcgi.