This chapter will walk you through installing Phalcon on your chosen platform. The installation process differs a little bit depending on which operating system you happen to be using. Since Phalcon is a PHP extension written in C, installation is a little bit more involved than downloading an archive of PHP files and uploading them to your server.
But installation should still take you less than an hour. Depending on your system and experience with PHP extensions, it could take only minutes. At the end of this chapter, you will have a high performance PHP framework that will simplify the process of developing your application and help you get your project up and running faster.
In this chapter, you will learn how to:
Meet Phalcon's system requirements
Find the correct Phalcon DLL for use on a Windows machine
Install the Phalcon DLL on a Windows machine
Install Phalcon on Linux
Install Phalcon on Mac
Install Phalcon on FreeBSD
php.inito use Phalcon
Configure Apache for Phalcon
You will of course need a web server in order to use Phalcon. Apache is the most common, but if you have an Nginx or Cherokee web server, that will work too. You can even use PHP's built-in web server as your Phalcon development server if you wish. For the purposes of this book, we will assume you have Apache installed.
Along with that, you will need PHP installed. While Phalcon will work with any version of PHP higher than Version 5.3.1, it is recommended that you use PHP 5.3.11 or greater. Versions of PHP before 5.3.11 still have security flaws and memory leaks that were fixed in the later versions.
But that is about all you need. Phalcon is not really very picky. There are a few other requirements you may need to meet on each specific operating system, and we will cover them in the individual installation processes.
In this section, we will walk through installing Phalcon on Windows. Even if you aren't installing the extension on Windows, please read through this section. Many of the steps involved, such as locating your
php.ini, apply to all operating systems.
The Phalcon extension for Windows comes compiled as a DLL. However, you must locate the correct DLL for your version of PHP. The first requirement when installing Phalcon in Windows is a PHP binary compiled with Windows Visual Studio 9.0. It is possible to have a Windows PHP installation compiled with another version of Visual Studio. If you have one of these other versions, you will have to install the correct version of PHP if you want Phalcon to work on your system.
So, let's take a look at your PHP installation to discover just which version of PHP you have installed. First, you need to locate the document root of your web server. Or, in other words, you need to locate the folder on your computer that corresponds to the page you get when you browse to
http://localhost or the folder on your server that maps to the home page of
http://yourdomain.com. We are going to create a PHP file that will tell us everything we need to know to pick the right version of the PhalconPHP DLL and drop it in that folder.
So, open up your text editor or IDE and create a new file. Name it
info.php. In that file, insert the following code:
<?php phpinfo(); ?>
Save the file to your document root. Where your document root is depends upon which web server stack you have installed on your machine. It could be Apache directly from the official site, such as XAMPP, Wamp, or AMPPS, or one of the other versions of Apache, PHP, and MySQL stacks available. Your best bet is to visit the site where you downloaded your chosen version of Apache and read the documentation.
Once you have saved the file, browse to
o.php and you should see a page like the following screenshot:
You will notice the PHP version at the top of the page. We will be paying attention to the first two numbers separated by a dot. They will either be 5.3 as shown in the previous screenshot or 5.4. If they are 5.2 or less, you will need to update your PHP installation.
Next, you want to make sure that your version of PHP was compiled with Microsoft Visual Studio 9.0. So, search for the word Compiler in your browser window. The command to open the search box on most browsers is Ctrl + F. It is usually near the top of the page as shown in the following screenshot. If your PHP was compiled with Visual Studio 9.0, you should see MSVC9 (Visual C++ 2008), as shown in the following screenshot:
Below the Compiler, you will see the Architecture listing. It will say x86 or x64. In the previous screenshot, PHP was compiled for the x86 architecture.
Next, we need to see if you have a thread-safe version of PHP or not. So, search for PHP Extension Build in your browser window. Next to this, you will see the build version of your PHP installation. If you see an NTS at the end of this number, then you do not have a thread-safe version of PHP.
Now that we have these pieces of information, we can go to http://phalconphp.com/en/download/windows.
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. You can also find all the code used in the book by visting GitHub page http://eristoddle.github.io/phalconBlog/.
Here, we will see the page as shown in the following screenshot, which lists multiple versions of the Phalcon PHP extension:
It may look daunting, but we just did the research that will enable us to choose the correct version. We know whether we have a thread-safe version or not. We know whether we have an x86 or x64 architecture. And we know whether we have PHP 5.3 or 5.4. Don't worry about the last number in the PHP version. We just need to download the extension that fits our configuration. The Phalcon extension for PHP 5.3.9 will work for all versions of PHP 5.3 and the extension for PHP 5.4.0 will work for all versions of PHP 5.4.
After downloading the extension, unzip it. Inside the zip file will be a DLL. Next, we need to move this DLL into the folder where your other PHP extensions exist. If you have XAMPP installed, this folder will most likely be
c:\xampp\php\ext, and with Wamp, that would be
php_version_number is replaced with the version number of PHP you have installed. For other versions of web server stacks, you will have to visit the documentation to find the PHP extension folder.
Now we have the extension where it needs to be, but we aren't done yet. We need to have PHP load the extension when it runs. So we have to locate the
php.ini file and edit it. But guess what, we can use our
info.php file to tell us where this file is. So load it again in your browser and search for Loaded Configuration File. Listed beside this entry is its location, as shown in the following screenshot:
Once you locate this file, open it with your text editor or IDE and scroll to the very bottom. This file can be quite long. At the very bottom of this file, you will want to add the following line to tell PHP to load Phalcon:
Save the file. Some server stacks like XAMPP have desktop software to stop and start Apache, while others like Apache from the official site only have a tray icon to control stopping and starting of the server. Or, you can just restart Apache from the command line. Restart Apache now and Phalcon is installed. But let's check to make sure. Load
ost/info.php in your browser again and search for Phalcon and you should be greeted by a page as shown in the following screenshot:
Linux is the most common operating system for a web server. Phalcon does require you to have root access to your web server. If you are on a shared server, you won't get root access, but you may be able to talk to your hosting provider to install Phalcon for you. If you have a VPS, cloud server, or dedicated server, you most likely have root access. If not, you can ask your hosting provider to give you access.
For all other versions of Linux, we will have to compile Phalcon ourselves. This is not hard, and if you have been using Linux for a while, you are probably used to compiling your own software by now. First, we have to be sure we have the following software installed:
PHP development resources
If you don't have these installed, you can install them via the command line.
For Ubuntu, the command line is as follows:
sudo apt-get install git sudo apt-get php5-devphp-mysqlgcc
For Fedora, CentOS, and RHEL, the command line is as follows:
sudo yum install git sudo yum install php-develphp-mysqlnd ccc libtool
For OpenSUSE, the command line is as follows:
yast2 -igit yast2 -iphp5-pear php5-develphp5-mysqlgcc
For Solaris, the command line is as follows:
pkginstall git pkg install gcc-45 php-53 apache-php53
Once you have met all the requirements, open up a terminal window and type the following commands to download the Git repository and compile the Phalcon extension:
git clone git://github.com/phalcon/phalcon.git cd cphalcon/build sudo ./install
You just compiled your first PHP extension. Now we need to add the reference to our extension to the
php.ini file. We can find it by creating the
info.php file as discussed in the Installing Phalcon on Windows section of this chapter, loading it in our browser, and searching the resulting page for Loaded Configuration File. Locate the file listed here in your file-system and add the following line to the very bottom of the file:
Now save the file. If you do happen to be installing Phalcon for PHP 5.4 on a Debian type Linux, the procedure for installing a new PHP extension has changed. It is no longer a best practice to edit the main
php.ini file. In the same folder where you found the
php.ini file, usually with the
/etc/apache5/ path, you will find a
mods-available folder. It is in this folder that you will be creating a custom
.ini file just for Phalcon, and you will call it
phalcon.ini. This file will be empty because we aren't adding any custom configuration settings to Phalcon. Then, to enable the extension, we just run the following in the command line:
sudo service apache2 restart And for Red Hat, CentOS or Fedora, you would type: sudo service httpd restart
To install Phalcon on Mac, the steps are basically the same as installing it on Linux. You must compile the extension from the source code. But first there are some requirements. You need to have Git, php5-devel, and Xcode with the command line tools installed. Xcode is available for free from the official Mac site, but is not installed by default.
Also, Xcode's command line tools are not installed with Xcode by default. In order to make sure to install these tools, open Xcode's Preference panel, choose the Download tab, and click on the Install button located next to the Command Line Tools listing.
To install Git, just point your web browser to http://git-scm.com/download/mac, download the file, and install it. You will also need to install php5-devel if you don't already have it on your system. How you install this depends on the package manager you have installed on your Mac. There are also some prebuilt php5-devel binaries available for the Mac. A search on Google for "install Mac php5-devel" should point you in the right direction.
Now that we have met all the requirements, the steps are the same as installing Phalcon on Linux. First, use Git to pull the source code down to your computer and build the extension by issuing the following commands in the Terminal:
git clone git://github.com/phalcon/phalcon.git cd cphalcon/build sudo ./install
Then, use the steps we went over in the Installing Phalcon on Windows section of this chapter to locate your
php.ini file. Once you have found your
php.ini file, open it and add the following line to the very bottom of your file:
Next, restart Apache and you're set.
pkg_add -r phalcon
export CFLAGS="-O2 -fno-delete-null-pointer-checks"cd/usr/ports/www/phalcon&& make install clean
You will only need these if you plan to use the functionality they provide. For the purposes of this book, you will need to make sure that you have the PDO, PDO/MySQL, Mcrypt, and mbstring extensions installed.
Phalcon is constantly being improved, but every now and then, you may run into issues where the standard installation process may not work or you may run into other bugs. It is beyond the scope of this book to cover everything that could happen while installing or using Phalcon. To find help with issues like this, I suggest three places to look:
In this chapter, we learned how to choose the correct version of the Phalcon PHP extension for our operating system. We also learned how to install the extension on a multitude of systems. We edited our
php.ini file so that Phalcon will now load with our PHP installation and become an integral part of the language.
In the next chapter, we will begin developing our Phalcon blog by putting together the skeleton structure of our site.