Pages

Friday, May 3, 2013

Complile and install PHP from source

Complile and install PHP from source

You can follow the steps shown below to install PHP5.2.x , PHP5.3.x and PHP5.4.x . You can download and install the desired PHP version. Please reade the Important notes before starting the installation.

Here i am using Centos 5.8 64 bit to install PHP. It will work in all the other CentOS 64 bit versions. In the 32 bit OS, there will be corresponding changes in yum install packages. You have to make sure that the installing packages are for 32 bit or not.

I have followed the steps in the following urls to setup LAMP server, i have tested and verified installation by following the steps described in there.

http://linuxadmintips4u.blogspot.in/2012/07/configure-apache.html

http://linuxadmintips4u.blogspot.in/2012/07/configuration-of-mysql.html
PHP
Important Notes:

***If you are using Apache2.4.x with PHP, it is recommanded PHP version above 5.3.x. There some complilation errors in using PHP5.2.x. There are some module mismatches in Apache2.4.x with PHP5.2.x.

***If you are planning to install SUPHP, there some changes in PHP complilation in PHP5.2.x and PHP5.3.x. If you are using PHP5.4.x, you can use the same installation as descripbed below.

***If you are using Apache2.4.x, it is not possible to install SUPHP with any of the PHP versions with the suphp-0.7.1. I am awaiting for the next version of suphp.

***You can follow the below url to install SUPHP in PHP5.2.x , PHP5.3.x and PHP5.4.x .
Installation

[root@vps ~]# cd /usr/local/src/

Download one of the following PHP packages and extract the packges as per the selected packages.

[root@vps src]# wget http://in1.php.net/distributions/php-5.2.17.tar.gz
or
[root@vps src]# wget http://in1.php.net/distributions/php-5.3.24.tar.gz
or
[root@vps src]# wget http://in1.php.net/distributions/php-5.4.14.tar.gz
[root@vps src]# tar -xzf php-5.2.17.tar.gz
[root@vps src]# cd php-5.2.17

Create a script, configure.sh with the following contents. I have tested the following script with all the versions of PHP and it is working fine for me.

#================================================================
#!/bin/bash

"./configure" \
"--prefix=/usr/local/php" \
"--with-apxs2=/usr/local/apache/bin/apxs" \
"--with-config-file-path=/usr/local/apache/conf" \
"--disable-debug" \
"--disable-rpath" \
"--enable-inline-optimization" \
"--enable-sockets" \
"--enable-wddx" \
"--enable-calendar" \
"--enable-ftp" \
"--enable-mbstring=all" \
"--enable-dom" \
"--enable-mbregex" \
"--enable-bcmath" \
"--with-openssl" \
"--with-kerberos" \
"--with-mcrypt" \
"--with-curl" \
"--with-gd" \
"--with-zlib-dir=/usr/lib" \
"--with-jpeg-dir=/usr/lib" \
"--with-png-dir=/usr/lib" \
"--with-freetype-dir=/usr/lib" \
"--with-regex=system" \
"--with-gettext" \
"--with-bz2" \
"--with-iconv" \
"--with-layout=GNU" \
"--with-imap=/usr/local/src/imap-2007f" \
"--with-imap-ssl" \
"--enable-soap" \
"--with-snmp" \
"--with-libdir=lib64" \
"--with-mysql=/usr/local/mysql" \
"--with-pdo-mysql"

#================================================================

[root@vps php-5.2.17]# chmod +x configure.sh
[root@vps php-5.2.17]# sh configure.sh

[root@vps php-5.2.17]# make
[root@vps php-5.2.17]# make install

Once the installation completed, create the following link to use the php command directly in terminal.

[root@vps ~]# ln -s /usr/local/php/bin/php /usr/bin/php

Once you done the above steps, check the version of PHP using the following command

[root@vps ~]# php -v
The above compilation command will enable the following modules. You can remove unwanted php modules from the compilation step. You may encounter errors while the compilation. All the errors which i got is added in the 'Possible Error' section at the end of this article. Please check there if you encountered an error during complilation.

Modules Installed During Installation
bcmath bz2 calendar Core ctype curl date dom filter ftp gd gettext hash iconv imap json libxml mbstring mcrypt mime_magic mysql openssl pcre PDO pdo_mysql pdo_sqlite posix Reflection session SimpleXML snmp soap sockets SPL SQLite standard tokenizer wddx xml xmlreader xmlwriter zlib
Possible Errors
I got the following errors while installing php with above modules and the solution i have given fixed those errors.

Compile Time Errors:

