Zend Framework 1 tutorial Part 1: Installing and configuring Apache, PHP, MySQL and an IDE

XAMPP: Apache, PHP, MySQL, PHPMyAdmin

This Zend Framework 1 application will be explain how to build a news portal or blogging website using the Zend Framework. But first let's install a development platform and an IDE. If you have already the tools needed you can jump to the next step.

If you prefer to install your development platform on a Mac, I recommend using Mamp (Mac, Apache, MySQL, PHP) or Homebrew (A package manager for OS X that can be used to quickly install MySQL, PHP and Apache).

If you want to install a development platform on Linux, for example on Cent OS 6 you might want to check out my Cent OS 6 Server Setup tutorial.

The development tools

If you have already a development environment with MySQL, Apache and PHP, as well as an IDE you can jump straight to Step2 of this tutorial.

Installing PHP, Apache and MySQL on windows

To get started and if you haven’t already done so, you might need some development tools like a local apache server with PHP, an IDE and a database, for example MySQL. The other tool you will need is an IDE, there are a lot of good opensource IDEs on the web. Personally I prefer and use Netbeans, but you might want to use another one like Eclipse. So first download and Install and IDE.

The first step of setting up a local development platform on windows is to install Apache, MySQL and PHP.

Why XAMPP?

I like to use XAMPP because it is really easy to install and use. XAMPP is a very popular package to get quickly started on windows of the environment of your choice consists of Apache, PHP and MySQL. XAMPP also contains PHPMyAdmin a cool tool to manage MySQL databases as well as some other nice features. PHP, MySQL and Apache are all three opensource, so you won’t have to pay any fees for using any of them.

You can also use another package like wampserver (Windows web development environment package with Apache2, PHP, MySQL and PhpMyAdmin) or Mamp (Mac, Apache, MySQL, PHP) if for example you want to develop on a Mac. If you are on Linux use your default package manager to fetch a copy of Apache, PHP and MySQL and install them.

Download XAMPP

You can download a copy of xampp from the www.apachefriends.org website. On their website, click download to get a copy of the latest XAMPP package. (XAMPP stands for X = cross-platform, Apache, MySQL, PHP and Perl)

Installing XAMPP

If XAMPP has finished downloading, click on the downloaded xampp-XXX.exe to install the package on your computer. When launching the installation script XAMPP might warn you that some antivirus softwares may slow down the installation or even block the Apache server or MySQL database in their firewall, if this is the case for you read the section "How do I configure my antivirus application?" in the XAMPP Windows FAQ. Now follow the steps in the setup wizard. To avoid problems due to UAC, I have installed XAMPP in "C:\xampp" as recommended by XAMPP. I have excluded some components like Tomcat and Perl because I won't need them for my ZF project.

Adding a vhost to Apache

First we will create an Apache virtual host for our application. This is important because later we might have several websites and its easier to manage them if each one has its own development domain. For our first app we will create a domain www.myapp.dev.

Open the XAMPP control panel (launch XAMPP) if this is not already done.

To create a new virtual host, in the Apache row click on "config". Now select <Browse> [Apache] option, then open the "conf" directory and then the "extra" directory. If you prefer you can use the Windows explorer and open the PATH_TO_XAMPP\apache\conf\extra\ directory yourself (for me "PATH_TO_XAMPP" is "C:\xampp" as I have installed XAMPP there, in your case it might be something else depending of what you have chosen during the installation). Now open the httpd-vhosts.conf file using a tool like Notepad++ or similar. This file is the Apache vhosts configuration file. Put the following lines in your vhosts file (at the end):


NameVirtualHost *:80

<VirtualHost *:80>
    ServerName www.myapp.dev
    DocumentRoot PATH_TO_XAMPP\htdocs\myapp\public
    ErrorLog "logs/myapp-error.log"
    CustomLog "logs/myapp-access.log" combined
    SetEnv APPLICATION_ENV "development"
    <Directory PATH_TO_XAMPP\htdocs\myapp\public>
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Change the "PATH_TO_XAMPP\htdocs\myapp\public" path with the real path you want to use, in my case I used "C:\xampp\htdocs\myapp\public".

Remember that you need to (at least) create a directory for your app and a public directory in the app directory. If you followed the directories tutorial everything should be ok, if you didn't open your XAMPP directory and in the "htdocs" directory create a directory called "myapp". Then in the "myapp" directory create a directory called "public".

The DirectoryIndex directive tells Apache which files it should look for, when the client requests a page for the first time. We have told Apache to try to find a file called index.php (I will describe how to create this file in one of the next tutorial parts).

You must have "AllowOverride All" in effect if you want to allow your htaccess to override all the default directives that have been previously declared by Apache. We will use the htaccess to set most of our directives because its a little bit more flexible then setting them in the apache conf. One of the advantages is that you don't have to reload / restart apache after changing a directive in your htaccess file. If you change a directive in the Apache httpd.conf or vhosts configuration file you must reload / restart Apache to activate those new directives.

Order allow,deny means that first, all "allow" directives (whitelist) are evaluated, at least one must match, or the request will get rejected by Apache. Next, all "deny" directives (blacklist) are evaluated. If any matches, the request will be rejected. Finally, any other requests which do not match any Allow or Deny directive will get denied by default.

You could for example use Allow from "your own IP" to only allow yourself to access the website until you finished testing it, or use Deny from "add an IP here that you want to block" to block an IP.

More information about the directory directives can be found in the Apache directory documentation.

Don't forget to save the file as soon as you finished editing it.

Creating a virtual domain for our vhost

Now you must open your windows hosts file. Open your text editor (for example Notepad) by right clicking on the icon and then choose "run as administrator" or you won’t be able to save the file after editing it. The hosts file can be found here: \PATH_TO_WINDOWS\System32\drivers\etc\hosts (for example for me "\PATH_TO_WINDOWS" is "C:\Windows"). If you still have problems editing the hosts file, you can also copy it to the desktop, edit the hosts file and when it's done copy it back to it's folder.

Add these two lines to your hosts file:


127.0.0.1       localhost
127.0.0.1 www.myapp.dev

Then save the file.

Testing the server

To be able to test the server we will have to create a test index.php file. Open the "public" folder of your app (C:\xampp\htdocs\myapp\public) and create a new file called "index.php". Then edit the file and paste the following lines of code into it:


<?php
echo 'Hello World :)';

Then save the file, open the XAMPP control panel and lauch Apache by on the "Start" button in the Apache row. In the "Module" column the word "Apache" should appear with a green background. If this is not the case, this means Apache could't start. To check what happend, click on the "Logs" button and select Apache (error.log) and check the last lines to see what kind of error occurred.

Now you can enter your project URL "http://www.myapp.dev/" into your browser address bar and see if the index file is correctly printing the message "Hello World :)".