How can I view a website on my server without assigning a domain name to it?

I have a server with vhosts conf that looks like this:

NameVirtualHost *:80

# My Virtual Hosts:
<VirtualHost *:80>
     ServerAdmin [email protected]
     ServerName domain

     DirectoryIndex index.php index.html
     DocumentRoot /var/www/domain/public

     # Custom log file locations
     LogLevel warn

     ErrorLog /var/www/domain/log/error_log
     CustomLog /var/www/domain/log/access_log combined

     <Directory "/var/www/dailysongfix/public">
         Options Indexes FollowSymLinks
         AllowOverride All
         Order allow,deny
         Allow from all
     </Directory>
</VirtualHost>

A typical vhost conf right? Well I also need to develop a few other sites on the server and these sites don't have domains hooked to them yet. How can I view them without a domain pointing at them? I am guessing maybe the IP address and a port, but not sure how to set that up.


Solution 1:

If you're going to use named virtual hosts, you need to assign some name to the server. In your example, the host will be addressed by the name domain (from the ServerName domain directive). You can try to contact your server by IP address directly by doing something like http://123.45.67.89/ but that won't cause Apache to trigger the named virtual host. To do this you will need to add something like this to your /etc/hosts file on the client:

123.45.67.89    domain

Then, you should be able to use http://domain/ in your browser. This sets up the mapping between name and IP address locally without having to mess with DNS.

The way this works is when the browser sends the HTTP request to the server, it includes whatever text it used after the // in the URL in a Host: header. Apache takes this text in the Host: header and matches it against ServerName values in each named virtual host. When it finds a match, it uses that as the server to process the rest of the request. Apache does not need to look up the mapping between the host name and IP address, which is why this works if you only change the client machine configuration.

Solution 2:

Use "Alias" instead of VirtualHost, it should work out fine if your PHP applications don't require a fixed URL.

Edit: You can add the aliases to the config you pasted above, just add them before the container.