← Other topics

PHP Configuration - Locating and Editing php.ini (Apache, Nginx, PHP FPM)

Video Notes

PHP’s initialization file, called php.ini, is responsible for configuring many of the aspects of PHP’s behavior.

To locate the php.ini file that your web server is using, run the built-in function phpinfo() in any .php file and note the value for Loaded Configuration File.

From the above example we see the php.ini file is located at /etc/php/8.1/fpm/php.ini.

If you’re working with PHP from command line, run the command php --ini to see what php.ini file is being used.

Note that the php.ini file your web server is using might be different than the one being used by your web server.

Directives

Below is an except from a php.ini file that configures PHP to display errors:

; This directive controls whether or not and where PHP will output errors,
; notices and warnings too. Error output is very useful during development, but
; it could be very dangerous in production environments. Depending on the code
; which is triggering the error, sensitive information could potentially leak
; out of your application such as database usernames and passwords or worse.
; For production environments, we recommend logging errors rather than
; sending them to STDOUT.
; Possible Values:
;   Off = Do not display any errors
;   stderr = Display errors to STDERR (affects only CGI/CLI binaries!)
;   On or stdout = Display errors to STDOUT
; Default Value: On
; Development Value: On
; Production Value: Off
; https://php.net/display-errors
display_errors = On

Observations about the above:

  • The semicolon character is used for comments.
  • Directives are set using the syntax directive = value.
  • Most directives are prefaced with a bunch of comments explaining what that directive does and what values you can set it to.

Updating configs

After making any changes to your php.ini file you need to prompt your server to recognize these changes. How you do this will depend on your server and its PHP handler, which is also reported in the output of phpinfo():

Below are a few different examples.

Example 1) Apache

The above screenshot was taken from an Ubuntu server running Apache. The PHP handler is shown as Apache 2.0 Handler in the phpinfo() output.

In this scenario, I simply need to restart Apache with the following command for any changes to php.ini to take effect:

> systemctl restart apache2

Example 2) Nginx / FPM FastCGI

I have another Ubuntu server but it’s running Nginx and the PHP handler is FPM FastCGI. In this case, I need to run the following command to specifically reload the PHP handler. If you’re running a version of PHP different than 8.1 you should update the command accordingly.

> systemctl reload php8.1-fpm

Example 3) Apache via XAMPP

I have a local server running via XAMPP. Like Example 1 above, it is also handling PHP via the Apache 2.0 handler, so I just need to restart the server which I can do via the XAMPP interface.

If this info helped you out you can say thanks and support future content by clicking my Amazon affiliate link: https://amzn.to/3UtgnYk. If you make a purchase on Amazon within 24 hours of clicking the link I may receive a micro-commission and it costs you nothing extra. Any income from these sales goes directly to supporting me in making new videos and guides. Thank you for your support!

← Other topics