1

My server information is

Server version: Apache/2.4.6 (CentOS)
Server built:   Nov 19 2015 21:43:13

I am trying to configure virtual host for 2 different sites: biz.example.com and pin.example.com which are hosted on the same server. There are 2 different folders located under 'var/www/html/' named 'biz' and 'pin' with their respected project files for the above mentioned 2 websites. I am trying to configure it on the below way.

Within /etc/hosts below configuration

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

xxx.xxx.xxx.xxx biz.example.com
xxx.xxx.xxx.xxx pin.example.com

xxx.xxx.xxx.xxx is replaced by the server IP address.

Within /etc/httpd/conf/httpd.conf

IncludeOptional sites-enabled/*.conf

Now, under /etc/httpd/sites-available there are biz.conf and pin.conf file. I also have the folder sites-enabled under /etc/httpd which has 2 files that points to the biz.conf and pin.conf of sites-available folder using the below command

ln -s /etc/httpd/sites-available/biz.conf /etc/httpd/sites-enabled/biz.conf

ln -s /etc/httpd/sites-available/pin.conf /etc/httpd/sites-enabled/pin.conf

biz.conf has the followings

<VirtualHost *:80>
ServerName http://biz.example.com/
ServerAlias http://biz.example.com/
DocumentRoot "/var/www/html/biz"
<directory "/var/www/html/biz">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Allow from 127.0.0.1
</directory>
</VirtualHost>

And the configuration within pin.conf file is mentioned as

<VirtualHost *:80>
ServerName http://pin.example.com/
ServerAlias http://pin.example.com/
DocumentRoot "/var/www/html/pin"
<directory "/var/www/html/pin">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Allow from 127.0.0.1
</directory>
</VirtualHost>

On this setup, if i try to access http://biz.example.com/ , the correct website (biz website) is loading. But if i try to access http://pin.example.com/ , then also biz website is loading instead of pin website. Multiple configuration is not working together.

I also tried to merge the virtual configuration of biz.conf and pin.conf within a single file, biz.conf, but it didn't work as well.

  • Are there error messages from Apache (this is Apache, right?) in its own log files or syslog messages? – Ned64 Feb 15 '18 at 14:39
  • 1
    I know is late post - but don't need http just domain example.com and you don't need trailing slash or quotes.. and allow from 127.0.0,1 - would that mean that only localhost can access localhost? surely access from all and enable cphulk? – treyBake Mar 5 '18 at 17:06
0

Answer:

1) Have to remove the trailing slash from ServerName and ServerAlias

2) Here, we can remove the ServerAlias as well as ServerName and ServerAlias both are same.

0

Remove double quotes from your path

DocumentRoot /var/www/html/pin
<directory /var/www/html/pin>
0

On this setup, if i try to access http://biz.example.com/ , the correct website (biz website) is loading. But if i try to access http://pin.example.com/ , then also biz website is loading instead of pin website.

That's because both ServerName and ServerAlias directives don't match (wrong syntax) and in this case the first defined VirtualHost gets all requests.

The behaviour is described in the documentation with a very similar config:

Running several name-based web sites on a single IP address (httpd.apache.org/docs/2.4/vhosts/examples.html)

# Ensure that Apache listens on port 80
Listen 80
<VirtualHost *:80>
    DocumentRoot "/www/example1"
    ServerName www.example.com

    # Other directives here
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/www/example2"
    ServerName www.example.org

    # Other directives here
</VirtualHost>

The asterisks match all addresses, so the main server serves no requests. Due to the fact that the virtual host with ServerName www.example.com is first in the configuration file, it has the highest priority and can be seen as the default or primary server. That means that if a request is received that does not match one of the specified ServerName directives, it will be served by this first <VirtualHost>.


Solution:

  1. The ServerName must be without http:// prefix and without trailing slash, i.e.

    ServerName biz.example.com
    

    and

    ServerName pin.example.com
    
  2. ServerAlias can be removed since it has the same value as ServerName

  3. <Directory> and </Directory> should start with a capital letter

  4. The old Apache 2.2 access control syntax should be changed to the new Apache 2.4 Require syntax.

    Order Deny,Allow
    Allow from 127.0.0.1
    

    should be replaced by

    Require local
    

    See


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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