7

I have been trying to get this to work for hours now!

I would like to set up a simple web server. My web files shall be in /var/www. I also want to have phpmyadmin. I created a directory /var/phpmyadmin. Now I want to acces the normal web files in the standard way.
For instance: The file /var/www/test.php should be accesible with http://localhost/test.php.
The phpmyadmin part should be acces like this: http://localhost/phpmyadmin. With the config below I get a 404. Also with this URL: http://localhost/phpmyadmin/index.php

For this I created this file in the sites-availble folder of nginx:

server {
  listen 80; ## listen for ipv4; this line is default and implied
  listen [::]:80 default_server ipv6only=on; ## listen for ipv6

  root /var/www;
  index index.html index.htm index.php;

  try_files $uri $uri/ $uri/index.html $uri/index.htm $uri/index.php;

  # This didn't work
  location /phpmyadmin/ {
     alias /var/phpmyadmin;
  }

  # And this did neither. (Never used both at the same time!)
  location /phpmyadmin/ {
     root /var;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    # With php5-cgi alone:
    #fastcgi_pass 127.0.0.1:9000;
    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }

  location ~ /\.ht {
    deny all;
  }
}

What am I doing wrong?

Edit:

Interesting to note is that this works (The root directory works (http://localhost)):

  root /var/www/htdocs;

  index index.php index.html index.htm;

  location /phpmyadmin/ {
    root /var/www/phpmyadmin;
  }

And this doesn't:

  index index.php index.html index.htm;

  location / {
    root /var/www/htdocs;
  }

  location /phpmyadmin/ {
    root /var/www/phpmyadmin;
  }

phpmyadmin still doesn't work!

9
+50

Your goal is to completely separate your "regular" web files from your phpMyAdmin installation.

It should be stressed that each server configuration in Nginx can (and should) have only one webroot. That being said, these are your options:

  • Install phpMyAdmin in a directory under your webroot, which in your case is /var/www/phpmyadmin. It can be accessed through http://localhost/phpmyadmin

    This is the simplest configuration and I'm including it here for the sake of completeness (and people coming here from search engines).

  • Install phpMyAdmin in a directory outside your webroot and then create a symlink named phpmyadmin in your webroot pointing to that directory. In that case, you need to make sure that you have specified disable_symlinks off in your server configuration.

  • You can achieve separation on the same vhost by creating 2 server configurations listening on different ports, having different webroots and communicating through proxy_pass directive. A basic outline of such configuration is the following:

    server {
        listen   80;
        server_name localhost;
        root /var/www/htdocs;
        index index.php index.html index.htm;
    
        location /phpmyadmin {
            proxy_pass http://127.0.0.1:8080/;
        }
    
        # ...Add more location directives, php support, etc...
    }
    
    server {
        listen 8080;
        server_name localhost;
        root /var/www/phpmyadmin;
        index index.php index.html index.htm;
    
        # ...Specify additional location directives, php support, etc...
    }
    

    In this case, all requests to phpMyAdmin will be transparently passed to the server instance listening on port 8080 through the /phpmyadmin location in the server instance listening on port 80.

  • Finally you can achieve separation on different vhosts by creating 2 server configurations listening on the same port, but having different server_name directives and different root locations. For example, a basic outline like this:

    server {
        listen   80;
        server_name dev.local;
        root /var/www/htdocs;
        index index.php index.html index.htm;
    
        # ...Add more location directives, php support, etc...
    }
    
    server {
        listen   80;
        server_name phpmyadmin.local;
        root /var/www/phpmyadmin;
        index index.php index.html index.htm;
    
        # ...Specify additional location directives, php support, etc...
    }
    

Then, you would go ahead and add the following entries to your /etc/hosts:

127.0.0.1    dev.local
127.0.0.1    phpmyadmin.local

and then you can access your files through http://dev.local and your phpMyAdmin instance through http://phpmyadmin.local. Obviously, from your local workstation.

  • Still doesn't work! – BrainStone Jun 4 '14 at 14:30
  • I update my question with more results from my attempts to get it to work! – BrainStone Jun 4 '14 at 14:42
  • @BrainStone I have updated my answer. Seems like I fell in one of the nginx configuration pitfalls. – dkaragasidis Jun 4 '14 at 15:13
  • I would still like to keep my phpmyadmin files out of my webfiles directory! This must be possible! – BrainStone Jun 4 '14 at 15:20
  • @BrainStone You can add a new server directive to listen on a different port and have different webroot pointing to phpmyadmin installation. Then you can create a /phpmyadmin/ location that includes a proxy_pass directive pointing to that new server directive you created that is responsible for serving your phpmyadmin installation. – dkaragasidis Jun 4 '14 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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