Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

Adding virtual hosts to apache

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
andre72
Asked:
andre72
  • 6
  • 5
  • 2
2 Solutions
 
farzanjCommented:
You have to set your DocumentRoot value to /var/www/web5
0
 
farzanjCommented:
From the code that you posted
<VirtualHost *:80>
      ServerAdmin webmaster@localhost

      DocumentRoot /var/www    <------THIS IS THE PROBLEM
Make
         DocumentRoot /var/www/web5
0
 
andre72Author Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
farzanjCommented:
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
 
andre72Author Commented:
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
 
Saikapian_4739Commented:
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
 
Saikapian_4739Commented:
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
 
andre72Author Commented:
But this is exact what I tried so what is the problem why my solution does not work ...
What I've to change?
0
 
farzanjCommented:
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
 
andre72Author Commented:
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
 
farzanjCommented:
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
 
andre72Author Commented:
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
 
andre72Author Commented:
It did not work because of missing NameVirtualHost * in ports.conf ...

Thanks for your support
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now