PHP scripts and tricks...

memcached on centos

Below is a summary of steps taken to get memcache support for php:

1. Install memcached and php-pear for pecl support to download and compile memcache for php.

    yum --enablerepo=epel install memcached php-pear
    pecl install memcache

2. Configure memcached (/etc/sysconfig/memcached) and start it up:

    chkconfig memcached on
    service memcached start

3. Create /etc/php.d/memcache.ini file with the below contents:

4. Reload apache and/or php-fpm processes:

    service httpd reload
    service php-fpm restart

5. Check with "php -i" or "phpinfo()" that memcache support is enabled.

6. Memcached listens to port 11211 by default. Status and stats:

    memcached-tool localhost:11211 display
    memcached-tool localhost:11211 stats

easy php-fpm install via yum

On CentOS, php-fpm can be easily installed via CentALT yum repository. This requires epel repository too and will pull down any dependencies if needed.

  • Install EPEL release:
    rpm -Uvh
  • Install CentALT release:
    rpm -Uvh
  • Install via yum:
    yum --enablerepo=CentALT --enablerepo=epel install php-fpm
  • Look through and edit /etc/php-fpm.conf . The config options are well commented... also available at
  • The default settings should work quite well.
  • Bring up the service via:
    /etc/init.d/php-fpm start

php with mysqlnd support

You can now get the latest PHP with mysqlnd (MySQL Native Driver) support via remis' yum repository as mentioned in PHP-5.3,-zts-and-mysqlnd. The blog mentions that this is enabld in php-zts. However, no php-pecl extension are available and neither are some of the extensions thread safe. So I went about rebuilding php package from source for mysqlnd support.

  1. Download the source php rpm from . Note, I have used php-5.3.2 which is the latest as of this writing.
  2. Install and apply the below diff patch to the spec file via `patch -p0 < {new_patch_file}` where "new_patch_file" has the below contents:

    --- php-5.3.2-remi.spec.orig 2010-03-11 23:07:04.000000000 -0600
    +++ php-5.3.2-remi.spec 2010-03-11 23:36:03.000000000 -0600
    @@ -24,6 +24,7 @@
    %global phpversion 5.3.2

    # Optional components; pass "--with mssql" etc to rpmbuild.
    +%define with_mysqlnd %{?_with_mysqlnd:1}%{!?_with_mysqlnd:0}
    %define with_oci8 %{?_with_oci8:1}%{!?_with_oci8:0}
    %define with_ibase %{?_with_ibase:1}%{!?_with_ibase:0}
    %if %{?rhel}%{?fedora} > 4
    @@ -677,6 +678,11 @@
    %if %{?fedora}%{?rhel:99} >= 10
             --with-system-tzdata \
    +%if %{with_mysqlnd}
    +        --with-mysql=shared,mysqlnd \
    +        --with-mysqli=shared,mysqlnd \
    +        --with-pdo-mysql=shared,mysqlnd \
    if test $? != 0; then
       tail -500 config.log
    @@ -704,8 +710,13 @@
           --enable-dba=shared --with-db4=%{_prefix} \
           --with-xmlrpc=shared \
           --with-ldap=shared --with-ldap-sasl \
    +%if %{with_mysqlnd}
    +      --with-mysql=shared,mysqlnd \
    +      --with-mysqli=shared,mysqlnd \
           --with-mysql=shared,%{_prefix} \
           --with-mysqli=shared,%{_bindir}/mysql_config \
    %ifarch x86_64
           %{?_with_oci8:--with-oci8=shared,instantclient,%{_libdir}/oracle/%{oraclever}/client64/lib,%{oraclever}} \
    @@ -725,7 +736,11 @@
           --enable-fastcgi \
           --enable-pdo=shared \
           --with-pdo-odbc=shared,unixODBC,%{_prefix} \
    +%if %{with_mysqlnd}
    +      --with-pdo-mysql=shared,mysqlnd \
           --with-pdo-mysql=shared,%{_prefix} \
           --with-pdo-pgsql=shared,%{_prefix} \
           --with-pdo-sqlite=shared,%{_prefix} \
           --with-pdo-dblib=shared,%{_prefix} \
    @@ -756,6 +771,16 @@

    +%if %{with_mysqlnd}
    +without_shared="--without-gd \
    +      --disable-dom --disable-dba --without-unixODBC \
    +      --disable-xmlreader --disable-xmlwriter \
    +      --without-sqlite \
    +      --disable-phar --disable-fileinfo \
    +      --disable-json --without-pspell --disable-wddx \
    +      --without-curl --disable-posix \
    +      --disable-sysvmsg --disable-sysvshm --disable-sysvsem"
    without_shared="--without-mysql --without-gd \
           --disable-dom --disable-dba --without-unixODBC \
           --disable-pdo --disable-xmlreader --disable-xmlwriter \
    @@ -764,6 +789,7 @@
           --disable-json --without-pspell --disable-wddx \
           --without-curl --disable-posix \
           --disable-sysvmsg --disable-sysvshm --disable-sysvsem"

    # Build Apache module, and the CLI SAPI, /usr/bin/php
    pushd build-apache

  3. Package with:
    rpmbuild -bb --with mysqlnd --define "rhel 5" SPECS/php-5.3.2-remi.spec
  4. To install via yum, change to the directory where rpms are located and recreate the repodata via:
    createrepo .
  5. Note: you may need to install some of the missing dependent devel packages. I used a combination of base, epel and remi repository to install the dependencies.

