To configure multiple websites on a single Nginx server, you need to create separate server blocks for each website. Server blocks (similar to Apache’s virtual hosts) allow you to specify different configurations for each domain or subdomain hosted on the same server. Here’s a step-by-step guide:
- Install Nginx: If you haven’t already, install Nginx on your server using the package manager for your operating system.
- Create root directories for each website: For each website, create a root directory where the website files will be stored. For example:
bash
sudo mkdir -p /var/www/site1.com/public_html
sudo mkdir -p /var/www/site2.com/public_html
- Set ownership and permissions: Ensure the correct user and group own the directories and set appropriate permissions:
bash
sudo chown -R your-user:your-group /var/www/site1.com/public_html
sudo chown -R your-user:your-group /var/www/site2.com/public_html
sudo chmod -R 755 /var/www
- Create server block configuration files: For each website, create a new server block configuration file in the
sites-available
directory:
arduino
sudo nano /etc/nginx/sites-available/site1.com
sudo nano /etc/nginx/sites-available/site2.com
- Configure server blocks: Edit each server block configuration file with the appropriate settings for the respective website. Here’s an example configuration for site1.com:
bash
server {
listen 80;
server_name site1.com www.site1.com;
root /var/www/site1.com/public_html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
For site2.com, replace the server_name
, and root
directives with the corresponding values for site2.com.
- Create symbolic links: Create symbolic links to enable the server blocks in the
sites-enabled
directory:
bash
sudo ln -s /etc/nginx/sites-available/site1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/site2.com /etc/nginx/sites-enabled/
- Test the configuration: Check the Nginx configuration for syntax errors:
sudo nginx -t
If the configuration is correct, you’ll see a message like this:
bash
nginx: configuration file /etc/nginx/nginx.conf test is successful
- Restart Nginx: Apply the changes by restarting Nginx:
sudo systemctl restart nginx
Now your Nginx server should be configured to host multiple websites. Remember to configure your DNS records to point your domain names to the IP address of your Nginx server.
To configure another website, follow these steps:
- Create a root directory for the new website:
bash
sudo mkdir -p /var/www/site3.com/public_html
- Set ownership and permissions:
bash
sudo chown -R your-user:your-group /var/www/site3.com/public_html
sudo chmod -R 755 /var/www
- Create a new server block configuration file for the new website in the
sites-available
directory:
arduino
sudo nano /etc/nginx/sites-available/site3.com
- Configure the server block for the new website (replace
site3.com
with the actual domain name):
bash
server {
listen 80;
server_name site3.com www.site3.com;
root /var/www/site3.com/public_html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# Add other configurations such as SSL, PHP, etc. if needed
}
- Create a symbolic link to enable the server block in the
sites-enabled
directory:
bash
sudo ln -s /etc/nginx/sites-available/site3.com /etc/nginx/sites-enabled/
- Test the configuration and restart Nginx:
sudo nginx -t
sudo systemctl restart nginx
Now, the new website should be configured, and Nginx should be serving it. Remember to configure your DNS records to point your domain name to the IP address of your Nginx server.