Installing and setting up Zend Framework on a Virtual Host

0. Full documentation here:

http://framework.zend.com/manual/en/learning.quickstart.create-project.html

1. Log onto your account. You’ll most like be in your home directory. You should know which directory is your public directory (Web Root). Usually this is something like ‘www‘, ‘public_html‘, or ‘html‘, or maybe even ‘your-domain.com‘. Regardless, you don’t need to be there yet. In the follow examples, anytime you see the directory ‘www‘, assume it’s the Web Root and replace it with whatever your Web Root is.

Also, in the below examples, I’ll use ‘/path/to’. For me, this is ‘/home/jason‘ For you, it might be ‘/home/username‘, or ‘/home/website.com‘. Whatever it is, it’s whatever comes before the rest of the paths. I’ll use ‘/path/to‘ below, just keep in mind, this is different for each person.

If your not sure what this is, when you first log in via SSH, run this command

[server]$ pwd

It will print out the directory your currently in. This is most likely the directory path you’ll want to use for ‘/path/to‘.

[server]$ mkdir src
[server]$ mkdir lib
[server]$ cd src

2. Download and ‘install’ the source

[server]$ wget http://framework.zend.com/releases/ZendFramework-1.10.8/ZendFramework-1.10.8.tar.gz
[server]$ tar xfz ZendFramework-1.10.8.tar.gz
[server]$ mv ZendFramework-1.10.8 ../lib
[server]$ cd ../lib
[server]$ ln -s ZendFramework-1.10.8/ Zend
[server]$ alias zf=/path/to/lib/Zend/bin/zf.sh

You can also place the zf alias in your .bash_profile in your home directory by doing the following

[server]$ cd ~
[server]$ vi .bash_profile

You can also use nano, or any other editor you are familiar with, and add this line

alias zf=/path/to/lib/Zend/bin/zf.sh

After all this is done, you’ve essentially installed Zend Framework on your virtual host. However, now you need to set it up for your site.

3. Creating your project

[server]$ cd ~
[server]$ zf create project PROJECTNAME

If you have an existing Web Root, you’ll want to move this or remove it, or change it. Basically, at this point, we want the web root pointing to the correct directory in the Zend Project we just created.

If my Web Root was a directory named ‘www’, I’d do this:

[server]$ mv www www_old

Keep in mind by doing this, I can no longer access my website hosted in that directory. We’ll set this up now.

[server]$ ln -s PROJECTNAME/public/ www

In this case, ‘www’ should be the name of your Web Root.

Now we need to need to make sure the site can find the Zend Framework. Remember, it’s in the lib directory.

[server]$ ln -s /path/to/lib/Zend/library/Zend .

At this point, you should be able to go to your website and check out the default web page. Congrats!

Also, log out, and then log back in. If you entered the alias for your .bash_profile, you should be able to run this:

[server]$ zf show version
Zend Framework Version: 1.10.8

Now you can use the zf script helper functions, and proceed with a working Zend Framework setup.

[server]$ zf help

This will also work.

.htaccess SetEnv Conditionals: mod_setenvif

The mod_setenvif module allows you to set environment variables according to whether different aspects of the request match regular expressions you specify.

via mod_setenvif – Apache HTTP Server.

I’ve never claimed to be a server guru, and I’ve never had much interest in delving into Apache configuration files, but web programming almost request that I dig down and learn about what I’m using in order to make the best use of the technologies I’m using. Besides, I’m well past the point where I can keep making excuses for not learning Apache.

Zend Framework 1.10 uses the .htaccess file to set the enviromental variable APPLICATION_ENV. It does this using SetEnv.

SetEnv APPLICATION_ENV production

This let’s us set the environment to production or development, obviously. The idea is that you can set the variable depending on where the files are: a production server or a development server.

However, I wanted something a bit simpler at the moment. I wanted to the variable based on the URL used. beta.example.com would get the development site, and www.example.com would load up the production site. Both sites share the same directory as their web directory, but I want the live site to display a ‘Coming soon…” message, while the beta site would let me work. I’m not concerned with people getting access to beta, and the only difference will be the display of the default page, but it’s a simple thing. Now, I could have done this in PHP, but I didn’t want to do this. At the same time, I had in mind the ability to use this setting of environmental variables something I could use for other things, like debugging, web services, and what not. Being able to let Apache handle this, and then letting Zend build the configuration based on this setting, would let me not worry about programing any of this logic into my system. I just program away, and I can make the assumption that whatever happens based on the URL will happen appropriately.

Anyways, a quick check of the docs for SetEnv lead me to discover SetEnvIf. I’ve seen this before, but I never paid much attention to this setting. It’s rather simple, and could be very powerful.

As a simple example, I want to set the APPLICATION_ENV variable to production if the website is www.example.com, and development if the site is beta.example.com. This worked:

SetEnvIf Host "www.example.com" APPLICATION_ENV=production
SetEnvIf Host "beta.example.com" APPLICATION_ENV=development

That was the initial code.  Of course, after reading through the manual, I read about SetEnvIfNoCase, which is the same as SetEnvIf, except the “matching is performed in a case-insensitive manner.”

SetEnvIfNoCase Host "www.example.com" APPLICATION_ENV=production
SetEnvIfNoCase Host "beta.example.com" APPLICATION_ENV=development

Pretty simple.