pecl runkit with php-5.2.x

As of writing, the current pecl runkit-0.9 package does not compile with PHP 5.2+ . Below is how I got the latest installed from svn trunk on CentOS-5.4 with php-5.2.9.

  • Install runkit:
          svn co
          cd trunk
          make install
  • Create "/etc/php.d/runkit.ini":

  • Restart apache and validate with phpinfo.

Note: you would need php-pear and php-devel installed already.

Static compile of openssl, apache, mod_ssl and php

As of this writing, the latest versions were:

  • apache_1.3.41
  • php-4.4.8
  • openssl-0.9.8g
  • mod_ssl-2.8.31-1.3.41

Once the files are downloaded and extracted; config, compile and install in the below order:

  1. Install openssl:
    $ ./config --prefix=/usr/local --openssldir=/usr/local/openssl
    $ make
    # make install
  2. mod_ssl patch:
    $ ./configure \
    --with-ssl=../openssl-0.9.8g \
  3. Pre-configure apache:
    $ ./configure
  4. Install php:
    $ ./configure \
    --with-gd \
    --with-jpeg-dir \
    --with-png-dir \
    --with-zlib-dir \
    --with-freetype \
    --with-freetype-dir=/usr/lib \
    --enable-gd-native-ttf \
    --enable-memory-limit \
    --with-ldap \
    --with-mysql \
    $ make
    # make install
  5. Install apache
    $ SSL_BASE=../openssl-0.9.8g \
    ./configure \
    --prefix=/usr/local/apache \
    --enable-module=rewrite \
    --enable-module=so \
    --activate-module=src/modules/php4/libphp4.a \

Static apache-1.3.x and php-4.x compile for dotProject

  1. Download and unpackage the source files:
    $ cd /usr/local/src
    $ wget
    $ wget
    $ tar -xvzf apache_1.3.41.tar.gz
    $ tar -xvzf php-4.4.8.tar.gz
  2. Preconfigure apache:
    $ cd apache_1.3.41
    $ make clean
    $ ./configure
  3. Configure, compile, install php:
    $ cd ../php-4.4.8
    $ make clean
    $ ./configure \
    --with-gd \
    --with-jpeg-dir \
    --with-png-dir \
    --with-zlib-dir \
    --with-freetype \
    --with-freetype-dir=/usr/lib \
    --enable-gd-native-ttf \
    --enable-memory-limit \
    --with-ldap \
    --with-mysql=/usr/local/mysql \
    $ make
    # make install
  4. Configure, compile, install apache:
    $ ./configure \
    --prefix=/usr/local/apache \
    --enable-module=rewrite \
    --enable-module=so \
    $ make
    # make install

Auto restart apache on segmentation fault error

Recently a particular folder in a site started coming up with blank php pages and the root cause being Segmentation fault after memory exhaustion. The subfolder contents would only come up after doing a restart of apache.

So something tried to access a region of memory that it did not have rights to. May be due to either bad RAM, or a code problem.

My first instinct was eAccelerator as I had done a recent php update and had not re-compiled it.

I still had the same issue the next day and bumped up the memory_limit from 16M to 32M in php.ini and from 32M to 64M in eaccelerator.ini .

Upgrading php on DirectAdmin Servers

Download the source tar.gz file from and place it in "/usr/local/directadmin/customapache"

Edit the "/usr/local/directadmin/customapache/build" file and change the version of php to the version that was downloaded.

Then run:

# cd /usr/local/directadmin/customapache/
# ./build clean
# ./build update
# ./build all

Restart apache when finished.

logging php errors on godaddy hosting

Godaddys' linux hosting allows for editing your own "php.ini" file and enables support for logging errors, good for debugging purposes when developing with php.

Add the below lines to php.ini in the document root, to log all errors:

error_reporting  =  E_ALL
log_errors = on
error_log = /home/content/p/a/t/pathto/html/error_log

You should also protect the php.ini and error_log file via .htaccess:

<FilesMatch "(error_log|php\.ini)$">
  Order deny,allow
  Deny from all

Hosting multiple domains pointed to the same web-space

Place the snippet of code in an index.php file to pull up a different web page for each domain pointed to the same web-space.

$serverName = $HTTP_HOST;
$serverName = str_replace("www.","",$serverName);
$serverName = str_replace(".com","",$serverName);
$serverName = str_replace(".net","",$serverName);
$serverName = str_replace(".org","",$serverName);
if (!(empty($serverName))) {

Syndicate content