在网站子目录中设置 phpMyAdmin

Setup phpMyAdmin inside website subdirectory

我有一个带有两个域的 NGINX Web 服务器,它还运行 phpMyAdmin。

phpMyAdmin 工作正常,我通过以下非 https url:

访问它

public-ip-address/phpMyAdmin

这是符号 link 的设置方式:

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

有什么方法可以将 phpMyAdmin 指向网站的子目录?

例如我想访问phpMyAdmin的登录页面 URL:

domain1.com/phpMyAdmin/

我怎样才能做到这一点? domain1.com 已启用 https。所以它也可以保护我的 phpMyAdmin 登录。

服务器块与 NGINX 的默认块相同。我通过将其复制到 /etc/NGINX/sites-available 文件夹中的 domain.com 创建了一个新的配置文件。

唯一的变化是 serverroot 路径标记。其他一切都是默认的。

server domain1.com www.domain1.com;

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

我正在使用 certbot 获取 Let's Encrypt SSL 证书。我的服务器块配置共享如下:

# 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
}

/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;

这是应该适合您的 location 块(至少类似的配置适合我):

    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;
        }
    }

将它放在之前默认的PHP处理程序location块,或者默认的PHP处理程序块将优先并且此配置获胜没用!

您可以简单地向 domain1.com 根添加另一个符号链接,同时保持其他一切不变,就像您对默认域所做的那样。

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

我错过了什么吗? 我在寻找另一个问题的解决方案时来到这个线程 (),但是由于您已经在通过 IP 访问的站点使用指向 phpmyadmin 的符号链接,因此您可以对任何域执行相同的操作。