Home / Teknologi / How To Configure Virtual Host In Nginx On Centos 7
How To Configure Virtual Host In Nginx On Centos 7
Daftar Isi:
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:
Vmware Files Freebsd Nginx Mysql Vmware Files Freebsd Nginx Mysql Introduction VMWare is a powerful virtualization platform that can help businesses reduce capital outlay for IT resources. One of the main benefits of using VMWare…
Install Letsencrypt Debian 9 Nginx Install Letsencrypt Debian 9 Nginx What is Letsencrypt? Letsencrypt is an open source, free, automated SSL service that provides users with the ability to secure their websites and use HTTPS…
Centos7 Nginx Php-Fpm Sock CentOS7 Nginx Php-Fpm Sock What is CentOS? CentOS (Community ENTerprise Operating System) is a Linux distribution that provides a free, enterprise-class, community-supported computing platform functionally compatible with its upstream source,…
There Are No Sites-Available Folder Nginx . There Are No Sites-Available Folder Nginx What is Nginx Server? Nginx is an open-source web server that is used to deliver webpages to users. It is a free and…
How To Install Php 5 Nginx Centos How To Install Php 5 Nginx Centos What Is PHP 5 Nginx Centos? PHP 5 Nginx Centos is an open source web server software that is optimised to serve dynamic…
Reverse Proxy Nginx Ubuntu 18.04 Reverse Proxy Nginx Ubuntu 18.04 Introduction Reverse Proxy is a type of proxy server that forwards requests to another server. It is mainly used by web servers, such as Nginx,…
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…
Install Webuzo Without Conlicting Nginx Centos 7 Install Webuzo Without Conlicting Nginx Centos 7 What is Nginx? Nginx is an open-source web server that was created and released in 2004. It is known for its speed, stability,…
Laravel Nginx Not Custom Domain Laravel Nginx Not Custom Domain Overview of Laravel, Nginx, and Custom Domains Laravel is an open-source model-view-controller web application development framework written in PHP. It is the most popular framework…
Config Ssl On Nginx Centos 7 Config SSL On Nginx Centos 7 Overview Secure Sockets Layer (SSL) is a type of cryptographic protocol used for secure communications on the Internet, as well as for secure access…
Nginx Multi Domain Centos 7 Nginx Multi Domain Centos 7 Introduction to Nginx Nginx is an open source, high performance web server software written in C language, designed to be deployed on Linux and Unix-like…
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…
Where Is Located Of Nginx.Conf Where is Located of Nginx.conf? What is Nginx.conf? Nginx.conf is an important configuration file for the popular Nginx web server. It contains all the configuration settings for the Nginx web…
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…
Nginx Stream Port Forwarding With Domain Nginx Stream Port Forwarding With Domain Introduction Port Forwarding is usually used to allow traffic from the Internet to reach the internal network. To achieve this, port forward requires router…
Where Default Root Folder Nginx Where Default Root Folder Nginx? What Is Nginx Nginx is an open source web server designed for high concurrency, performance and low memory consumption. The official website defines it as…
Nginx Config Multiple Proxy_Pass Nginx Config Multiple Proxy_Pass What is Nginx? Nginx is a high-performance web server that is widely used for deploying web applications and handling static content. It is also capable of…
Listen Directive In Www.Conf Nginx Listen Directive In WWW.conf Nginx What is a Listen Directive? The listen directive is a configuration directive that informs the Nginx server to accept requests on a network port. It…
Install Nginx Php5.6 Mysql Centos 7 Install Nginx Php5.6 Mysql Centos 7 Requirements Before we get started, let us go through the system requirements to install Nginx, Php5.6 and MySQL on CentOS 7. CentOS 7 Root…
Webmin Change Apaceh With Nginx Webmin Change Apache With Nginx Understanding Apache and Nginx Apache and Nginx are two of the most popular web servers on the market. Both are open source, highly configurable and…
Install Nginx 1.17 Centos 8 Install Nginx 1.17 Centos 8 Introduction to Nginx Nginx is one of the most popular web servers in the world. It is reliable, free, and open source software. It is…
Install Nginx Phpmyadmin Centos 7 Install Nginx Phpmyadmin Centos 7 What is Nginx? Nginx is a web server that is used in Linux-based operating systems such as Centos 7. It is designed to handle high-traffic…
Install Nginx Php Mysql Windows Server Install Nginx Php Mysql Windows Server Install Nginx Php Mysql Windows Server Installing Nginx Nginx is a web server that can be used to serve dynamic web pages as well…
Nginx Config Domain Based Root Nginx Config Domain Based Root What is Nginx Config? Nginx (pronounced “engine-x”) is a popular web server software program. It is open source, high performance and is being used by…
Centos 7 Nginx Letsencrypt Https And Https Both Active Centos 7 Nginx Letsencrypt Https And Https Both Active Introduction Are you overwhelmed with the number of steps required to set up an SSL certificate in CentOS 7? If so,…
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…
Install Phpmyadmin For Nginx Debian 8 Install Phpmyadmin For Nginx Debian 8 Introduction The Nginx web server popularly known as Nginx is a lightweight web server written in C programming language. It is an open source…
Centos 7 Nginx Error Log Not Write Centos 7 Nginx Error Log Not Write What is Nginx? Nginx is an open source web server and web application framework created by Igor Sysoev for the common website hosting…
Nginx Listen To Differnt Port Nginx Listen To Differnt Port What is Nginx? Nginx is an open-source web server software used to serve content to the web. It is used to host web applications and…
Ldap Not Found Centos Nginx Php Ldap Not Found Centos Nginx Php What is LDAP? LDAP, or Lightweight Directory Access Protocol, is a standard protocol for storing user and server information over a network. It is…