Installing PHP

Here we will learn how to Compile and Install PHP from source code. After that we will Configure PHP for basic functionality and Secure it for general usage.

Installing PHP from source code involves the following steps.
1) Installing missing dependencies

2) Downloading the source code
3) Unpacking & Installing
4) Post Installation Procedures
5) Installation Review

After installing PHP, it is recommended to view the following sections.
Securing PHP
Customizing PHP

 

 

1) Installing missing dependencies
Pspell support for PHP requires installation of Aspell and Aspell Development packages.
Installation instructions here.
Otherwise you can remove --with-pspell from the list of configure options.

IMAP support for PHP requires installation of C-client and C-client Development packages.
Installation instructions here.
Otherwise you can remove --with-imap --with-imap-ssl from the list of configure options.

Enchant support for PHP requires installation of Enchant Development package.
Installation instructions here.
Otherwise you can remove --with-enchant from the list of configure options.

GDBM and NDBM support for PHP requires the procedure explained here.
Otherwise you can remove --with-gdbm and --with-ndbm from the list of configure options.

ICU support for PHP requires installation of ICU development package.
Installation instructions here.
Otherwise you can remove --enable-intl from the list of configure options.

Mcrypt support for PHP requires installation of libmcrypt library.
Installation instructions here.
Otherwise you can remove --with-mcrypt from the list of configure options.

T1lib support for PHP requires installation of T1lib Development package.
Installation instructions here.
Otherwise you can remove --with-t1lib from the list of configure options.

Tidy support for PHP requires installation of Tidy Development package.
Installation instructions here.
Otherwise you can remove --with-tidy from the list of configure options.

Tokyo Cabinet support for PHP requires installation of Tokyo Cabinet Development package.
Installation instructions here.
Otherwise you can remove --with-tcadb from the list of configure options.

libvpx support for PHP requires installation of libvpx Development package.
Installation instructions here.
Otherwise you can remove --with-vpx-dir=/usr from the list of configure options.

 

 

2) Downloading the source code
PHP is available from the official PHP website.
Let us goto http://php.net. On the top of page, click on the Downloads tab, which will take us to the download page listing different downloads. Look for the section referring to PHP 5.5.14. Under this section, click on the gzipped tarball php-5.5.14.tar.gz. This will take us to the country specific download page. Clicking on the link of a mirror site must start our download.
The downloaded file php-5.5.14.tar.gz will be 16.4 MB in size.

 

 

3) Unpacking & Installing
Unpacking
Make sure you have copied the downloaded file php-5.5.14.tar.gz to /usr/src/ directory. In the terminal, change to /usr/src/ directory.

# cd /usr/src


Extract the gzipped tarball containing PHP.

# tar -zxvf php-5.5.14.tar.gz
OPTIONS EXPLAINED

-x
extract files from an archive

-v
verbosely list files processed

-z
filter the archive through gzip

-f
use archive file or device ARCHIVE


Installing
NOTE: Installation is performed based on the rules iRULE1 and iRULE3.
Before proceeding with the installation, it is strongly recommended to read the following. So that, you may know what you are doing and its implications, rather than to understand later it is too late.
Disabling unnecessary extensions.

Now we will have a directory php-5.5.14 with the extracted files. Switch to the directory in terminal.

# cd php-5.5.14


Execute the configure script as shown below.
NOTE:
Read about disabling unnecessary extensions.
View the complete list of configure options for PHP.

# ./configure --sysconfdir=/etc --enable-opcache --enable-fpm --with-apxs2 --with-mysqli --with-pdo-mysql --with-pdo-odbc=unixODBC,/usr --with-unixODBC=/usr --enable-dba --with-gdbm --with-ndbm --with-db4 --with-dbm --with-tcadb --enable-ftp --with-snmp --with-openssl-dir=/usr --with-ldap --with-ldap-sasl --with-imap --with-imap-ssl --with-openssl --with-kerberos --with-mhash --with-mcrypt --enable-bcmath --with-gmp --enable-shmop --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-soap --enable-wddx --with-xsl --with-xmlrpc --with-readline --enable-calendar --with-tidy --with-curl --with-enchant --with-pspell --with-gettext --enable-intl --enable-mbstring --with-zlib --with-bz2 --enable-zip --enable-exif --with-gd --with-vpx-dir=/usr --with-jpeg-dir=/usr --with-png-dir=/usr --with-zlib-dir=/usr --with-xpm-dir=/usr --with-freetype-dir=/usr --with-t1lib --enable-gd-native-ttf --enable-gd-jis-conv
OPTIONS EXPLAINED


--sysconfdir=DIR
read-only single-machine data [PREFIX/etc]


--enable-opcache
Enable Zend OPcache support


--enable-fpm
Enable building of the fpm SAPI executable


