In this chapter we will get our development environment set up and configured in order to start development with Zend Framework 2.0. We will set up a PHP Application Server, install MySQL, and create a development database that will be used in subsequent chapters for our Zend Framework learning exercises. So, let's get started.
The last major release of Zend Framework, which happened in 2007, was version 1.0; during the last five years, Zend Framework has undergone a lot of changes to be a successful PHP-based framework. But by merely updating the framework, Zend Framework has retained some of the issues that were inherently present in Zend Framework 1.0.
Zend Framework 2.0 is an attempt to make Zend Framework better by rearchitecting the framework right from the core. Some of the key features of Zend Framework 2.0 over its previous version are listed as follows:
We will get to know about implementing the new features of Zend Framework 2.0 in the coming chapters.
In this chapter we will cover the installation and configuration of some of the prerequisites of Zend Framework 2.0. ZF2 can be installed on most PHP-enabled web servers that support PHP 5.3.3 or later.
We have used Zend Server Community Edition as our default web server; however, any other PHP stack that supports PHP 5.3.3 can be used. Alternatively, you can also download Apache and PHP separately and install PHP over Apache.
Zend Server Community Edition is the free version of the popular Zend Server stack. The Zend Server stack provides a pre-integrated PHP application stack that could be used across development, testing, and production. This enables application development teams to have a consistent environment across all stages of development.
Zend Server CE also provides features such as Zend Optimizer+ for PHP bytecode caching and Zend Guard for encoding files.
Zend Server offers installers for Windows, Mac OS X, and a universal installation package compatible with most Linux distributions.
More details on the installation requirements can be found at http://www.zend.com/en/products/server/system-requirements.
Our next step will be to download and install Zend Server CE; I am running Ubuntu 12.04 Precise Pangolin. The installation procedure for other operating systems could be different; you can always refer to the Zend Server website for installation instructions. The following are the steps to install the Zend Server CE:
Visit the Zend Server Community Edition website (http://www.zend.com/en/community/zend-server-ce) and download the latest version of Zend Server that is applicable to your operating system. In this case, we will be downloading the Linux installer.
Once the installer is downloaded, extract the contents of the installer to a temporary location:
$ tar -zxvf ZendServer-5.6.0-RepositoryInstaller-linux.tar.gz
After extracting, the installer needs to be started with administrator privileges:
$ cd ZendServer-RepositoryInstaller-linux/ $ sudo ./install_zs.sh 5.3 ce
During the installation, the installer will request you to download various packages:
Zend Server will be installed into
/usr/local/zend
by default; the default document root will point to/var/www
. You can use the following files to make configuration changes to the Zend Server instance:Apache master configuration is available in
/etc/apache2/apache2.conf
PHP configuration is controlled by
/var/local/zend/etc/php.ini
The following screenshot shows the installed location of Zend Server:
Once the installation is completed, you should be able to open
http://localhost
on your web browser. This should take you to a test page like the one shown in the following screenshot:
Zend Server CE is installed and ready to be used. Now we have a web server and a compatible version of PHP running—this satisfies the core requirements for running Zend Framework 2.0.
We will be using Git to check out Zend Framework from Github; one of the major changes that happened to Zend Framework 2.0 is that the source control has changed from SVN to Git.
Your next task will be to install Git. We will be making use of Git when we are setting up our Zend Framework project.
Tip
Git binaries can either be downloaded from http://www.git-scm.com/ or installed from your operating system's repositories.
Installation instructions for Git can be found at the following link:
Our next step will be to set up Zend Server CE and make some configuration changes that will enable us to run other PHP applications.
Zend Server CE's Administration Interface is a web-based user interface that provides the following features:
Managing PHP extensions
Configuring PHP directives
Managing Zend Server components
Monitoring PHP status, extension status, and application/server logs
In our next task, we will be making a configuration change to Zend Server by using its Administration Interface.
The Zend Server needs to be configured after the installation is completed. The following are the steps for configuring Zend Server CE:
Open the admin console of Zend Server in your default browser (
http://localhost:10081/
).When opening the Zend Server Administration Interface for the first time, you will be presented with a configuration wizard. Review and accept the terms and conditions of Zend's End User License Agreement page:
As shown in the following screenshot, you will be asked to set the password for the Zend Server installation:
After the initial configuration wizard is completed, you will be redirected to the Zend Server Administration Interface's home page.
We need to set the session save path. In order to do this, perform the following steps:
Navigate to Directives in Server Setup.
Search for
session.save_path
.Set the value to
/tmp
.Click on Save Changes and then Restart PHP.
MySQL doesn't need an introduction—it is the world's most widely used open source database application. It's free and is available on the Internet to individuals and businesses that wish to develop their websites and applications using the MySQL database.
Zend Framework 2.0 has driver support for MySQL along with SQLite, PostgreSQL, and Microsoft SQL Server.
Our next exercise will be to install MySQL on our development machine. MySQL is available for download from all Linux repositories. Windows and Mac users will have to download the installer from the MySQL website (http://dev.mysql.com/downloads/).
MySQL Server and Client need to be installed using the following steps; we will be using MySQL as our primary database in this book:
In a standard Ubuntu installation, MySQL can be installed by executing the following command in the shell prompt:
$ sudo apt-get install mysql-server mysql-client
After the installation is complete, MySQL Server will start automatically. To check if MySQL Server is running, run the following command:
$ sudo netstat -tap | grep mysql
The command should give an output that is similar to the following; this means that the MySQL daemon is running:
tcp 0 0 localhost:mysql *:* LISTEN 923/mysqld
If, for some reason, MySQL Server is not running, you can start the server by running the
restart
command:$ sudo service mysql restart
We have just installed MySQL; we have the LAMP stack ready too. Our next step will be to create a database in MySQL Server.
Having gone through this section, feel free to attempt the task in the following section.
phpMyAdmin is a free, open source web-based database administration tool written in PHP. phpMyAdmin provides a web-based UI to manage MySQL Database Server; add / remove / manage databases, users, privileges; and so on. In this book, we will be using phpMyAdmin as the database Administration Interface for managing our database(s).
Now that we have Apache, PHP, and MySQL installed, our next step will be to create a blank database in MySQL Server.
For doing this, we need to install and configure phpMyAdmin in the Zend Server.
Tip
phpMyAdmin can either be downloaded from http://www.phpmyadmin.net/ or installed from your operating system's repositories.
Installation instructions for phpMyAdmin can be found at the following link:
In our next task we will be creating a MySQL database, creating users in the MySQL server and also grant them access permissions to connect to the database and perform database operations.
To create a new database, open an instance of phpMyAdmin in your web browser and follow the steps described here:
Open phpMyAdmin in your web browser by visiting
http://localhost/phpmyadmin
:Choose Databases, enter the name of the new database as
zf_app
in Create new database, and click on Create:After creating the database, create a database user for this database; this can be done by selecting Add a new user from Privileges. Provide the following details:
After doing this you will get the following screen:
After the user is created, go to the Privileges section and choose Edit Privileges for the
zf_user
.In the Database-specific privileges section, select the
zf_app
database.You will be redirected to the privileges section of the
zf_app
database for thezf_user
user. Choose Check All and click on Go.
You can now test the database by logging out of phpMyAdmin and logging in again with the user credentials of zf_user
. You should now be able to see only the zf_app
database.
We just created our first database in MySQL. We have also created a user in the database and mapped the user to the database with administrative rights; we can now use these credentials in the application that we will be building in our next chapters.
Now that you have the PHP web server up and running and also have a MySQL database, create a simple table called Students
and add a few records to the table using phpMyAdmin.
Your task will be to create a simple PHP web page that will display all the records in the Students
table in the page.
Q1. What is the minimum version of PHP needed to run Zend Framework 2.0?
PHP 4.3 and above
PHP 5.2.0 and above
PHP 5.3.3 and above
PHP 5.4.7 and above
Q2. What is the default location of php.ini
in the new Zend Server installation?
/home/<user>/etc/php/php.inc
/etc/php/php.ini
/var/www/php.ini
/usr/local/zend/etc/php.ini
In this chapter we have learned the setup and configuration of Zend Server's PHP application stack. We went on to install MySQL Server and created our first database. In your exercises, you have learned about the installation of Git and phpMyAdmin.
In the next chapter, we will learn about the structure of a Zend Framework project and core MVC components such as views and controllers.