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.
- Login to your instance (remote or local) of Nginx web server and type “sudo yum update” in order to update the entire operating system.
- Type in “sudo yum install nginx” to install nginx and it’s components on your server.
- After the installation is completed, configure the nginx virtual host by creating a new configuration file and modifying the existing configuration file.
- Create a new configuration file with the following command “sudo nano /etc/nginx/sites-available/default”.
- In the new configuration file, add the following lines of code –
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;
}
- 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”.
- Create a sample index.html file in the particular directory with the help the following command “nano /var/opt/html/domainname.com/index.html”.
- In the file, enter the following content –
test web page
Virtual Host worked!
- Save the file and exit.
- Once that is done, restart the nginx server with the command “sudo service nginx restart”.
- 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.
Related Posts:
- How To Debian Nginx Php How To Debian Nginx Php Step 1 – Setup Your VPS The first step to setting up your web server is getting your VPS (Virtual Private Server). This is usually…
- Virtual Host Nginx Ubuntu 16.04 Virtual Host Nginx Ubuntu 16.04 Introduction A virtual host (also known as Virtual Private Server or VPS) is a service that allows a single physical server to host multiple websites.…
- Nginx Ubuntu Access Virtual Host From Another… Nginx Ubuntu Access Virtual Host From Another Machine Over LAN Are you trying to access a virtual host set up using Nginx on Ubuntu from another machine over a LAN?…
- Vps Webserver Nginx Php7 Mysql Vps Webserver Nginx Php7 Mysql What Is VPS Webserver? A VPS webserver is a Virtual Private Server (VPS) that has been configured to act as a web server. A VPS…
- Multi Host In Nginx Debian 9 Multi Host In Nginx Debian 9 Introduction Nginx is a web server that can be used for hosting multiple websites on a single server. This guide will help you set…
- Nginx Centos 7.6 Virtual Host Nginx Centos 7.6 Virtual Host Introduction to Nginx Virtual Hosts Virtual Hosts, also called Virtual Servers, are a very important function of web hosting. They allow multiple websites to run…
- How To Add Domain In Nginx How To Add Domain In Nginx Introduction To Nginx Nginx (Internet Information Server) is a popular open-source web server that is able to serve a variety of content, from simple…
- Failed Install Nginx On Centos Failed Install Nginx On Centos What is Nginx? Nginx is an open source web server designed to handle high traffic websites. It has proven to be reliable over the years…
- Forward Nginx To Another Subdomain Forward Nginx To Another Subdomain What is Nginx? Nginx is an open-source, high-performance web server originally developed by Igor Sysoev. Since its initial release in 2004, Nginx has become one…
- Install Web Server Nginx Centos 7 Install Web Server Nginx Centos 7 Introduction Are you looking for a way to set up a web server on your Linux-based system? If so, then installing Nginx on CentOS…
- Membuat Virtual Host Di Nginx Membuat Virtual Host Di Nginx Apa itu Virtual Host di Nginx? Virtual Host adalah konfigurasi Nginx yang memberikan Anda kemampuan untuk membuat domain virtual termasuk subdomain. Ini memberi Anda kemampuan…
- Centos 7 Nginx Multiple Websites Centos 7 Nginx Multiple Websites Introduction Are you looking for a way to set up multiple websites on your CentOS 7 server utilizing the Nginx web server? If so, you’ve…
- Laravel Nginx Without Custom Domain Laravel Nginx Without Custom Domain Overview of Laravel Nginx Without Custom Domain Laravel is an open-source PHP web framework used to create powerful web applications. It is based on the…
- Setting Sites Available Di Nginx Ubuntu Setting Sites Available Di Nginx Ubuntu Understanding Nginx and its Parts Nginx is a great tool for managing web applications. It allows you to host multiple websites, as well as…
- Nginx Php-Fpm Php Mariadb Mysql Centos 7.2 Digitalocean Nginx, Php-Fpm, Php, Mariadb, Mysql, Centos 7.2 Digitalocean Introduction: What is Nginx? Nginx is a powerful web server that was first released in 2004. It is known for being a…
- Nginx Alway Direct To Main Domain Nginx Always Direct To Main Domain What is Nginx? Nginx is a popular open-source web server software used by many webmasters and website owners. It is an incredibly fast and…
- Web Server Nginx Install Di Centos 7 Web Server Nginx Install Di Centos 7 Introduction to Nginx Nginx is a popular open source web server used to host websites and other applications. It is known for its…
- Install Nginx With Waf Centos 7 Install Nginx With Waf Centos 7 Overview Nginx is a popular open-source web server used for hosting websites and applications. It is widely used due to its high performance, efficient…
- Reverse Proxy Nginx Centos 7 Reverse Proxy Nginx Centos 7 What is a Reverse Proxy? A reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or…
- Docker Nginx Php Mysql In Xampp Docker Nginx Php Mysql In Xampp Overview of Docker Nginx Php Mysql in Xampp Docker Nginx Php Mysql in Xampp is a powerful web development platform used for developing complex…
- Nginx Css And Image Not Showing Centos Nginx Css And Image Not Showing Centos Common Problems When Nginx CSS And Images Aren't Showing When you're working with Nginx on the Centos operating system, you may have run…
- Redirect Ip To Domain Nginx Redirect IP to Domain Nginx Introduction to Redirecting IP to Domain Nginx A procedure for redirecting IP address to domain name in a web server is called IP forwarding or…
- Nginx Multiple Domains Same Port Only Get One Domain Nginx Multiple Domains Same Port Only Get One Domain Understanding the Problem You just recently set up your Nginx web server and you're trying to set up multiple domains on…
- Server_Name_In_Redirect Nginx Server_Name_In_Redirect Nginx What is Server Name In Redirect (SNIR)? Server Name In Redirect (SNIR) is a method to use an Nginx webserver to route requests from multiple domains to a…
- Nginx Windows Create Virtual Host Nginx Windows Create Virtual Host What Is Nginx? Nginx is an open source web server software. First released in 2004, it has steadily become the preferred choice of web administrators…
- Nginx Proxy Cache Redis Wordpress Centos Nginx Proxy Cache Redis Wordpress Centos Understanding Nginx Proxy Cache Nginx Proxy Cache is a web-acceleration software from Nginx, Inc. It is designed to speed up dynamic web content delivery.…
- Nginx Dev Mapper Centos-Root Is 100 Full Nginx Dev Mapper CentOs-Root Is 100 Full What Is Nginx Dev Mapper? Nginx Dev Mapper is a file system space mapping tool for the Linux operating system and is shipped…
- Nginx Responding To Any Doimain Name Nginx Responding To Any Domain Name Web servers such as Nginx, Apache, and IIS are all used to respond to requests from web browsers. These requests come from domain names,…
- Installing Nginx And Php In Docker Installing Nginx And Php In Docker Understanding Docker & Its Basics Docker is a virtualization platform for running applications in a container, without a virtual environment. It provides a bridge…
- Remove Nginx Completely Centos 7 Remove Nginx Completely Centos 7 Overview This article will provide a brief overview of Nginx, some of the ways it can be removed from a Centos 7 system and directions…