--with-apxs2=FILE
Build shared Apache 2.0 Handler module. FILE is the optional pathname to the Apache apxs tool apxs


--with-mysqli=FILE
Include MySQLi support. FILE is the path to mysql_config. If no value or mysqlnd is passed as FILE, the MySQL native driver will be used


--with-pdo-mysql=DIR
PDO: MySQL support. DIR is the MySQL base directory. If no value or mysqlnd is passed as DIR, the MySQL native driver will be used


--with-pdo-odbc=flavour,dir
PDO: Support for 'flavour' ODBC driver. include and lib dirs are looked for under 'dir'. 'flavour' can be one of: ibm-db2, iODBC, unixODBC, generic. If ',dir' part is omitted, default for the flavour you have selected will be used. e.g.: --with-pdo-odbc=unixODBC will check for unixODBC under /usr/local. You may attempt to use an otherwise unsupported driver using the \"generic\" flavour. The syntax for generic ODBC support is: --with-pdo-odbc=generic,dir,libname,ldflags,cflags. When built as 'shared' the extension filename is always pdo_odbc.so


--with-unixODBC=DIR
Include unixODBC support /usr/local


--enable-dba
Build DBA with bundled modules. To build shared DBA extension use --enable-dba=shared

--with-gdbm=DIR
DBA: GDBM support

--with-ndbm=DIR
DBA: NDBM support

--with-db4=DIR
DBA: Oracle Berkeley DB 4.x or 5.x support

--with-dbm=DIR
DBA: DBM support

--with-tcadb=DIR
DBA: Tokyo Cabinet abstract DB support


--enable-ftp
Enable FTP support


--with-snmp=DIR
Include SNMP support

--with-openssl-dir=DIR
SNMP: openssl install prefix


--with-ldap=DIR
Include LDAP support

--with-ldap-sasl=DIR
LDAP: Include Cyrus SASL support


--with-imap=DIR
Include IMAP support. DIR is the c-client install prefix

--with-imap-ssl=DIR
IMAP: Include SSL support. DIR is the OpenSSL install prefix


--with-openssl=DIR
Include OpenSSL support (requires OpenSSL >= 0.9.6)

--with-kerberos=DIR
OPENSSL: Include Kerberos support


--with-mhash=DIR
Include mhash support


--with-mcrypt=DIR
Include mcrypt support


--enable-bcmath
Enable bc style precision math functions


--with-gmp=DIR
Include GNU MP support


--enable-shmop
Enable shmop support


--enable-sockets
Enable sockets support


--enable-sysvmsg
Enable sysvmsg support


--enable-sysvsem
Enable System V semaphore support


--enable-sysvshm
Enable the System V shared memory support


--enable-soap
Enable SOAP support


--enable-wddx
Enable WDDX support


--with-xsl=DIR
Include XSL support. DIR is the libxslt base install directory (libxslt >= 1.1.0 required)


--with-xmlrpc=DIR
Include XMLRPC-EPI support


--with-readline=DIR
Include readline support (CLI/CGI only)


--enable-calendar
Enable support for calendar conversion


--with-tidy=DIR
Include TIDY support


--with-curl=DIR
Include cURL support


--with-enchant=DIR
Include enchant support. GNU Aspell version 1.1.3 or higher required.


--with-pspell=DIR
Include PSPELL support. GNU Aspell version 0.50.0 or higher required


--with-gettext=DIR
Include GNU gettext support


--enable-intl
Enable internationalization support


--enable-mbstring
Enable multibyte string support


--with-zlib=DIR
Include ZLIB support (requires zlib >= 1.0.9)


--with-bz2=DIR
Include BZip2 support


--enable-zip
Include Zip read/write support


--enable-exif
Enable EXIF (metadata from images) support


--with-gd=DIR
Include GD support. DIR is the GD library base install directory BUNDLED

--with-vpx-dir=DIR
GD: Set the path to libvpx install prefix

--with-jpeg-dir=DIR
GD: Set the path to libjpeg install prefix

--with-png-dir=DIR
GD: Set the path to libpng install prefix

--with-zlib-dir=DIR
GD: Set the path to libz install prefix

--with-xpm-dir=DIR
GD: Set the path to libXpm install prefix

--with-freetype-dir=DIR
GD: Set the path to FreeType 2 install prefix

--with-t1lib=DIR
GD: Include T1lib support. T1lib version >= 5.0.0 required

--enable-gd-native-ttf
GD: Enable TrueType string function

--enable-gd-jis-conv
GD: Enable JIS-mapped Japanese font support

configure will check our system for required dependencies, assigns values for system-dependent variables and use these values to generate the Makefile.


Execute the make command.

# make

make will look at our Makefile, compile our program code and create the executables in the sequence described.


