Skip to content

How to Host Multiple Websites on a Single Server

Nginx Logo

Hosting multiple websites on a single server can be a cost-effective and efficient way to manage your online presence. Whether you’re managing personal projects, client sites, or a combination of both, learning to configure your server for multiple sites is an invaluable skill. In this guide, we’ll walk you through the steps to set up multiple websites on a single server using Nginx.

First, ensure your server is up and running with the necessary software installed. For this guide, we’ll assume you’re using a Linux-based server (such as Ubuntu) with Nginx installed. If Nginx is not installed, you can install it using the following command:

sudo apt update
sudo apt install nginx

Step 2: Configuring Nginx for Multiple Websites

Section titled “Step 2: Configuring Nginx for Multiple Websites”

Nginx uses server blocks to manage multiple websites on a single server. Each server block defines a website configuration. Let’s create configurations for two websites: example.com and demo.lessons4you.info.

Create a new configuration file for example.com:

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

Add the following content to the file:

server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}

Save and close the file. Next, create the web root directory and a sample index file:

sudo mkdir -p /var/www/example.com
echo "<h1>Welcome to example.com!</h1>" | sudo tee /var/www/example.com/index.html

Enable the configuration by creating a symbolic link to the sites-enabled directory:

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

Creating a Server Block for demo.lessons4you.info

Section titled “Creating a Server Block for demo.lessons4you.info”

Similarly, create a configuration file for demo.lessons4you.info:

sudo nano /etc/nginx/sites-available/demo.lessons4you.info.conf

Add the following content to the file:

server {
listen 80;
server_name demo.lessons4you.info;
client_max_body_size 4G;
keepalive_requests 100000;
keepalive_timeout 3000;
add_header Accept-Ranges bytes;
access_log /var/log/nginx/demo.lessons4you.info-access.log;
error_log /var/log/nginx/demo.lessons4you.info-error.log;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Server-name $server_name;
proxy_redirect off;
proxy_connect_timeout 600;
proxy_read_timeout 1200;
proxy_pass http://<web_server_ip>:9763;
}
}

Replace <web_server_ip> with the IP address of the server hosting the content for demo.lessons4you.info.

Create the web root directory and a sample index file:

sudo mkdir -p /var/www/demo.lessons4you.info
echo "<h1>Welcome to demo.lessons4you.info!</h1>" | sudo tee /var/www/demo.lessons4you.info/index.html

Enable the configuration:

sudo ln -s /etc/nginx/sites-available/demo.lessons4you.info.conf /etc/nginx/sites-enabled/

Test your Nginx configuration for syntax errors:

sudo nginx -t

If the test is successful, reload Nginx to apply the changes:

sudo systemctl reload nginx

To secure your websites with SSL, you can use Certbot to obtain and install SSL certificates. Install Certbot and the Nginx plugin:

sudo apt install -y certbot python3-certbot-nginx

Obtain and install the SSL certificate for your domain:

sudo apt install -y certbot python3-certbot-nginx

Certbot will automatically configure Nginx to use the new certificates and reload the configuration.

To ensure your SSL certificates are always up to date, you can set up a cron job to automatically renew them. Certbot includes a script to test the renewal process:

sudo certbot renew --dry-run

If the test is successful, add a cron job to check for renewal twice a day. Open the crontab file:

sudo crontab -e

Add the following line:

0 0,12 * * * /usr/bin/certbot renew --quiet

This will run the renewal command at midnight and noon every day.

By following these steps, you can successfully host multiple websites on a single server using Nginx. This setup helps you manage resources efficiently and reduces costs. If you have any questions or run into issues, feel free to leave a comment below!

https://youtu.be/ensExSEyjuk?si=v9fnROThf8O5ZPjo