Centos – Configuring CentOS Apache Server for IP Based VirtualHost

apache-2.2centosvirtualhost

I'm running CentOS 5.6 on my server with Apache. I need to create a "staging" site so that client can view the development in progress. I understand that this is done via Apache virtual host. Where is this file located on the server?

Since we have 1 IP address on the test server, and we don't want to change DNS for the domain name just yet, how do I setup the virtual host to resolve the http://my.ip.addy to our test server's /var/www/html/public directory?

Thanks much for your guidance.

Best Answer

It sounds like you are trying to get a domain name to resolve to an IP as well as getting the computer at that IP to respond appropriately. To do that you want to modify your httpd.conf file, which is located at /etc/httpd/conf/ by default. You would then add the following at the bottom:

<VirtualHost *:80> # note the asterisk.
ServerName staging.example.com
DocumentRoot /var/www/html/public
ErrorLog logs/staging.example.com-error_log
</VirtualHost>

For ServerName you put whatever you want as a domain. Changing the *:80 to your.ip.add.ress:80 means that the machine will only respond by the ServerName provided if the request is to that IP (You may want that too, but I generally avoid it).

Then, on the client side, I would modify the hosts file to point that domain to the IP of the testing server. This will let you navigate to the domain and arrive at the testing site. It might be something like this:

127.0.0.1         localhost loopback
::1               localhost
your.ip.add.ress  staging.example.com