How to Setup Multiple WordPress Sites on Nginx

Share this Article :

This article describes how to install and configure multiple WordPress sites on Nginx and the steps have been prepared based on CentOS 7.0 and Nginx 1.6.3. NGINX (pronounced Engine ex) is an open source high performance web servers and able to handle large number of concurrent connections. It has the lowest memory footprint if compared to the alternate web server, apache http server. Please follow the following steps to host multiple WordPress sites on Nginx. Please note that this configuration also tested working on RHEL 7 and Oracle Linux 7.

Steps to Setup Multiple WordPress Sites on Nginx

1. First, we need to set up directories for multi-sites the server blocks and additional WordPress configuration files :

# mkdir -p /etc/nginx/conf.d
# mkdir -p /etc/nginx/sites-available

2. Tell the main nginx.conf file to look for the new setup directories :

# vi /etc/nginx/nginx.conf

Add the following into the configuration file :

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-available/*.conf;
user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;


    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-available/*.conf;
}

3. Add new wordpress configuration file :

# vi /etc/nginx/conf.d/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;
}
# REQUIREMENTS : Enable PHP Support
location ~ \.php$ {
    # SECURITY : Zero day Exploit Protection
    try_files $uri =404;
    # ENABLE : Enable PHP, listen fpm sock
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_send_timeout 300s;
    fastcgi_read_timeout 300s;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 256 4k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_intercept_errors on;
}
# PLUGINS : Enable Rewrite Rules for SiteMap
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml$ "/index.php?xml_sitemap=params=$2" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml\.gz$ "/index.php?xml_sitemap=params=$2;zip=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html$ "/index.php?xml_sitemap=params=$2;html=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html.gz$ "/index.php?xml_sitemap=params=$2;html=true;zip=true" last;

4. Add new common configuration file :

# vi /etc/nginx/conf.d/common.conf
# Global configuration file.
# ESSENTIAL : Configure Nginx Listening Port

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/html;
    }
# SECURITY : Deny all attempts to access hidden files .abcde
location ~ /\. {
    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|bmp|rtf)$ {
    access_log off; log_not_found off; expires 30d;
   #    expires max;
   add_header Pragma no-cache;
   add_header Cache-Control "public";
}

5. Create the document root for mutiple websites :

# mkdir -p /var/www/html/example-site1
# mkdir -p /var/www/html/example-site2

6. Add the server block configuration for example-site1.com :

# vi /etc/nginx/sites-available/example-site1.com.conf

Add lines as below :

server {
    listen     80;
    server_name example-site1.com;
    rewrite ^/(.*)$ http://www.example-site1.com/$1 permanent;
}

server {
        server_name www.example-site1.com;
        root /var/www/html/example-site1;
        access_log /var/log/nginx/example-site1.com.access.log;
        error_log /var/log/nginx/example-site1.com.error.log;
        include conf.d/common.conf;
        include conf.d/wordpress.conf;
}

7. Add the server block configuration for example-site2.com :

# vi /etc/nginx/sites-available/example-site2.com.conf

Add lines as below :

server {
    listen     80;
    server_name example-site2.com;
    rewrite ^/(.*)$ http://www.example-site2.com/$1 permanent;
}

server {
        server_name www.example-site2.com;
        root /var/www/html/example-site2;
        access_log /var/log/nginx/example-site2.com.access.log;
        error_log /var/log/nginx/example-site2.com.error.log;
        include conf.d/common.conf;
        include conf.d/wordpress.conf;
}

8. Check the nginx syntax :

# sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

9. Restart Nginx service to apply for Multiple WordPress sites on Nginx configuration :

# sudo systemctl restart  nginx.service

Multiple WordPress sites on Nginx

Resource

One Response

  1. adnan

Leave a Reply