Apache is the most prevalent web server operating on the internet and there’s good reason for this. It’s robust, secure, stable and easy to configure with a myriad of add-ons (mods) to allow you to set up websites running PHP, Java and Ruby amongst many other uses such as a reverse proxy or web application firewall. Getting a website up and running is very easy with Apache as all Linux distributions give you access to Apache using the distributions installation tools such as Yum (Red Hat based) or apt (Debian based).
The guide below is tailored towards Ubuntu, a very popular Debian based Linux distribution but the guide is easily followed and adjusted for CentOS and other Red Hat based distributions.
At the end of this tutorial we will have achieved the following
- A single installation of Apache serving 2 sites
- site1.adminnation.com has content served from /var/www/site1.adminnation.com
- site2.adminnation.com has content served from /var/www/site2.adminnation.com
First step though is installing a basic operational Apache. Installation of Apache from Repositories 1. Install Apache using apt-get
apt-get install apache2
2. This will install Apache and place its configuration files in /etc/apache2. The configuration area is split up into multiple areas.
The main configuration file under Ubuntu is called /etc/apache2/apache2.conf. This file references other areas via Include statements. A default site is already installed and its configuration file is in /etc/apache2/sites-available/default. There is another similar directory called /etc/apache2/sites-enabled and in here are symbolic links to the “available-sites” configuration files. The reason for this set up is that it allows you to configure multiple websites and enable them and disable them with ease – you just delete the symbolic link from the “sites-enabled” directory but preserves the config file for later use.
- /etc/apache2/apache2.conf # Main configuration file
- /etc/apache2/sites-available/ # Website configuration files
- /etc/apache2/sites-enabled/ # Websites that are enabled
3. When serving multiple websites from a single Apache this called Virtual Hosting. Each Website configuration file under Apache is called a Virtual Host file, or vhost file for short. The default vhost is as follows:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
This defines a number of things, namely for the purpose of this tutorial, the important ones are “VirtualHost *:80” and “DocumentRoot“. These state that this vhost will be invoked when someone access the server on any address (the * reference of VirtualHost) on port 80 (standard http port) and that it will load up HTML pages from the directory /var/www. If you place an index.html file in this directory and you access your Apache webserver of your network (or localhost) it will render it as HTML. If you place test.html in there you can access this file with http://yourserver/test.html.
If Apache isn’t running, start this up
sudo /etc/init.d/apache2 start
Congratulations – you have installed Apache and seen how to load up content to serve on the internet.
Setting up Apache Virtual Hosting
But Apache is far more powerful for just serving up a static index.html so how do you set up Apache so that it can host multiple websites? First a little understanding on how Apache understands how it knows to serve which files as web content for that site. When a user visits a website, the web browser sends some information in the background that usually the user doesn’t know or care about. The webserver on the other hand uses this information to know what to do with the request and what site to load up.
A typical request looks like the following. In my browser I enter “http://www.adminnation.com/” and this is what actually gets sent.
GET / HTTP/1.1 Host: <a href="../">www.adminnation.com</a> User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:6.0) Gecko/20100101 Firefox/6.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 DNT: 1 Connection: keep-alive
Your computer does a look up for the IP address of www.adminnation.com and then connects to port 80 of that IP address and sends that information to it. Apache uses this information to load up the correct site and it does this by entering the corresponding elements required in the vhost files.
Let’s set up the vhost for “site1.adminnation.com”
1. Edit /etc/apache2/sites-available/site1.adminnation.com (note its best practice to name your vhost files to match the name of the website)
<VirtualHost *:80> ServerName site1.adminnation.com ServerAdmin <a href="mailto:webmaster@adminnation.com">webmaster@adminnation.com</a> ServerSignature email DocumentRoot /var/www/site1.adminnation.com CustomLog logs/site1.adminnation.com.access.log combined ErrorLog logs/site1.adminnation.com.error.log </VirtualHost>
2. Note that this is far simpler than the default presented from Ubuntu, that’s because Apache is very flexible and you can include many directives and configurations under vhost files that is appropriate for your site. For the purpose of this tutorial serving static content that differs per site, the above is good enough. As we don’t need the default any more you can safely remove this from the sites-available directory by simply deleting the symbolic link called 000-default.
Key lines are as follows:
- ServerName is very important for virtual hosting – Apache uses this to distinguish between the different virtual hosts/sites served from this Apache web server. This is the line that matches the “Host:” request header your browser sends as part of the request.
- ServerAdmin is an email address that people will use if they see the default error pages, and we enable that feature by saying that when an error displays, display the email address too (ServerSignature).
- DocumentRoot is set to /var/www/site1.adminnation.com so create this area with the correct permissions of the person or group that will manage this website (i.e. it shouldn’t be “root” or “apache” owned if it was a production site).
- CustomLog and ErrorLog reference the log files (CustomLog is the access log and ErrorLog is self explanatory).
3. Create an index.html file in /var/www/site1.adminnation.com and put in “site1.adminnation.com” as the text
mkdir -p /var/www/site1.adminnation.com echo "site1.adminnation.com" > /var/www/site1.adminnation.com/index.html
4. Now remember we created this file in the “sites-enabled” directory, so to enable it do the following
ln -s /etc/apache2/sites-available/site1.adminnation.com /etc/apache2/sites-enabled/site1.adminnation.com
5. Restart Apache to pick up the change (this can be done on a running server without interruption to service using the following):
/etc/init.d/apache2 graceful
A graceful restart waits for existing connections to die, and new ones are loaded with the new configuration.
That’s “site1.adminnation.com” complete.
6. Now repeat steps 1-5 replacing all references to site1.adminnation.com with site2.adminnation.com and you have 2 sites available on different hostnames but served from the same Apache web server.
Test the set up
To test this set up on your own Linux setup add the following lines to /etc/hosts
127.0.0.1 site1.adminnation.com site2.adminnation.com
Now in your browser go to http://site1.adminnation.com and then go to http://site2.adminnation.com – you should see the text change when you visit those sites. From here you can create more complex set ups based on this simple configuration of Apache.




Pingback: Running Multiple Websites using Apache — Admin Nation | RedHat Certification , RedHat Exams , RedHat Practice Exams , RedHat Braindumps
Pingback: Multiple websites on Apache « 0ddn1x: tricks with *nix
Pingback: Virtual Server Hosting using Apache « System Administration and Architecture Blog
Pingback: Running Multiple Websites using Apache — Admin Nation | Dzker