Skip to content

Instantly share code, notes, and snippets.

@bashilbers
Last active August 29, 2015 14:21
Show Gist options
  • Save bashilbers/cd9b9a6bfeec0e21b11d to your computer and use it in GitHub Desktop.
Save bashilbers/cd9b9a6bfeec0e21b11d to your computer and use it in GitHub Desktop.
php.ini tuning

ini files

  • The CLI sapi is using a different ini file. Make sure you optimize both the web and CLI one.
  • Scan your php.ini for best security practices with PHP iniscan tool.

memory

Save system resources by adjusting the memory_limit value. Don't know what an appropriate value would be? Ask yourself these questions:

  • what is the total amount of memory that you can allocate for PHP?
    • 512mb should a reasonable amount for a 2gb machine)
  • how much memory on average, is consumed by a single process?
    • check with memory_get_peak_usage(), this is often between 5-20mb
  • how many PHP-processes can I afford?
    • if you have 512mb of memory allocated and each process takes 15mb, you can afford 34 processes

stress testing

Do you have enough system resources? (stress-test with Apache Bench or Siege before you go into production.

OPcache

Configure the Zend OPcache (php 5.5.0+)

  • depending on application size, adjust opcache.memory_consumption: 16mb for small apps and 64 for large ones
  • bump opcache.interned_strings_buffer (pointer buffer of identical strings) from 4mb to 16mb
  • make sure that opcache.max_accelerated_files is larger than the number of files in your application
  • disable opcache.validate_timestampts for production, but keep it enabled for development
  • set opcache.revalidate_freq to 0, making sure that PHP checks compiled files for changes on every request during development
  • turn on opcache.fast_shutdown by setting 1 which delegates object deconstruction

file uploads

Don't use file uploads? set file_uploads to 0 in order to improve security

You do?

  • set a maximum upload filesize that your application accepts and maximum number of uploads that your application accepts at one time.
    • bump the upload_max_filesize setting to 10mb or higher according to the requirements
    • adjust the client_max_body_size in your nginx virtual host in addition to php.ini

max execution time

Set max_execution_time from 30 to 5.

sessions

Change the default session handler to a faster in-memory data store (like memcached or redis), eliminating unnecessary file I/O with a bonus of enabling future scalability. Be sure to change the session.save_handler.

output buffering

Change implicit_flush to false and make sure output_buffering is a multiple of 4 (32-bit system) or 8 (64-bit system).

realpath

Change the realpath_cache_size (cache of files so PHP does not have to search the include path) accordingly.

  • check by bumping to 256k and then calling realpath_cache_size().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment