Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am testing a configuration on my local system to learn Apache configuration and use the same method to handle my production server.

I need to configure two domains example.com and example.org

I have the directories where the php scripts are there:

/server/example.com for example.com and /server/example.org for example.org

I ran this command on both the directories:

sudo chcon -R -h -t httpd_sys_content_t /server/example.*

index.php in example.com contains

<?
$debug = ($_SERVER['HTTP_HOST'] == 'example.com');
if ($debug) echo "in example.com";
?>

and

index.php in example.org contains

<?
$debug = ($_SERVER['HTTP_HOST'] == 'example.org');
if ($debug) echo "in example.org";
?>

also I created a apache conf file named example.conf under /etc/httpd/conf.d/example.conf

Here are the contents of the .conf file:

<VirtualHost example.com:81>
    DocumentRoot /server/example.com
    ServerName example.com
    <Directory "/server/example.com">
        AllowOverride None
        Options All
        Order allow,deny
        Allow from all
        DirectoryIndex index.php index.html index.htm
    </Directory>
</VirtualHost>


<VirtualHost example.org:82>
    DocumentRoot /server/example.org
    ServerName example.org
    <Directory "/server/example.org">
        AllowOverride None
        Options All
        Order allow,deny
        Allow from all
        DirectoryIndex index.php index.html index.htm
    </Directory>
</VirtualHost>

When I access example.com the site works as expeted. The script file in /server/example.com get evaluated to true and displays in example.com

Now, when I access example.org. the site shows nothing.

Also, here is my /etc/host that I modified:

127.0.0.1 example.com
127.0.0.1 example.org

Can anybody suggest what I am missing here?

share|improve this question
1  
Minor point about SELinux, chcon is only valid for the files you specify and won't be inherited to new files. Use semanage and restorecon instead. This being the case, you should check your PHP files definitely have the right SELinux context with ls -lZ. –  James O'Gorman Feb 11 '12 at 23:30
    
@JamesO'Gorman, Thanks for the input on chcon and SELinux. I have disabled SELinux for now. –  Ashwin kumar Feb 12 '12 at 5:08
add comment

1 Answer 1

up vote 5 down vote accepted

Are you accessing example.org:82, or just example.org? Currently, your sites are bound to different ports. This probably isn't what you want; you probably want to use name-based virtual hosting instead.

You'll need a NameVirtualHost directive somewhere, and to change your <VirtualHost> directives to match it.

Something like this:

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /server/example.com
    ServerName example.com
    ServerAlias www.example.com
    # etc
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /server/example.org
    ServerName example.org
    ServerAlias www.example.org
    # etc
</VirtualHost>
share|improve this answer
1  
He also needs to remove the hosts file entries if he's going to use the hostname in the <VirtualHost> directives. Apache resolves the hostnames and uses their ip, so his virtualhost directives end up being <VirtualHost 127.0.0.1:81>. But of course if he uses the wildcard like in your example config its not an issue. –  Patrick Feb 11 '12 at 21:03
    
@Patrick Yes indeed - and trusting something like that to name resolution tends to be nothing but trouble. –  Shane Madden Feb 11 '12 at 22:26
    
@ShaneMadden, I want to access the site as example.com & example.org. I followed your instructions exactly and it did not work as expected. after I restarted httpd service and when I am trying to access example.com or example.org it is pointing to /var/www/html/ directory. It is not pointing to /server/example.com/ and/or /server/example.org/. –  Ashwin kumar Feb 12 '12 at 4:06
1  
@Woooyyyeee Then you have other config elsewhere that's causing your configuration to be ineffective. What output do you get from apache2ctl -S? –  Shane Madden Feb 12 '12 at 4:36
1  
@Woooyyyeee Absolutely - as long as you don't need to support encryption (which opens up a whole can of worms, see here), then running two different sites off the same IP address is no problem at all (that's what the NameVirtualHost config line is doing - allowing multiple sites to be served from one IP address). –  Shane Madden Feb 12 '12 at 5:07
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.