How to Configure Virtual Hosts in Nginx on Ubuntu ⋆ ALexHost SRL

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
31.10.2024

How to Configure Virtual Hosts in Nginx on Ubuntu

Configuring virtual hosts in Nginx allows you to host multiple websites on a single server, with each website having its own domain name. Nginx virtual hosts are configured using “server blocks” that define settings for each website. This guide will walk you through setting up virtual hosts on an Ubuntu server with Nginx.

1. Prerequisites

  • Nginx Installed: Ensure Nginx is installed on your server. If not, install it with:
    sudo apt update sudo apt install nginx
  • Domain Names: Have the domain names you want to host pointed to your server’s IP address. You can modify your /etc/hosts file for testing purposes.

2. Set Up Directories for Each Website

Each website should have its own directory for storing website files. Let’s create directories for two example websites, example1.com and example2.com.

sudo mkdir -p /var/www/example1.com/html sudo mkdir -p /var/www/example2.com/html

Grant ownership of these directories to the Nginx user (www-data) to ensure Nginx has the necessary permissions.

sudo chown -R www-data:www-data /var/www/example1.com/html sudo chown -R www-data:www-data /var/www/example2.com/html

Set appropriate permissions:

sudo chmod -R 755 /var/www

3. Create Sample Content

To test the virtual hosts, add sample HTML files for each site.

Example1.com:

echo "<h1>Welcome to Example1.com!</h1>" | sudo tee /var/www/example1.com/html/index.html

Example2.com:

echo "<h1>Welcome to Example2.com!</h1>" | sudo tee /var/www/example2.com/html/index.html

4. Create Virtual Host Configuration Files

Now, we’ll create a server block (virtual host) configuration file for each website. Nginx stores these configuration files in the /etc/nginx/sites-available/ directory.

Step 1: Create Configuration for Example1.com

Create a new configuration file for example1.com:

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

Add the following configuration:

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

Step 2: Create Configuration for Example2.com

Create a configuration file for example2.com:

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

Add the following configuration:

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

5. Enable the Virtual Hosts

Nginx uses symbolic links to enable virtual hosts. Link the configuration files from sites-available to sites-enabled:

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

6. Test the Configuration

To ensure there are no syntax errors in your configuration, run:

sudo nginx -t

If everything is correct, you’ll see a confirmation message. If there are errors, Nginx will display details to help you troubleshoot.

7. Restart Nginx

After confirming the configuration, restart Nginx to apply the changes:

sudo systemctl restart nginx

8. Access the Websites

If the domain names are pointed to your server, you should now be able to access each site by visiting:

  • http://example1.com
  • http://example2.com

For local testing, you can edit your /etc/hosts file to map the domain names to your server’s IP address. Add entries like:

127.0.0.1 example1.com 127.0.0.1 example2.com

Save and close the file, then try accessing the sites in your browser.

9. Enabling HTTPS for Each Site (Optional)

For secure connections, you can set up SSL certificates using Let’s Encrypt. To enable HTTPS:

Install certbot:

sudo apt install certbot python3-certbot-nginx

Run the following command to obtain and configure SSL certificates for each domain:

sudo certbot --nginx -d example1.com -d www.example1.com sudo certbot --nginx -d example2.com -d www.example2.com

Follow the prompts, and Certbot will automatically set up HTTPS in your Nginx configuration.

Conclusion

By setting up virtual hosts in Nginx on Ubuntu, you can host multiple websites on a single server, each with its own configuration and content. This setup is efficient and scalable, making Nginx an excellent choice for hosting multiple sites on the same server.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills