在网站子目录中设置 phpMyAdmin [英] Setup phpMyAdmin inside website subdirectory

查看:72
本文介绍了在网站子目录中设置 phpMyAdmin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个域的 NGINX 网络服务器,它也运行 phpMyAdmin.

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

phpMyAdmin 工作正常,我通过以下非 https 网址访问它:

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

public-ip-address/phpMyAdmin

public-ip-address/phpMyAdmin

符号链接是这样设置的:

This is how the symbolic link was setup:

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

有没有办法将 phpMyAdmin 指向网站的子目录?

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

例如,我想通过访问以下 URL 来访问 phpMyAdmin 登录页面:

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

domain1.com/phpMyAdmin/

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

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

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

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.

唯一的变化是 serverroot 路径标签.休息一切都是默认的.

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/

我使用 certbot 来加密 SSL 证书.我的服务器块配置在下面共享:

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
}

/etc/nginx/snippets/fastcgi-php.conf 的内容:

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;

推荐答案

这是应该对你有用的 location 块(至少类似的配置对我有用):

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

将它放在之前默认的 PHP 处理程序 location 块,否则默认的 PHP 处理程序块将优先并且此配置将不起作用!

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

这篇关于在网站子目录中设置 phpMyAdmin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