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?
Asked
Active
Viewed 375 times
-1
-
Hello. Welcome to SO. Its often best to include your own research in the question and list what you tried and what did not work. – Nikhil Sahu Jul 09 '20 at 17:49
-
Thank you! i will keep that in mind for my further queries. – Aayush Raj Joshi Jul 10 '20 at 14:57
1 Answers
0
- 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.
- 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.
- 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
-