How to Host Multiple Websites on a Single Server
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
Section titled “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:
sudo apt updatesudo apt install nginxStep 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.
Creating a Server Block for example.com
Section titled “Creating a Server Block for example.com”Create a new configuration file for example.com:
sudo nano /etc/nginx/sites-available/example.comAdd 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.comecho "<h1>Welcome to example.com!</h1>" | sudo tee /var/www/example.com/index.htmlEnable 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.confAdd 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.infoecho "<h1>Welcome to demo.lessons4you.info!</h1>" | sudo tee /var/www/demo.lessons4you.info/index.htmlEnable the configuration:
sudo ln -s /etc/nginx/sites-available/demo.lessons4you.info.conf /etc/nginx/sites-enabled/Testing the Configuration
Section titled “Testing the Configuration”Test your Nginx configuration for syntax errors:
sudo nginx -tIf the test is successful, reload Nginx to apply the changes:
sudo systemctl reload nginxStep 3: Securing Your Websites with SSL
Section titled “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:
sudo apt install -y certbot python3-certbot-nginxObtain and install the SSL certificate for your domain:
sudo apt install -y certbot python3-certbot-nginxCertbot will automatically configure Nginx to use the new certificates and reload the configuration.
Auto-Renewing SSL Certificates
Section titled “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:
sudo certbot renew --dry-runIf the test is successful, add a cron job to check for renewal twice a day. Open the crontab file:
sudo crontab -eAdd the following line:
0 0,12 * * * /usr/bin/certbot renew --quietThis will run the renewal command at midnight and noon every day.
Conclusion
Section titled “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!