This will walk through the process of adding sub domains to your local server ( onesite.localhost , twosite.localhost, etc ). To understand this need you are liekly working on multiple sites at once on your local development machine. So lets say you have an excellent CakePHP application, a gnarly JS sandbox, and your personal start page all belonging to different directories. Problem is, there is only one localhost. But just like your live server can have subdomains, you can skip DNS and use virtualhosts in Apache do the work. Imagine, unlimited sub domains all live at one on your localhost pointing to folders all across your machine. First off I will be be clear that this is instructions for a Linux device. If your not running Linux, then start today. You’ll be happy you made the switch to the ubiquitous LAMP architecture.
Most of you have already done this step long ago, but just to be certain, you have set your documentroot and settings in
DocumentRoot “/srv/www/htdocs”
Options All # AllowOverride controls what directives may be placed in .htaccess files. # It can be “All”, “None”, or any combination of the keywords: # Options FileInfo AuthConfig Limit AllowOverride All # Controls who can get stuff from this server. Order allow,deny Allow from all
Options Indexes MultiViews AllowOverride All Order allow,deny Allow from all
Options All AllowOverride All Order allow,deny Allow from all
In order for this to work we’ll need to be specific about which sub domain points where, easy enough. You’ll notice I am not using httpd.conf, but rather a configuration file in a sub direcotry that is referenced in the main configuration file. This is the typical setup, and any *.conf file in most of the *.d directoriess should be read. If the folder vhosts.d does not exist, add this code directly to the end of httpd.conf.
NameVirtualHost localhost:80
DocumentRoot “/srv/www/phpMyAdmin/” #Same for the Client Site DocumentRoot “/home/eddie/workspace/Digital_Business/app/webroot/”
You may add as many as you want ( to a limit I Imagine) by adding more of the < through> blocks. The very first line of the code should only be used once. The names you use here are the host names we’ll need below, so keep note.
For this part you need to edit your you can either edit /etc/hosts directly, or for those who are unsure, use the systems administration panel > network (services) > host(name)s. I’m running suse so my system panel is Yast, for you it may differ. [caption id=“attachment_154” align=“aligncenter” width=“300” caption=“Begin by launching the Adminstration Panel”][/caption] [caption id=“attachment_155” align=“aligncenter” width=“300” caption=“Run the Host Configuration module”][/caption] Once your inside the Host configuration module (or hosts file) just add a new record for every sub domain. In my example I use ::1 as the IP address only because IPv6 is enabled on my server. You may need to use 127.0.0.1. [caption id=“attachment_156” align=“aligncenter” width=“300” caption=“Enter each sub domain as a new record”][/caption] If you open that image up you’ll see I have already added ‘digbiz.localhost’ and was in the process of adding ‘phpmyadmin.localhosts.’ Remember, these are the virtual hosts we setup just before.
Once your done adding the sub-domains clcik finish and the settings will be saved. You can now restart apache and test it out.
Note: I was curious if you could set up completely new domains, mylocalhost. I didn’t have much luck though. If anyone has a reason, or has in fact succeeded I would love to hear about it.