Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Purespeed: PHP tuning

Parent Feed: 

The Buzz is all about PHP 7, and rightly so, it's pretty incredible some of the performance metrics its pulling even relative to Facebook's HHVM. Brad Fisher wrote about a one-line installer to get up and going with PHP 7 if you want to play around with it. But then reality sets in...

It's still a bit too early to run PHP 7 in production and Drupal 7 has some issues ancedotally from testing. Nothing deal breaking, but nothing that we're going to drop everything we're doing today and fix all the potential issues (a few months from now, yeah maybe). So, what can you do today to crank up PHP? Well that's where we pick things up. The following assumes CentOS/RHEL 6.x but similar commands will work for all the others (the locations change)

PHP.ini tuning

The first place to start is the thing that's pretty global to what version of PHP you are running. The settings that I recommend is drawn from reading forums, feedback from modules like apdqc (which makes recommendations) and other sources.

You can find the full file here but the key settings are:
max_execution_time = 90
max_input_time = 60

memory_limit = 256M

upload_max_filesize = 50M
post_max_size = 50M

; Recommendation from @mcarper
realpath_cache_size = 1M
realpath_cache_ttl = 3600

Max execution is useful because you might have a cron hit or drush command that takes a long time to process and you don't want things to timeout. Memory limit I always set really high again, in case there's a cache rebuild that takes a lot of memory. I dont see things get anywhere near this but it never hurts.

Upload and post sizes are useful if you have files that people upload to the site and they go above default which is only like 1 meg. The last 2 settings are recommendations from mcarper via the apdqc module (which we'll cover in a later posting).

APC

If you run PHP 5.3, you'll want to get APC up and going. APC allows for caching the location and content of PHP files in memory. This allows PHP to compile a hell of a lot faster since it doesn't have to load all the files all the time. We also recommend exploring APC's user bin as it can provide a drop dead simple way to make Drupal fly on limited resources. If you're running PHP 5.3...

Command

sudo yum install php-pecl-apc

Tuning

Apply the following script to sudo vi /etc/php.d/apc.ini

Upgrading off of 5.3

If you can, there's a lot of reasons to upgrade from 5.3 for performance and security reasons. If your currently on 5.3, you can run something like the following to get up to PHP 5.5. The biggest change in upgrade is that APC will stop working (if you have it currently applied) because Zend Opcache is available for it (so basically core)! We'll upgrade using Remi though Brad often recommends Webtatic.

wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
yes | yum -y --enablerepo=remi,remi-php55 install php-opcache php-pecl-apc php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

This will set the repo to download from to REMI (a popular compiled source) and then get a lot of the dependencies that you'll want for running Drupal like pear, apc, opcache, mcrypt, mbstring, memcached, gd and others.

Tuning Opcache

Opcache is a big deal as it's basically a better supported and FASTER version of APC. I read a few articles on tuning opcache and while there are different ways to do it, I'm running these in production with success.

Add these settings to /etc/php.d/opcache.ini

# optimize opcodecache for php 5.5
opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=100000
opcache.max_wasted_percentage=10
opcache.revalidate_freq=2
opcache.validate_timestamps=1
opcache.fast_shutdown=1
opcache.interned_strings_buffer=8
opcache.enable_cli=1

The key settings here are the max_accelerated_files, memory_consumption and the fact that it's enabled :)

APCu

Because APC is no longer the prefered opcache module and Zend Opcache doesn't support the user bin side of APC, if you still want that then you'll need a repo for it. The command to upgrade php to 5.5 includes apcu. If you want to add to this file by enabling progressive uploads then you can use this file.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web