Learn how to set up Nginx Vhost Config on Ubuntu. Our Nginx Support team is here to help you with your questions and concerns.
How to set up Nginx Vhost Config on Ubuntu
Nginx virtual hosts are also known as server blocks. It lets us host multiple websites on a single server.
This is done by encapsulating configuration details for each domain, similar to how Apache virtual host files work.
Here’s a step-by-step guide to setting up and managing Nginx virtual hosts on an Ubuntu server.
- First, we need to install Nginx on our Ubuntu server. We can update our package list and install Nginx using these commands:
sudo apt update
sudo apt install nginx
- Then, create a new Nginx configuration file for the domain under `/etc/nginx/sites-available`.
sudo nano /etc/nginx/sites-available/domain_name
Replace domain_name with the actual domain name:
- Next, add the following content to the file:
server {
listen 80;
listen [::]:80;
server_name domain_name;
root /var/www/domain_name;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
- Finally, save the file and close the text editor.
- After that, create a directory for the website files and set permissions:
mkdir -p /var/www/domain_name/html
chmod -R 755 /var/www/domain_name
- Now, it is time to enable the virtual host by creating a symbolic link from the configuration file in `sites-available` to the `sites-enabled` directory:
ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
- Then, verify the Nginx configuration for any errors and then restart the Nginx service to apply the changes.
sudo nginx -t
sudo systemctl restart nginx
By following the above steps, we can create and enable a Nginx virtual host configuration for the website on an Ubuntu server.
How to Disable an Nginx Virtual Host
If we want to disable an Nginx virtual host, follow these steps:
- First, delete the symbolic link from the `sites-enabled` directory:
sudo rm /etc/nginx/sites-enabled/domain_name
- Then, check the Nginx configuration to ensure there are no errors:
sudo nginx -t
- Finally, restart the Nginx service to apply the changes.
Configuring Nginx virtual hosts lets us easily manage multiple websites on a single server. With these steps, we can set up, enable, and disable virtual hosts, ensuring our server is well-organized and each domain is properly configured.
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
In brief, our Support Experts demonstrated how to set up Nginx Vhost Config on Ubuntu.
0 Comments