How To Configure Virtual Host In Nginx On Centos 7


How To Configure Virtual Host In Nginx On Centos 7

Introduction

We all need to configure virtual host on Nginx webserver on CentOS 7 in order to place multiple websites or applications on a single server. This is done by mapping domains, subdomains and IP addresses to different domains or application. When a user requests for a particular domain or IP address, it is served with the respective web files and applications. In this article, we’ll discuss about how you can set up multiple virtual hosts in Nginx web server on CentOS 7.

Steps To Configure Virtual Hosts On Nginx

Follow the steps mentioned below in order to configure virtual host in Nginx on CentOS 7.

  1. Login to your instance (remote or local) of Nginx web server and type “sudo yum update” in order to update the entire operating system.
  2. Type in “sudo yum install nginx” to install nginx and it’s components on your server.
  3. After the installation is completed, configure the nginx virtual host by creating a new configuration file and modifying the existing configuration file.
  4. Create a new configuration file with the following command “sudo nano /etc/nginx/sites-available/default”.
  5. In the new configuration file, add the following lines of code –

  6. server {
    listen 80;
    server_name domainname.com www.domainname.com;
    root /var/opt/html/domainname.com;
    index index.html index.htm;
    error_log /var/log/nginx/domainname.com/error.log;
    access_log /var/log/nginx/domainname.com/access.log;
    }

  7. After that, create the directory structure for your website as stated in the configuration. To do that, use the command “mkdir –p /var/opt/html/domainname.com”.
  8. Create a sample index.html file in the particular directory with the help the following command “nano /var/opt/html/domainname.com/index.html”.
  9. In the file, enter the following content –



  10. test web page


    Virtual Host worked!



  11. Save the file and exit.
  12. Once that is done, restart the nginx server with the command “sudo service nginx restart”.
  13. Verify that the virtual host has been configured correctly by accessing the website url in the browser. The page must contain the content specified above.

Setting up Multiple virtual hosts

The process for setting up multiple virtual hosts is essentially the same as setting up a single virtual host. The only difference here is that you need to create multiple configuration files with the different domain name and root directory for each virtual host.

Once the new configuration file is created, add it to the available sites directory as stated in the steps above. Post that, activate the virtual host with the following command “sudo ln -s /etc/nginx/sites-available/domainname.com /etc/nginx/sites-enabled/domainname.com”.

You should repeat the above steps for each virtual host you have. Post that, restart the nginx server with the command “sudo service nginx restart”. You can now access the new website in the browser.

Conclusion

Virtual hosting provides great flexibility to host multiple applications or websites on a single server. However, it is important to be careful while configuring the virtual hosts as even a small mistake can lead to the website being down.

We hope that the article has helped you in setting up virtual hosts on Nginx web server on CentOS 7. If you have any queries, feel free to ask in the comments section.

FAQs

  • Q: What is virtual hosting?

    A: Virtual hosting is the process of hosting multiple websites or applications on a single server. Each website is assigned with its own domain name, and has its own IP address.

  • Q: How to enable virtual hosting in Nginx?

    A: Virtual hosting can be enabled in Nginx by creating configuration files, and triggering the change with the command “sudo service nginx restart”.

  • Q: How to setup multiple virtual hosts in Nginx?

    A: Setting up multiple virtual hosts require you to create multiple configuration files. Once the new configuration files have been created, they need to be added to the available sites directory. Post that, you can activate the virtual host by using the command “sudo ln -s /etc/nginx/sites-available/domainname.com /etc/nginx/sites-enabled/domainname.com”.

Thank you for reading this article. Please read other articles for more information.

Leave a Reply

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