# HOWTO do multiple virtual hosts on apache2

## karneevor

HOWTO do multiple virtual hosts on apache2 

I'm writing this because I could find it in the first place.

This is my first howto, so bear with me.  :Smile:  If some of you could suggest some BB tags to make it look more readable, please tell me.

My setup

I need to have 3 virtual servers running. I created 3 new directories in /var/www/localhost and put my php files in those directories.

Installing apache2

First we install apache:

```
emerge apache
```

Emerge php and mysql if you need. I certainly did. Remember to enable php in /etc/conf.d/apache2 if you need it.

Add apache to the default runlevel:

```
rc-update add apache2 default
```

Configuring apache

When I start apache, it complains about "fully quallified server name" or the likes of it. Go into /etc/apache2/conf/apache2.conf and set the right ServerName.

Go to the section Virtual Hosts and uncomment:

```
Include conf/vhosts/vhosts.conf
```

Save and exit.

Edit the /etc/apache2/conf/vhosts/vhosts.conf

Insert the following in the end of the file(the rest is commented allready):

```
NameVirtualHost *:80

<VirtualHost *:80>

  ServerName www.provingground.dk

  ServerAlias provingground.dk *.provingground.dk

  ServerAdmin webmaster@provingground.dk

  DocumentRoot /var/www/localhost/provingground.dk

  ServerPath /www.provingground.dk

  <Directory /var/www/localhost/provingground.dk>

    Order Allow,Deny

    Allow from all

  </Directory>

  Setenv VLOG /var/log/apache2/

  ErrorLog /var/log/apache2/provingground_dk_error_log

  CustomLog /var/log/apache2/provingground_dk_access_log combined

</VirtualHost>

<VirtualHost *:80>

  ServerName   www.charlottearp.dk

  ServerAlias  charlottearp.dk *.charlottearp.dk

  ServerAdmin  webmaster@charlottearp.dk

  DocumentRoot /var/www/localhost/charlottearp.dk

  ServerPath   /www.charlottearp.dk

  <Directory /var/www/localhost/charlottearp.dk>

    Order Allow,Deny

    Allow from all

  </Directory>

  Setenv VLOG /var/log/apache2/

  ErrorLog /var/log/apache2/charlottearp_dk_error_log

  CustomLog /var/log/apache2/charlottearp_dk_access_log combined

</VirtualHost>

<VirtualHost *:80>

  ServerName www.it-crew.dk

  ServerAlias it-crew.dk *.it-crew.dk

  ServerAdmin webmaster@it-crew.dk

  DocumentRoot /var/www/localhost/it-crew.dk

  ServerPath /www.it-crew.dk

  <Directory /var/www/localhost/it-crew.dk>

    Order Allow,Deny

    Allow from all

  </Directory>

  Setenv VLOG /var/log/apache2/

  ErrorLog /var/log/apache2/it-crew_dk_error_log

  CustomLog /var/log/apache2/it-crew_dk_access_log combined

</VirtualHost>

```

Some explanation about the configuration:

The <Directory> directive is nesesary to get it working. I spent hours to find the solution. I got an 403 error from the web-server.

It's a good idea to have each virtual host writing to its own set of log files.

Start apache

It's time to test apache. Run:

```
/etc/init.d/apache2 start
```

Make absolutely sure that DNS is set up correct or the domainname is set in your /etc/hosts.

Good luck.

----------

## christsong84

methinks this belongs in the documentation, tips & tricks forum  :Wink: 

but great howto  :Smile: 

----------

## amiatrome

Haven't tried it yet but it looks good.  :Wink: 

----------

## perseguidor

It's a nice HOWTO, I'm sure people will find it handy. 

Personally, I prefer the dynamic hosts way. I give my friends subdomains, so they sure come handy for me, as all I have to do is create a new directory inside /var/www and it's already set.

To go that way, you just have to uncomment

```
Include conf/vhosts/dynamic-vhosts.conf

```

in apache2.conf instead. Then check in vhosts/dynamic-vhosts.conf that

```
VirtualDocumentRoot /var/www/%0/
```

 is set.

Finally, create directories of the form /var/www/subdomain.domain.com and you're good to go. Oh, don't forget to set your DNS to redirect *.yourdomain.com to your machine instead of doing so only for explicit domains.

I hope this little contribution helped someone  :Smile: 

----------

## perseguidor

This has been covered before:

http://gentoo-wiki.com/Virtual_domain_hosting

But it's nice to have yours here, I'd say.

----------

## harshaw

Note that if you are missing:

NameVirtualHost *:80 

only the first site will work :/

----------

## rernst

The last comment should be BOLDED as big as it gets. I chased it for days on end.

----------

## slay

There is no need to test this method. I can see with naked eye that this is a realy good , plain and SIMPLE howto. Greajob. This should be in tips&tricks

----------

