Recently we have been playing around with nginx for a couple of projects we have been working on and I think I have fallen in love with a web server. After using apache for so many years, I did not think that this could be possible, but it is!! For what we are using it for, it is superior in almost every aspect: installation, configuration, speed, memory usage, etc. Not to mention the fact that most of our apache deployments are often tainted by the beast that is cPanel/WHM… The reason I am writing this article is to hopefully alleviate the pain of trying to figure out how to setup phpmyadmin or other such applications in a non-document root setup when you are just learning the ins-and-outs of nginx configuration.
Just a quick tip because it is probably the main stumbling block when learning nginx configuration vs apache configuration; the root directive works differently in nginx. In nginx it will append the directory from the matching location block to the request. Taken from the docs: “so that a request for “/i/top.gif” will not look in “/spool/w3/top.gif” [but in /spool/w3/i/top.gif] like might happen in an Apache-like alias configuration where the location match itself is dropped. Use the alias directive to achieve the Apache-like functionality.” But I digress.
Most of the online documentation and tutorials explain how to get php running globally or how to get phpmyadmin to work from document root, but I had to dig deep to get it to work like www.domain.com/phpmyadmin/ . I didn’t want every .php script to be run as php and I did not want phpmyadmin installed at document root. Here is what I found:
location /phpmyadmin { root /usr/local/nginx/html; index index.php; }location ~ ^/phpmyadmin.+.php$ { root /usr/local/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
This combined with spawn-fcgi got everything working exactly how I wanted it to. The first location block is so that static files are served from the correct directory on the server (note how nginx uses the root directive) and makes it so that if you only specify the path in the URL it will execute the correct index file. The second location block tells nginx to only pass files that are in /phpmyadmin and end with .php to the fastcgi process listening on port 9000.
Not the most advanced stuff in the world, but it took me several hours to figure out. I hope it saves you from the dreaded ‘No Input File Specified’ error message and trying to track down permission issues that don’t exist.
Thanks for this post. The difference between root and alias directives in your second paragraph is what saved me. Every blog example on the internet was using root when what I needed was alias.
I love nginx but sometimes it is very difficult to wrap your head around some of the concepts. The documentation is getting better and better every day, but it is still nowhere near the apache documentation.