How to setup Nginx Virtual Hosting in CentOS/Rhel 7.x

Setup Nginx Virtual Hosting in CentOS/Rhel 7.x

 

setup

Prerequisites : https://www.natsav.com/blog/2017/01/28/how-to-install-and-configure-nginx-on-centosrhel-7-x

You need to create websites to share the same IP address :

#mkdir -p /var/www/virtualhost/site1.example.com
#mkdir /var/www/virtualhost/site2.example.com
#chmod 755 /var/www/virtualhost

Now create index page for both websites :

#cd /var/www/virtualhost
#vim site1.example.com/index.html
#vim site2.example.com/index.html

Now configure nginx.conf on CentOS/Rhel located at /etc/nginx/nginx.conf file :

include “/etc/nginx/conf.d/*.conf”;

Now restart Nginx Service to apply the new configuration :

#systemctl start nginx

Now configure your virtual hosts for as follows :

vim /etc/nginx/conf.d/site1.example.com.conf

Add the following line into it :

## site1
server {
listen 80;
server_name site1.example.com
# define the root directory of your site and index files.
 location / {
 root /var/www/virtualhost/site1.example.com;
 index index.php index.html index.htm;
}}
vim /etc/nginx/conf.d/site2.example.com.conf
## site2
server {
listen 80;
server_name site2.example.com
# define the root directory of your site and index files.
 location / {
 root /var/www/virtualhost/site2.example.com;
 index index.php index.html index.htm;
}}

Now restart the Nginx services, And test both websites.

Leave a Reply

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