Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Adding virtual hosts to apache

Posted on 2011-03-10
13
Medium Priority
?
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 31

Expert Comment

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

Expert Comment

by:farzanj
ID: 35101516
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
ID: 35101973
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Accepted Solution

by:
farzanj earned 1000 total points
ID: 35102778
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
ID: 35106624
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 1000 total points
ID: 35107484
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
 
LVL 2

Expert Comment

by:Saikapian_4739
ID: 35107497
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
ID: 35107547
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
ID: 35108122
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
ID: 35108879
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
ID: 35108957
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
ID: 35109157
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
ID: 35109684
It did not work because of missing NameVirtualHost * in ports.conf ...

Thanks for your support
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

636 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