Execute make install.

# make install
OPTIONS EXPLAINED

install
install will look for the target install in Makefile, and install MySQL Server to the specified location.

 

 

4) Post Installation Procedures
Adding PHP libraries to System library path
The PHP installation has an Opcache shared library located at /usr/local/lib/php/extensions/no-debug-zts-20121212. For other applications to find this library at run-time, the shared library cache in system must be updated with this information.

Run ldconfig command.
NOTE: Before running this command, make sure you have added the directory /usr/local/ to the System library path as instructed here.

# ldconfig

ldconfig updates the links and cache to the most recently shared libraries.; so that dynamic-linker can find them during runtime.


Adding PHP manpages to MANPATH

The PHP manpages are located at /usr/local/php/man/ directory. We must add it to the system MANPATH.
Open the file /etc/man.config. Look for the section where system manpages are defined. This starts at the line MANPATH /usr/man. To the end of this section, add the following content.

MANPATH /usr/local/php/man

Save and close the file. Now, we will be able to access the PHP manpages using man command.


Setting the configuration file

PHP comes bundled with two configuration files.
php.ini-development – It is recommending only for development environments. It is much more verbose when it comes to errors which are shown to application users leading inadvertently secure information.
php.ini-production – It is recommended for production and testing environments. It contains settings which hold security, performance and best practices at its core. But these may break compatibility with older or less security conscience applications.

Let us copy the PHP configuration file intended for production environment, to /usr/local/lib/ directory.

# cp -v php.ini-production /usr/local/lib/php.ini
OPTIONS EXPLAINED

-v
explain what is being done


Create a symbolic link for /usr/local/lib/php.ini to the /etc/ directory.

# ln -s /usr/local/lib/php.ini /etc/php.ini
OPTIONS EXPLAINED

-s
make symbolic links instead of hard links



Setting Date & Time

If we do not set Timezone for PHP in the configuration file, PHP will rely on the operating system’s timezone setting. However it is good for PHP to have it’s own timezone, in case the operating system’s timezone ever goes wrong.

Open the configuration file php.ini.

Let us set the timezone. We must set this to the value where our server resides. A list of possible values are given in PHP documentation at http://php.net/manual/en/timezones.php. This list is based upon the latest version of timezone database available from PECL at http://pecl.php.net/get/timezonedb.
Look for the line containing date.timezone. My server is situated in India; so I set the value to Asia/Kolkata. Also I have uncommented the line. Now mine looks like below.
NOTE: Replace Asia/Kolkata with the timezone of the country where your server resides.

date.timezone = Asia/Kolkata


Now let us set the Location. Setting this allows servers on different locations to work co-ordinately. This also allows us to determine when the sun will rise and set for a particular user and take action accordingly.
Look for the lines containing date.default_latitude and date.default_longitude. These values refer respectively to the Latitude and Longtitude which together describes the location of the server.

NOTE:
If you do not know the location of your Server
• Check with your Hosting provider.
If your server is a Desktop machine
• Ask your friends.
• Use Google. Example Keyword: Dallas Coordinates
• North is +ve and South is -ve with respect to Equator.
East is +ve and West is -ve with respect to Greenwich Meridian.
So the coordinates 08°55′45″N and 76°55′05″E means -08°55′45″S and -76°55′05″W.

• The coordinates we obtain from Google are already in degrees. Means there will be no minutes or seconds. So we can use them just as they are. However if we obtain a value in degree, minute and second from another source; we must convert it completely into degrees. I have given an example below.
EXAMPLE:
I have got the coordinates (08°55′45″N, 76°55′05″E).
08°55′45″N means 8 Degrees, 55 Minutes and 45 Seconds North.
76°55′05″E means 76 Degrees, 55 Minutes and 05 Seconds East.
Now let us convert them into degrees.

08°55′45″N = [08 + (55/60) + (45/3600)]° N = 8.929167° N
76°55′05″E = [76 + (55/60) + (05/3600)]° E = 76.918056° E
So (08°55′45″N, 76°55′05″E) equals to (8.929167° N, 76.918056° E).

My server is situated at the place Anchal. So I have set the coordinates of Anchal, which are 8.929167° N and 76.918056° E. Also I have uncommented the line. Now mine looks like below.
NOTE: Replace 8.929167 and 76.918056 with the coordinates of the location where your server resides.

http://php.net/date.default-latitude
date.default_latitude = 8.929167

http://php.net/date.default-longitude
date.default_longitude = 76.918056

Save and close the file.

NOTE: Just below the Location options, we will find two other two options date.sunrise_zenith and date.sunset_zenith.

; http://php.net/date.sunrise-zenith
;date.sunrise_zenith = 90.583333

; http://php.net/date.sunset-zenith
;date.sunset_zenith = 90.583333

