PHP

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:

    extension=memcache.so

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 http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-3.noarch.rpm
  • Install CentALT release:
    rpm -Uvh http://centos.alt.ru/repository/centos/5/x86_64/centalt-release-5-3.noarch.rpm
  • 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 php-fpm.org
  • 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 http://rpms.famillecollet.com/SRPMS/ . 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
        &nbsp;    --with-system-tzdata \
    %endif
    +%if %{with_mysqlnd}
    +    &nbsp;   --with-mysql=shared,mysqlnd \
    +    &nbsp;   --with-mysqli=shared,mysqlnd \
    +    &nbsp;   --with-pdo-mysql=shared,mysqlnd \
    +%endif
    $*
    if test $? != 0; then
       tail -500 config.log
    @@ -704,8 +710,13 @@
        &nbsp;  --enable-dba=shared --with-db4=%{_prefix} \
        &nbsp;  --with-xmlrpc=shared \
        &nbsp;  --with-ldap=shared --with-ldap-sasl \
    +%if %{with_mysqlnd}
    +    &nbsp; --with-mysql=shared,mysqlnd \
    +    &nbsp; --with-mysqli=shared,mysqlnd \
    +%else
        &nbsp;  --with-mysql=shared,%{_prefix} \
        &nbsp;  --with-mysqli=shared,%{_bindir}/mysql_config \
    +%endif
    %ifarch x86_64
        &nbsp;  %{?_with_oci8:--with-oci8=shared,instantclient,%{_libdir}/oracle/%{oraclever}/client64/lib,%{oraclever}} \
    %else
    @@ -725,7 +736,11 @@
        &nbsp;  --enable-fastcgi \
        &nbsp;  --enable-pdo=shared \
        &nbsp;  --with-pdo-odbc=shared,unixODBC,%{_prefix} \
    +%if %{with_mysqlnd}
    +    &nbsp; --with-pdo-mysql=shared,mysqlnd \
    +%else
        &nbsp;  --with-pdo-mysql=shared,%{_prefix} \
    +%endif
        &nbsp;  --with-pdo-pgsql=shared,%{_prefix} \
        &nbsp;  --with-pdo-sqlite=shared,%{_prefix} \
        &nbsp;  --with-pdo-dblib=shared,%{_prefix} \
    @@ -756,6 +771,16 @@
        &nbsp;  --with-recode=shared,%{_prefix}
    popd

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

    # 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 http://svn.php.net/repository/pecl/runkit/trunk/
        &nbsp; cd trunk
        &nbsp; phpize
        &nbsp; ./configure
        &nbsp; make
        &nbsp; make install
  • Create "/etc/php.d/runkit.ini":
          extension=runkit.so
  • 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 \
    --with-apache=../apache_1.3.41
  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 \
    --with-apache=../apache_1.3.41
    $ 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 \
    --enable-module=ssl

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

  1. Download and unpackage the source files:
    $ cd /usr/local/src
    $ wget http://www.ibiblio.org/pub/mirrors/apache/httpd/apache_1.3.41.tar.gz
    $ wget http://us2.php.net/get/php-4.4.8.tar.gz/from/us.php.net/mirror
    $ 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 \
    --with-apache=../apache_1.3.41
    $ make
    # make install
  4. Configure, compile, install apache:
    $ ./configure \
    --prefix=/usr/local/apache \
    --enable-module=rewrite \
    --enable-module=so \
    --activate-module=src/modules/php4/libphp4.a
    $ 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 php.net 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)$&quot;>
  Order deny,allow
  Deny from all
</FilesMatch>

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.",&quot;",$serverName);
$serverName = str_replace(".com",&quot;",$serverName);
$serverName = str_replace(".net",&quot;",$serverName);
$serverName = str_replace(".org",&quot;",$serverName);
if (!(empty($serverName))) {
   include("./".$serverName.".html");
}
?>

Comment