How to configure Apache virtual host in FreeBSD

To configure Apache virtual host in FreeBSD

Virtual hosting is a technique that is performed to run more than one website on a single or a set of servers. Virtual hosting makes it possible for a server to use multiple host names while sharing its resources such as memory and process cycles. The configuration process of Apache Virtual Host in FreeBSD is thoroughly briefed in this article.

To Install Apache

First step is to update the repository by running the following command.

root@linuxhelp:~ # pkg update
Updating FreeBSD repository catalogue...
Fetching meta.txz: 100% 940 B 0.9kB/s 00:01
Fetching packagesite.txz: 100% 6 MiB 101.3kB/s 00:57
Processing entries: 100%
FreeBSD repository update completed. 25381 packages processed.


Once the repository is updated, install Apache with the following command.

root@linuxhelp:~ # pkg install apache24
Updating FreeBSD repository catalogue...
FreeBSD repository is up-to-date.
All repositories are up-to-date.
Updating database digests format: 100%
New version of pkg detected  it needs to be installed first.
The following 1 package(s) will be affected (of 0 checked):
.
.
.
[2/3] Installing apr-1.5.2.1.5.4_1...
[2/3] Extracting apr-1.5.2.1.5.4_1: 100%
[3/3] Installing apache24-2.4.23_1...
===>  Creating groups.
Using existing group ' www' .
===>  Creating users
Using existing user ' www' .
[3/3] Extracting apache24-2.4.23_1: 100%
Message from apache24-2.4.23_1:
To run apache www server from startup, add apache24_enable=" yes" 
in your /etc/rc.conf. Extra options can be found in startup script.
Your hostname must be resolvable using at least 1 mechanism in
/etc/nsswitch.conf typically DNS or /etc/hosts or apache might
have issues starting depending on the modules you are using.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

- apache24 default build changed from static MPM to modular MPM
- more modules are now enabled per default in the port
- icons and error pages moved from WWWDIR to DATADIR

If build with modular MPM and no MPM is activated in
httpd.conf, then mpm_prefork will be activated as default
MPM in etc/apache24/modules.d to keep compatibility with
existing php/perl/python modules!

Please compare the existing httpd.conf with httpd.conf.sample
and merge missing modules/instructions into httpd.conf!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


Now enable the Apache service on rc.conf file.

root@linuxhelp:~ # nano /etc/rc.conf

apache24_enable=" YES" 


Once the service is enabled, start the Apache service with the help of following command.

root@linuxhelp:~ # service apache24 start
Performing sanity check on apache24 configuration:
Syntax OK
Starting apache24.


After Installing Apache, open the browser and enter your IP address to verify if it is installed properly.

To configure Apache virtual host

Create a directory for virtual host by applying the following command. The virtual host directory can be created anywhere as per your choice.

root@linuxhelp:~ # cd /usr/local/www/
root@linuxhelp:/usr/local/www # mkdir vhost1 
root@linuxhelp:/usr/local/www # mkdir vhost2


Once the above command is done, create an index file for virtual host domain by utilizing the following command.

root@linuxhelp:/usr/local/www # nano vhost1/index.html
this is my first virtualhost...!
root@linuxhelp:/usr/local/www # nano vhost2/index.html
this is my virtual host2&hellip .!!


Open the httpd.conf file by running the following command.

root@linuxhelp:~ # nano /usr/local/etc/apache24/httpd.conf

Configure the httpd.conf file by adding the following lines to it.

< Directory /> 
AllowOverride All
# Require all denied
Order Allow,Deny
Allow from All
< Directory /> 
.
.
.
#Virtual host configuration
< VirtualHost *:80 /> 
ServerAdmin webmaster@linuxhelp.com
DocumentRoot " /usr/local/www/vhost1/" 
ServerName vhost1.linuxhelp.com
< /VirtualHost/> 

< VirtualHost *:80/> 
ServerAdmin webmaster@linuxhelp.com
DocumentRoot " /usr/local/www/vhost2/" 
ServerName vhost2.linuxhelp.com
< /VirtualHost/> 


After making the neccesary changes in the configuration file, add the virtual host domain name in the host file through the following command.

root@linuxhelp:~ # nano /etc/hosts
192.168.7.130 vhost1.linuxhelp.com
192.168.7.130 vhost2.linuxhelp.com


Next restart the Apache service by using the following command.

root@linuxhelp:~ # service apache24 restart
sanity check on apache24 configuration:
Syntax OK
Stopping apache24.
Waiting for PIDS: 889.
Performing sanity check on apache24 configuration:
Syntax OK
Starting apache24.


After restarting apache, open the browser and enter the virtual host domain name. The browser will display your file name if you have configured Apache Virtual Host aptly in your FreeBSD.


Tag : Virtualhost
FAQ
Q
Why need to use the Apache virtual host?
A
Virtual hosting is a technique that is performed to run more than one website on a single or a set of servers. Virtual hosting makes it possible for a server to use multiple host names while
Q
How to make Modify Virtualhost Containers With Include Files in Cpanel?
A
Without SSL /usr/local/apache/conf/userdata/std/2_4/$user/$includename.conf I put that php.ini code to this config /usr/local/apache/conf/userdata/std/2_4//user.conf then I upda
Q
How can I make sure an .htaccess file is usable even if it's in a sub direcectory of the domain's main directory
A
Make sure that the directory and .htaccess are readable, but not writeable, by the web server. Then make sure that the specific directory has or has inherited the overrides (http://httpd.apac
Q
A simple method for vhost configurations.
A
vhosts are Virtual Hosts -- additional domains on one server (think test.com as the main, with test.co.uk or example.test.com as vhosts). Basically, it tells the server how to serve the doma
Q
Thanks for this post and I feel like know the basic idea about the Apache virtual host?
A
Thanks for your support. Keep support and subscribe "www.linuxhelp.com"