Setup phpMyAdmin inside website subdirectory
Asked Answered
B

2

3

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;
Beitch answered 13/6, 2020 at 14:9 Comment(4)
Right way to do it depends on your nginx config. If you can, add to your question the server configuration block for domain1.com domain.Longheaded
Thanks. I shall edit the question.Beitch
Can you show how is your default PHP handler looking? Usually it is location ~ \.php$ { ... } block.Longheaded
@IvanShatsky I have updated my server block config in the main question. Can you please take a look?Beitch
L
8

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!

Longheaded answered 13/6, 2020 at 16:13 Comment(8)
So this location block shall be accessible at domain1.com/phpmyadmin? Do I need to edit the existing symbolic links?Beitch
@ShoumikDas Under domain1.com/phpMyAdmin as you wrote in your question (case sensitive). To make the check case insensitive replace ~ with ~*. No symlinks required at all for this config to work.Longheaded
I get this error with sudo nginx -t: nginx: [emerg] "fastcgi_index" directive is duplicate in /etc/nginx/sites-enabled/domain1.com:26 nginx: configuration file /etc/nginx/nginx.conf test failedBeitch
@ShoumikDas Maybe it is already present in your snippets/fastcgi-php.conf? If it is and it's value is index.php, remove the corresponding line from this location block.Longheaded
@ShoumikDas If it still won't work, add contents of this file to your question too.Longheaded
I get a 404 Not Found Error. These are the contents: # 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;Beitch
@ShoumikDas Nothing of this scrap is needed except fastcgi_index index.php; and include fastcgi.conf; :) Check the updated answer.Longheaded
Yes. That helped. I edited the location block to use include fastcgi.conf;. I did not change anything in fastcgi-php.conf and it worked perfectly. Thanks a ton for your help. My phpadmin is now accessible through SSL. I am relieved.Beitch
N
0

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.

Nancee answered 4/4, 2021 at 2:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.