0

I've been trying to set up phpmyadmin on my nginx server using this outdated tutorial from DigitalOcean. The idea is to have the following configuration:

http(s)://example.com => /usr/share/nginx/html

http(s)://example.com/phpmyadmin => /usr/share/phpmyadmin

The tutorial's "solution" is to just create a symlink in /usr/share/nginx/html that points to /usr/share/phpmyadmin. This is the same approach used in this question. Unfortunately, this creates some other problems which I won't go into here.

It seems to me that I should be able to use individual location blocks to control this behavior. I've tried the approach suggested here:

server {
        listen 80 default;

        server_name localhost;

        root /usr/share/nginx/html;
        index index.php index.html index.htm index.nginx-debian.html;

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
                include fastcgi_params;
        }

        location /phpmyadmin {
            root /usr/share/phpmyadmin;
        }

        location ~ /phpmyadmin/.+\.php$ {
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_param  SCRIPT_FILENAME /usr/share/phpmyadmin$fastcgi_script_name;
        }
}

I can still reach nginx's default page at http://example.com, and even execute PHP scripts via URLs such as http://example.com/info.php. Unfortunately, whenever I try to visit any http://example.com/* URL, I get a 404 error.

How should I configure this properly? I'm running PHP7.0 with PHP-FPM on a Ubuntu server.

alexw
  • 371
  • 3
  • 12

1 Answers1

0

I have made some suggestions below. DISCLAIMER: It has not been tested.

server {
    listen 80 default;

    server_name localhost;

    root /usr/share/nginx/html;
    index index.php index.html index.htm index.nginx-debian.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        try_files $uri =404;

        include snippets/fastcgi-php.conf;
        # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ^~ /phpmyadmin {
        root /usr/share;
        try_files $uri $uri/ =404;

        location ~ \.php$ {
            try_files $uri =404;

            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}

Your root statement was wrong, unless phpmyadmin is below /usr/share/phpmyadmin/phpmyadmin.

You can nest the PHP location blocks, and use the ^~ modifier, which looks tidier. See this document for details.

I have added a few try_files statements, in accordance with best practice. Also, a location / block to handle normal URIs.

You may want to change some of those =404 codes into default actions.

Richard Smith
  • 12,834
  • 2
  • 21
  • 29