Web Servers
How to Configure Web Sites in Apache

Have you ever wondered how control panels such as Plesk and cPanel make changes to the Apache web server to host multiple websites on the same server? In this article, I will show you how to configure apache to host multiple websites.

Hosting services, such as Amazon EC2 and Slicehost, have no control panel installed with the operating system by default. I like having the ability to customize the operating system and web server.

Setup a Directory to Store Web Files

The first thing we need to do is setup a directory to store the web files for each of the websites to be hosted on the server. The directories can be setup in any number of places. The most popular are /var/www and /home/www. I prefer to create a root level folder called www and mount it to a separate drive, but if that is not available, then /home/www will do.

Let's setup /home/www and setup some directories for web files and logs. Connect to the server using SSH and run each of the lines below.

  1. mkdir /home/www
  2. mkdir /home/www/websiteone.com
  3. mkdir /home/www/websiteone.com/htdocs
  4. mkdir /home/www/websiteone.com/logs
  5. mkdir /home/www/websitetwo.com
  6. mkdir /home/www/websitetwo.com/htdocs
  7. mkdir /home/www/websitetwo.com/logs

If you get a permission denied error when creating the above directories, use su to run actions as root.

Setup Website Configuration in Apache

The next step is to add each website to the apache configuration. The apache configuration is typically located in /etc/apache2 or /etc/httpd. The following configuration code is for Apache version 2.2. Navigate to the sites-available folder in the apache configuration directory. Next, add a file for each website.

  1. touch websiteone.com
  2. touch websitetwo.com

If you get a permission denied error here, remember to use the root user to add the above files. Next, open the websiteone.com file in your favorite editor. I prefer to use nano, since it is similar in function to Notepad in Windows.

  1. <VirtualHost *:80>
  2.         ServerAdmin webmaster@websiteone.com
  3.         ServerName websiteone.com
  4.         ServerAlias websiteone.com
  5.  
  6.         DirectoryIndex index.php index.html
  7.         DocumentRoot /home/www/websiteone.com/htdocs/
  8.  
  9.         <Directory /home/www/websiteone.com/htdocs/>
  10.                 Options Indexes FollowSymLinks MultiViews
  11.                 AllowOverride all
  12.                 Order allow,deny
  13.                 Allow from all
  14.         </Directory>
  15.  
  16.         ErrorLog  /home/www/websiteone.com/logs/error.log
  17.         CustomLog /home/www/websiteone.com/logs/access.log combined
  18. </VirtualHost>

Make sure to replace any of the "websiteone.com" references with your domain name. The above code is a simple example of a configuration file for Apache. Each of the options are explained in the Apache Server Documentation.

Add the same configuration code above to the websitetwo.com file and any additional configuration files you have created. Next, we need to enable each of the configuration files with the web server.

  1. a2ensite websiteone.com
  2. a2ensite websitetwo.com

The Apache configuration will need to be reloaded for the changes to take affect.

/etc/init.d/apache2 reload

The command to reload the configuration for Apache may be different, depending on the operating system you are running.

The last step is to create a few sample html files, point the dns to your server, and test the results. If all went well, you will be able to load the sample pages you created.

Categories: 

Related Posts

Post new comment