Setup phpMyAdmin inside website subdirectory

Viewed 2816

I have an NGINX web server with two domains and it also runs phpMyAdmin.

phpMyAdmin is working fine and I access it through the below non-https url:

public-ip-address/phpMyAdmin

This is how the symbolic link was setup:

sudo ln -s /usr/share/phpmyadmin/ /var/www/html

Is there a way I can point phpMyAdmin to a website's subdirectory?

For example, I would like to access the phpMyAdmin login page by accessing the following URL:

domain1.com/phpMyAdmin/

How can I achieve this? domain1.com has https enabled. So it would also secure my phpMyAdmin login.

The server block is the same as the default block for NGINX. I have created a new config file by copying it to domain.com in the /etc/NGINX/sites-available folder.

The only changes are in the server and root path tags. Rest everything is default.

server domain1.com www.domain1.com;

root /var/www/domain1.com/html/

I am using certbot for Let's Encrypt SSL certificates. My server block config is shared below:

# Server Block Config for domain1.com
server {
    root /var/www/domain1.com/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name domain1.com www.domain1.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        # try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php?q=$uri&$args; 
    }

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = www.domain1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = domain1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;

    server_name domain1.com www.domain1.com;
    return 404; # managed by Certbot
}

Contents of /etc/nginx/snippets/fastcgi-php.conf:

# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;
2 Answers

Here is the location block that should work for you (at least the similar config works for me):

    location ~* ^/phpmyadmin(?<pmauri>/.*)? {
        alias /usr/share/phpmyadmin/;
        index index.php;
        try_files $pmauri $pmauri/ =404;
        location ~ \.php$ {
            include fastcgi.conf;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$pmauri;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }
    }

Place it before the default PHP handler location block, or the default PHP handler block will take precedence and this configuration won't work!

You can simply add another symlink to the domain1.com root, while keeping everything else same, like you did for the default domain.

sudo ln -s /usr/share/phpmyadmin/ /var/www/domain1.com/html

Am I missing something? I came to this thread while looking for a solution to another problem (Nginx alias breaks due to try_files $uri alias bug) but since you are already using symlink to phpmyadmin for the site you access through IP, you can do the same for any domain.

Related