How to set up a Virtual Host in Apache on FreeBSD

To configure Virtual Hosts on Apache web server under FreeBSD you first need to create a folder where your websites data will store:

mkdir -p /data/www/

Here we create a directory for our site howtounix.info and 3 more directories:

cd /data/www/howtounix.info/
mkdir {htdocs,log,tmp}

htdocs directory is your main website directory;
log directory contains logs;
tmp directory is for temporary files. Chown them for www user

Now we proceed to creating Virtual Hosts. Change directory:

cd /usr/local/etc/apache22/

Create two more directories:

mkdir -p sites-available
mkdir -p sites-enabled
sites-available directory is for all available Virtual Hosts;
sites-enabled is for hosts enabled by web server.

In sites-available directory we create our Virtual Host howtounix.info.conf

#Virtual Hosts
NameVirtualHost *:80

#VirtualHost example:
#Almost any Apache directive may go into a VirtualHost container.
#The first VirtualHost section is used for all requests that do not
#match a ServerName or ServerAlias in any block.
<VirtualHost *:80>
ServerAdmin admin@howtounix.info
DocumentRoot "/data/www/howtounix.info/htdocs"
ServerName howtounix.info
ServerAlias *.howtounix.info
ErrorLog "/data/www/howtounix.info/log/error.log"
CustomLog "/data/www/howtounix.info/log/access.log" common
php_admin_value open_basedir "/data/www/howtounix.info:."
php_admin_value upload_tmp_dir "/data/www/howtounix.info/tmp"
php_admin_value session.save_path "/data/www/howtounix.info/tmp"
AddType application/x-httpd-php .php .php3 .php4 .php5 .phtml
AddType application/x-httpd-php-source .phps
<Directory "/data/www/howtounix.info/htdocs">
AllowOverride All
DirectoryIndex index.php index.html index.htm
Order Allow,Deny
Allow from All
</Directory>
</VirtualHost>

Add to the end of http.conf file:

Include etc/apache22/sites-enabled/*.conf
Include etc/apache22/sites-other/*

Enable Virtual Host:

cd /usr/local/etc/apache22/
ln -s sites-available/howtounix.info.conf sites-enabled/howtounix.info.conf

Restart Apache:

/usr/local/etc/rc.d/apache22 forcerestart






  1. 2015-08-25 18:19:57
    You spelt 'available' incorrectly here:
    mkdir -p sites-avaliable
    
    Should be:
    mkdir -p sites-available
    
  1. Sergey
    2015-08-25 18:21:44
    Thanks, fixed

Got a comment?

captcha =

Categories

  1. System (20)
    1. FreeBSD (5)
    2. Linux (9)
  2. Email (2)
  3. DNS (2)
  4. Databases (1)
  5. WebServer (27)
 
Copyright © 2012-2015 HowToUnix - *nix Howtos and Tutorials
All Rights Reserved.