3

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?

Ashwin
  • 173
  • 3
  • 10
  • 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 Feb 12 '12 at 05:08

1 Answers1

7

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>
Shane Madden
  • 112,982
  • 12
  • 174
  • 248
  • 1
    He also needs to remove the hosts file entries if he's going to use the hostname in the directives. Apache resolves the hostnames and uses their ip, so his virtualhost directives end up being ``. But of course if he uses the wildcard like in your example config its not an issue. – phemmer 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 Feb 12 '12 at 04: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 04:36
  • @ShaneMadden, brilliant! I executed: apachectl -S and it returned [Sun Feb 12 10:07:45 2012] [error] (EAI 2)Name or service not known: Could not resolve host name example.com/ -- ignoring! httpd: apr_sockaddr_info_get() failed for dexter.brains httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName [Sun Feb 12 10:08:05 2012] [error] VirtualHost 127.0.0.1:80 -- mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results – Ashwin Feb 12 '12 at 04:47
  • contd... VirtualHost configuration: 127.0.0.1:* is a NameVirtualHost default server wyswtf.com (/etc/httpd/conf.d/wordpress.conf:2) port 80 namevhost wyswtf.com (/etc/httpd/conf.d/wordpress.conf:2) wildcard NameVirtualHosts and _default_ servers: *:80 is a NameVirtualHost default server example.org (/etc/httpd/conf.d/example.conf:21) port 80 namevhost example.org (/etc/httpd/conf.d/example.conf:21) Syntax OK – Ashwin Feb 12 '12 at 04:47
  • I had wordpress.conf as well in etc/httpd/conf.d/ directory. For time being I removed the configuration file and it started working – Ashwin Feb 12 '12 at 04:48
  • @ShaneMadden, I have one more question: I have purchased two domain names from godaddy.com and I have one private server in amazon. I have pointed both the domain names to that server (temporarily). I want to host two different sites on the same server. Will the same configuration work? – Ashwin Feb 12 '12 at 04:55
  • 1
    @Woooyyyeee Absolutely - as long as you don't need to support encryption (which opens up a whole can of worms, see [here](http://serverfault.com/questions/109800)), 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 05:07
  • @ShaneMadden, That answers my question. At this time I am not supporting encryption. But I have future plans to do so. Will get familiar handling the apache conf files before giving encryption a try. – Ashwin Feb 12 '12 at 05:17