Translations of this page:

Installing PHP on Gentoo Linux

The Gentoo Linux server I was installing for a client did not come with PHP pre-installed. While I could probably have asked the hosting provider to add their standard install the advantage of running your own server means that you can grab the most recent version in the 4* branch which will include the latest security updates. This also meant I could control what extras were included with PHP. For performance and security it is best to have a minimalist install. Using Gentoo’s emerge:

 # USE="-* curl zlib pcre session apache2 crypt spell mysql gd xml expat" emerge "=dev-lang/php-4*"

At the end of the process. emerge reported 16 configuration file conflicts in /etc! All but one of these were related to the initial install.

Performance Improvements for PHP

PHP is an interpreted language and for many this will ring performance alarm bells. The Zend engine converts PHP source code into byte codes every time a script is run. Interpretation has the advantage that scripts can be changed on-the-fly making the test/debug cycle much quicker.

Executing a PHP scripts takes three steps:

  1. PHP loads the file,
  1. The source file is parsed and and transforms it into byte codes that can be executed by the interpreter
  1. The bytecodes are run

Apart from tuning the PHP code itself there are a couple of things we can do to improve the performance of an application. Run an optimizer and cache the translated byte codes for re-use rather than throwing them away.

The best known PHP optimizer is the one from Zend. An optimizer can look over the PHP byte codes modifying them for speed and removing redundant operations such as empty loops. This is an expensive process and little is to be gained if the optimized code is simply discarded after running. Many benchmarks show little performance difference with the Zend optimizer installed. The Zend platform includes a byte-code cache. This stores the translated byte codes, if the script is called again rather than performing the expensive translate → optimize cycle the byte code is retrieved directly from cache. This relatively trivial operations can give massive performance improvements; even without the optimization phase.

There are a number of PHP byte code caches available. Which one you use depends partly on which version of PHP you are running. Here are some suggestions

  • IonCache
  • xcache - from the developers of lighttpd

Optimization can cause problems, sometimes scripts will crash or crash erratically. You may have to adjust some parameters for less aggressive optimization or use a different version or different optimizer. Changing scripts while the optimizer is running can also produce erratic behaviour. APC in particular had a bug in this area.

In the end I settled for the pecl-apc optimizer, it is free and it will be included as standard in php6 so seems to have an assured future.

After downloading and unpacking installation is the standard configure, make, make install process

 # ./configure --enable-apc-mmap --with-apxs --with-php-config=/usr/lib/php4/bin/php-config --with-apxs=/usr/sbin/apxs2 
 # make
 # make install
   Installing shared extensions:     /usr/lib/php4/lib/php/extensions/no-debug-non-zts-20020429/Apc.so

To get APC running you may need to adjust some of the paths to point to your copy of axps and your php-config. Additionally, if you’re using PHP 4, add the Apache include directory to CPPFLAGS and specify that you’re running PHP 4. (If you’re using PHP5 you can skip this step, it’s done automatically.)

 $ export CPPFLAGS="–I/usr/local/apache2/include –DAPC_PHP4_STAT"

With the apc.so now installed, it’s time to enable APC. Add the following lines to your php.ini file:

 extension=apc.so
 apc.enabled=1
 apc.shm_segments=1
 apc.optimization=0
 apc.shm_size=128
 apc.ttl=7200
 apc.user_ttl=7200
 apc.num_files_hint=1024
 apc.mmap_file_mask=/tmp/apc.XXXXXX

After a Web server restart, APC should be enabled. You can confirm that by running phpinfo().

Out of the box APC performs a stat() on each file before serving the code from the opcode cache to see if the file has changed and needs retranslating. System calls are quite expensive operations and to get every last bit of performance it is possible to skip this ech by putting

 apc.stat=0

into the php.ini file. However you will need to flush the APC cache or restart the web server after any PHP changes. This option is ideal, in fact recommended, for production servers or if you are installing standard packages rather than doing your own development. It is possible to cache only a certain virtual host, set apc.cache_by_default to 0 in php.ini and add the respective php_admin_value to your <VirtualHost> section in the Apache configuration file.

APC provides lots information about what it is doing via the apc.php script. This script shows the APC/PHP version information, runtime settings, diagrams of both memory usage and cache hits and misses. It also shows which scripts are being cached.

Before installing open the script and set the ADMIN_PASSWORD. Copy the file somewhere under the htdocs directory. Open the file location in your web browser. APC should provide a performance increase in practically every script, especially complex scripts. However database, network or file access can still be bottlenecks to overall user perceptions.

Tuning APC

There are two main areas you can adjust. The amount of shared memory used by APC and also the file update check previously mentioned. The shared memory size is controlled by the

 apc.shm_size

directive.

When running apc.php look at the Cache full count to see how often the cache has been filled. When this happens any entries not accessed since

apc.ttl seconds are removed. This is an expensive operation and you need to configure the optimizer to keep these operations to a minimum either by giving APC more memory or setting

 apc.filters

to cache fewer scripts. The

 apc.optimization

configures the experimental optimizer. Zero disables optimizations, higher values employ more aggressive optimization. However as this modifies bytecodes it can cause crashes and does not yield large performance improvements.

Results

The following two charts show CPU cycles and memory before and after the installation of APC. As you can see the cpu requirements of the web server drop appreciably

at the expenses of more memory usage

of course from a user experience viewpoint memory usage is invisible

References

tech/linux/php.txt · Last modified: 2009/03/19 09:23 by davidof
Recent changes RSS feed