CREATE MULTIPLE SITES FOR NGINX ON CENTOS 7

Assuming your LEMP( Linux, Nginx. Mysql, PHP) is ready to go but you need some configuration for multiple sites for Nginx on your Centos 7 Linux OS.

Normally, the default root folder is /usr/share/nginx/html/   We’ll change that and create a default folder for let’s say main site

create a new folder called default

$ sudo mkdir /usr/share/nginx/html/default

move all files under html to the new default folder

$ sudo mv /usr/share/nginx/html/*.* /usr/share/nginx/html/default

change permission

$ sudo chmod 755 -R /usr/share/nginx/html/api/

change user and groups

$ sudo chown -R root:nginx /usr/share/nginx/html/default/

since we’ll use multiple sites, add bucket size in global nginx config

$ sudo vi /etc/nginx/nginx.conf
server_names_hash_bucket_size 64;

delete previous phpymyadmin symlink and create new one under new folder

$ sudo rm /usr/share/nginx/html/phpMyAdmin
$ sudo ln -s /usr/share/phpMyAdmin/ /usr/share/nginx/html/default/phpmyadmin

create new config file for the new default folder

$ sudo vi /etc/nginx/conf.d/default.conf

server {

listen 80;
server_name apps.site.com;
root /usr/share/nginx/html/default;
index index.php index.html index.htm;

        location / {
                try_files $uri $uri/ =404;
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location /50x.html {
                root /usr/share/nginx/html/default;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

test your site

http://apps.site.com
http://apps.site.com/phpmyadmin

restart

$ sudo systemctl restart nginx
$ sudo systemctl restart php-fpm

Adding 2nd Site to the Server

We’ll add another site called api. Let’s go through all the same steps quickly

$ sudo mkdir /usr/share/nginx/html/api
$ sudo chmod 755 -R /usr/share/nginx/html/api/
$ sudo chown -R root:nginx /usr/share/nginx/html/api/
$ sudo vi /etc/nginx/conf.d/api.conf

server {

listen 80;
server_name api.site.com;
root /usr/share/nginx/html/api;
index index.php index.html index.htm;

        location / {
                try_files $uri $uri/ =404;
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location /50x.html {
                root /usr/share/nginx/html/api;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

restart

$ sudo systemctl restart nginx
$ sudo systemctl restart php-fpm

test all your sites (we  symlinked phpmyadmin only under default folder not in api folder!)

http://api.site.com 

http://apps.site.com
http://apps.site.com/phpmyadmin

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s