There is no need to change these value unless it is done for a reason and you know exactly what you are doing.
Still if you want to know what Zenith is and its importance in servers, follow the below links.

http://en.wikipedia.org/wiki/Solar_zenith_angle
http://en.wikipedia.org/wiki/Horizontal_coordinate_system
http://en.wikipedia.org/wiki/Position_of_the_Sun
http://timeanddate.com/worldclock/aboutastronomy.html
http://ssd.jpl.nasa.gov/horizons.cgi
http://esrl.noaa.gov/gmd/grad/solcalc/
http://solardat.uoregon.edu/SolarPositionCalculator.html
http://nrel.gov/midc/solpos/
http://www.sunearthtools.com/dp/tools/pos_sun.php



Configuring settings related to MySQL Server

We will be using either MySQLi or PDO MySQL extension for interacting with the MySQL Server.
So we will set the configurations for both these MySQL database extensions.
NOTE: Read about MySQL database extensions for PHP.

Open the PHP configuration file php.ini.

Let us set the configurations for MySQLi extension. Look for the [MySQLi] section.
Set the value for MySQL Server socket. Look for the line containing mysqli.default_socket.
Set the value to /tmp/mysql.sock, which is the default value for our MySQL Server.
Now it must now look as shown below.

mysqli.default_socket = /tmp/mysql.sock

Set the MySQL Server hostname. Look for the line containing mysqli.default_host.
I set the value to localhost, because I prefer connecting through MySQL Server socket.
Now it must now look as shown below.
NOTE: Make sure you have read Disabling remote login for MySQL.

mysqli.default_host = localhost

Set the username under which MySQL Server runs. Look for the line that contains mysqli.default_user.
Set the value to mysql, which is the default value for our MySQL Server.
Now it must now look as shown below.

mysqli.default_user = mysql


Now, let us set the configurations for PDO MySQL extension. Look for the section [Pdo_mysql].
Set the MySQL Server socket. Look for the line that contains pdo_mysql.default_socket.
Set the value to /tmp/mysql.sock, which is the default value for our MySQL Server.
Now it must now look as shown below.

pdo_mysql.default_socket = /tmp/mysql.sock

Save and close the file.



Configuring Apache HTTP Server

Ope Apache HTTP Server‘s main configuration file httpd.conf.

Look for the line containing libphp5.so. There is a spacing in this line that prevents the PHP module from loading. Correct the spacing. Now the line must look as shown below.

LoadModule php5_module modules/libphp5.so


PHP files will have an extension .php. We have to tell Apache HTTP Server how to parse PHP files. Also we must notify Apache HTTP Server about the new PHP index page, which is index.php.

First, let us tell Apache HTTP Server, how to parse PHP files. The straightforward way is to add PHP as a file type under mime_module section, using AddType or AddHandler directives. But this can give rise to a security issue. As PHP files are executable, an attacker can upload a file like filename.php.jpg, rename it to filename.php and then execute it.
So we must tell Apache HTTP Server to parse PHP files based on file type. Add the following contents to httpd.conf file.

<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>


Now let us add a default page type index.php.
Look for the dir_module section. It must start with the line <IfModule dir_module>.
Add index.php as a default page type. Now the section must look as shown below.

<IfModule dir_module>
DirectoryIndex index.php index.html index.htm
</IfModule>

Save and close the file.


Restart the apache service gracefully.

# service apache graceful



Creating a PHP Index page

In the directory /usr/local/apache2/htdocs/, create a PHP file index.php with the below content.

<html>
<h1> Hello </h1>
<body>
<?php echo 'This is my first PHP page'; ?>
</body>
</html>

Open the web broswer and goto http://example.com. We must see our default PHP page.

 

 

5) Installation Review
Install location
/etc
/usr/local/apache2/modules/
/usr/local/bin
/usr/local/include
/usr/local/lib/
/usr/local/lib/php/
/usr/local/php/
/usr/local/sbin

DSO Module
/usr/local/apache2/modules/libphp5.so

Executables
Listed and explained

Configuration files
Listed and explained

Per-directory configuration file
.user.ini

Log files
Listed and explained

Get information on PHP configuration
Create a file named phpinfo.php in directory /usr/local/apache2/htdocs/ using the below content.
NOTE:
• This PHP file can be a valuable debugging tool as it contains all EGPCS (Environment, GET, POST, Cookie, Server) data. But it can also be a security risk if left alone in the server for anyone to view. So make sure to remove the file after using it.
• The name of the PHP file is not important.

<?php
	phpinfo();
?>

Open the web broswer and goto http://example.com/phpinfo.php.
We must see the complete information about our current PHP installation.

 

 

 

After installing PHP, it is recommended to view the following sections.
Securing PHP
Customizing PHP