How to set up phpMyAdmin with nginx


It seems that phpMyAdmin is generally served with Apache or lighttp, since those are the options the Ubuntu package gives for configuring during the install. However, I use nginx for my web server. This is how you can use nginx to serve phpMyAdmin.

Install nginx and phpMyAdmin
If you haven't installed nginx and phpMyAdmin yet, do that. I use Ubuntu and this is how you install them with that OS:

sudo apt-get install nginx phpmyadmin

An important part to note is that you shouldn't select apache or lighttp when phpmyadmin's installer askes which server you use.

Also, go ahead and allow it to create a database (you should have MySQL installed). When the installer asks about using dbconfig-common to set up your database, select yes.

Then follow the prompts, giving it your root MySQL password and remembering the password you assign phpMyAdmin for later.

Configure nginx to serve phpMyAdmin
nginx needs a configuration block to serve phpMyAdmin pages. Your root directory for phpMyAdmin should be in /usr/share/phpmyadmin, or something similar. To serve phpMyAdmin using php5-fpm socket, you will need this server block in your nginx configuration. I serve phpMyAdmin on port 81 and visit it via localhost:81.

    server {
        listen          81;
        server_name     localhost;
        root        /usr/share/phpmyadmin;
        index       index.php index.html index.htm;
        if (!-e $request_filename) {
            rewrite ^/(.+)$ /index.php?url=$1 last;
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include /etc/nginx/fastcgi_params;

Now you should reload nginx's configuration: sudo nginx -s reload

When you visit localhost:81, you should see phpMyAdmin's log in page.