![web hosting](https://focalsoft.ae/wp-content/uploads/2024/03/website-hosting-concept-with-futuristic-composition.webp)
Nginx, a powerful web server, can efficiently host multiple websites on a single Ubuntu server. This approach, known as virtual hosting, allows you to manage various websites using a single server, optimizing resource allocation and cost-effectiveness. Here’s a comprehensive guide to setting up multiple websites on your Ubuntu server with Nginx:
Prerequisites:
Ubuntu server with Nginx installed.
Root or sudo privileges.
Domain names or subdomains pointing to your server’s IP address.
Steps:
Create Directory Structure:
/var/www/
website1
html
website2
html
Set Permissions:
Repeat these commands for each website directory.
sudo chown -R www-data:www-data /var/www/website1/html
sudo chmod -R 755 /var/www/website1/html
Create Test Files (Optional):
To verify your setup, create a basic index.html file within each website’s html
directory with unique content, for instance:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Website 1</title>
</head>
<body>
<h1>Welcome to Website 1!</h1>
</body>
</html>
Output:
Configure Nginx:
Edit the main Nginx configuration file:
sudo nano /etc/nginx/nginx.conf
Locate the http block and add server blocks for each website. A server block defines configurations specific to a website. Here’s an example server block:
server {
listen 80; # Listen on port 80 (default)
server_name example.com www.example.com; # Replace with your domain names
# Specify the document root directory for this website
root /var/www/website1/html;
location / {
# Serve the index.html file for requests to the website root
try_files $uri $uri/ /index.html;
}
}
Create Symbolic Links (Optional):
sudo ln -s /etc/nginx/sites-available/website1.conf /etc/nginx/sites-enabled/
Test and Restart Nginx:
Test your configuration for syntax errors:
sudo nginx -t
If no errors occur, restart Nginx to apply the changes:
sudo systemctl restart nginx
Access Your Websites:
Open a web browser and navigate to your domain names or subdomains. You should see the corresponding website content for each domain.
Additional Considerations:
As an alternative to using domain names, you can configure websites to listen on different ports. This method requires modifying the listen directive in each server block to specify a unique port number.
For secure communication, you can obtain and configure SSL certificates for your domain names using tools like Let’s Encrypt.
By following these steps and considering the additional options, you can effectively host multiple websites on your Ubuntu server using Nginx, streamlining your web hosting needs.
![web hosting](https://focalsoft.ae/wp-content/uploads/2024/03/website-hosting-concept-with-futuristic-composition.webp)
Nginx, a powerful web server, can efficiently host multiple websites on a single Ubuntu server. This approach, known as virtual hosting, allows you to manage various websites using a single server, optimizing resource allocation and cost-effectiveness. Here’s a comprehensive guide to setting up multiple websites on your Ubuntu server with Nginx:
Prerequisites:
Ubuntu server with Nginx installed.
Root or  sudo privileges.
Domain names or subdomains pointing to your server’s IP address.
Steps:
Create Directory Structure:
/var/www/
website1
html
website2
html
Set Permissions:
sudo chown -R www-data:www-data /var/www/website1/html
sudo chmod -R 755 /var/www/website1/html
Repeat these commands for each website directory.
Create Test Files (Optional):
To verify your setup, create a basic index.html file within each website’s html
directory with unique content, for instance:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Website 1</title>
</head>
<body>
<h1>Welcome to Website 1!</h1>
</body>
</html>
Output:
Â
  Â
     Welcome to Website 1!
Â
Â
Configure Nginx:
Edit the main Nginx configuration file:
sudo nano /etc/nginx/nginx.conf
Locate the http block and add server blocks for each website. A server block defines configurations specific to a website. Here’s an example server block:
server {
listen 80; # Listen on port 80 (default)
server_name example.com www.example.com; # Replace with your domain names
# Specify the document root directory for this website
root /var/www/website1/html;
location / {
# Serve the index.html file for requests to the website root
try_files $uri $uri/ /index.html;
}
}
Create Symbolic Links (Optional):
sudo ln -s /etc/nginx/sites-available/website1.conf /etc/nginx/sites-enabled/
Test and Restart Nginx:
Test your configuration for syntax errors:
sudo nginx -t
If no errors occur, restart Nginx to apply the changes:
sudo systemctl restart nginx
Access Your Websites:
Open a web browser and navigate to your domain names or subdomains. You should see the corresponding website content for each domain.
Additional Considerations:
As an alternative to using domain names, you can configure websites to listen on different ports. This method requires modifying the listen directive in each server block to specify a unique port number.
For secure communication, you can obtain and configure SSL certificates for your domain names using tools like Let’s Encrypt.
By following these steps and considering the additional options, you can effectively host multiple websites on your Ubuntu server using Nginx, streamlining your web hosting needs.