Error:
checking libxml2 install dir... no
checking for xml2-config path...
configure: error: xml2-config not found. Please check your libxml2 installation.

Solution:
[root@vps php-5.2.17]# yum install libxml2-devel -y

-------------------------------------------------------------------------------------------------------------------------------------------------

Error:
configure: error: Cannot find OpenSSL's <evp.h>

Solution:
[root@vps php-5.2.17]# yum install openssl-devel -y

-------------------------------------------------------------------------------------------------------------------------------------------------

Error:
configure: error: Please reinstall the BZip2 distribution

Solution:
[root@vps php-5.2.17]# yum install bzip2-devel.x86_64 -y

--------------------------------------------------------------------------------------------------------------------------------------------------

Error:
checking for cURL in default path... not found
configure: error: Please reinstall the libcurl distribution

Solution:
[root@vps php-5.2.17]# yum install curl-devel.x86_64 -y

-----------------------------------------------------------------------------------------------------------------------------------------------

Error:
configure: error: libjpeg.(a|so) not found.

Solution:
[root@vps php-5.2.17]# yum install libjpeg-devel.x86_64 -y

------------------------------------------------------------------------------------------------------------------------------------------------

Error:
configure: error: libpng.(a|so) not found.

Solution:
[root@vps php-5.2.17]# yum install libpng-devel.x86_64 -y

----------------------------------------------------------------------------------------------------------------------------------------------

Error:
configure: error: freetype.h not found.

Solution:
[root@vps php-5.2.17]# yum install freetype-devel.x86_64 -y

-----------------------------------------------------------------------------------------------------------------------------------------------

Error:
checking OpenSSL dir for SNMP... no
checking whether to enable UCD SNMP hack... no
checking for net-snmp-config... no
configure: error: snmp.h not found. Check your SNMP installation

Solution:
[root@vps php-5.2.17]# yum install net-snmp-devel -y

-----------------------------------------------------------------------------------------------------------------------------------------------

Error:
configure: error: mcrypt.h not found. Please reinstall libmcrypt

Solution:
[root@vps php-5.2.17]# yum install libmcrypt.x86_64 libmcrypt-devel.x86_64 -y

------------------------------------------------------------------------------------------------------------------------------------------------

Error:
configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing. This should not happen. Check config.log for additional information.

Solution:

Install imap using the script described in the following url.

http://linuxadmintips4u.blogspot.in/2013/04/error-utf8mime2text-has-new-signature.html

----------------------------------------------------------------------------------------------------------------------------------------

Error:
checking for specified location of the MySQL UNIX socket... no
checking for MySQL UNIX socket location... no
configure: error: Cannot find libmysqlclient_r under /usr/local/mysql.
Note that the MySQL client library is not bundled anymore!

Solution:

http://linuxadmintips4u.blogspot.in/2013/04/error-cannot-find-libmysqlclientr-under.html

--------------------------------------------------------------------------------------------------------------------------------------------

Errors in Make
Error:
/usr/bin/ld: cannot find -lltdl
collect2: ld returned 1 exit status
make: *** [libphp5.la] Error 1

Solution:

Here's what you need to do to fix this issue,
1.Verify that the libtool and libtool-ltdl packages are installed.
[root@vps php-5.2.17]# yum install libtool-ltdl.x86_64 libtool.x86_64 -y

2.Symlink libltdl.so to libltdl.so.x.x.x

PHP looks for the libltdl library only at (for x64 at least) /usr/lib64/libltdl.so. The symlink to this file is not included in the libtool packages,you'll have to make it yourself
[root@vps php-5.2.17]# cd /usr/lib64
[root@vps lib64]# ln -s libltdl.so.3.1.4 libltdl.so

Try once again...

------------------------------------------------------------------------------------------------------------------------------------------------

Error:
/usr/local/imap-2007f/lib/libc-client.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [libphp5.la] Error 1
Solution:

Install imap using the script described in the following url.

http://linuxadmintips4u.blogspot.in/2013/04/error-utf8mime2text-has-new-signature.html

Complile and install MYSQL

Complile and install MYSQL
Preliminary installations

[root@vps ~]# yum -y install ncurses-devel
Complile and install Mysql

[root@vps ~]# groupadd mysql
[root@vps ~]# useradd -r -g mysql mysql
[root@vps ~]# cd /usr/local/src/
[root@vps src]# wget http://downloads.mysql.com/archives/mysql-5.5/mysql-5.5.0-m2.tar.gz
[root@vps src]# tar -xzf mysql-5.5.0-m2.tar.gz
[root@vps src]# cd mysql-5.5.0-m2

