May 15th, 2012
Team Development Tip: VirtualHost
At Big Spaceship it’s always bothered me how we all have different configurations for running things on our local machines. For example, I had adopted the convention of using ‘skoch.local’ for all of my projects. When I’d start a new project, I’d set up an alias in my httpd.conf file something along the following:
Alias /project-name "/Users/skoch/Documents/Development/WORK/2012/CLIENT/PROJECT/dev/bin" Options Indexes MultiViews AllowOverride All Order allow,deny Allow from all
… which would then allow me to use ‘http://skoch.local/project-name’ as the testing URL on my machine. Handy, indeed, however ultimately you end up with a lot of aliases and it’s not as convenient as using ‘project-name.local’ which allows for the team to agree on a ‘local’ url to use in the code and thus, easier to set up for everyone involved in the project. I prefer the ‘local’ concept but if you don’t set things up properly, you will lose the alias to ‘skoch.local’ for anything else.
Furthermore, if I was asked to help out on a particular project, I had to jump through rings of fire (it seemed) to get my machine set up to use another-project.local for development. I felt like I was wasting time trying to figure out what I should change in my httpd.conf file and/or my hosts file to match what the other developer has set up as the testing URL.
Well, I’m here to tell you how we can all set up things for faster development for a team of developers AND maintain your respective local (skoch.local) aliases in THREE EASY STEPS.
A few things to note before we begin: Our network is almost entirely mac and all of our developers are on macs. As such, most are using a MAMP configuration. If you have a different configuration, please edit the files corresponding to your configuration. If you are not on a mac friendly network, using *.local URL’s could be made to work for you, but that would require editing the DNS on your internal network.
- Create a file (if one doesn’t already exist) in /Applications/MAMP/conf/apache named vhosts.conf
- Paste the following (editing ‘DocumentRoot’, ‘ServerName’, and ‘Directory’ appropriately):
DocumentRoot /Users/skoch/Sites ServerName skoch.local Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all
This will allow everyone to find your root should you want to share something with someone on your network. (eg: http://skoch.local/js/tests/spritesheets/)
- Open /private/etc/hosts and add the the server name you used in the above step to your local IP (you need admin privileges)
- Open up your httpd.conf file and add the following to the very end of the file (changing the path to where you saved the vhosts.conf file, if necessary):
NameVirtualHost *:80 Include /Applications/MAMP/conf/apache/vhosts.conf
- Restart Apache using MAMP control panel or use the command:
At this point you should be able to use your standard alias (skoch.local) for all your usual needs using any aliases you already have. To add a specific VirtualHost, just follow the two steps above but once again change ‘DocumentRoot’, ‘ServerName’, and ‘Directory’ appropriately. For example:
ServerName foobaz.local DocumentRoot /Users/skoch/Documents/Development/WORK/2012/FOO/BAZ/site/dev/bin Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all
… and then in the hosts file add:
Lastly, if you want to allow other people on the network to access a particular project, you can edit the settings in System Preferences -> Sharing and then they will be able to access the project from their machine. Handy for design and functionality reviews!
Setting up a project for everyone to view as well as having other developers jump in can be a pain. Using this method, you will be able to use the same URL on your local machine as everyone else on the network and it’s painless to set up. Open two files, edit to add the ServerName your team has agreed on, restart Apache, done.