In this article, I will assume you already have apache and php installed and configured. If not, check out my article on installing apache, php and mysql. Ok, so lets get started shall we?
First things first however. Chances are, you have multiple directories in your web root for each of your projects, such as 'mywebsite1','mywebsite2','some-other-site-stuff', etc. The problem now lays in the fact that Zend recommends the following directory structure:

If you keep your default document root set to say C:/webdev/ for example, you can not simply go to http://localhost/my-zf-project/ and have it work as expected, without using htaccess files to redirect everything to the www (or public directory). Also, it makes it less secure to point directly at your sites root directory. Anybody interacting with your site should only ever be able to access whats in your www (public) directory.
The solution? Virtual hosts. To get this working my self, I tried several various approaches until I found one that actually worked the way I wanted it to. First off, open your apache/conf/httpd file and add the following lines: (note that I added them after the default DocumentRoot variable.
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot "C:\webdev"
</VirtualHost>
What this does is allow localhost to be used to access the webroot. Let's save the changes, restart apache and test our changes. Simply go to http://localhost/ once apache has started back up. If you do not have an index file in your web root, you will probably get a directory tree listing.
Ok so how do I configure virtual host for my zend framework projects? After the first virtual host block, add the following for each of your zf based projects: (note that this actually works for any site in which uses a simular directory structure)
<VirtualHost 127.0.0.1>
ServerName my-zf-site1.local
DocumentRoot "C:\webdev\
</VirtualHost>
Now of course your going to need to modify the document root and servername to reflect your projects path and name. Were not done just yet though, we now have to tell our computer how to resolve the address. We do this by adding the following to our host file which is normally found in C:\windows\system32\drivers\etc. Once you have opened the host file, you should see the following line:
127.0.0.1 localhost
This is how localhost is bound to our local address by default. Underneath of this line, we'll add the name in which we set for the 'ServerName' in our virtual host settings. For example:
127.0.0.1 my-zf-site1.local
This will now bind http://my-zf-site1.local/ to our zf project's public folder. Everything else should work 'out of the box' from here on out now. Now to access your other non zf projects, you can simply do the following:
http://localhost/my-other-stuff/somefile.php
To work with additional zf based projects, simply repeat the steps above for each new project... add a new virtual host block to your httpd file, then modify the host file (example: 127.0.0.1 my-new-zf-site.local). Make sure to always restart apache for the changes to take effect.
Ok, I hope I have been able to help at least someone out there. Please feel free to post any questions or suggestions you have.

1 comment:
Thanks for the tip and the article. I got it to work on the first attempt and it definitely makes life easier.
Post a Comment