[root@vps mysql-5.5.0-m2]# ./configure --prefix=/usr/local/mysql --localstatedir=/usr/local/mysql/data --disable-maintainer-mode --with-mysqld-user=mysql --with-unix-socket-path=/tmp/mysql.sock --without-comment --without-debug --without-bench

[root@vps mysql-5.5.0-m2]# make && make install

[root@vps mysql-5.5.0-m2]# ./scripts/mysql_install_db

[root@vps mysql-5.5.0-m2]# chown -R root:mysql /usr/local/mysql
[root@vps mysql-5.5.0-m2]# chown -R mysql:mysql /usr/local/mysql/data
-To set the proper ownership for the MySQL directories and data files, so that only MySQL (and root) can do anything with them.

***Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
[root@vps mysql-5.5.0-m2]# cp support-files/my-medium.cnf /etc/my.cnf
[root@vps mysql-5.5.0-m2]# chown root:sys /etc/my.cnf
[root@vps mysql-5.5.0-m2]# chmod 644 /etc/my.cnf
[root@vps ~]# cd /usr/local/mysql/bin
[root@vps ~]# for file in *; do ln -s /usr/local/mysql/bin/$file /usr/bin/$file; done
-To set up symlinks for all the MySQL binaries, so they can be run from anyplace without having to include/specify long paths.
Create Startup service for mysql
[root@vps ~]# echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
***Do not issue the above command more than once.

[root@vps ~]# ldconfig
[root@vps ~]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysql
[root@vps ~]# chmod +x /etc/rc.d/init.d/mysql
[root@vps ~]# chkconfig mysql on

You can now start/stop mysql using the following commands.

[root@vps ~]# /etc/rc.d/init.d/mysql start
[root@vps ~]# /etc/rc.d/init.d/mysql stop
***If you are encounter any problems in start and stop mysql, you can find the reason from the error log of mysql. Error log name is in the <hostname>.err format.
Here my server hostname is vps.arun.com and therefore the error log name og mysql is vps.arun.com.err . You can use find the error log in the var directory of mysql installation. Here in my case it is /usr/local/mysql/var/vps.arun.com.err.
You can set new mysql root password using the followiing command.
[root@vps ~]# mysqladmin -u root password <newpassword>

Notes:-

Error:
Configure: error: No curses termcap library found
Fix:
[root@vps mysql]# yum install ncurses-devel -y
Error:
Error in /usr/local/mysql/var/vps.arun.com.err
/usr/local/mysql/libexec/mysqld: File './mysql-bin.index' not found (Errcode: 13)
fix:
Change the ownership of var directory in mysql's installation directory, as like follows.
chown -R mysql:mysql /usr/local/mysql/var

Complile and install Apache

Complile and install Apache
Here i am explaining the steps to setup a LAMP server. I am hre using CentOS 5.8 64 bit to setup LAMP. There will be similar variations in other OS in LINUX. I think this will work in most of the CentOS versions.

Apache

Preliminary Checking Installations for Apache

In most of the CentOS installations httpd may be installed by default. It is better to remove those installation at first.

[root@vps ~]# rpm -q httpd
httpd-2.2.3-65.el5.centos

The following command will uninstall httpd-2.2.3-65.el5.centos rpm package.

[root@vps ~]# rpm -e httpd-2.2.3-65.el5.centos
It is needed to install the necessary compilers to use the make command. The following comand will help you to install the necessary packages.
[root@vps ~]# yum install gcc gcc-c++ -y
It is needed to Install APR and APR-UTIL packages to install Apache.

Install APR

[root@vps ~]# cd /usr/local/src
[root@vps src]# wget http://apache.techartifact.com/mirror//apr/apr-1.4.6.tar.gz
[root@vps src]# tar -xzf apr-1.4.6.tar.gz
[root@vps src]# cd apr-1.4.6
[root@vps apr-1.4.6]# ./configure --prefix=/usr/local/apr
[root@vps apr-1.4.6]# make
[root@vps apr-1.4.6]# make install
Install APR-UTIL

[root@vps ~]# cd /usr/local/src
[root@vps src]# wget http://apache.techartifact.com/mirror//apr/apr-util-1.5.2.tar.gz
[root@vps src]# tar -xzf apr-util-1.5.2.tar.gz
[root@vps src]# cd apr-util-1.5.2
[root@vps apr-util-1.5.2]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
[root@vps apr-util-1.5.2]# make
[root@vps apr-util-1.5.2]# make install

