Installing Zend Framework 2 on OS X Lion

Published on September 13, 2012 by

Zend Framework 2 has just arrived. In this article we will walk through how to make it work with OS X Lion 10.7, assuming that the default Apache installation is used (Web Sharing). If you are using a different installation such as MAMP or AMPPS, simply change the paths accordingly. Please note that there are many ways of accomplishing this, but in this tutorial, we will use the recommended approach by using Composer.

Downloading the Skeleton Application

The first step is to download the Skeleton Application, which is, as the name indicates, a sample application for getting things up and running. You can download the skeleton in a tar.gz or zip file. Extract the contents of this file to a desired folder that is accessible to Apache, e.g. /Users/Andy/Sites/zf2.

Installing the Zend Framework 2 Library

Now this is where people tend to get confused. On the official website, it is recommended to use Composer to install Zend Framework.

The problem that Composer solves is this:

a) You have a project that depends on a number of libraries.

b) Some of those libraries depend on other libraries .

c) You declare the things you depend on

d) Composer finds out which versions of which packages need to be installed, and installs them (meaning it downloads them into your project)

By looking in composer.json within our project folder, we can see that the only dependencies we have are the Zend Framework itself and PHP 5.3.3.

{
    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for ZF2",
    "license": "BSD-3-Clause",
    "keywords": [
        "framework",
        "zf2"
    ],
    "homepage": "http://framework.zend.com/",
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.*"
    }
}

For the actual installation process, we need to use the Terminal. The first thing to do is to go to our project folder.

cd /Users/Andy/Sites/zf2

To resolve the dependencies by using Composer, we will use the file composer.phar which is located in the root of our project folder. Issue the command below to do so.

php composer.phar install

Everything should now install. It may, however, occur that Composer will notify you of errors in your PHP settings, in which case it will tell you exactly what to change in your php.ini configuration file.

Configuring a Virtual Host

If you have not already enabled virtual hosts for your Apache installation, then you need to do so. This is done by removing a comment in a configuration file. First, open the configuration file for editing by entering the below into the Terminal.

open /etc/apache2/httpd.conf

