Domain – Multiple domain names on the same apache server

apache-2.2configurationdomain

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?

Best Answer

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>