Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've recently installed phpmyadmin and nginx on my server, but I want to make it accessible to all domains without copying it to every single domain's web root. Is this possible? if so, how?

EDIT: This is what's in my config now, lain

        location /phpmyadmin/
                alias /home/phpmyadmin/;

        location ~ ^/phpmyadmin/(.+\.php)$
                alias /home/phpmyadmin/$1;
                include fcgi.conf;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;
share|improve this question
up vote 4 down vote accepted

According to nginx documentation, location cannot be used inside http block. This means that you have to add locations for every virtualhost. However you can save your bunch of layouts into a separate file and include it with includecommand. Here is an example for a generic virtualhost:

server {
  listen 80;
  root /var/www/nginx-default;

  include /etc/nginx/global.d/*.conf;

And you write all global locations in a separate *.conf files under /etc/nginx/global.d. Your code may be placed in /etc/nginx/global.d/phpmyadmin.conf, for example.

Update: As for your PHPMyAdmin locations: instead of aliasing every file you should pass SCRIPT_FILENAME variable in the following way:

location ~ ^/phpmyadmin/(.+\.php)$ {
    include fcgi.conf;
    fastcgi_index index.php;
    fastcgi_pass  unix:/tmp/php-cgi.sock;
    fastcgi_param SCRIPT_FILENAME /home/phpmyadmin$fastcgi_script_name;

Note that you need to use /home/phpmyadmin instead of $document_root because alias directive does not change document root. This means that for my generic virtualhost (as above) requesting URL http://localhost/phpmyadmin/test.php will try to execute /var/www/nginx-default/phpmyadmin/test.php which is not the expected behaviour.

share|improve this answer
That's rather genius. Thanks. However, why is my location not working? Do you know? – Rob Mar 7 '12 at 0:33
thanks for the update. Still getting 404 though, and /home/phpmyadmin/ IS where the files are. – Rob Mar 8 '12 at 11:48
In most cases 404 error is issued by nginx. If this is the nginx 404 page and not fastcgi 404 error (blank screen for php-cgi/php-fpm, for example) then your php files don't match fastcgi location. Please check your nginx error.log for more information which one location takes precedence over your /phpmyadmin/.+\.php location. – jollyroger Mar 9 '12 at 9:36
What if it's the fastcgi? – Rob Mar 10 '12 at 4:02
You'll get an empty screen on default PHP-cgi/fastcgi setup. You'll get some meaningfull HTML results only if debug/error reporting flags are enabled in correspondent php.ini. In other cases you should look for a cgi/fastcgi-wrapper logs. – jollyroger Mar 11 '12 at 9:31

Put your phpmyadmin in a common directory that is accessible by all sites and try an Alias

location  /phpmyadmin/ {
  alias  /path/to/phpmyadmin/;

location ~ ^/phpmyadmin/(.+\.php)$ {
  alias /path/to/phpmyadmin/$1;
  include /path/to/your/fastcgi.config;
  fastcgi_pass unix:/tmp/php.socket;

If you don't use the unix socket fastcgi then the last line would probably be something like this

share|improve this answer
Will I need to put that line in every server {}, or is there a way to make it global? – Rob Mar 1 '12 at 7:55
I think so, there isn't any way to make a global loation. – Iain Mar 1 '12 at 8:22
After a bit of research, I've found the answer is no. As such, I've put that line in a few of the server blocks for a few domains; it isn't working :( It returns 404 – Rob Mar 1 '12 at 8:22
Did you put the trailing / on the URL in your browser ? Without it as written above it will 404. f you remove the trailing / from the location block you don't need it in your URL. – Iain Mar 1 '12 at 8:29
Well this is strange. It seems it simply can't find the .php files... .html and .txt files are working fine. – Rob Mar 1 '12 at 8:37

@Rob you can change your log level to debug to follow nginx operations. You have to fix your SCRIPT_FILENAME parameter from fcgi.conf file or you can set it in your conf file.

share|improve this answer
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; What do you mean fix? What's wrong with it? – Rob Mar 4 '12 at 10:37
if you change log level to debug, you can find problem. It will show you which files are tried to open or you can use strace easily to follow nginx process. – dirigeant Mar 6 '12 at 3:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.