How To Setup a VirtualHost on Apache For Your Development Environment
I recently got tired of my URL’s on my local dev machine (Ubuntu 11.04) looking like “http://localhost/projects/someproject/public/” and decided to setup virtual hosting. Now my projects are configured to be accessed like “http://myapp.dev”. Here is how to do it.
You need to edit your Apache server’s configuration file. Here are some common locations:
- /etc/httpd/httpd.conf (Fedora, RHEL, and others)
- /etc/apache2/httpd.conf (Debian, Ubuntu, and others)
- /usr/local/zend/etc/httpd.conf (Zend Server on *nix machines)
- C:\Program Files\Zend\Apache2\conf (Zend Server on Windows machines)
On Ubuntu 11.04 I ended up having to edit “/etc/apache2/sites-available/allsites” because for some reason my httpd.conf file wasn’t working correctly. Either way, you’re going to want to put the following into your configuration file
NameVirtualHost 127.0.0.1 <VirtualHost *:80> ServerName myapp.dev DocumentRoot /path/to/myapp/public/ SetEnv APPLICATION_ENV "development" <Directory /path/to/myapp/public/> DirectoryIndex index.php AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>
Be sure to change “myapp.dev” to your own application name as well as the DocumentRoot and Directory path to your own path.
Open your “/etc/hosts” file if on *nix or “C:\WINDOWS\system32\drivers\etc” file on Windows. Add the following line right after the similar looking ones:
Restart your Apache server and open a browser. Navigate to “http://myapp.dev” and, if everything went well, you should see your site!