In the part 1 on compiling your own nginx + php-frm we’ve focus on nginx part of stack. In this part we will be adding the php-fpm daemon. Php-fpm is the way to go if you want to run php inside nginx. So let’s get started.
Compiling Php (with php-fpm)
Download latest stable version of PHP from http://php.net/downloads.php
wget http://ca.php.net/get/php-5.4.32.tar.gz/from/this/mirror -O php-5.4.32.tar.gz
Extract the source and go into the source directory.
tar -xzf php-5.4.32.tar.gz cd php-5.4.32
Next I like to create a create a script called configure.sh which can be reused to recompile with updates later on. Note that you might have to change the location of –with-libdir=lib/x86_64-linux-gnu to something else depending on your version of linux.
#!/bin/bash ######## # Php-fpm Configure Script ######## ./configure \ --enable-fpm \ --enable-pdo \ --with-pdo-mysql \ --enable-sockets \ --enable-exif \ --enable-soap \ --enable-ftp \ --enable-wddx \ --enable-pcntl \ --enable-soap \ --enable-bcmath \ --enable-mbstring \ --enable-dba \ --enable-gd-native-ttf \ --enable-zip \ --enable-calendar \ --with-mysql \ --with-mysqli \ --with-pdo-sqlite \ --with-iconv \ --with-zlib \ --with-bz2 \ --with-gettext \ --with-xmlrpc \ --with-openssl \ --with-mhash \ --with-mcrypt \ --with-xsl \ --with-curl \ --with-pcre-regex \ --with-gd \ --with-freetype-dir=/usr \ --with-jpeg-dir=/usr \ --with-png-dir=/usr \ --with-ldap \ --with-pear \ --with-fpm-user=www-data \ --with-fpm-group=www-data \ --with-config-file-path=/etc/php/ \ --with-config-file-scan-dir=/etc/php/conf.d/ \ --with-libdir=lib/x86_64-linux-gnu
Before compiling we make sure we have the needed dependencies.
sudo apt-get install build-essential autoconf libtool libxml2 libxml2-dev openssl libcurl4-openssl-dev \ libbz2-1.0 libbz2-dev libjpeg-dev libpng12-dev libfreetype6 libfreetype6-dev \ libldap-2.4-2 libldap2-dev libmcrypt4 libmcrypt-dev libmysqlclient-dev \ libxslt1.1 libxslt1-dev libxt-dev
We are now ready to configure and compile.
chmod +x ./configure.sh ./configure.sh make sudo make install
Nginx is now installed you can test by issuing the following command
/usr/local/bin/php -v # Outputs: # PHP 5.4.4 (cli) (built: Jun 29 2012 17:00:35) # Copyright (c) 1997-2012 The PHP Group # Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologiesnginx version: nginx/1.2.1
Lastly we copy the provided init.d scripts and php.ini
sudo mkdir -p /etc/php sudo cp php.ini-production /etc/php/php.ini sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm sudo chmod +x /etc/init.d/php-fpm sudo update-rc.d php-fpm defaults
Before starting php-fpm you need to create the php-fpm configuration
sudo cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
Then uncoment the pid and error_log lines in /usr/local/etc/php-fpm.conf.
We’re now ready to start php-fpm
# start it sudo /etc/init.d/php-fpm start # check that it's running sudo netstat -tlnp |grep php-fpm
That’s it we now have a working php installation.
This is also helpful for compiling PHP 7 if you omit “–with-mysql” as it has been removed.
If you expect you’ll want to use the interactive mode of the command line interpreter you’ll also need “–with-readline”.
I’m tring do anything according to instructions, but any extension does not appear in the directory except opcache.so. What is the problem?
What linux disto / version are you using?
If you just want the latest PHP versions and you have apt package manager I suggest taking a look at https://github.com/oerdnj/deb.sury.org/wiki/PPA-migration-to-ppa:ondrej-php