« Dealing with duplicated code | Making better object oriented design decisions » |
There’s always a lag behind new releases of PHP and releases of packages for operating systems such as Ubuntu. This lag time means that you could be kept from upgrading to the latest and greatest PHP for a year or more, unless you use an outside repository like Dotdeb. Of course, even when using one of these outside sources, you may still find a lag in the security and bugfixes made available to you.
Instead, I roll my own version of PHP. It’s simple and easy to do, and something that any developer can do. Here’s my instructions for doing so on a fresh Ubuntu installation.
There are a ton of great PHP packages out there. Some people use the default packages built into the package repositories; others use Dotdeb. But for true professional PHP developers, understanding how to install and compile PHP against their operating system is somewhat important.
In my case, I want to use the latest versions of PHP, since the tools I’m using often track the most recent releases (PHPUnit, PHPMD, etc.). Therefore, it’s important that the small improvements are available to me.
It’s worth noting that I started with a completely clean install of Ubuntu, that had never had Apache or PHP installed on it before. If you’re starting with a server that’s had either of these installed on it, you will need to modify your instructions.
I’m assuming that you’re running these commands as root; therefore, I do not include the sudo command. Add this command if you are not running as root (standard disclaimer applies about not running as root unless you know what you’re doing).
In order to properly install PHP, we’ll need to prepare a number of dependencies.
I like to have support for Apache, MySQL and Postgres, so we’ll want to make sure and install those things so that PHP can find them:
aptitude install build-essential vim aptitude install apache2 apache2-mpm-prefork apache2-prefork-dev apache2-utils apache2.2-common aptitude install postgresql-9.1 postgresql-client-9.1 postgresql-client-common postgresql-common postgresql-server-dev-9.1 aptitude install mysql-client mysql-client-5.5 mysql-common mysql-server mysql-server-5.5 mysql-server-core-5.5 libmcrypt-dev curl openssl
Note that I’m also installing some additional libraries here: openssl, curl, libmcrypt-dev, and build-essential (which is necessary for make and make install).
There are a number of other dependencies that PHP will rely upon, so it’s important to build those as well. Lucky for us, we can use a single, simple command to build the dependencies that PHP will need:
apt-get build-dep php5
This command will fetch and build all the dependencies that PHP 5 relies upon. Bear in mind that you can skip this step and install the libraries yourself, if you so desire (or if you need a special configuration).
If you don’t run this command or install the required libraries, you’ll get all kinds of unpleasant errors from the PHP configure command.
Once you’ve got the dependencies dealt with, it’s time to go forward and build PHP. Once you’ve downloaded PHP 5.5 (see www.php.net for the latest RC1 build), untar it and change into that directory.
I like a pretty standard set of configuration options with my PHP installations. However, I do include a few unique settings that I will explain here:
./configure --enable-opcache --prefix=/opt/php --with-apxs2=/usr/bin/apxs2 --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pgsql=/usr --with-tidy=/usr --with-curl=/usr/bin --with-openssl-dir=/usr --with-zlib-dir=/usr --with-xpm-dir=/usr --with-pdo-pgsql=/usr --with-pdo-mysql=mysqlnd --with-xsl=/usr --with-ldap --with-xmlrpc --with-iconv-dir=/usr --with-snmp=/usr --enable-exif --enable-calendar --with-bz2=/usr --with-mcrypt=/usr --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --with-freetype-dir=/usr --enable-mbstring --enable-zip --with-pear --with-libdir=/lib/x86_64-linux-gnu --with-config-file-path=/opt
Once you have successfully run the ./configure command, you’ll need to run the make command to compile PHP.
make
After make is finished running, you’ll be prompted to run make test. If you’re compiling an unreleased version of PHP, do the community a favor and run these tests. Report any test failures you encounter. If you come across failures, run make clean and recompile.
After running make test, it’s time to install PHP. Run make install to install PHP to the destination you chose in configuration.
make install
When you install PHP from a package, it usually takes care of installing the required configuration in Apache. However, it has been my experience that I need to insert the correct information into Apache for Apache to recognize PHP files. This is the configuration I use in apache.conf:
<IfModule php5_module> AddType application/x-httpd-php .php AddType application/x-httpd-php .phps <IfModule dir_module> DirectoryIndex index.html index.php </IfModule> </IfModule>
Be sure and restart Apache after this is completed.
PHP doesn’t automatically install an INI file, so you’ll want to do that. Note that there are two in the directory where you unpacked the tarball: php.ini-development, and php.ini-production. Select the correct configuration file for your server and copy it to the path you selected for the PHP INI directory.
You’ll need to add the location of your PHP binary to your path in order to run PHP from the command line; my PHP binary is in /opt/php/bin and this is in my path. Edit the ~/.profile file and add the following line:
export PATH=$PATH:/opt/php/bin
This will add the PHP binary to your path. You may also need to add the path of the PHP binary to your sudoers file. Run visudo as root and edit the secure path to include the correct location for the PHP binary directory.
In order to use the opcode cache in PHP 5.5, you’ll need to add zend_extension=opcache.so to your php.ini file. You’ll also need to modify the opcache.enable=0 line to opcache.enable=1 (this was around line 1865 in my php.ini file).
Brandon Savage is the author of Mastering Object Oriented PHP and Practical Design Patterns in PHP
Posted on 5/13/2013 at 7:00 am
Chuck Burgess (@ashnazg) wrote at 5/15/2013 10:54 am:
If you want a faster compile and don’t mind using up your CPU, “make -jX” will spawn multiple concurrent compile steps, where “X” = “CPU cores + 1”. Of course, you can use any integer there, so using “1 <= X <= CPU cores + 1" can give you a range to choose from. I picked up the "+1" piece back in my Gentoo days, so I don't know exactly why to add it… I just trusted that the Gentoo folks knew what they were doing ;-)
This can also speed up the test step: "make -j5 test"
Tran Minh Quang (@https://twitter.com/tmquang6805) wrote at 7/5/2013 3:05 am:
Hi Brandon Savage,
I follow your post and build my LAMP successful. But, openssl don’t active so I cannot send email by gmail smtp. When I check php info, not openssl extension. Can you help me?
This is my php info
php -m
[PHP Modules]
bz2
calendar
Core
ctype
curl
date
dom
ereg
exif
fileinfo
filter
gd
hash
iconv
json
ldap
libxml
mbstring
mcrypt
mysql
mysqli
mysqlnd
pcre
PDO
pdo_mysql
pdo_pgsql
pdo_sqlite
pgsql
Phar
posix
Reflection
session
SimpleXML
snmp
SPL
sqlite3
standard
tidy
tokenizer
xdebug
xml
xmlreader
xmlrpc
xmlwriter
xsl
Zend OPcache
zip
zlib
Guish (@guish59) wrote at 7/30/2013 8:45 am:
Hello
I have the same problem to use openssl extension
I have this message when i want to install dependancies of symfony 2 with composer :
[RuntimeException] You must enable the openssl extension to download files via https
In phpinfo :
OpenSSL support => disabled (install ext/openssl)did you find a solution at this problem since your last message ?
Thomas wrote at 8/8/2013 10:58 am:
For OpenSSL support you need to install libcurl4-openssl-dev first:
aptitude install libcurl4-openssl-dev
Chris wrote at 8/8/2013 4:03 pm:
Errors, errors, errors. Seriously, best PHP 5.5 setup post so far, but fails on Ubuntu 12.04 LTS (like all other tutorials). What distro have you used ? What have you done before ? What’s in your sources.list ? What kind of “source URIs” do i need in my sources.list ? Why is PHP make always failing with “xml2 dependencies”. Thanks for your effort to write this tutorial, but this does not really work out of the box. :(
Why is it so goddam hard to use linux ? I’m going to bed now and cry a little bit.
« Dealing with duplicated code | Making better object oriented design decisions » |