How to add a new domain to your Nginx server

Saul Goodman May 15, 2019 Loading...

This article teaches you how to add a new site into your Nginx server in a few easy steps.

Step 1: create a directory structure within /var/www for your site (your-domain.com) by typing:

sudo mkdir -p /var/www/your-domain

Note that you can use another name for this folder instead of your-domain such as my-dog, my-cat, etc.

Step 2: assign ownership of the directory with the $USER environment variable:

sudo chown -R $USER:$USER /var/www/your-domain

Step 3: Set the permissions of your web roots by typing:

sudo chmod -R 755 /var/www/your-domain

Step 4: Create a sample index.html page to test your website.

nano /var/www/your-domain/index.html

And add some html code into it:

<html>
    <body>
        <h1>This is a very professional website!</h1>
    </body>
</html>

Save and close the html file.

Step 5: Create a file name your-domain.com in /etc/nginx/sites-available by typing:

sudo nano /etc/nginx/sites-available/your-domain.com

Then, coopy and paste the following code into this file:

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

        root /var/www/your-domain;
        index index.html index.htm index.php index.php7 index.php5 index.nginx-debian.html;

        server_name your-domain.com www.your-domain.com;

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

Save and close the file.

Step 6: enable the file you’ve created in step 5 by creating a link from it to the sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/your-domain.com /etc/nginx/sites-enabled/

Step 7: restart your Nginx server:

sudo systemctl restart nginx

Step 8: Type “your-domain.com” or “www.your-domain.com” into your browser address bar to test your new site.

Done!

Related posts

avatar
Welcome to KINDACODE. Have a nice day and code well!