Once opened, simply do a search for Include /private/etc/apache2/extra/httpd-vhosts.conf and remove the comment on the line (the #). Once you have done so, you are ready to move on and define a new virtual host.

We will now make a virtual host that will be used to access our installation. You should be able to access your site already at localhost/zf2/public granted that you have placed your project folder within the “main Apache folder”. Open /private/etc/apache2/extra/httpd-vhosts.conf and add an entry like below.

<VirtualHost *:80>
ServerName zf2.dev
SetEnv APPLICATION_ENV development
DocumentRoot "/Users/Andy/Sites/zf2/public"

<Directory "/Users/Andy/Sites/zf2/public">
	DirectoryIndex index.php
	Options FollowSymLinks Indexes
	AllowOverride All
	Order deny,allow
	allow from All
</Directory>

</VirtualHost>

Note that a Top Level Domain (TLD) other than .local is used above as it significantly speeds up the response time. Be sure to specify the route to your project’s public folder. In the above, we are setting some settings for when zf2.dev is accessed; the application environment is set to development, which makes Zend Framework show stack traces on errors. Furthermore, we are allowing URLs to be rewritten by a .htaccess file by setting AllowOverride to All. We are also setting the DirectoryIndex to index.php, which means that this file will be executed when the index of a directory is requested. Restart your web server for the changes to take effect.

Now we have to make sure that zf2.dev is mapped to 127.0.0.1. Open /etc/hosts in a text editor and add the line below to the file.

127.0.0.1	zf2.dev

Now, trying to access zf2.dev on port 80 (standard http port) will direct our request to index.php in our ZF2 project’s public folder.

Testing That It Works

To test if everything works as intended, open your browser and go to http://zf2.dev where you should see a welcome page like shown below.

Zend Framework 2 Welcome Page

If you encountered any problems when installing Zend Framework 2, please post a comment. If not, happy coding!

Featured

Learn Zend Framework today!

Take an online course and become a ZF2 ninja!

Here is what you will learn:

  • Understand the theory of Zend Framework in details
  • How to implement an enterprise-ready architecture
  • Develop professional applications in Zend Framework
  • Proficiently work with databases in Zend Framework
  • ... and much more!
Zend Framework logo
Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

28 comments on »Installing Zend Framework 2 on OS X Lion«

  1. shain

    which folders or files can i safely deleted after a success setup of the framework?
    because the project folder is a bit messy with composer files and read me files and stuff.

    • Andy

      Hello shain,

      Thank you for your reply.

      There are several folders and files that you can technically delete because your installation does not depend on them. Some of them are, however, recommended to keep. Since your virtual host should point to the public folder, you do not have to worry about these files being executed by anyone.

      If you really want to clean up the folder, here are some options:

      – README.md and LICENSE.txt are not necessary for executing your project. I would leave the latter for license purposes, but it can be removed without interfering with your project

      – composer.json, composer.lock, composer.phar; I would strongly suggest that you do not remove these files as they make it easy to update your project in the future. Especially the composer.lock file will require some effort to recreate. For a description of what each file does, please follow this link: http://getcomposer.org/doc/01-basic-usage.md

      – The data folder is not used initially. It can, however, be a good idea to use it depending on the nature of your application. As the cache subfolder indicates, cached files could be placed here. Initially it is not required, though.

      I hope that helped.

      Best regards,
      Andy

  2. Petchy

    Hi Andy,

    Excellent instruction, that was a big help..

    My question is, since we use now a public/ directory, how or where I could find the Model View Controller Directories?

    Thanks
    Petchy

    • Andy

      Hello Petchy,

      Thank you very much for the feedback. I am happy that it was helpful to you.

      The files for MVC are stored within each module. Each module has a folder within the module folder. The Zend Skeleton Application comes with the Application module, which basically configures the project with some default settings and provides an index page. Within the Application module’s folder, you will find a view folder, which contains all of the view scripts for that module.

      The models and controllers are found in the module’s src folder. In this folder, you will typically see a folder of the same name as the module. This is to ensure that one can have multiple namespaces in a module. Inside this folder, controllers and models are normally stored in the Controller and Model directories, but you can technically use other directory names if you wish to.

      The official getting started tutorial for ZF2 discusses the directory structure a few places. If you have any further questions, do not hesitate to ask.

      Hope it helps.
      // Andy

  3. joe

    Thanks Andy, this was very helpful.
    FYI: When I first tried to access the site I received the error “You don’t have permission to access / on this server.” I fixed this by adding the following to the beginning of the .htaccess file “Options FollowSymLinks”

    • Andy

      Hello joe.

      Thank you very much for your comment. I am happy that this article helped you.

      Did you use the virtual host from this article? I also have such an entry in mine. Either way, thank you for telling others your solution.

      // Andy

  4. Bongo

    In my OS X Mountain Lion installation vhosts was turn off in /etc/apache2/httpd.conf. Turned it on and everything worked perfect.

    • Andy

      Hello Bongo.

      Thank you for your comment. I believe I turned vhosts on myself a long time ago and forgot to write this in the article. I will update the article with this information. Thank you!

      • Bongo

        My localhost also points to zf2.dev when I turned on vhost. Any idea?

  5. Gregory

    Thanks for your tutorial. However, I kept receiving the error “You don’t have permission to access /somePath on this server.” when I access http://localhost/~myUserName/zf2/public/ or http://zf2.dev . But all my other php project in “~/Sites” works fine.

    Also, if I access http://localhost/~myUserName/zf2/ , like a file explorer, I can see all the files and directory in “~/Sites/zf2” except “public” directory.

    I’ve google it and tried “sudo chmod -R 777 ~/Sites” or add “Options Indexes FollowSymLinks” into in httpd.conf.

    Do you know why this happen? Maybe this is a dump question, any hints or manual links is fine : )

    • Hello Gregory,

      Thank you for your comment.

      Did you try to set the permissions of the parent directory of your project to 755? Unfortunately it has been a while since I worked with these kind of things, so I will probably be unable to assist you. I apologize for that and wish you the best of luck and hope that you resolve your problem soon.

      Best regards,
      Bo Andersen

      • Gregory

        Hi Andersen,

        Thanks for your help. And I fix it by edited my user directory configure file (/etc/apache2/users/myUserName.conf).

        What I did is add FollowSymLinks option, after edit the myUserName.conf look likes this:

        
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
        
        

        Maybe this is a difference bewteen OS X 10.7 and OS X 10.8 (my current OS).

        Again, this post is really great. Much better than the official one. Thx : )

        Gregory Sek

        • Hello Gregory,

          I am happy that you resolved the problem and thank you very much for posting the solution. Hopefully it will help others who face the same problem.

  6. nachiketa kashyap

    thank you, been trying to modify the structure of directories for while, this helped!

    • My pleasure – always happy to help! :-)

  7. lukasz

    Hello Bo Andersen
    Thank for a nice guide, but I am not able to make installation on my mac (

    I created the path /users/myuser/sites/zf2
    and when I type in terminal:

    cd /Users/myuser/Sites/zf2
    php composer.phar install

    I am getting error :

    Could not open input file: composer.phar

    I think I put the skieleton of Framework wrong…..
    I am using XAMPP – do I need to create there? If yes which location…

    Thanks for help
    lukasz

    • Hello Lukasz,

      The installation process has changed a while ago. Please try the following:

      1. mkdir myproject
      2. cd myproject
      3. git clone git://github.com/zendframework/ZendSkeletonApplication.git
      4. cd ZendSkeletonApplication
      5. git checkout tags/release-2.3.3
      6. php composer.phar self-update
      7. php composer.phar install

      Be sure to replace the tag with whichever release you want to use.

      Best regards,
      Bo Andersen

      • lukasz

        Hello Bo Andersen
        Thank you for the quick reply.
        The installation done.

        Writing lock file
        Generating autoload files

        so now i should continue from step:
        Configuring a Virtual Host )

        Thanks a lot

        • lukasz

          Hello again )
          The installation is done:)
          and i did installation on the location:
          /Applications/XAMPP/xamppfiles/users/lukasz/sites/zf2

          Also i removed # from include /private/etc/apache2/extra/httpd-vhosts.conf

          and when i want test localhost/zf2/public – is showing me PAGE 404 ….

          Thanks for help)

          • Glad you got it to work. You need to use the site name within your virtual host in your browser, e.g. mysite.dev. You also have to route that to 127.0.0.1 within your /etc/hosts file.

  8. Lukasz

    Thank for reply…. i did but the result is page404 … like on my last comment…. what is wrong……
    Thank for reply
    Lukasz

    • Ah okay. Your virtual host should point directly to the public directory. This means that you should not specify that within the URL in the browser. So if your virtual host (example.com) points to /path/to/zf2/public, then you just have to enter example.com in your browser. You mentioned trying to access the page at localhost/zf2/public. That’s not going to work, because you need to use the server name that matches the one defined within the virtual host.

      I hope that helps.

      • lukasz

        Thank for message)
        I opened the file (/private/etc/extra/httpd-vhosts.conf) and did edit to:

        ServerAdmin [email protected]
            DocumentRoot "/Applications/XAMPP/xamppfiles/users/lukasz/sites/zf2/public"
            ServerName dummy-host.example.com
            ServerAlias www.dummy-host.example.com
            ErrorLog "logs/dummy-host.example.com-error_log"
            CustomLog "logs/dummy-host.example.com-access_log" common
        
        
        
            ServerAdmin [email protected]
            DocumentRoot "/Applications/XAMPP/xamppfiles/users/lukasz/sites/zf2/public"
            ServerName dummy-host2.example.com
            ErrorLog "logs/dummy-host2.example.com-error_log"
            CustomLog "logs/dummy-host2.example.com-access_log" common

        but is not working……..

  9. lukasz

    Hello again)
    Hm I dont know, but is not working… sorry(
    My file httpd-vhosts.conf and is located : /private/etc/extra/httpd-vhosts.conf

    ServerName zf2.dev
    SetEnv APPLICATION_ENV development
    DocumentRoot "/Applications/XAMPP/xamppfiles/users/lukasz/sites/zf2/public"
    
    
        DirectoryIndex index.php
        Options FollowSymLinks Indexes
        AllowOverride All
        Order deny,allow
        allow from All
    

    thank for help again
    lukasz

    • Hello again,

      If you are editing the virtual host file located at /private/etc/extra/httpd-vhosts.conf, then I think you are adding your virtual hosts in the wrong place. I am not familiar with XAMPP on Mac, but I have used MAMP before. MAMP keeps its configuration files within the /Application/MAMP directory, so I imagine that XAMPP does the same thing. This means that the file that you are changing, is for the Apache web server that is built into macOS and not the XAMPP one. You should try to find the corresponding files within the XAMPP application directory.

      Also remember to restart Apache after each change.

      • lukasz

        Thank u for message
        So it means that I should to change the /private/etc/extra/httpd-vhosts.conf to :

        ServerName zf2.dev
        SetEnv APPLICATION_ENV development
        DocumentRoot "/Applications/XAMPP"
        
        
            DirectoryIndex index.php
            Options FollowSymLinks Indexes
            AllowOverride All
            Order deny,allow
            allow from All

        but my project folders is in :
        /Applications/XAMPP/xamppfiles/users/lukasz/sites/zf2/public

        so I dont understand… I was doing similary like you wrote on the instruction above:

        ServerName zf2.dev
        SetEnv APPLICATION_ENV development
        DocumentRoot "/Users/Andy/Sites/zf2/public"
        
        
        	DirectoryIndex index.php
        	Options FollowSymLinks Indexes
        	AllowOverride All
        	Order deny,allow
        	allow from All

        so it means that your project folder is in :
        /Users/Andy/Sites/zf2/public

        I also created the same path like you : /Users/Andy/Sites/zf2/public
        and in virtual host put the same like you but is not working….
        sorry for me that I am boring )
        I am able to buy some course from you but i want to start this my zend framework on my mac )

        Thanks
        lukasz

        • No, your document root should be /Applications/XAMPP/xamppfiles/users/lukasz/sites/zf2/public since that’s where your project files are located.

          You should not edit the file at /private/etc/extra/httpd-vhosts.conf, because that’s for the Apache web server built straight into macOS, and not your XAMPP web server. So you need to find the virtual host config for XAMPP within /Applications/XAMPP/config/apache2 or whatever it is for XAMPP. Use your Finder and you should be able to find it somewhere. Edit those config files as per this article and you should be good to go.

          • lukasz

            Hello mate
            Sorry for long reply…
            I fount one file .conf – I think that is this one:
            /Applications/XAMPP/xamppfiles/apache2/httpd.conf
            —————-
            Alias /bitnami/ “/Applications/XAMPP/xamppfiles/apache2/htdocs/”
            Alias /bitnami “/Applications/XAMPP/xamppfiles/apache2/htdocs”

            Options Indexes FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all

            ————-

            so how should I replace this one?
            Thanks a lot
            Lukasz

Leave a Reply

Your e-mail address will not be published.