Article Image
Article Image

Nginx supports running multiple domains on the same server. Firstly you set up the DNS from your register (or CDN) to point to the same machine where your server is running.

Then you tell the server which specific folder to serve for each domain.

This works if the websites have a limited amount of traffic. Once the traffic exceeds a certain threshold (based on the capability of your server/VPS) you need to move it to a dedicated machine.

For this example I’ve already everything set up for my main website and I want to copy all of its configurations over to serve a different domain as well. I’m going to use the following placeholders. You should change the site with the domain (without the tld) so valentinourbano for example. Change the tld to your tld for example .com. The user is the account that should have permissions to read the files, in my case I have an account set up as valentinourbano. If you already have a server already setup you can check the username to use by running ls -l on the folder and check the owner and group permission setting for the specific file you want to copy and make sure to match both on your copied file.

$TLD = "TLD"
$USER = "valentinourbano"

sudo cp /etc/nginx/sites-available/ /etc/nginx/sites-available/$NEWSITE.$TLD

sudo nano /etc/nginx/sites-available/$NEWSITE.$TLD

You have now created the configuration for the new url. It is time to create the folder for the files to be served for your new website and point the server to it.

sudo mkdir -p /var/www/$NEWSITE.$TLD/html
sudo chown -R $USER:$USER /var/www/$NEWSITE.$TLD/html
sudo chmod -R 755 /var/www/$NEWSITE.$TLD
nano /var/www/$NEWSITE.$TLD/html/index.html

This is just a basic HTML example to show that the server is working and it is serving the correct folder for the URL.

<html lang="en">

To be able to see the website you need to enable it, by moving it from the available sites to the enabled ones:

sudo ln -s /etc/nginx/sites-available/$NEWSITE.$TLD /etc/nginx/sites-enabled/

After enabling it you need to restart the server for your changes to be loaded.

sudo systemctl reload nginx

The next step is optional. If you don’t need PHP you can leave it disabled. Firstly create a file to check your php configuration. Remember to remove it after you’ve finished the setup so as not to expose sensitive information about your server to everyone.

sudo nano /var/www/html/info.php

Edit the generic configuration file if you’ve never used php on this matching, or the site configuration file to it for this specific site.

Change the base URL to point to index.php instead of index.html if your main file is a php file. You can also change it to a subfolder if you want to serve content from a subfolder.

sudo nano /etc/nginx/sites-available/default

Check your configuration sudo nginx -t Restart the server to load the changes: sudo systemctl reload nginx

Blog Logo

Valentino Urbano



Valentino Urbano

iOS Developer, Swift, Writer, Husband

Back to Overview