Setting up virtual hosts with Apache

11 11 2008

Thought I should post about setting up virtual hosts in Apache, since this was one of the first things I had to do. I got all this info from this site, which was incredibly useful. For all the following instructions, when you see these instructions:

enter some/text/here

You need to enter this text exactly as you see it in your terminal, which can be found at Applications>Accessories>Terminal

First, you need to set up your folders where the sites are gonna be stored:

mkdir /home/www/www.example.com
mkdir /home/www/www.example.net

Then you need to tell Apache that you are gonna use virtual hosts. Create a file using:

sudo touch /etc/apache2/conf.d/virtual.conf

You need to add the following content, use:

sudo gedit /etc/apache2/conf.d/virtual.conf

and enter the following:

#
#  We're running multiple virtual hosts.
#
NameVirtualHost *

Now, you have to create an entry in both the sites-enabled and sites-available directories in /etc/apache2. So, first create the following file:

sudo touch /etc/apache2/sites-available/www.example.com

and enter the following by using:

sudo gedit /etc/apache2/sites-available/www.example.com

and enter:

#
#  Example.com (/etc/apache2/sites-available/www.example.com)
#
<VirtualHost *>
        ServerAdmin webmaster@example.com
        ServerName  www.example.com
        ServerAlias example.com

        # Indexes + Directory Root.
        DirectoryIndex index.html
        DocumentRoot /home/www/www.example.com/htdocs/

        # CGI Directory
        ScriptAlias /cgi-bin/ /home/www/www.example.com/cgi-bin/
        <Location /cgi-bin>
                Options +ExecCGI
        </Location>

        # Logfiles
        ErrorLog  /home/www/www.example.com/logs/error.log
        CustomLog /home/www/www.example.com/logs/access.log combined
</VirtualHost>

Next, do the same for your second site, so create a file using:

sudo touch /etc/apache2/sites-available/www.example.net

and enter the following by using:

sudo gedit /etc/apache2/sites-available/www.example.net

and enter:

#
#  Example.net (/etc/apache2/sites-available/www.example.net)
#
<VirtualHost *>
        ServerAdmin webmaster@example.net
        ServerName  www.example.net
        ServerAlias example.net

        # Indexes + Directory Root.
        DirectoryIndex index.html
        DocumentRoot /home/www/www.example.net/htdocs/

        # CGI Directory
        ScriptAlias /cgi-bin/ /home/www/www.example.net/cgi-bin/
        <Location /cgi-bin>
                Options +ExecCGI
        </Location>

        # Logfiles
        ErrorLog  /home/www/www.example.net/logs/error.log
        CustomLog /home/www/www.example.net/logs/access.log combined
</VirtualHost>

Finally, you need to create a copy of these files in the sites-enabled directory, using what is called a symbolic link. You can use a funky little program called a2ensite to do this. So, enter the following:

sudo a2ensite www.example.com
sudo a2ensite www.example.net

And finally (I was lying last time), you need to restart Apache for this to all take effect:

sudo /etc/init.d/apache2 reload

And tada, it should all be working, yay!

Advertisements

Actions

Information

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s