Use phpmyadmin with nginx and php7

phpmyadmin page

Install the phpmyadmin following the normal application installation
process.

Install phpmyadmin

sudo apt-get install phpmyadmin

Follow the normal installation process of phpmyadmin. Once the
phpmyadmin configuration is finished, we need to configure the
nginx configuration.

Nginx Configuration

Edit the nginx configuration (/etc/nginx/sites-available/default)
and add the following configurations to it. Open the sample configuration

# 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 127.0.0.1:9000;
               #fastcgi_param HTTPS on; # <-- add this line
		       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;
   }

php7 and php-gettext symbolic links

Since php7 by default do not come with php-gettext we will need to install
these packages too.

sudo apt-get install php-gettext

If this package is not available then you will see following error messages
in your nginx error logs in /var/log/nginx/error.log

2016/04/28 10:22:30 [error] 17184#0: *6 FastCGI sent in stderr: “PHP message: PHP Fatal error: require_once(): Failed opening required ‘./libraries/php-gettext/gettext.inc’ (include_path=’.:/usr/share/php’) in /usr/share/phpmyadmin/libraries/select_lang.lib.php on line 395” while reading response header from upstream, client: 127.0.0.1, server: localhost, request: “GET /phpmyadmin/ HTTP/1.1”, upstream: “fastcgi://unix:/var/run/php/php7.0-fpm.sock:”, host: “localhost”

Once php-gettext is installed use the below command to reconfigure the
symbolic links to gettext.

rm /usr/share/phpmyadmin/libraries/php-gettext/*
ln /usr/share/php/php-gettext/* /usr/share/phpmyadmin/libraries/php-gettext

Restart the nginx server

sudo service nginx restart

Browse http://localhost/phpmyadmin and it should show up the phpmyadmin login page.
– You may have to hard refresh to get to the page, since browser caches this page.

References:


https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-with-nginx-on-an-ubuntu-14-04-server
https://www.digitalocean.com/community/questions/issues-with-phpmyadmin-on-nginx-ubuntu-14-04

Samundra Shrestha

I work as fullstack developer. Having spent more than 5 years in the web development technologies, I aspire to create an environment where we can share, learn and grow together.

12 thoughts on “Use phpmyadmin with nginx and php7

  • March 19, 2017 at 7:36 AM
    Permalink

    Man you save my days!! I love you! YOu are the best!

    Reply
  • March 17, 2017 at 4:34 PM
    Permalink

    I also have this redirect problem and wanted to fix it. The solution is here but badly explained.

    I know what to add and to what file, but where exactly in that file? Tried to add it at the end, on the beginning, each time it messed the nginx itself and had to go back to default default file ;).

    Can anyone tell exactly in which place I must add this block of text? On other site someone mentioned I should add it to server block but I have no idea what is it, there is so many lines in that file mentioning server, so placing that text in wrong position easily can mess everything. Any help?

    Reply
    • March 17, 2017 at 5:15 PM
      Permalink

      Hi Mike,

      You can do it in different ways. Easiest is to place it directly in nginx. Please check this https://gist.github.com/samundra/01f4cc8086680d0640465eee1fe6d817 I have updated the code near the bottom. After updating it you should restart nginx. Before restarting server, I would also suggest you check that your nginx configuration file has no syntax problems. You can do that with `sudo nginx -t` at command line. Please let me know if you still have problem with it.

      Reply
      • March 17, 2017 at 10:08 PM
        Permalink

        Wow, thanks for such quick replay!

        I tried to use this code but nginx fails, so I check it as you suggested and got:

        nginx: [emerg] “location” directive is not allowed here in /etc/nginx/sites-enabled/default:84
        nginx: configuration file /etc/nginx/nginx.conf test failed

        If I counted well to 84, the issue is with those lines:

        location /phpmyadmin {
        root /usr/share/;
        index index.php index.html index.htm;
        location ~ ^/phpmyadmin/(.+\.php)$ {

        Seems like location of files are incorrect, but then again, I do have /usr/share/phpmyadmin so I have no clue what it wants.

        I’m on Ubuntu Server 16.04 if that helps.

        Reply
  • September 13, 2016 at 7:13 AM
    Permalink

    Actually, the fix for the redirect issue was much simpler.

    Simply disable the following line in php.ini again:
    cgi.fix_pathinfo

    Reply
  • September 7, 2016 at 11:01 AM
    Permalink

    How to config /etc/nginx/sites-available/default when I want to have phpmyadmin under url site.com/myuniqupage and not site.com/phpmyadmin?

    Reply
    • October 9, 2016 at 4:27 PM
      Permalink

      If you haven’t found the solution yet. Actually /phpmyadmin is just an alias. You can edit phpmyadmin.ini to use your own alias.

      Reply
  • June 28, 2016 at 12:26 PM
    Permalink

    I found hundreds of very bad and incorrect answers out there to a problem of phpmyadmin redirecting to http://my.server.ip/ instead of http://my.server.ip/phpmyadmin after logging in.

    Your configuration section above to be added to the nginx default conf file is the only correct solution out there.

    I have added it as a suggested answer at http://stackoverflow.com/questions/37408704/nginx-with-phpmyadmin-wrong-direction-on-login

    Thanks for your help, works like a charm.

    Reply
    • August 8, 2016 at 12:49 AM
      Permalink

      Hey Mitchell,

      Thanks for stopping by and sharing your thoughts. I really feel grateful that it worked :)

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *