ADDING MULTIPLE SITES TO THE NGINX SERVER IN UBUNTU 14.04

We already have /etc/nginx/sites-enabled/default config for single site. Now our goal is to add multiple sites without using the default at all. Actually names don’t matter as long as you define your sites correctly

OK we won’t use default anymore, so remove it from sites-enabled. Sites-enabled only contains symlinks, the actual files rely on  /etc/nginx/sites-available/

$ sudo rm /etc/nginx/sites-enabled/default

copy new site file from default called oyvent

$ sudo cp /etc/nginx/sites-available/default   /etc/nginx/sites-available/oyvent

create symlink to the enabled sites

$ sudo ln -s /etc/nginx/sites-available/oyvent   /etc/nginx/sites-enabled/

edit nginx oyvent site config, we also point phpmyadmin here and some rewrite rules

$ sudo vi /etc/nginx/sites-available/oyvent

server {
        listen 80;
        listen [::]:80;

        root /usr/share/nginx/html/oyvent;
        server_name oyvent.com;
        index index.php index.html index.htm;

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # phpmyadmin
        location /phpmyadmin {
                root /usr/share/;
                index index.htm index.html index.php;
                location ~ ^/phpmyadmin/(.+\.php)$ {
                        try_files $uri =404;
                        fastcgi_intercept_errors on;
                        root /usr/share/;
                        fastcgi_pass unix:/var/run/php5-fpm.sock;
                        fastcgi_index index.php;
                        include fastcgi_params;
                }
        }

        location = /phpMyAdmin {
                alias /usr/share/phpMyAdmin/;
        }

        # nginx htaccess configuration
        location / {
                rewrite ^/([a-zA-Z0-9_\-]+)$ /admin/album.php?usr=$1;
        }

        error_log /var/log/nginx/project_error.log;
        access_log /var/log/nginx/project_access.log;
}

restart nginx

$ sudo service nginx restart

Test your site:  http://oyvent.com

 

Adding 2nd Site

Now we go from the same steps for the 2nd site called xtalk except this time create the config file from oyvent because they will be very similar

$ sudo cp /etc/nginx/sites-available/oyvent   /etc/nginx/sites-available/xtalk
$ sudo ln -s /etc/nginx/sites-available/xtalk   /etc/nginx/sites-enabled/
$ sudo vi /etc/nginx/sites-available/xtalk

xtalk is almost the same with oyvent config file except we don’t have any rewrite rule but xtalk also has same phpmyadmin. It’s up to you if you want to include phpmyadmin or not.

The xtalk host is actually xtalkapp.com. The hostname doesn’t have to be the same as the site name. It’s up to you what you define your site’s name.

server {
        listen 80;
        listen [::]:80;

        root /usr/share/nginx/html/xtalk;
        server_name xtalkapp.com;

        index index.php index.html index.htm;

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # phpmyadmin
        location /phpmyadmin {
                root /usr/share/;
                index index.htm index.html index.php;
                location ~ ^/phpmyadmin/(.+\.php)$ {
                        try_files $uri =404;
                        fastcgi_intercept_errors on;
                        root /usr/share/;
                        fastcgi_pass unix:/var/run/php5-fpm.sock;
                        fastcgi_index index.php;
                        include fastcgi_params;
                }
        }

        location = /phpMyAdmin {
                alias /usr/share/phpMyAdmin/;
        }

        error_log /var/log/nginx/project_error.log;
        access_log /var/log/nginx/project_access.log;
}

restart nginx

$ sudo service nginx restart

Test both of your sites: (phpmyadmins are same)

http://xtalkapp.com
http://xtalkapp.com/phpmyadmin

http://oyvent.com
http://oyvent.com/phpmyadmin

Since we use multiple sites, uncomment server_names_hash_bucket_size 64 on the global nginx config file

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

 

Adding PHP Symfony Project  as 3rd Site to the Nginx

Let’s add now the 3rd site called kamp which is a php symfony project. This time create config file from xtalk. It doesn’t matter actually because we’ll edit anyway.

$ sudo cp /etc/nginx/sites-available/xtalk   /etc/nginx/sites-available/kamp
$ sudo ln -s /etc/nginx/sites-available/kamp   /etc/nginx/sites-enabled/
$ sudo vi /etc/nginx/sites-available/kamp

Symfony project layout is different. You need to point the root to the web folder and have rewrite rules

server {
        listen 80;
        listen [::]:80;

        root /usr/share/nginx/html/kamp/web;
        server_name kampapp.com;

        location / {
                # try to serve file directly, fallback to rewrite
                try_files $uri @rewriteapp;
        }

        location @rewriteapp {
                # rewrite all to app.php
                rewrite ^(.*)$ /app.php/$1 last;
        }

        location ~ ^/(app|app_dev|config)\.php(/|$) {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # phpmyadmin
        location /phpmyadmin {
           root /usr/share/;
           index index.htm index.html index.php;
           location ~ ^/phpmyadmin/(.+\.php)$ {
                try_files $uri =404;
                fastcgi_intercept_errors on;
                root /usr/share/;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
           }
        }

        location = /phpMyAdmin {
                alias /usr/share/phpMyAdmin/;
        }

        error_log /var/log/nginx/project_error.log;
        access_log /var/log/nginx/project_access.log;
}

if neede, edit some settings from php ini file

$ sudo vi /etc/php5/fpm/php.ini

post_max_size = 40M
pload_max_filesize = 20M

restart nginx

$ sudo service nginx restart

Test all of your sites: (phpmyadmins are same)

http://kampapp.com
http://kampapp.com/phpmyadmin

http://xtalkapp.com
http://xtalkapp.com/phpmyadmin

http://oyvent.com
http://oyvent.com/phpmyadmin

You can remove the phpmyadmins from 2 of the sites config and use only one site as accessing phpmyadmin

YEEYYYY!!!

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