Solved

Adding virtual hosts to apache

Posted on 2011-03-10
13
389 Views
Last Modified: 2012-05-11
Hi,

I try to config my apache to to work with virtual hosts.
In a tutorial I found a sample how to config apache to also use mydomain.de with my apache.
Also the tutorial point out that I've to keep my default settings as without virtual hosts are not possible.
So I add my /etc/apache2/sites-available/mydomain I also include and enable did:
sudo a2ensite mydomain
After this I did a apache2 reload but the matter ist that mydomain.de directs to /var/www and not like I want to /var/www/web5 - I read the tutorial many time but I don't find any mistype ...

Can somebody please helpme make it work?

Thanks

Andre
<VirtualHost *>
	ServerName www.mydomain.de
	ServerAlias mydomain.de *.mydomain.de
	ServerAdmin webmaster@localhost
	
	DocumentRoot /var/www/web5/
	<Directory /var/www/web5/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		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
	ServerSignature On

</VirtualHost>

Open in new window

<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 /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>

Open in new window

0
Comment
Question by:andre72
  • 6
  • 5
  • 2
13 Comments
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
You have to set your DocumentRoot value to /var/www/web5
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
From the code that you posted
<VirtualHost *:80>
      ServerAdmin webmaster@localhost

      DocumentRoot /var/www    <------THIS IS THE PROBLEM
Make
         DocumentRoot /var/www/web5
0
 

Author Comment

by:andre72
Comment Utility
But this is the default - in this case any URL points to /var/www right?
Zhat's not what I'm looking for, keep default to /var/www but let mydomain1.com redirect to /var/www/web1, mydomain2.com to /var/www/web2 etc. ...
0
 
LVL 31

Accepted Solution

by:
farzanj earned 250 total points
Comment Utility
Ok.  Now I understand.

You will need to use mod rewrite.

Here is how it works.


Options +FollowSymLinks
Options +Indexes
RewriteEngine On

RewriteCond %{HTTP_HOST} mydomain1.com
RewriteRule .* /web1 [QSA,L]

RewriteCond %{HTTP_HOST} mydomain2.com
RewriteRule .* /web2 [QSA,L]


This should be close to what you need.

Another way is that I think you need one virtual host for mydomain1.com and another  virtual host for mydomain2.com.  This way both the virtual hosts will have a separate documentroot and the problem will be solved .
0
 

Author Comment

by:andre72
Comment Utility
Thanks for your support farzani.
Maybe you can also show me the other way with two seperate hosts as that what I'm looking for and try out here but I can't make it work ...
0
 
LVL 2

Assisted Solution

by:Saikapian_4739
Saikapian_4739 earned 250 total points
Comment Utility
Here is the second way for which you are looking for, setting up two seperate vhosts for two diffenert domains :

<VirtualHost *>
        ServerName www.mydomain1.com
       
        ServerAdmin webmaster@localhost
       
        DocumentRoot /var/www/web1/
        <Directory /var/www/web5/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ErrorLog /var/log/apache2/error1.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access1.log combined
        ServerSignature On

</VirtualHost>


<VirtualHost *>
        ServerName www.mydomain2.com
       
        ServerAdmin webmaster@localhost
       
        DocumentRoot /var/www/web2/
        <Directory /var/www/web5/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ErrorLog /var/log/apache2/error2.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access2.log combined
        ServerSignature On

</VirtualHost>

 This is really straight forward, this will set up two different vhosts for two different domains with two different Document roots from where the content will be served.

Thanks !!
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Expert Comment

by:Saikapian_4739
Comment Utility
Sorry.. Just one typing mistake in the above post.

<Directory /var/www/web5/> ----> <Directory /var/www/web2/> for www.mydomain2.com

<Directory /var/www/web5/> ----> <Directory /var/www/web1/> for www.mydomain1.com
0
 

Author Comment

by:andre72
Comment Utility
But this is exact what I tried so what is the problem why my solution does not work ...
What I've to change?
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
All basically what you need is two virtual hosts:
Listen 80

<VirtualHost IPADDR:80>
DocumentRoot /var/www/web1/
ServerName  www.mydomain1.com
</VirtualHost>

<VirtualHost IPADDR:80>
DocumentRoot  /var/www/web2/
ServerName  www.mydomain2.com
</VirtualHost>

Open in new window

0
 

Author Comment

by:andre72
Comment Utility
Yes I know about the config files but you never take care about my default settings or if you do I don't understand ...

Ok, I start again to explain:
I've one global domain, let's call it myglobal.com and a fistful other domains, mydomain1.com, mydomain2.com etc. ...
Now I like to config them like that:
myglobal.com refers to /var/www
mydomain1.com refers to /var/www/web1
mydomain2.com refers to /var/www/web2
...

So I do begin to write config files for like the included ones.

Now I open mydomain1.com and it will refer me to /var/www and not to /var/www/web1 like I want to.
How to modify my VirtualHost files to make this work?
Config file "default", used for eg. myglobal.com:
<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 /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>

Config file for mydomain1.com:
<VirtualHost *>
	ServerName www.mydomain1.de
	ServerAdmin webmaster@localhost
	
	DocumentRoot /var/www/web1/
	<Directory /var/www/web1/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		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
	ServerSignature On

</VirtualHost>

Config file for mydomain2.com:
<VirtualHost *>
	ServerName www.mydomain2.de
	ServerAdmin webmaster@localhost
	
	DocumentRoot /var/www/web2/
	<Directory /var/www/web2/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		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
	ServerSignature On

</VirtualHost>

Open in new window

0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
I don't understand why you are keeping everything nested together.  What is the downside of making three virtual hosts?  And the <Directory> tags can be separated too.  One tag for each DocumentRoot.  So you have three tags out of the big nested config you have.  Think about it.
Listen 80

<VirtualHost IPADDR:80>
DocumentRoot /var/www
ServerName  myglobal.com 
</VirtualHost>


<VirtualHost IPADDR:80>
DocumentRoot /var/www/web1/
ServerName  www.mydomain1.com
</VirtualHost>

<VirtualHost IPADDR:80>
DocumentRoot  /var/www/web2/
ServerName  www.mydomain2.com
</VirtualHost>

Open in new window

0
 

Author Comment

by:andre72
Comment Utility
The reason for is the German tutorial from here (http://wiki.ubuntuusers.de/Apache/Virtual_Hosts)

Im Verzeichnis /etc/apache2/sites-available/ befindet sich die Datei default. Diese kann man als Vorlage für die weiteren virtuellen Hosts verwenden. Für jeden virtuellen Host wird dann eine eigene Datei angelegt.

Auch sollte die default-Site nicht deaktiviert werden, da die darin enthaltene "NameVirtualHost"-Direktive erst den Betrieb von Virtual Hosts möglich macht.

I try to translate:
In the directory /etc/apache2/sites-available/ you will find a default file. You can use it as template for every virtual host. Make an own file for every virtual host.

You shouldn't deactivate the default site as the included tag "NameVirtualHost" make Virtual Hosts working ...


I'm sorry for if this documentation is wrond but I did like discrebed here:
1. Make a copy of the default file: I did and config mydomain1.com and mydomain2.com as shown
2. Don't deactivate the default site (this is my second attachment beginning with <VirtualHost *:80>)


I see call it myglobal.com was a stupid idea for sure this is just an other virtual host - it really should be the default ...
0
 

Author Closing Comment

by:andre72
Comment Utility
It did not work because of missing NameVirtualHost * in ports.conf ...

Thanks for your support
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now