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:
- Config Node Js And Nginx Config Node Js And Nginx Introduction Node.js is a popular server-side language for building web applications and services, and Nginx is a popular web server used for serving static web…
- Create Subdomain Nginx Debian 9 Server Create Subdomain Nginx Debian 9 Server Understanding Subdomain A subdomain is a subsection of a domain that a user can create to host one or more websites, usually for a…
- Laravel 5.5 Configure Nginx Laravel 5.5 Configure Nginx Introduction To Nginx Nginx is a web server software often deployed as a reverse proxy. It is open-source and available to download for free. Nginx has…
- How To Start Nginx Windows How To Start Nginx Windows Introduction to Nginx Windows Nginx is an open-source web server platform that is used for hosting web content. It is especially useful for high-traffic websites…
- 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…
- Nginx 1.14 Create Virtual Host Nginx 1.14 Create Virtual Host Overview Virtual hosting is a process for hosting multiple websites on a single physical server and IP address. Nginx version 1.14 is the most recent…
- Selinux Enable Php Fpm Nginx Centos 7 Selinux Enable Php Fpm Nginx Centos 7 What is Selinux Enable Php Fpm Nginx Centos 7? Selinux Enable Php Fpm Nginx CENTOS 7 is an easy-to-use web server and operating…
- 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…
- Centos 6 Nginx Auto Reject When Upload Files Centos 6 Nginx Auto Reject When Upload Files Introduction to Centos 6 Centos 6 is a powerful, open-source Linux distribution that has been designed for advanced user, administrators, and server…
- How To Install Nginx In Ubuntu How To Install Nginx In Ubuntu Introduction to Nginx Nginx is a very powerful web server for hosting websites and applications. It is a fast and reliable server, and is…
- How To Setup Nginx On Ubuntu How To Setup Nginx On Ubuntu Introduction Nginx is a powerful web server that is very popular among Linux users. It is open-source and comes with great features such 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 Listen Multiple Ip Addresses Nginx Listen Multiple Ip Addresses What is Nginx? Nginx is a open-source web server created by Igor Sysoev and first publicly released in 2004. Since its release, Nginx has become…
- Site-Available Nginx Whas Delete Ubuntu Site-Available Nginx What Delete Ubuntu What is Nginx and how does it work with Ubuntu? Nginx is a web server designed for high performance, scalability, and reliable internet connectivity. It…
- Instal Python Web Centos 7 Django Nginx Instal Python Web Centos 7 Django Nginx Introduction to Installing Python Web Centos 7 Django Nginx In this article, we will be covering an installation of Python web framework Django,…
- Etc Nginx Sites Available Default Etc Nginx Sites Available Default What Is Nginx? NGINX is a web server and reverse proxy for HTTP, HTTPS, SMTP, POP3, and other services. It was developed in 2002 by…
- Where Is Nginx Document Root Where Is Nginx Document Root What Is Nginx? Nginx is an open source, high-performance web server and reverse proxy software popular on Linux and Unix. It is used to serve…
- 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.…
- Setting Domain In Nginx Digitalocean Setting Domain In Nginx Digitalocean What is Nginx? Nginx (pronounced “engine-ex”) is a high performance web server software. It is open source and widely used as a web server. It…
- Nginx Reverse Proxy Centos 7 Nginx Reverse Proxy Setup on Centos 7 What is a Reverse Proxy? A reverse proxy is a type of server that takes a client request, then forwards the request to…
- 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…
- Access Nginx On Virtualbox Centos 7 Access Nginx On Virtualbox Centos 7 Introduction to VirtualBox VirtualBox is a great tool for hosting virtual machines on your own personal computer. It works on a variety of operating…
- How To Use Nginx Laravel Laragon How To Use Nginx Laravel Laragon Introduction Laragon is a powerful, lightweight, robust web server stack that is used to develop and host applications on Windows and Linux. Laragon uses…
- Centos 6 Nginx Multiple Php Version Centos 6 Nginx Multiple PHP Versions What is CentOS 6? CentOS 6 is a Linux-based operating system. It is a free, open-source operating system that is based on Red Hat…
- 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?…
- 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…
- How To Install Nginx On Centos 6 How To Install Nginx On Centos 6 Introduction Nginx is a powerful web server that is open-source and free to use. It is becoming increasingly popular as a web server,…
- 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…
- Cannot Get Index.Html Nginx Cannot Get Index.Html Nginx What is Nginx? Nginx is a web server software designed for high-traffic websites and web applications. It is an open source reverse proxy server for HTTP,…
- Laravel 5.4 Vps Nginx Config File Centos 7 Laravel 5.4 VPS Nginx Config File Centos 7 What is a Nginx Config File? A config file is a settings file used by Nginx server to configure how it behaves…