-1

I have two domains sharing same codebase and database. Now I need two domains to connect to two different database respective to domain. I am using docker compose to serve in my local setup. Then i go to my browser and enter localhost:2000 to see my project served. I need two different domain say xyz.com and xyz.net to point to same project. Can anyone help me on this?

1 Answers1

0
  1. you need to resolve that both your domains when accessed point to localhost. In /etc/hosts add:
127.0.0.1 xyz.com
127.0.0.1 xyz.net

Now if you access xyz.com or xyz.com in your browser it will resolve to your local machine.

  1. Configure apache vhosts to point to localhost:2000, something like this:
<VirtualHost *:80>
        ServerName xyz.com
        ProxyPreserveHost On
        ProxyRequests off

        <Location />
                ProxyPass http://localhost:2000/
                ProxyPassReverse http://localhost:2000/
                Order allow,deny
                Allow from all
        </Location>
</VirtualHost>

And second one:

<VirtualHost *:80>
        ServerName xyz.net
        ProxyPreserveHost On
        ProxyRequests off

        <Location />
                ProxyPass http://localhost:2000/
                ProxyPassReverse http://localhost:2000/
                Order allow,deny
                Allow from all
        </Location>
</VirtualHost>

Enable both virtual hosts (sudo a2ensite [name].conf) and reload apache. You will also need proxy_http mod: sudo a2enmod proxy_http. Now both url point to your localhost:2000.

  1. In your PHP app detect domain and based on that connect to whatever db you need. I used this file g.php:
<?php

var_dump($_SERVER['HTTP_HOST']);

and served it with development server in php on localhost:2000 (you will have it on docker):

php -S localhost:2000 g.php 
PHP 7.0.33-0ubuntu0.16.04.15 Development Server started at Thu Jul  9 20:34:03 2020
Listening on http://localhost:2000

When I access xyz.net in browser i get: string(7) "xyz.net" When I access xyz.com in browser i get: string(7) "xyz.com"

blahy
  • 1,294
  • 1
  • 8
  • 9
  • Thank you for replying. I am not very knowledgeable so I am a bit confused in 3rd step. I still have to do "xyz.net:2000" to serve my project. You seem to use just xyz.net to serve could you explain that ? – Aayush Raj Joshi Jul 10 '20 at 15:01
  • @AayushRajJoshi It seems to me you did step 1 and not step 2. If you do step 2 correctly then entering xyz.net in your browser would point to localhost:2000 (so where your docker is exposed). Now you have only step 1. – blahy Jul 10 '20 at 18:15
  • Thank you so much, it worked and helped alot! – Aayush Raj Joshi Jul 13 '20 at 04:47