Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple vhosts on one and the same docker container

I'm trying to run two different domains on one and the same Docker container and port.

The Docker container runs CentOS. docker-compose.yml looks like so:

web:
  image: fab/centos
  ports:
    - "80:80"
  volumes:
    - ./src/httpd.conf:/etc/httpd/conf/httpd.conf
    - ./src:/var/www/html
    - ./src/hosts:/etc/hosts
  environment:
   - VIRTUAL_HOST=dummy.dev,tests.dev

I also declared both .dev domain names inside of /etc/hosts on the host computer (OS X.)

It's been a while since I configured virtual hosts. My understanding was that I just needed to declare them and that Apache would automatically serve the proper files depending on the HTTP HOST being requested.

This is what I have, added at the end of httpd.conf:

<VirtualHost *:80> # first host = default host
    DocumentRoot /var/www/html/default
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot /var/www/html/dummy
    ServerName dummy.dev
    ServerAdmin [email protected]
    ErrorLog logs/dummy.dev-error_log
    CustomLog logs/dummy.dev-access_log common
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot /var/www/html/tests
    ServerName tests.dev
    ServerAdmin [email protected]
    ErrorLog logs/tests.dev-error_log
    CustomLog logs/tests.dev-access_log common
</VirtualHost>

However, in practice, visiting either dummy.dev or tests.dev actually serves /var/www/html/default. This is as if Apache didn't realize which host is being called (though a dump of $_SERVER in PHP does show the expected HTTP_HOST value, i.e.: either 127.0.0.1, dummy.dev or tests.dev depending on which URL I visit.)

What did I miss?

It's unclear to me whether this is an Apache issue or a Docker one.

(Please note this is a different question from how to host multiple apps on the same domain with different port. In my case, I do want the virtual hosts to be all inside/on the same app/port/container.)

like image 441
Fabien Snauwaert Avatar asked May 04 '17 14:05

Fabien Snauwaert


1 Answers

Turns out this was an Apache configuration issue.

I needed to explicitly enable domain-named virtualhosts, like so:

NameVirtualHost *:80

This answer helped.

Docker had nothing to do with the matter.

like image 66
Fabien Snauwaert Avatar answered Sep 21 '22 13:09

Fabien Snauwaert