How to Configure Nginx For Multi-Domain Hosting
Nginx is a powerful web server that is widely used for hosting multiple domains seamlessly. Configuring Nginx for multi-domain hosting can enhance performance and manageability. In this guide, we will walk you through the step-by-step process of setting up Nginx for hosting multiple domains.
Step 1: Install Nginx
First, ensure that Nginx is installed on your server. You can install it on a Ubuntu system using the following commands:
sudo apt update
sudo apt install nginx
Once the installation is complete, start Nginx and enable it to run on startup:
sudo systemctl start nginx
sudo systemctl enable nginx
Step 2: Set Up Your Domain Names
Next, you need to set up the domain names you want to host. Make sure that your domain names are registered and point to your server’s IP address. You can verify the DNS settings by pinging your domain in the terminal:
ping yourdomain.com
Step 3: Create Server Blocks
Server blocks in Nginx allow you to host multiple domains on a single IP address. Each server block consists of a `server` directive that specifies how to handle requests for that domain. Create a directory for each domain in the `/var/www/` directory:
sudo mkdir -p /var/www/yourdomain.com/html
sudo mkdir -p /var/www/anotherdomain.com/html
Set permissions for the newly created directories:
sudo chown -R $USER:$USER /var/www/yourdomain.com/html
sudo chown -R $USER:$USER /var/www/anotherdomain.com/html
Step 4: Create Nginx Configuration Files
Create a configuration file for each domain in the `sites-available` directory:
sudo nano /etc/nginx/sites-available/yourdomain.com
Inside the file, add the following configuration:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Do the same for the other domain:
sudo nano /etc/nginx/sites-available/anotherdomain.com
And include this configuration:
server {
listen 80;
server_name anotherdomain.com www.anotherdomain.com;
root /var/www/anotherdomain.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Step 5: Enable the Configuration Files
To enable the new server block configurations, create symbolic links in the `sites-enabled` directory:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/anotherdomain.com /etc/nginx/sites-enabled/
Step 6: Test the Nginx Configuration
Before restarting Nginx, test the configuration to ensure there are no syntax errors:
sudo nginx -t
If the test is successful, proceed to restart Nginx to apply the changes:
sudo systemctl restart nginx
Step 7: Create a Simple HTML File
To verify that your domains are hosting correctly, create a simple index.html file in each domain's root directory:
echo "Welcome to YourDomain.com
" | sudo tee /var/www/yourdomain.com/html/index.html
echo "Welcome to AnotherDomain.com
" | sudo tee /var/www/anotherdomain.com/html/index.html
Step 8: Access Your Domains
Now, you can access your domains in a web browser. Enter your domain names: