Setting from Beginning
Apache Installation
$
sudo aptitude install apache2
$sudo apt-get install apache2-utils apache2-common
NameVirtualHost
With the default configuration you are only serving up one site, and that site is based on your IP address. What I’m setting up is name-based virtual hosting, meaning the Apache server will serve specific content based on the domain name requested. In this way a single server can host multiple sites, and serve up unique content based on the domain requested.
My preferred method of using name based virtual hosting is creating a separate file for each domain. These can all be done within one file, but I’ll be creating a new file for each site.
First we need to define to Apache that we’re using name based virtual hosting instead of IP based. You can append the following line to your /etc/apache2/apache2.conf to define this:
NameVirtualHost ip.address:port
The above mention information could be configured seperately into ports.conf as this file is included in apache2.conf. In my case the following is the configuration file for /etc/apache2/ports.conf
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default
# This is also true if you have upgraded from before 2.2.9-3 (i.e. from
# Debian etch). See /usr/share/doc/apache2.2-common/NEWS.Debian.gz and
#NameVirtualHost 10.96.11.6:3000
# * takes localhost ip 127.0.0.1 while you may put your local / public IP for hosting globally.
NameVirtualHost *:80
NameVirtualHost *:3030
NameVirtualHost *:3000
NameVirtualHost *:4000
Listen 80
Listen 3000
Listen 3030
Listen 4000
<IfModule mod_ssl.c>
# If you add NameVirtualHost *:443 here, you will also have to change
# the VirtualHost statement in /etc/apache2/sites-available/default-ssl
# to <VirtualHost *:443>
# Server Name Indication for SSL named virtual hosts is currently not
# supported by MSIE on Windows XP.
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
The above should be your public facing IP address (assuming you’re creating a public site), and port is generally port 80 by default. After this we’ll create the base configuration for your virtual hosts. Debian and Ubuntu use /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/ directories for defining virtual hosting. One nice thing about this is that you can have more sites “available” than you have “enabled”, meaning not everything configured is actually live and listening. This is nice to quickly disable a site for whatever reason.
I like to create unique files for each of my domains within the /etc/apache2/sites-available/ folder. For example I have 3 different files called “drpaudel.com.np” “bishnu.com” and “shree.com” in that directory, with the following contents. Here I have only given example of “drpaudel.com.np” for other 2 check port and other details.
<VirtualHost *:3030>
ServerAdmin rajme690@gmail.com
ServerName drpaudel.com.np
ServerAlias www.drpaudel.com.np
DocumentRoot /home/dambar/public_html/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/dambar/public_html/>
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 /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/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>
What these settings do is as follows:
- ServerName listens for requests asking for a certain domain
- ServerAlias defines any additional domains that should match
- ServerAdmin is the contact for the site
- DocumentRoot is the path to the content for that site
Setting Document Root Directories
The document root directory must be as defined in /etc/apache2/site-available/sitename file. In my case I am placing individual www-data into seperate users directory in home folder ie. /home/user/public_html/ but document can be archived in one folder at any place.
Linking site-available with site-enable
By default site-available with site-enable is already there. Sometimes while adding new site these linking may not be working so we specified a new host to apache but it is not yet linked to the repertory where apache actually look for virtual hosts. Let go to:
$cd /etc/apache2/sites-enabled/
and create a link to the file we just created:
$sudo ln -s /etc/apache2/sites-available/drpaudel.com.np drpaudel.com.np
Now apache is almost ready to restart, but before doing so, we must inform our linux system that drpaudel.com.np and drpaudel are not to be looked for on the net, but on the local machine instead.
To do so, simply edit /etc/hosts and add the new host names at the end of the line beginning by 127.0.0.1, which is localhost.
In the end, your file should look like:
127.0.0.1 localhost.localdomain localhost drpaudel.com.np www.drpaudel.com.np drpaudel
Enable Virtual Host
sudo a2ensite drpaudel.com.np
And now we are done, simply reload apache:
sudo /etc/init.d/apache2 reload
Test your URL
your site will be working fine.