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.
Step 1: Setting Up Your Server
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:
1 2 3 4 | sudo apt update sudo apt install nginx |
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
.
Creating a Server Block for example.com
Create a new configuration file for example.com
:
1 2 3 | sudo nano /etc/nginx/sites-available/example.com |
Add the following content to the file:
1 2 3 4 5 6 7 8 9 10 11 12 | 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:
1 2 3 4 | 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:1 2 3 | sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ |
Creating a Server Block for demo.lessons4you.info
Similarly, create a configuration file for demo.lessons4you.info
:
1 2 3 | sudo nano /etc/nginx/sites-available/demo.lessons4you.info.conf |
Add the following content to the file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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:
1 2 3 4 | 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:
1 2 3 | sudo ln -s /etc/nginx/sites-available/demo.lessons4you.info.conf /etc/nginx/sites-enabled/ |
Testing the Configuration
Test your Nginx configuration for syntax errors:
1 2 3 | sudo nginx -t |
If the test is successful, reload Nginx to apply the changes:
1 2 3 | sudo systemctl reload nginx |
Step 3: Securing Your Websites with SSL
To secure your websites with SSL, you can use Certbot to obtain and install SSL certificates. Install Certbot and the Nginx plugin:
1 2 3 | sudo apt install -y certbot python3-certbot-nginx |
Obtain and install the SSL certificate for your domain:
1 2 3 | sudo apt install -y certbot python3-certbot-nginx |
Certbot will automatically configure Nginx to use the new certificates and reload the configuration.
Auto-Renewing SSL Certificates
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:
1 2 3 | 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:
1 2 3 | sudo crontab -e |
Add the following line:
1 2 3 | 0 0,12 * * * /usr/bin/certbot renew --quiet |
This will run the renewal command at midnight and noon every day.
Conclusion
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!