Howto apache virtual hosts

From openSUSE

This article or section should be merged with Apache Quickstart HOWTO. (Discuss)


Introduction

In this tutorial we want to set up multiple virtual hosts.

Prerequisites

First you have to plan your website infrastructure. It's helpful to use a directory ("/srv/www/htdocs/") including one subdirectory for each virtual host.

You have to decide which sort of virtual host you need.

It's possible to create a sub domain for each of the following options.

  • IP-address
  • Domain-name
  • Port

Please replace {DOMAINNAME} with an IP or an domain name you want.

Procedure

Enter the directory /etc/apache2/vhosts.d/. As you can see there are two sample configuration files - one with ssl; one without ssl. We use the template without ssl. Only files ending with ".conf" are automatically included to the apache configuration.

Copy the sample configuration file "vhost.template" to "{DOMAINNAME}.conf" and create the subdirectory for this virtual host.

# cp vhost.template {DOMAINNAME}.conf
# mkdir /srv/www/htdocs/{DOMAINNAME}/

Now edit the copied configuration file. The first important line regulate the responsibility of the virtual host.

<VirtualHost *:80>

it's possible to replace the "*" with a domain name or an IP and "80" with a non standard http port.

change the "DocumentRoot srv/www/vhosts/dummy-host.example.com" line to something like this

DocumentRoot /srv/www/htdocs/{DOMAINNAME}/

Change in the following line the path to your document root path.

<Directory "/srv/www/vhosts/dummy-host.example.com">