Introduction

This page is meant to help those configuring PHP and the web server it is running on to be very secure.

Below you will find information on the proper settings for the php.ini file and instructions on configuring Apache, Nginx, and Caddy web servers.

For general PHP codebase security please refer to the two following great guides:

PHP Configuration and Deployment

php.ini

Some of following settings need to be adapted to your system, in particular session.save_path, session.cookie_path (e.g. /var/www/mysite), and session.cookie_domain (e.g. ExampleSite.com).

You should also be running PHP 7.2 or later. If running PHP 7.0 and 7.1, you will use slightly different values in a couple of places below (see inline comments). Finally look through the PHP Manual for a complete reference on every value in the php.ini configuration file.

You can find a copy of the following values in a ready-to-go php.ini file here.

PHP error handling

  1. expose_php = Off
  2. error_reporting = E_ALL
  3. display_errors = Off
  4. display_startup_errors = Off
  5. log_errors = On
  6. error_log = /valid_path/PHP-logs/php_error.log
  7. ignore_repeated_errors = Off

Keep in mind that you need to have display_errors to Off on a production server and it’s a good idea to frequently notice the logs.

PHP general settings

  1. doc_root = /path/DocumentRoot/PHP-scripts/
  2. open_basedir = /path/DocumentRoot/PHP-scripts/
  3. include_path = /path/PHP-pear/
  4. extension_dir = /path/PHP-extensions/
  5. mime_magic.magicfile = /path/PHP-magic.mime
  6. allow_url_fopen = Off
  7. allow_url_include = Off
  8. variables_order = "GPCS"
  9. allow_webdav_methods = Off
  10. session.gc_maxlifetime = 600

allow_url_* prevents LFIs to be easily escalated to RFIs.

PHP file upload handling

  1. file_uploads = On
  2. upload_tmp_dir = /path/PHP-uploads/
  3. upload_max_filesize = 2M
  4. max_file_uploads = 2

If your application is not using file uploads, and say the only data the user will enter / upload is forms that do not require any document attachments, file_uploads should be turned Off.

PHP executable handling

  1. enable_dl = Off
  2. disable_functions = system, exec, shell_exec, passthru, phpinfo, show_source, highlight_file, popen, proc_open, fopen_with_path, dbmopen, dbase_open, putenv, move_uploaded_file, chdir, mkdir, rmdir, chmod, rename, filepro, filepro_rowcount, filepro_retrieve, posix_mkfifo
  3. # see also: http://ir.php.net/features.safe-mode
  4. disable_classes =

These are dangerous PHP functions. You should disable all that you don’t use.

PHP session handling

Session settings are some of the MOST important values to concentrate on in configuring. It is a good practice to change session.name to something new.

  1. session.save_path = /path/PHP-session/
  2. session.name = myPHPSESSID
  3. session.auto_start = Off
  4. session.use_trans_sid = 0
  5. session.cookie_domain = full.qualified.domain.name
  6. #session.cookie_path = /application/path/
  7. session.use_strict_mode = 1
  8. session.use_cookies = 1
  9. session.use_only_cookies = 1
  10. session.cookie_lifetime = 14400 # 4 hours
  11. session.cookie_secure = 1
  12. session.cookie_httponly = 1
  13. session.cookie_samesite = Strict
  14. session.cache_expire = 30
  15. session.sid_length = 256
  16. session.sid_bits_per_character = 6 # PHP 7.2+
  17. session.hash_function = 1 # PHP 7.0-7.1
  18. session.hash_bits_per_character = 6 # PHP 7.0-7.1

Some more security paranoid checks

  1. session.referer_check = /application/path
  2. memory_limit = 50M
  3. post_max_size = 20M
  4. max_execution_time = 60
  5. report_memleaks = On
  6. track_errors = Off
  7. html_errors = Off

Suhosin

Suhosin is a patch to PHP which provides a number of hardening and security features that are not available in the default PHP build. However, Suhosin only works with PHP 5, which is unsupported and should not be used.

There are a number of projects that have attempted to implement similar features for PHP 7 such as Snuffleupagus and Suhosin-ng, but these are both in the pre-release stage, and as such should not be used in production.