Bobcares

How to set up Nginx Vhost Config on Ubuntu

by | Jun 6, 2024

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.

  1. 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

  2. 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:

  3. Next, add the following content to the file:

    How to set up Nginx Vhost Config on Ubuntuserver {
    listen 80;
    listen [::]:80;
    server_name domain_name;
    root /var/www/domain_name;
    index index.html;
    location / {
    try_files $uri $uri/ =404;
    }
    }

  4. Finally, save the file and close the text editor.
  5. 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

  6. 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/

  7. 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:

  1. First, delete the symbolic link from the `sites-enabled` directory:

    sudo rm /etc/nginx/sites-enabled/domain_name

  2. Then, check the Nginx configuration to ensure there are no errors:

    sudo nginx -t

  3. 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

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.