Welcome Demo
Setting up multiple websites on one Nginx server is a common task in web hosting. This tutorial will guide you through the process of configuring Nginx to host multiple websites on a single server using Server Blocks (Nginx's equivalent of Apache Virtual Hosts). We'll assume you have a basic understanding of Linux and have already installed Nginx on your server.
Note: This tutorial is based on a Linux environment. The specific steps may vary depending on your distribution, but the concepts are generally the same.
Step 1: Prepare Your Server
Make sure your server is up-to-date and Nginx is installed. You can do this by running the following commands:
sudo apt update
sudo apt upgrade
sudo apt install nginx
Step 2: Create Website Directories
Create directories for your websites. Each website should have its own directory structure. For example:
sudo mkdir -p /var/www/website1.com/html
sudo mkdir -p /var/www/website2.com/html
Step 3: Set Permissions
Make sure Nginx can read these directories:
sudo chown -R www-data:www-data /var/www/website1.com/html
sudo chown -R www-data:www-data /var/www/website2.com/html
Step 4: Create Sample Web Pages
You can create simple HTML pages or copy your website files into the respective directories (e.g., /var/www/website1.com/html
and /var/www/website2.com/html
).
Step 5: Create Server Block Configuration Files
Create a Server Block configuration file for each website you want to host. These files will tell Nginx how to handle each domain. You can create these files in the /etc/nginx/sites-available/
directory:
sudo nano /etc/nginx/sites-available/website1.com
Inside the file, add the following configuration for website1.com
. Customize it according to your needs:
server {
listen 80;
server_name website1.com www.website1.com;
root /var/www/website1.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Save the file and exit the text editor.
Repeat the process for website2.com
:
sudo nano /etc/nginx/sites-available/website2.com
And add similar configuration with the appropriate values for website2.com
.
Step 6: Enable Server Blocks
Enable the server blocks you've created by creating symbolic links from the sites-available
directory to the sites-enabled
directory:
sudo ln -s /etc/nginx/sites-available/website1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/website2.com /etc/nginx/sites-enabled/
Step 7: Test Configuration and Reload Nginx
Before you reload Nginx, it's a good idea to test your configuration for syntax errors:
sudo nginx -t
If you receive a message that says "syntax is okay" and "test is successful," you can proceed to reload Nginx:
sudo systemctl reload nginx
Step 8: Configure DNS
Point the DNS records for your domain names to the IP address of your server. This can usually be done through your domain registrar's control panel.
Step 9: Verify Your Websites
In your web browser, visit http://website1.com
and http://website2.com
to verify that your websites are working correctly.
Congratulations! You have successfully set up multiple websites on a single Nginx server using Server Blocks. You can repeat the process to add more websites as needed, following the same general steps.