Unable to setup multisite on nginx & php7.0-fpm

April 20, 2017 7.6k views
Nginx PHP WordPress PHP Frameworks LEMP Ubuntu 16.04

Hi all,

I am trying to solve this issue for few weeks to no avail :( I am trying to set up multiple websites in my Ubuntu 16.04 droplet by following this guide: https://www.digitalocean.com/community/tutorials/how-to-host-multiple-websites-securely-with-nginx-and-php-fpm-on-ubuntu-14-04

Problem is, domain1.com does NOT work (it shows empty pages), while domain2.com works fine.

I have this error and I do not know why:

$ php-fpm7.0 -y /etc/php/7.0/fpm/php-fpm.conf
[20-Apr-2017 16:48:28.895668] ERROR: pid 14360, fpm_sockets_new_listening_socket(), line 182: An another FPM instance seems to already listen on /var/run/php/php7.0-fpm-domain1.sock
[20-Apr-2017 16:48:28.895927] ERROR: pid 14360, fpm_init(), line 74: FPM initialization failed

These are the processes for php-fpm:

root     14272  0.0  3.1 364016 32140 ?        Ss   16:39   0:00 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
domain1 14276  0.0  0.7 364008  7996 ?        S    16:39   0:00 php-fpm: pool domain1
domain1 14277  0.0  1.1 364392 11312 ?        S    16:39   0:00 php-fpm: pool domain1
domain2 14278  0.0  0.7 364008  7996 ?        S    16:39   0:00 php-fpm: pool domain2
domain2 14279  0.0  0.7 364008  7996 ?        S    16:39   0:00 php-fpm: pool domain2

and the sockets:

$ ll /var/run/php/
total 4
drwxr-xr-x  2 www-data   www-data  100 Apr 20 16:39 ./
drwxr-xr-x 26 root       root     1080 Apr 20 16:11 ../
srw-rw----  1 domain2 www-data    0 Apr 20 16:39 php7.0-fpm-domain2.sock=
srw-rw----  1 domain1   www-data    0 Apr 20 16:39 php7.0-fpm-domain1.sock=
-rw-r--r--  1 root       root        5 Apr 20 16:39 php7.0-fpm.pid

Nginx Configuration

/etc/nginx/sites-enabled/domain1
server {
    # redirect www.domain.com to domain.com
    server_name www.domain1.com;
    return 301 $scheme://domain1.com$request_uri;
}
server {
        root /var/www/domain1/public_html;
        server_name domain1.com;
        #access_log /var/log/nginx/domain1.access.log;
        #error_log /var/log/nginx/domain1.error.log;

        location ~ \.php$ {
                try_files $uri =404;
                include fastcgi_params;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm-domain1.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        include global/common.conf;
        include global/wordpress.conf;
}

/etc/nginx/sites-enabled/domain2
server {
    # redirect www.domain.com to domain.com
    server_name domain2.com;
    return 301 $scheme://www.domain2.com$request_uri;
}
server {
        root /var/www/domain2/public_html;
        server_name www.domain2.com;
        #access_log /var/log/nginx/domain2.access.log;
        #error_log /var/log/nginx/domain2.error.log;

        location ~ \.php$ {
                try_files $uri =404;
                include fastcgi_params;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm-domain2.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        include global/common.conf;
        include global/wordpress.conf;
}

/etc/nginx/global/common.conf
# Global configuration file.
# ESSENTIAL : Configure Nginx Listening Port
listen 80;
#listen [::]:80;
# ESSENTIAL : Default file to serve. If the first file isn't found,
index index.php index.html index.htm;
# ESSENTIAL : no favicon logs
location = /favicon.ico {
    log_not_found off;
    access_log off;
}
# ESSENTIAL : robots.txt
location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}
# ESSENTIAL : Configure 404 Pages
error_page 404 /404.html;
# ESSENTIAL : Configure 50x Pages
error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/www;
    }
# SECURITY : Deny all attempts to access hidden files .abcde
location ~ /\. {
    log_not_found off;
    deny all;
}
# PERFORMANCE : Set expires headers for static files and turn off logging.
location ~* ^.+\.(js|css|swf|xml|txt|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bm
p|rtf)$ {
    access_log off; log_not_found off; expires 30d;
}
/etc/nginx/global/wordpress.conf
# WORDPRESS : Rewrite rules, sends everything through index.php and keeps the appended query string intact
location / {
    try_files $uri $uri/ /index.php?q=$uri&$args;
}

# SECURITY : Deny all attempts to access PHP Files in the uploads directory
location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
}
# Allow only internal access to .php files inside wp-includes directory
location ~* ^/wp-includes/.*\.(php|phps)$ {
    internal;
}
# REQUIREMENTS : Enable PHP Support
#location ~ \.php$ {
#    include snippets/fastcgi-php.conf;

    # SECURITY : Zero day Exploit Protection
#    try_files $uri =404;
    # ENABLE : Enable PHP, listen fpm sock
#    fastcgi_split_path_info ^(.+\.php)(/.+)$;
#    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
#    fastcgi_index index.php;
#    include fastcgi_params;
#}
# PLUGINS : Enable Rewrite Rules for Yoast SEO SiteMap
rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;

PHP-FPM Configuration

/etc/php/7.0/fpm/pool.d/domain1.conf
[domain1]
user = domain1
group = domain1
listen = /var/run/php/php7.0-fpm-domain1.sock
listen.owner = domain1
listen.group = www-data
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
/etc/php/7.0/fpm/pool.d/domain2.conf
[domain2]
user = domain2
group = domain2
listen = /var/run/php/php7.0-fpm-domain2.sock
listen.owner = domain2
listen.group = www-data
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /

Does anyone know why this issue occurs? Thanks so much!

3 Answers
hansen April 20, 2017
Accepted Answer

Hi @amellie
Maybe something went wrong at one point, when you restarted php-fpm after doing some configuration changes. Everything looks okay, as far as I can see.
Try to stop php-fpm, then force-kill any lingering processes, and start it again.

service php7.0-fpm stop
killall php-fpm
service php7.0-fpm start

@amellie

Looking at your configuration, I don't really see anything wrong or out of place. If you're still having an issue after shutting down php-fpm and killing off the processes, you may want to try using TCP over sockets.

All you'd need to do is change the listen and fastcgi_pass directives.

This:

listen = /var/run/php/php7.0-fpm-domain1.sock

Would become:

listen = 127.0.0.1:9000

And this:

fastcgi_pass unix:/var/run/php/php7.0-fpm-domain1.sock;

Would become:

fastcgi_pass 127.0.0.1:9000

For each site, you'd raise the port, so 9000 becomes 9001 for the second, 9002 for the third, etc.

Once the changes are made, you'd restart php-fpm and nginx.

Do you was See This Tutorial

sudo nano /etc/nginx/nginx.conf

Then change

http {
    . . .

    server_names_hash_bucket_size 64;

    . . .
}

by Justin Ellingwood
When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server. In this guide, we'll discuss how to configure server blocks in Nginx on an Ubuntu...
Have another answer? Share your knowledge.