Hosting multiple websites on Apache2

So you have just installed an Apache2 HTTP server on your Ubuntu 7.10 Gandi server and now you want to start hosting websites for your friends or customers.

When it is freshly installed, Apache will show the same web page to every host name that points to it. So while you can put your web pages directly in /var/www/ this will only work if you intend on only having one website hosted on your server, and letting everyone point to it by an A record to your Gandi server's IP address.

Creating Virtual Hosts

Apache can host more than just one website. Indeed, it is possible to use Apache in a way so as to act like a web host for a limitless number other web sites. This method of virtual hosting is made possible by setting up what are called “virtual hosts” within your Apache server.

At the most basic level, there are only two directories that need to be used when setting up virtual hosts: /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/.

In apache 1.x, setting up virtual hosts was done by adding <virtualhost> entries in the /etc/apache/httpd.conf file. Apache2 maintains virtual hosts differently - by individual “sites-available” files that are linked to from the “sites-enabled” directory”.

In Apache2, to create virtual hosts on your server, you will need to add a file to your sites-available directory that corresponds to the virtually hosted website. A quick and dirty way to think of it is: One sites-available file = One hosted website

In the following example we will be adding a website called www.djmadjack.com to our Gandi server. However, you can host as many other websites as you want on your server. To do that, simply repeat the same operations as described below, but adapting them to the name of the other websites.

1. Make a new "sites-available" file

In the /etc/apache2/sites-available/ directory, you will see a default file called “default”. Use this as a template for your virtual host files. To to this, simply make a copy of it, that is renamed something else. For example:

cp default djmadjack.com

For clarity, it is a good idea to name the file to be the same as the domain name of the website that will be hosted. This will allow easily identifying the files on your server. You can call them anything you want however.

2. Edit the new "sites-available" file

Now you need to edit the file you have just created so that it will work for the website you want to host on your server. Start by opening it in your favorite text editor..

nano djmadjack.com

…and edit it so that it contains the name of the website you want to host. In this case, let's say I want to host djmadjack.com's website. Below is an example of how the beginning of the edited new “sites-available” will look:

<VirtualHost *>
      ServerAdmin eric@djmadjack.com
      ServerName www.djmadjack.com
      DocumentRoot /var/www/djmadjack.com/
      <Directory />
              Options FollowSymLinks
              AllowOverride None
      </Directory>
      <Directory /var/www/djmadjack.com/>
              Options Indexes FollowSymLinks MultiViews
              AllowOverride None
              Order allow,deny
              allow from all
      </Directory>

 (...)

You can remove the first line NameVirtualHost * that is copied from the default file.

Be sure to add the ServerName line, which is not in the default file.

The DocumentRoot line is where you specify the directory the web pages of your friend will go on your server. You can make up anything you want to identify this, but be sure that you have made this directory. This is where the web pages for djmadjack.com will go.

There is no limit to how many websites you want to virtually host. All you need to do is to create a new file in the sites-available directory. This being said, the more websites that you host on your server, the slower your server will be. Therefore, if your Gandi server is handling the websites slowly, then you can simply add more shares in order to increase its power and speed up the websites.

Finally, you need to create a symbolic link to your file in the /etc/apache2/sites-enabled/ directory. To do this type:

cd /etc/apache2/sites-enabled

To move to the sites-enabled folder, and then:

ln -s /etc/apache2/sites-available/djmadjack.com

To create a link to your newly-created virtual host file.

Alternatively you can use a2ensite and a2dissite to enable an disable your new domain configuration.

To enable your new domain configuration, you can also create a link automatically using the command:

a2ensite djmadjack.com

And to disable the djmadjack.com configuration you can type:

a2dissite djmadjack.com

4. Restart apache

When done, be sure to restart apache so that your changes will be taken into consideration by your server. Do this by typing in the following:

/etc/init.d/apache2 restart

For minor changes in configuration you can just reload the configuration of Apache, this will have a smaller impact on your running site.

/etc/init.d/apache2 reload

That is all! Do not forget that you need to point the domains of the websites that you are hosting to your Gandi server.

5. Troubleshooting

Do not forget to restart apache after making changes to its configuration!

Error:

apache2: apr_sockaddr_info_get() failed for ubuntu

Solution: in etc/hosts, add the name of your machine before localhost like the following:

127.0.0.1       ubuntu localhost

Error: apache2: Could not reliably determine the server's fully qualified domain name Solution: You need to add your server's name in the /etc/apache2/httpd.conf file. If you don't know yous server's name, type the following in your terminal:

hostname

It will then return the name of your server; for example, ubuntu. Then, using your favorite text editor, add the an entry in your /etc/apache2/httpd.conf file as follows (replace ubuntu with whatever the name of your server is):

ServerName ubuntu


Last modified: 02/26/2008 at 09:51 by .