You are here

Part VI: PHP

I assume that you are using PHP 5.3 or 5.4, which gives a significant performance boost compared to older versions. This section will look at memory usage, realpath cache, logging and op-code caching (APC) to boots performance.

Memory

Drupal contains many lines of code and you may have installed a large number of modules and if PHP runs out-of-memory your users will get the WSOD. So you may need to adjust the memory size based on the modules and you sites audience. I have seen users upload very large images that would make image styles crash do to insufficient memory during image resizing (See Find and Image Magick). So edit your php.ini, which on most installations is located in /etc/php5/apache2/php.ini.

Find the memory_limit line and change the amount that you need to run your site (most will suffices with 196Mb).

memory_limit = 196M

Realpath

In PHP 5.3 real path cache have been introduced which caches expensive file/directory lookups, thereby boosting performance. Realpath cache is maintained as a per thread cache and is cleared when the thread is killed.

Drupal uses a lot of files with relative paths (require_once/include_once) and it's here the realpath cache comes into play. On most systems the default size is 16K, which is to small for Drupal or other large PHP projects. Change the value in php.ini to something like the ones below to take better advantages of the cache.

realpath_cache_size = 256k
realpath_cache_ttl = 600

You can use the performance report module to get a look into the usage of realpath cache for your Drupal site.

Logging

In a production environment you should setup PHP to send log message to the systems sys-logger and not display errors to the end user (you have of cause used the E_STRICT setting during development).

Find and edit the lines below in php.ini.

error_reporting = E_ALL & ~E_DEPRECATED
display_errors = Off
display_startup_errors = Off
log_errors = On
error_log = php_errors.log
error_log = syslog

APC

The most important trick in getting PHP to run faster is using an opcode cache that stores the scripts as compiled byte code. This will give you a large performance boost as you skip the step of interpreting the PHP script every time its executed. Alternative PHP Cache (APC) stores the compiled byte code in memory ready for execution.

First you need to install APC, which can be done using the dotdeb sources or via pecl. I use dotdeb through apt-get so it will be updated together with the rest of the system.

~$ apt-get install php5-apc

The following settings configures APC to use a shared memory segment of 32Mb with a time to live (TTL) set to 4 hours and to not check the disk for new versions. The apc.stat tells APC not to check the disk for new versions of the script, which gives a yet a performance boots, but it also requires you to restart Apache to re-read the script if changed (or flush the APC cache from inside PHP). For more information see Optimizing APC for Drupal it's from 2010 but still applies.

apc.enabled=1
apc.shm_segments=1
apc.optimization=0
apc.shm_size=32M
apc.ttl=7200
apc.user_ttl=14400
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.enable_cli=1
apc.cache_by_default=1
apc.stat=1

APC comes with a script that when accessed on you web-server shows the memory usage of APC and you should use it to optimize APC. If you give APC to little memory it will flush the cached files and you will have the opposite effect thereby getting a performance penalty. So use the script to give you cache the right amount of memory in relation to your site.

Other benefits that you may notice is that your Apache threads memory footprint gets smaller when using APC, so revisit the Apache section to adjust you threads and memory usages.

You can use the performance report module to get a look into the usage of APC from inside your Drupal site.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.