Compile and Install Apache 2.2.x

[root@vps ~]# cd /usr/local/src
[root@vps src]# wget http://apache.techartifact.com/mirror//httpd/httpd-2.2.24.tar.gz
[root@vps src]# tar -xzf httpd-2.2.24.tar.gz
[root@vps src]# cd httpd-2.2.24

[root@vps httpd-2.2.24]# ./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr/bin/apr-1-config --with-apr-util=/usr/local/apr-util/bin/apu-1-config --enable-ssl=/usr/include/openssl --enable-rewrite --enable-deflate --enable-suexec

*** You can check the option available for the configuration using the following command. As per your need, you can add new modules.

[root@vps httpd-2.2.24]# ./configure --help

[root@vps httpd-2.2.24]# make
[root@vps httpd-2.2.24]# make install

The following command will enable you to use use httpd command instead of giving full path of apachectl.

[root@vps httpd-2.2.24]# ln -s /usr/local/apache/bin/apachectl /usr/bin/httpd
Once you have done the above configuration and the creating the symbolic link, You can check the settings of Apache using the following commands.
[root@vps ~]# httpd -M
[root@vps ~]# httpd -V

Compile and Install Apache 2.4.4

***I have noted that in Apache 2.4.x, suphp-0.7.1 cannot compile with any of the php version. There no development in suphp after 2009-03-14. So i personally do not recommend Apache 2.4.x at this time.
***Also I have noted that there are some issues in Apache 2.4.x with PHP5.2.x. So think before using Apache 2.4.x in your server.

[root@vps ~]# cd /usr/local/src
[root@vps src]# wget http://apache.techartifact.com/mirror//httpd/httpd-2.4.4.tar.bz2
[root@vps src]# tar -xjf httpd-2.4.4.tar.bz2
[root@vps src]# cd httpd-2.4.4

[root@vps httpd-2.4.4]# ./configure --prefix=/usr/local/apache --enable-load-all-modules --with-apr=/usr/local/apr/bin/apr-1-config --with-apr-util=/usr/local/apr-util/bin/apu-1-config --with-pcre=/usr/local/pcre/bin/pcre-config

[root@vps httpd-2.4.4]# make
[root@vps httpd-2.4.4]# make install

The following command will enable you to use use httpd command instead of giving full path of apachectl.

[root@vps httpd-2.2.24]# ln -s /usr/local/apache/bin/apachectl /usr/bin/httpd
Once you have done the above configuration and the creating the symbolic link, You can check the settings of Apache using the following commands.
[root@vps ~]# httpd -M
[root@vps ~]# httpd -V
Notes:-

It is needed to install pcre to install apache2.4.x. Locate if the 'pcre-config' is in your system. If it is not available, you will encounter the following error while the installation of Apache.

Error:
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

Fix:
============================================================================================

[root@vps ~]# cd /usr/local/src
[root@vps src]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.gz
[root@vps src]# tar -xzf pcre-8.32.tar.gz
[root@vps src]# cd pcre-8.32
[root@vps pcre-8.32]# ./configure --prefix=/usr/local/pcre
[root@vps pcre-8.32]# make
[root@vps pcre-8.32]# make install
============================================================================================

Create A startup Script for httpd
Create a file /etc/init.d/httpd with the following contents in it. You have to give the pid file location, httpd and apachectl correctly in the script to run the script.
Here in my case, these are the settings.

[root@vps ~]# locate httpd.pid
/var/run/httpd.pid

================================================================================
#!/bin/bash
#
# Startup script for the Apache Web Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# pidfile: /var/run/httpd.pid
# config: /usr/local/apache/conf/httpd.conf

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=/usr/local/apache/bin/httpd
pid=/var/run/httpd.pid
prog=httpd
RETVAL=0
# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure. So we just do it the way init scripts
# are expected to behave here.
start() {
echo -n $"Starting $prog: "
daemon $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/httpd
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/httpd $pid
}
reload() {
echo -n $"Reloading $prog: "
killproc $httpd -HUP
RETVAL=$?
echo
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f $pid ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status"
echo $"|fullstatus|graceful|help|configtest}"
exit 1
esac

exit $RETVAL
================================================================================
[root@vps ~]# chmod +x /etc/init.d/httpd
[root@vps ~]# chkconfig --add httpd
[root@vps ~]# chkconfig httpd on

This will enable you to start and stop apache using the following commands

[root@vps ~]# /etc/init.d/httpd start
[root@vps ~]# /etc/init.d/httpd stop