Results 1 to 5 of 5

Thread: Apache2 virtual host problem

  1. #1
    Join Date
    May 2008
    Location
    Auckland, New Zealand
    Beans
    69
    Distro
    Ubuntu 8.04 Hardy Heron

    Apache2 virtual host problem

    I have a live server running Ubuntu 7.10 and Apache2. The hostname is example.com

    Code:
    ~$ hostname
    example.com
    I successfully have multiple virtual hosts running. If the host header is not specified in a request a default page will be displayed. eg http://255.255.255.255/ will display the default page.

    But I can not get the example.com virtual host running correctly. A request for example.com will incorrectly display the default page.

    In the default page <?php echo $_SERVER['SERVER_NAME']; ?> will correctly display either the ip address or example.com

    I can get it working if I change "ServerName example.com" to "ServerName test.example.com", and setup the appropriate DNS CNAME record for test.example.com pointing to example.com

    Code:
    :/etc/apache2/sites-available$ cat ./example.com
    <VirtualHost *>
    	ServerAdmin webmaster@example.com
    	ServerName example.com
    
    	DocumentRoot /var/www/example.com/htdocs
    
    	CustomLog /var/log/apache2/vhosts/example.com.access.log combined
    
    	<Directory />
    		Options FollowSymLinks
    		AllowOverride None
    	</Directory>
    
    	<Directory /var/www/example.com/htdocs>
    		Options FollowSymLinks
    		DirectoryIndex index.php index.html
    		AllowOverride None
    		Order Allow,Deny
    		Allow from all
    	</Directory>
    </VirtualHost>
    Last edited by botfish; September 4th, 2009 at 11:40 AM.

  2. #2
    Join Date
    May 2008
    Location
    Auckland, New Zealand
    Beans
    69
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Apache2 virtual host problem

    To archive this I think I need to used a mixture of ip based and name based virtual hosting.

    From the apache2 docs:

    If the main_server has no ServerName at this point, then the hostname of the machine that httpd is running on is used instead.
    Does anyone have experience with this?

    Regards

  3. #3
    Join Date
    Mar 2009
    Location
    New York City
    Beans
    144
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Apache2 virtual host problem

    Might want to look into mod_rewrite.

  4. #4
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Apache2 virtual host problem

    If you define hostname as example.com, it means that the machine hostname is "example" and the domain is "com". That is not what you want. You must give your machine a hostname in the example.com domain, that's why it works when you change your ServerHost to test.example.com.

    Alo, the Apache doc says that the <VirtualHost> block whose ServerName is the same as the one defined on the global config file must go first:

    If you are adding virtual hosts to an existing web server, you must also create a <VirtualHost> block for the existing host. The ServerName and DocumentRoot included in this virtual host should be the same as the global ServerName and DocumentRoot. List this virtual host first in the configuration file so that it will act as the default host.
    From http://httpd.apache.org/docs/2.2/vhosts/name-based.html.
    Last edited by Bachstelze; September 23rd, 2009 at 11:35 PM.
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    May 2008
    Location
    Auckland, New Zealand
    Beans
    69
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Apache2 virtual host problem

    Thanks for your reply Bachstelze.

    So should my machine name be something.example.com and I should not have a virtual host with the same name?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •