The setup is : Two applications are hosted on same linux server using vhost entries in apache configuration. say :
and
Both of them have a functionality where they do server side http request to a third url (using php cURL / file_get_contents) :
Now there is a host entry of an in the hosts file '/etc/hosts'
xx.xx.xx.xx apis.example.com
This is only intended for greatapp.example.com code, and not for superapp.example.com.
How to configure the server such that greatapps.example.com uses the host entry, but superapp.example.com uses the public ip of apis.example.com
Launch Notepad and open the hosts file located at C:\windows\system32\drivers\etc\hosts (You may not be able to see the windows folder–some files are hidden by default under Windows.)
ReplyDeleteOn Vista, you’ll also need to have access to change the hosts file. To do that, launch Notepad by right clicking on Notepad from the Start menu and choosing "Run As Administrator." This will give you permission to edit and save the file.
At the end of that file type: 127.0.0.1 clientA.local
Save and close the hosts file.
In Notepad open the Apache configuration file located at C:\xampp\apache\conf\extra\httpd-vhosts.conf
At the bottom of that file add.
NameVirtualHost *
<VirtualHost *>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *>
DocumentRoot "C:\Documents and Settings\Me\My Documents\clientA\website"
ServerName clientA.local
<Directory "C:\Documents and Settings\Me\My Documents\clientA\website">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
The first five lines of code turn on the Virtual Host feature on Apache, and set up the C:\xampp\htdocs folder as the default location for http://localhost. That’s important since you need to be able to access the XAMPP web pages at http://localhost/ so that you can use PHPMyAdmin.
Save and close the Apache configuration file, and restart Apache from the XAMPP control panel.
Start a Web browser and type a URL for the virtual host. For example: http://clientA.local/
You should now see the home page for your site.
If you want to add additional Virtual hosts add the proper entry to the hosts file and add another block of text like that in yellow above to the Apache configuration file. For example, say you had another Web site for ClientB. You’d add 127.0.0.1 clientB.local in the hosts file and the C:\xampp\apache\conf\extra\httpd-vhosts.conf would look like this:
NameVirtualHost *
<VirtualHost *>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *>
DocumentRoot "C:\Documents and Settings\Me\My Documents\clientA\website"
ServerName clientA.local
<Directory "C:\Documents and Settings\Me\My Documents\clientA\website">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *>
DocumentRoot "C:\Documents and Settings\Me\My Documents\clientB\website"
ServerName clientB.local
<Directory "C:\Documents and Settings\Me\My Documents\clientB\website">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
It cannot be done directly: host name space is global per host.
ReplyDeleteYou could run one of the apaches in a virtual machine with a different /etc/hosts file.
Here's how I would do it:
ReplyDeleteEach of my apps will have a config file that will be read. That config file can specify what url to use. You can use existing libraries to read config files. In this example I will use Zend_Config_Ini. Here's what your config file can look like for great app:
[settings]
urls.third_party = 'xxx.xxx.xxx.xxx'
This is one is for superapp
[settings]
urls.third_party = 'http://apis.example.com'
2.Read the url from a config file using Zend_Config_Ini for example:
require_once 'Zend/Config/Ini.php';
$config = new Zend_Config_Ini('application.ini', 'settings');
$app_config = $config->toArray();
$third_party_url = $app_config['urls']['third_party'];
I think you're more flexible this way as you can easily change the url in the config file of the app concerned easily.