Home > Documentation > Apache web server > Virtual hosts

Virtual hosts

Posted by peter on October 24, 2011

Suppose you wanted a website called peter.co.za and a website called dna.co.za to run off the same web server on the same port (80)

1. Create two folders under the directory “/srv/www/” one called “peter” and the other called “dna” copy an index.html page to each folder.

2. make sure the file and folder permissions are correct on each, for example:

chown wwwrun /srv/www/dna

chown wwwrun /srv/www/dna/index.html

3. now browse to the folder “/etc/apache2/vhosts.d/” folder and copy the template found there and paste it as peter.conf and dna.conf. s shown below

cp  vhost.template  peter.conf

 

4. vi each file and change the following:

<VirtualHost peter.co.za:80>

ServerName peter.co.za

DocumentRoot /srv/www/peter

ErrorLog /var/log/apache2/peter.co.za-error_log

CustomLog /var/log/apache2/peter.co.za-access_log combined

UseCanonicalName On

ScriptAlias /cgi-bin/ “/srv/www/cgi-bin”

<Directory “/srv/www/cgi-bin”>

AllowOverride None

Options +ExecCGI –Includes

Order allow,deny

Allow from all

</Directory>

<Directory “/srv/www/peter/”>

AllowOverride None

Options Indexes FollowSymLinks

Order allow,deny

Allow from all

</Directory>

5. Now do the same for the other Virtual hosts

6. Next browse to the folder “/etc/apache2/” and edit the file “listen.conf” and add the following line for each of your virtual hosts. In this example I have added the line for the two sites peter.co.za and dna.co.za

virtualhosts1-300x144.jpg

7. You can now test your config by typing the following at the command prompt

apache2ctl configtest

8. Reload Apache by typing “apache2 restart” at the command prompt

9. Test by browsing to http://peter.co.za and http://dna.co.za

TIP

For testing Virtual hosts you will need to create host name to ip address mapping in your host file. The host file is found in the “/etc” folder

Comments:

Leave a Reply



(Your email will not be publicly displayed.)

Please type the letters and numbers shown in the image.Captcha CodeClick the image to see another captcha.