How to Configure Nginx Virtualhost in FreeBSD

To Configure Nginx Virtualhost in FreeBSD

Virtual hosting is a technique used to run more than one website on a single server. It allows the users to use multiple host names while sharing its resources. This article explains the configuration process of Nginx Virtual Host in FreeBSD.

 

Configuration of Nginx Virtualhost

First install the FEMP by using the following command.

https://www.linuxhelp.com/how-to-install-femp-in-freebsd/

 

Utilize the following command to check whether the Nginx is installed or not.

root@linuxhelp:~ # service -e
.
.
.
/usr/local/etc/rc.d/php-fpm
/usr/local/etc/rc.d/nginx
/usr/local/etc/rc.d/mysql-server
.
.
.
Done!

Create a new directory for virtualhost domains.

root@linuxhelp:~ # mkdir -p /var/www/vhost1.com
root@linuxhelp:~ # mkdir -p /var/www/vhost2.com 


Then create the index.html file for both virtualhost domains.

root@linuxhelp:~ # nano /var/www/vhost1.com/index.html

FIRST VHOST
This is first virtualhost....!

root@linuxhelp:~ # nano /var/www/vhost2.com/index.html

SECOND VHOST
This is Second virtualhost....!

Now you need to configure the virtual host in nginx.conf file.

 Configure virtualhost within the “http” tag.

root@linuxhelp:~ # nano /usr/local/etc/nginx/nginx.conf
.
.
.
http { 
.
.
.
#first virtualhost
server {
        listen       80;
       server_name  test.vhost1.com;
        location / {
        root /var/www/vhost1.com;
        index  index.html index.htm;
        }
           }
#second virtualhost
server {
        listen       80;
       server_name  test.vhost2.com;
        location / {
        root /var/www/vhost2.com;
        index  index.html index.htm;
        }
           }

}


Immediately add the virtualhost domain in the hosts file.

root@linuxhelp:~ # nano /etc/hosts 

192.168.5.130   test.vhost1.com
192.168.5.130   test.vhost2.com


Restart the Nginx service to take effect.

root@linuxhelp:~ # service nginx restart
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Stopping nginx.
Waiting for PIDS: 655.
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configu
ration file /usr/local/etc/nginx/nginx.conf test is successful
Starting nginx.


Now its time to access your virtual host domains in web browser. Open your web browser with the IP address.

Web_browser

 

Check your configured virtualhost as shown below,

configured virtualhost

Second_virtualhost


Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.
For more help topics browse our website www.linuxhelp.com
Be the first to comment, we value your suggestions. For further queries please comment below.

Tags: Nginx FreeBSD
user image Author :  Connor