August 23, 2017 | 2:07 am
You are here:  / HOWTO / HOWTO: NameVirtualHost

HOWTO: NameVirtualHost

Every wonder how to set up multiple domain names on one IP address?

Here’s how:

Install Apache2.

In the /etc/apache2/sites-available directory, you’ll need to create a file for each virtual host that you want.
For our example, we will create 2 sites, example1.com and example2.com
First, you will want to create the directories for our sites:

# mkdir /home/example1.com
# mkdir /home/example1.com/logs
# mkdir /home/example1.com/www
# mkdir /home/example2.com
# mkdir /home/example2.com/logs
# mkdir /home/example2.com/www

Next, you will need to edit /etc/apache2/sites-available/default and add the following item to the top of the file:

<NameVirtualHost *>

Save the file and continue:
Open up your favorite editor (vi in my case) and punch in the following lines:

<VirtualHost *>

ServerAdmin webmaster@example1.com
ServerName www.example1.com
ServerAlias *.example1.com
DocumentRoot /home/example1.com/www/
ScriptAlias /cgi-bin/ /home/example1.com/www/cgi-bin/
<Directory>
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all

</Directory>
ErrorLog /home/example1.com/log/apache2/error.log
LogLevel warn
CustomLog /home/example1.com/log/access.log combined
</VirtualHost>

Save the file as /etc/apache2/sites-available/example1.com.
Then copy that file to example2.com:

# cp example1.com example2.com

Open the new file in your favorite editor and change all the instances of example1.com to example2.com.
Now, all you need to do is create links in the /etc/apache2/sites-enabled directory to the config files you just created:

# ln -s /etc/apache2/sites-available/example1.com /etc/apache2/sites-enabled/example1.com
# ln -s /etc/apache2/sites-available/example2.com /etc/apache2/sites-enabled/example2.com

Reload Apache:

# /etc/init.d/apache2 reload

That is pretty much it.
Create index.html pages in each new home directory so you can see that they are pulling from the correct directories.