Skip to content

Instantly share code, notes, and snippets.

@santoshachari
Last active October 3, 2024 21:26
Show Gist options
  • Save santoshachari/8238a7a4e7132d3c86a8720a81cd9fa1 to your computer and use it in GitHub Desktop.
Save santoshachari/8238a7a4e7132d3c86a8720a81cd9fa1 to your computer and use it in GitHub Desktop.
Setup additional sites on Nginx

Inspired and edited from this Digital Ocean tutorial.

Follow the steps on this gist to setup a LEMP stack with PHP 7.0.

The steps assume Nginx has been configured correctly.

Setup the root directories

For the domains example.com and test.com, create the folders.

sudo mkdir -p /var/www/example.com/html
sudo mkdir -p /var/www/test.com/html

Setup right permissions

sudo chown -R $USER:$USER /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/test.com/html
sudo chmod -R 755 /var/www

For testing purposes, setup the default html pages.

nano /var/www/example.com/html/index.html

And paste:

<html>
    <head>
        <title>Welcome to Example.com!</title>
    </head>
    <body>
        <h1>Success!  The example.com server block is working!</h1>
    </body>
</html>

Similary for test.com:

cp /var/www/example.com/html/index.html /var/www/test.com/html/
nano /var/www/test.com/html/index.html

And modify:

<html>
    <head>
        <title>Welcome to Test.com!</title>
    </head>
    <body>
        <h1>Success!  The test.com server block is working!</h1>
    </body>
</html>

Configure nginx for the domains

Example.com:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
sudo nano /etc/nginx/sites-available/example.com

Edit the root and server_name. Also note, there could be only one server block with default_server tags with listen. Keep as per need.

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

        root /var/www/example.com/html;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name example.com www.example.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?$query_string;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

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

        # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
        #location /RequestDenied {
        #       proxy_pass http://127.0.0.1:8080;
        #}
        
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

Simlarly for test.com

sudo cp /etc/nginx/sites-available/example.com /etc/nginx/sites-available/test.com
sudo nano /etc/nginx/sites-available/test.com

And paste:

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

        root /var/www/test.com/html;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name test.com www.test.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?$query_string;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

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

        # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
        #location /RequestDenied {
        #       proxy_pass http://127.0.0.1:8080;
        #}
        
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

Enable the server blocks

You'd have to create symboitic links in sites-enabled directory for these server blocks to be active.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/

If you set any of them as the default_server, then unlink the one that came with the nginx package.

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

Adjust Nginx config

sudo nano /etc/nginx/nginx.conf

And uncomment this line:

server_names_hash_bucket_size 64;

Now restart nginx

sudo service nginx restart

Testing (Optional)

If you have followed the exact steps as above, and want to try with test.com and example.com, modify the hosts file in your operating system to point to the server:

Mac/Linux:

sudo nano /etc/hosts

Windows:

  1. Cmd + R (to get the run dialog)
  2. Navigate to: %windir%\System32\Drivers\etc\
  3. Edit hosts file.

Add the following two lines:

<Server IP address> example.com
<Server IP address> test.com

You can now call the domains on your browsers.

http://example.com should output:

Example.com

and http://test.com should output:

Test.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment