Home

Debian/Ubuntu Tips & Tricks

Debuntu.org: .deb packages, Unix/Linux Tutorials and Articles.

sponsors


User login

Poll

Syndicate

Syndicate content


Tips


Virtual Hosting using Apache 2 on a linux machine

Virtual Hosting allow web servers to host more than one website on a sing machine. This is how sharing hosting works. I become pretty handy as well while develloping different web project on the same machine and allows you to access to your local repository using addresses such as http://dev.mysite.com instead of http://localhost/~myuser/myproject/ :) .
This tutorial is based on a machine runnning ubuntu/linux but should be the same on any debian based distribution and almost the same on other distributions.

First of all, you need an apache server ready to run on your machine, if it is not yet install, open a terminal and type

$sudo apt-get install apache2-utils apache2-common

Once the server is installed, it is time to get into apache 2 configuration.
Let's open apache's main configuration file, name /etc/apache2/apache2.conf. A search for the word virtual bring us to the following line:

Include /etc/apache2/sites-enabled/[^.#]*

This mean that when starting apache, it will look for files in /etc/apache2/sites-enabled/.
Lets go there and see what is in.

$cd /etc/apache2/sites-enabled/

$ls -l

total 1

lrwxrwxrwx 1 root root 36 2005-12-27 01:42 000-default -> /etc/apache2/sites-available/default

Well, this only links to the file in directory /etc/apache2/sites-available/ . You might wonder what is the point in doing such. Well, this simply allows you, mainly when you are using your box as a web server, to:

  1. Have a simple main configuration file
  2. Do be able to edit or create a new host by creating/editing a file from /etc/apache2/sites-available/
  3. In case your web server doesn't restart because of misconfiguration, you can simply remove the link from the file in /etc/apache2/sites-enabled/ pointing to the malformed file in /etc/apache2/sites-available/

Now let say you want to be able to map the domain name dev.example.com to you local machine, using the code file in /home/myuser/public_html/example.com/.
While in /etc/apache2/sites-available, create a new file (let say example.com.conf)

$sudo vi example.com.conf

Now add the following lines:

<VirtualHost dev.example.com>
ServerAdmin webmaster@localhost
#We want to be able to access the web site using www.dev.example.com or dev.example.com
ServerAlias www.dev.example.com
DocumentRoot /home/myuser/public_html/example.com
#if using awstats
ScriptAlias /awstats/ /usr/lib/cgi-bin/
#we want specific log file for this server
CustomLog /var/log/apache2/example.com-access.log combined
</VirtualHost>

Note:People who don't want to bother knowing how the site enabling system works might just jump to the end of the article to find debian built-in command syntax. If you want to know how it works, or do not use a debian based distro, carry on.

Now, 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/example.com.conf example.com.conf

Now apache is almost ready to restart, but before doing so, we must inform our linux system that dev.example.com and www.dev.example.com 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 dev.example.com www.dev.example.com

And now we are done, simply reload apache:

sudo /etc/init.d/apache2 reload

Open your web browser and enter the following address dev.example.com. Magic, it runs the same as when you were using http://localhost/~myuser/example.com but it is far more usefull when devellopping a web service and want to be able to develop applications on your machine just like it is where the real web site.

Edit: As you can see from the comments, many people pointed out that you can use a debian specific command (so if you are not using a debian based system, don't expect to find that command :) ).
to enable a new virtual host simply type:

sudo a2ensite mysiteavailable-site

to disable a virtual host:

sudo a2dissite mysiteavailable-site

where mysiteavailable-site is the name of the virtual hos you want to enable/disable, so in out example: example.com.conf

Hope this helped.

step by step...

hello I'm a new user!

I've followed step by step this guide but I still got a problem.
When I type my personal url in the browser, the document root is pointing in var/www/ and show me "it works!"

but in my madjammer.com.conf file I set the documentroot in /home/madmaster/public_html/madjammer/sito

this is my configuration file :

//

# MadJammer.no-ip.biz

ServerName madjammer.no-ip.biz

ServerAlias www.madjammer.no-ip.biz
ServerAdmin webmaster@localhost

DocumentRoot /home/madmaster/public_html/madjammer/sito
#if using awstats
ScriptAlias /awstats/ /usr/lib/cgi-bin/
#we want specific log file for this server
CustomLog /var/log/apache2/madjammer.com-access.log combined

Options All

AllowOverride All

order allow,deny

allow from all

//

please help I need to understand

Cheers form italy!

Mac Os X

Hi there,

Thankyou for your post, it was really great.
It went perfectly on my Linux machine. However, I wondered how to Enable virtual hosts on my mac
I have a mac book pro with tiger installed. I'm not sure about the version, i think it's 10.4 as it worked perfectly following this
tutorial on set up virtual hosts : http://hickit.com/category/unix/mac-os-x/10-4/server/apache2/virtual-hosts/
I post it here if any of you have a mac...

Thank you again,

Best regards

Apache2 and VirtualHost

Ubuntu 8.04 Apache2 seems working "It Works" comes up in localhost.
PHP5 and MySQL installed.

Followed your http://www.debuntu.org/2006/02/22/7-virtual-hosting-using-apache-2 ....using dafhobson.com instead of example.com etc etc

In Firefox, typing dev.dafhobson.com I get:-

" Forbidden. You don't have permission to access / on this server.
Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.3 with Suhosin-Patch Server at dev.dafhobson.com Port 80 "

The hosts file... /etc/apache2/hosts was defaulted to 127.0.0.1 = localhost. and 127.0.1.1 = kitchen-desktop (computer name)

Apache2 reload gives:-
* Reloading web server config apache2 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName

So in the hosts file I changed kitchen-desktop to dev.dafhobson.com (127.0.1.1) and restaring Apache2 gives:-
* Reloading web server config apache2 [ OK ]

I still have Frobidden message in Browser.

dafhobson.com uses some php.

Yes I'm obviously newbie but utterly determined never to return to Microsoft

If anyone can point me I would be grateful.
Daf Hobson

ps:

Just reinstalled Apache2 and localhost does not render "It Works"

Oh heck It's hell being a newbie

index.html

Hi,
do you have an index.html file at the root of dafhobson.com ?
Debuntu

Apache2 and VirtualHost

No. it is index.php

Thanx

It works but how to access the sites from another PC?

Thanks so much for the clear and simple guide. I have 2 PCs, one running Ubuntu 8.04 and one running Win XP in my home. I set up Apache2 with 2 samples sites on the Ubuntu PC as outlined and it works. But when I try to access those sample sites from the other PC (Win XP), the browser can't find the sites. Please help.

OK, the Hostname is lamp.

OK, the Hostname is lamp. And the WinXP PC navigates to one of the sites with url http://lamp/ but not with http://samplesitename, which it can't find.

Problem fixed

It turned out that I need to add the sites in the client HOSTS file located in %system32/drivers/etc/hosts in my WinXP PC.

httpd (no pid file) not running

Hi, I followed this and other similar tutorials to have virtual host with apache2, but with in all cases i get the same, at the end of all when i have to restar my apache it fails, i tried with force-reload but i gives me the following error:

#/etc/init.d/apache2 force-reload
Forcing reload of web server (apache2)....httpd (no pid file) not running
failed!


not running

This means that http is not running.

# /etc/init.d/apache2 start

might help.
If not, look at /var/log/apache2/error.log , there might be some useful hints there.

Debuntu

Thanks

The /etc/init.d/apache2 start didn't work, but i check the log file and i discover the problem, and url from the log file of my test.com domain was wrong, thank you very much

Problems..

Hello:

I have two DNS names that are linked to my external IP address for my router. I have a server inside of my NAT and I am trying to get apache2 to work with virtual servers.

Right now I am only able to get the first website to show up and not the second. and I am unable to access the sites from the net for some reason (when not using virtual server I can access my single website) once I try to use multiple websites I can not access any of the sites from the web and only one of the sites from my local machine.

In my sites-available folder i have the following:

File ----- default

NameVirtualHost *:80
DocumentRoot /var/www

File ------ websites

< VirtualHost firstwebsite.com>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/first
CustomLog /var/log/apache2/first combined


< VirtualHost secondwebsite.com>
ServerAdmin webmaster@localhost
ServerAlias www.secondwebsite.com
DocumentRoot /var/www/second
CustomLog /var/log/apache2/second combined

In my sites-enabled folder i have the following:

File ----- 000-default (link to file mentioned above)

File ------ websites (link to file mentioned above)

In my hosts file i have the following

File ----- Hosts

127.0.0.1 localhost.domain localhost firstwebsite.com secondwebsite.com
127.0.1.1 servername

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback servername
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

I can start apache2 but get the following warnings:

* Reloading web server config apache2 12753
[Wed Apr 02 14:58:02 2008] [warn] The Alias directive in /etc/apache2/apache2.conf at line 240 will probably never match because it overlaps an earlier Alias.
[Wed Apr 02 14:58:02 2008] [warn] VirtualHost firstwebsite:0 overlaps with VirtualHost secondwebsite.com:0, the first has precedence, perhaps you need a NameVirtualHost directive
[Wed Apr 02 14:58:02 2008] [warn] NameVirtualHost *:80 has no VirtualHosts

Can anyone suggest how I would repair this problem? Why am I unable to host multiple websites and why can I not access any of them from the net?

Thanks.


dont understand

Hai, Im in new user so here i dont understand , pls write details for me....

" Well, this only links to the file in directory /etc/apache2/sites-available/ . You might wonder what is the point in doing such. Well, this simply allows you, mainly when you are using your box as a web server, to:

1. Have a simple main configuration file

2. Do be able to edit or create a new host by creating/editing a file from /etc/apache2/sites-available/

3. In case your web server doesn't restart because of misconfiguration, you can simply remove the link from the file in /etc/apache2/sites-enabled/ pointing to the malformed file in /etc/apache2/sites-available/

Now let say you want to be able to map the domain name dev.example.com to you local machine, using the code file in /home/myuser/public_html/example.com/.
While in /etc/apache2/sites-available, create a new file (let say example.com.conf)

$sudo vi example.com.conf

"

pls mail me ...
thanks

Diference?

Is there a difference in configuring Virtual Host at Apache2 on Ubuntu 6.06.1 LTS server from one that is written here?

Tnx for quick answer.

No, it should be working

No, it should be working fine. As far as I can remember this was done on a Ubuntu box.
Debuntu

Not Working

I added the virtual host and I think it is all configured right in the sites-enabled directory. I added my domain name to the hosts file.

When I go to my site, I get the apache default index file.

Index of /

link to apache2-default

Any Ideas?

VirtualHost

You might want to make sure your VirtualHost parameter
<VirtualHost dev.example.com>
or you ServerAlias parameter
ServerAlias www.dev.example.com
is properly set.
Then finally reload apache
/etc/init.d/apache2 reload
Hope this helps,
Debuntu

Adding more virtual host

I have added one more virtual host and got this message after restarting apache:
root@andrew-desktop:/etc/apache2/sites-enabled# apache2ctl -k restart
apache2: Could not determine the server's fully qualified domain name, using 127. 0.1.1 for ServerName
[Wed Dec 06 22:46:19 2006] [warn] VirtualHost exampractice.com:0 overlaps with Vi rtualHost phpmyadmin.com:0, the first has precedence, perhaps you need a NameVirt ualHost directive

Do I have to use NameVirtualHost directive if I add more virtual host?

Regards,
Andrew

A way to do it

In my /etc/apache2/sites-available/default I will have:

NameVirtualHost *:80

        DocumentRoot /what/ever/location/for/default/server
...
...

and thereafter, I will define new files by domains or owner. Let say we create /etc/apache2/sites-available/myhosts with inside:

#define first web server

        DocumentRoot /what/ever/location/for/myhost
        ServerName www.myhost.com
        ServerAlias myhost.com subdomain1.myhost.com
...
...

#define second web server

        DocumentRoot /what/ever/location/second/host
        ServerName othersubdomain.myhost.com
        ServerAlias www.othersubdomain.myhost.com
...
...

This has been working perfectly on any system dalt with. Hope it works for you :)
Debuntu

help

Just wondering if there aren't any other files to be edited other than the ones obove. I was trying this using two global Ip's instead of DNS (because I don't have spare DNS' to experiment with). and so i followed everything above but only the 1st site works. It can't detect the 2nd site.... here is my site2.conf:


ServerAdmin webmaster@localhost
ServerName 202.71.182.15
ServerAlias 202.71.182.15
DocumentRoot /var/www/lala/

Options FollowSymLinks
AllowOverride None


Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
#RedirectMatch ^/$ /apache2-default/

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all

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
ServerSignature On

Alias /doc/ "/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


Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all


ServerAdmin webmaster@localhost
ServerName 192.168.15.121
ServerAlias 192.168.15.121
DocumentRoot /var/www/testing/

Options FollowSymLinks
AllowOverride None


Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
#RedirectMatch ^/$ /apache2-default/

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all

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
ServerSignature On

Alias /doc/ "/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


Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all



It works

Thanks Chantra. It worked.

good news

Nice to hear it helped you out.
If you were to had ssl support, you will need to define a NameVirtualHost *:443 entry, and then each ssl'ed host will be define between
<VirtualHost *:443>
...
...
</VirtualHost>
tags
Debuntu

403 Forbidden

Hi,

I used the following:
<VirtualHost exampractice.com>
ServerAdmin webmaster@localhost
#We want to be able to access the website using www.exampractice.com or just exampractice.com
ServerAlias www.exampractice.com
DocumentRoot /home/andrew/public_html/
#if using awstats
ScriptAlias /awstats/ /usr/lib/cgi-bin/
#we want specific log file for this server
CustomLog /var/log/apache2/exampractice.com-access.log combined
</VirtualHost>

and then restarted apache2 and got the following error:
Forbidden

You don't have permission to access / on this server.

Did I miss something?

Regards,
Andrew

403 Forbidden

I just added:

<Directory /home/andrew/public_html>
Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>

and it worked.

Options help

Hi Andrew,
I believe both:

Options FollowSymLinks

and

Order deny,allow
Allow from all

helped
Debuntu

external ip

Hello Folks

Set up my ubuntu apache server as suggested above when I browse locally to example.com I go to the right place, in my case home/darrenl/sites/ludlam/index.htm.

But after adding example.com to no-ip.com setting up my router I get redirected to the apache default homepage when I browse externally ie to /var/www/

Hope this is understandable
Darren


Hi, Did you actually add the

Hi,
Did you actually add the right domain name in your apache host file? I mean, did you inform apache that it should use the files in /home/darren/sites/ludlam/ for mysubdomain.no-ip.com ?
Is your router the same machine then the apache machine? If not, make sure that port 80 is redirected to your apache server.
Hope this helps,
Debuntu

Hi, Which apache host file

Hi,

Which apache host file is it? please response.

Thanks,
Ron

Host files are the ones in

Host files are the ones in /etc/apache2/sites-available/ .
You need to define your host in this directory and then using a2ensite, enable the hosts defined into that file.
Debuntu

I know this post is rather

I know this post is rather old but rather that using symbolic links it's easier to use a2ensite/a2dissite commands. Same applies with modules: a2enmod/a2dismod

Cheers


yes indeed, I actually

yes indeed, I actually pointed this out in an article on how to enable apache module on debian.
the main idea was to make people understand how it works, so you can apply it on other distributions.
regards,
chantra
Debuntu


easy site enable-ment command

you can also use:

sudo a2ensite example.com.conf

which creates the link automatically.


ln -s /etc/apache2/site-avail

ln -s /etc/apache2/site-available/example.com.conf example.com.conf

should be:
ln -s /etc/apache2/sites-available/example.com.conf example.com.conf

Just a typo... nothing much

Thanks chantra
Very good help


Thanks for pointing this out

Thanks for pointing this out Pushkar...(a pretty nice town by the way ;)), I corrected it.