skip to Main Content

I’m trying to figure out the best way of securing access to my MariaDB database. I have a root non-wordpress site with 2 wordpress sites as directories (/blog and /shop) – each with separate databases – that use phpMyAdmin as a database viewer (accessible at /phpmyadmin). I want to increase the security so that it can’t be hacked so easily. However, I can’t seem to implement any of the recommended security measures.

Creating a .htaccess and in /usr/share/phpmyadmin and adding the following to whitelist IPs and block all other IPs has no effect:

Order Deny,Allow
Deny from All
Allow from 12.34.56.78

Changing the phpMyAdmin url via the config file (so it’s not accessible at /phpmyadmin) also seems to have no effect.

I’m assuming that it’s because apache is not running (I use Nginx to run my main domain and the 2 wordpress sites). I can’t run apache and Nginx simultaneously (presumably because they’re both fighting for port 80), but what I don’t get is that when Nginx is running and apache is supposedly not running, how is the /phpmyadmin link still accessible?

Here’s my .conf file in /etc/nginx/sites-available (also symlinked to sites-enabled):

upstream wp-php-handler-four {
        server unix:/var/run/php/php7.4-fpm.sock;
}
server {
    listen 1234 default_server;
    listen [::]:1234 default_server;

    root /var/www/site;

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

    location /blog {
        try_files $uri $uri/ /blog/index.php?$args;
    }
    location /shop {
        try_files $uri $uri/ /shop/index.php?$args;
    }
    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass wp-php-handler-four;
    }
}

I followed a tutorial to set this up (maybe I’m misunderstanding how it’s fully set up) but is this not actually using apache to access /phpmyadmin or is it using some web socket? How can I make the above security attempts work?

Note: the /usr/share/phpmyadmin/ dir is symlinked to /var/www/site/

2

Answers


  1. Creating a .htaccess in /usr/share/phpmyadmin and adding the following to whitelist IPs and block all other IPs has no effect:

    Order Deny,Allow
    Deny from All
    Allow from 12.34.56.78
    

    Of course it won’t have any effect since this file processed only by apache.

    I can’t run apache and Nginx simultaneously (presumably because they’re both fighting for port 80)

    In an early days of nginx there was a technique to use nginx for static files and apache to process PHP scripts. Apache was running on some other port (for example, 8080) and listening only on local IP (127.0.0.1). Nginx configuration for that was looking like

    upstream apache {
        server 127.0.0.1:8080;
    }
    server {
        ...
        location ~ .php$ {
            proxy_pass http://apache;
        }
    }
    

    Nowadays it is rarely used since using PHP-FPM is more flexible and gives a less server overhead. However it can be used when you have a complex .htaccess configuration and don’t want to rewrite it for nginx/PHP-FPM.

    but what I don’t get is that when Nginx is running and apache is supposedly not running, how is the /phpmyadmin link still accessible?

    Is this not actually using apache to access /phpmyadmin or is it using some web socket?

    This configuration uses UNIX socket /var/run/php/php7.4-fpm.sock where PHP-FPM daemon is listening for requests (you can read an introduction to this article to get some additional details).

    How can I make the above security attempts work?

    One of many possible solutions is

    1. Unlink /usr/share/phpmyadmin/ from /var/www/site/

    2. Use the following location block (put it before the location ~ .php$ { ... } one:

    location ~ ^/phpmyadmin(?<subpath>/.*)? {
        allow 12.34.56.78;
        # add other IPs here
        deny all;
        alias /usr/share/phpmyadmin/;
        index index.php;
        try_files $subpath $subpath/ =404;
        location ~ .php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$subpath;
            fastcgi_pass wp-php-handler-four;
        }
    }
    
    Login or Signup to reply.
  2. To add to the otherwise quite thorough answer:

    Since Nginx doesn’t use .htaccess files or the same syntax as Apache, you aren’t being restricted as Apache would do. You may wish to find some other solution, or you could use what’s built in to phpMyAdmin: there is a allow/deny functionality built in that you can learn about in the documentation: https://docs.phpmyadmin.net/en/latest/config.html#cfg_Servers_AllowDeny_order (and https://docs.phpmyadmin.net/en/latest/config.html#cfg_Servers_AllowDeny_rules); this will let you restrict access based on username and IP address.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search