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.
Grant ownership of these directories to the Nginx user (www-data) to ensure Nginx has the necessary permissions.
Set appropriate permissions:
3. Create Sample Content
To test the virtual hosts, add sample HTML files for each site.
Example1.com:
Example2.com:
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:
Add the following configuration:
Step 2: Create Configuration for Example2.com
Create a configuration file for example2.com:
Add the following configuration:
5. Enable the Virtual Hosts
Nginx uses symbolic links to enable virtual hosts. Link the configuration files from sites-available to sites-enabled:
6. Test the Configuration
To ensure there are no syntax errors in your configuration, run:
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:
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:
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.