0

I'm triyng to setup nginx so every vhost can access phpmyadmin like so example.com/phpmyadmin
I tried using this configuration but no luck
this is my default file

server {
    #return 404;
        location /phpmyadmin {
               root /usr/share/nginx/phpmyadmin/;
               index index.php index.html index.htm;
               location ~ ^/phpmyadmin/(.+\.php)$ {
                       try_files $uri =404;
                       root /usr/share/nginx/phpmyadmin/;
                       fastcgi_pass unix:/run/php/php7.1-fpm.sock;
                       fastcgi_index index.php;
                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                       include /etc/nginx/fastcgi_params;
               }
               location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                       root /usr/share/nginx/phpmyadmin/;
               }
        }
        location /phpMyAdmin {
               rewrite ^/* /phpmyadmin last;
        }
}

thanks.

phper
  • 155
  • 9

3 Answers3

1

As far as I know you'll need to set those location /phpmyadmin {...} directives for each server, each virtual host, you can't set them once and be active for all server {} blocks.

The easiest is probably to create an phpmyadmin.include file which you load from every server:

# conf/phpmyadmin.conf
location /phpmyadmin {
           root /usr/share/nginx/phpmyadmin/;
           index index.php index.html index.htm;
           location ~ ^/phpmyadmin/(.+\.php)$ {
                   try_files $uri =404;
                   root /usr/share/nginx/phpmyadmin/;
                   fastcgi_pass unix:/run/php/php7.1-fpm.sock;
                   fastcgi_index index.php;
                   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                   include /etc/nginx/fastcgi_params;
           }
           location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                   root /usr/share/nginx/phpmyadmin/;
           }
}
location /phpMyAdmin {
           rewrite ^/* /phpmyadmin last;
}

and then

server { 
  listen       80;
  server_name  example.com www.example.com;
  include      conf/phpmyadmin.conf;
}
server { 
  listen       80;
  server_name  example.org www.example.org;
  include      conf/phpmyadmin.conf;
}
HBruijn
  • 77,029
  • 24
  • 135
  • 201
0

Its better to use alias rather than rooting to /phpmyadmin/ directory because of security reasons.

and its possible to add an alias to nginx default config file (located in /etc/nginx/sites-available/default) after line server_name _; this way:

location /secretpath {
    alias /usr/share/phpmyadmin/;
    index index.html index.htm index.php;
    location ~ ^/secretpath(.+\.php)$ {
        alias /usr/share/phpmyadmin$1;
      #### here I'm using php8.2 with php8.2-fpm beside nginx webserver
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; #OR fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME /usr/share/phpmyadmin$1;
        include fastcgi_params;
        fastcgi_intercept_errors        on;
    }
}

and then you can use urls like http://anyhosteddomain.com/secretpath to access phpmyadmin. You need to use http protocol rather than https here because of lacking a fixed domain name for including certificates in this method.

Mojtaba Rezaeian
  • 451
  • 5
  • 14
0

As far I know, nginx don't allow share location between servers. You should add phpmyadmin location in each server section(for each domains) or configure nginx to listen on additional port(e.g. listen 88) and configure there location for phpmyadmin, to have it on all domains, but on another port.

Alexander Tolkachev
  • 4,608
  • 3
  • 14
  • 23