9

I'm setting up phpMyAdmin with nginx. I can visit phpMyAdmin at http://localhost/phpmyadmin. However, when I logged in, the URL is redirected to http://localhost/sql.php instead of http://localhost/phpmyadmin/sql.php.

I have phpMyAdmin symlinked in my /var/www/html/ folder.

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

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

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

    server_name _;

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        include /etc/nginx/snippets/fastcgi-php.conf;
        # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        include fastcgi_params;
    }

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

7 Answers 7

36

You can do as simple as editing phpmyadmin config file without overheading NGINX

  • Open config.inc.php file sudo nano /etc/phpmyadmin/config.inc.php

  • Add the following code to it

    $cfg['PmaAbsoluteUri'] = $_SERVER[HTTP_HOST].dirname($_SERVER[SCRIPT_NAME]);

  • Done

Credits : xaz0r

8
  • This should be the accepted answer. Also, if this does not work, try placing the code into /var/lib/phpmyadmin/config.inc.php instead, not sure why PMA has two possible configuration locations (dumb af), but it is what it is. Somebody needs to rewrite/modernize this great tool.
    – ineedhelp
    Nov 8, 2017 at 6:38
  • Yeah. This too works. But the problem is according to linux best practices of using system folders are "/etc" is used for configurations (.conf files etc). here you find all the configs and settings for your system. "/var" is usually used for log files, 'temporary' files (like mail spool, printer spool, etc), databases, and all other data not tied to a specific user. Logs are usually in "/var/log", databases in "/var/lib" (mysql - "/var/lib/mysql"), etc. Credits : serverfault.com/questions/384342/… Nov 8, 2017 at 8:29
  • 1
    Agreed completely, I wish the PMA devs would follow the protocol. As someone who really values proper and sane organization the codebase is pretty appalling at brief glance. I also just learned that if no config file is found, PMA will default to /usr/share/phpmyadmin/libraries/config.default.php, which states within that it should not be edited, but my custom config in the root directory (where config.sample.php lives) is ignored. Do you know offhand if there is any trouble if the /var/lib/phpmyadmin folder is deleted?
    – ineedhelp
    Nov 9, 2017 at 2:08
  • Also should add, the $cfg['PmaAbsoluteUri'] var is set in the config.default.php file, but it is set to an empty string.
    – ineedhelp
    Nov 9, 2017 at 2:13
  • I searched for two days for a working solution to this problem. This is the ONLY clean one that worked for me! Changing cgi.fix_pathinfo back to 1 could be an easy one, but I was searching for a clean one. I would do +10 if I could! Thank you CaptainAdmin.
    – Vladimir
    Jul 16, 2018 at 12:27
28

I have actually been through so many solutions on StackOverflow today and sadly none of which work and some even given some horrid recommendations. What's scary is how many I came across that were marked as answers.

I just did a brand new Ubuntu 16.04 LEMP server, everything cleanly installed this morning Nginx, mySQL, PHP7.0 and PhpMyAdmin.

This problem of redirecting to

h**p://my.server.ip/

after logging into phpymadmin instead of

h**p://my.server.ip/phpmyadmin

is nothing actually to do with the cgi.fix_pathinfo being set to 0 as recommended by all those guides you read. Read up a little more on why it should be set to 0 in your php.ini file and don't just go and disable it as above.

So in other words leave (as recommended to you) cgi.fix_pathinfo = 0 in your config file for PHP.

THE FIX from this web site (the only one with the correct answer) is to add the following to your /etc/nginx/sites-available/default configuration file. Then restart Nginx ... works immediately, no more re-directing back to root after login.

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

   location /phpMyAdmin {
       rewrite ^/* /phpmyadmin last;
   }
9
  • How to config /etc/nginx/sites-available/default when I want to have phpmyadmin under url site.com/myuniqupage and not site.com/phpmyadmin? Sep 7, 2016 at 10:59
  • Does not work for me. Also, what's with the weird 'add this line'? Should that be enabled or not? (And no, still doesn't work)
    – Diana
    Dec 30, 2016 at 18:04
  • I have corrected the code block removing the two commented out lines as they are not needed. To answer Jasom Dotnet, you need to place this code block in the server {} block for your web site "site.com" and test, it should work fine.
    – MitchellK
    Jan 2, 2017 at 8:22
  • MitchellK I am not sure if you answered the question of @Jason Dotnet, what if don't use the default /phpmyadmin path? What do I have to change? I tried your answer with the default and my own path, both returned a 404. (Leaving the first "location" always my own path)
    – 0lli.rocks
    Mar 4, 2017 at 14:23
  • Apologies for late reply, been running up and down to hospitals for 6 weeks with my very will father, major stress. This post should answer about moving phpmyadmin to a different location - stackoverflow.com/questions/28431926/…
    – MitchellK
    Mar 16, 2017 at 10:37
1

Changing cgi.fix_pathinfo back to 1 fixed the issue for me as well. However, I'm not certain that this is a good solution since guides tell:

"This is an extremely insecure setting because it tells PHP to attempt to execute the closest file it can find if the requested PHP file cannot be found. This basically would allow users to craft PHP requests in a way that would allow them to execute scripts that they shouldn't be allowed to execute."

So it seems there should be a better fix to this so I wouldn't have to compromise on security.

The first post in this topic doesn't work for me. It just disables phpmyadmin... So at the moment I'm left only with cgi.fix_pathinfo workaround.

0

Some of the guides teach you that you have to enable cgi.fix_pathinfo param in the php.ini and set its value to 0 (so effectively disabling it ;)). I did make this change aswel on my php.ini files. After this installed phpmyadmin and the error of redirecting occurs.

After loads of searching, and none of the answers i found were working, it suddenly crossed my mind i'd changed the value of cgi.fix_pathinfo, i put it back to its default value and voila no weird redirection.

1
  • This is too insecure for production, you would have to find another solution.
    – Gary
    May 4, 2017 at 15:05
0

I also ran into this problem and all the solutions I found looked a lot like workarounds which might break once you update phpmyadmin or nginx. I therefore decided to use a subdomain for phpmyadmin.

My phpmyadmin is now available at https://phpmyadmin.your-domain.com. This might be a bit of an overhead, since you need to add the subdomain and get a certificate for this subdomain but at least it won't break in case there is an update for phpymadmin, nginx, etc.

0

After google for some time, just open config.inc.php and add:

$cfg['PmaAbsoluteUri'] = 'your based uri'; 

and everything works perfectly!

0

For me it helped to rewrite the location header of the redirect within the nginx directive:

location /pma/ {
  proxy_pass http://phpmyadmin/;
  proxy_redirect ~/(.+) /pma/$1;
}
    

the phpmyadmin service is reachable by /pma and the redirects after login/logout are rewritten by proxy_redirect ~/(.+) /pma/$1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.