Managing multiple domains on a single server can be a challenging task. Nginx, a powerful web server software, offers a convenient solution: virtual hosts (vhosts). In this tutorial, we’ll guide you through the process of adding additional domains to Nginx as a vhost entry, simplifying your server management and streamlining your configurations.
Prerequisites
Before proceeding, make sure you have the following in place:
- An Nginx server installed and running on a Linux-based system. If you need help with the installation, follow the official Nginx installation guide.
- Root or sudo access to your server.
- At least one domain name with its DNS records pointing to your server’s IP address.
Step 1: Create Directories for Additional Domains
For each new domain you wish to add to your Nginx vhost, create a directory to store its files. Replace yourdomain.com
with your actual domain name:
sudo mkdir -p /var/www/yourdomain.com/html
Next, adjust the ownership and permissions for the newly created directory:
sudo chown -R $USER:$USER /var/www/yourdomain.com/html
sudo chmod -R 755 /var/www/yourdomain.com/html
Step 2: Create a Sample Index Page
Create a sample index.html file for the new domain:
nano /var/www/yourdomain.com/html/index.html
Add some basic HTML content to the file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome to Yourdomain.com</title>
</head>
<body>
<h1>Success! Yourdomain.com is now hosted on Nginx.</h1>
</body>
</html>
Save and close the file.
Step 3: Configure Nginx Server Block
Create a new Nginx server block configuration file for the additional domain:
sudo nano /etc/nginx/sites-available/yourdomain.com
Add the following configuration, replacing yourdomain.com
with your actual domain name:
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;
}
}
Save and close the file.
Step 4: Enable the New Server Block
Create a symbolic link to the newly created server block configuration file:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
Verify the Nginx configuration for any syntax errors:
sudo nginx -t
If there are no errors, reload Nginx to apply the changes:
sudo systemctl reload nginx
Step 5: Test Your New Domain
Visit your new domain in a web browser, and you should see the sample index page you created earlier. If you see the “Success!” message, you’ve successfully added the additional domain Nginx. By following this step-by-step guide, you can easily add additional domains to your Nginx virtual host, allowing you to manage multiple domains on a single server efficiently. This process can be repeated for each new domain you’d like to host on your Nginx server.
As your business grows and you need to manage more domains, consider partnering with Random Linux LLC for professional Linux and cloud consulting services. Our team of experts can help you optimize your server configurations and ensure a smooth, secure, and scalable hosting environment for all your domains.