close
close

How to enable/disable PHP native features – php.ini Tutorial

In this article, I would like to give you the step-by-step procedure to enable or disable php native functions by editing the “disable_functions” directive in the php.ini file.

Sometimes the default PHP configuration on your server needs to be adjusted to accommodate your system’s compatibility with the production environment.

Why this lesson

I am the author of Inspector.dev, the real-time monitoring package for Laravel and Symfony. This package uses pro_open And proc_close php native functions to perform data transfer from your server to the Inspector API asynchronously.

It can also be configured to use PHP’s native CURL features that are enabled by default, but in this case it will send the monitoring data to Inspector in a synchronous manner, just like other packages do like Sentry, Bugsnag, etc. That means Your application may become a bit slower because the PHP engine has to wait before terminating the connection with the client.

Recently some developers contacted me because the proc_open and proc_close functions were disabled in their php configuration, so they need to unlock them to take advantage of asynchronous data sending.

It’s quite rare, but I decided to write this tutorial as a support resource for this use case.

What is the php.ini file

The php.ini file is THE configuration file for PHP. The file contains a list of key/value pairs called “directives.” Guidelines are grouped into sections, although these are primarily for organizational purposes.

When PHP starts, it reads this file and sets up internal data structures to store the configuration. At runtime, PHP refers to these internal data structures to determine how it should behave in many important areas, such as: error handling and logging, resource limits (memory, execution time), file uploads, database connections, and more.

For anyone also wondering, PHP can work even if there is no php.ini file, it will simply apply the default values ​​to all directives.

For those unfamiliar, here is an example of a php.ini file:

https://github.com/php/php-src/blob/master/php.ini-production

Enable/disable features in php.ini

By editing the php.ini file we can disable native PHP functions that you cannot call in your PHP code.

Sometimes this feature is used to restrict the use of certain features in shared hosting environments, as improper use can cause security issues for other users. Features such as exec() are usually disabled, but sometimes server providers are too conservative and may disable features needed in everyday programming tasks.

Locate the php.ini file

Run the command below in your machine’s terminal to get the current location of the file (it works for both Win and Unix):

php --ini
Go to full screen mode

Exit full screen mode

Common locations: /etc/php.ini, /etc/php/8.x/php.ini (replace x with the version number of your installation)

To edit files on the server I prefer to use the vim editor which should be available on every Unix machine.

// Use the file path from the command above
sudo vim /etc/php.ini
Go to full screen mode

Exit full screen mode

Scroll down to the “disable_functions” directive. It should be in the first half of the file and contains a list of functions separated by a comma.

disable_functions=exec,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
Go to full screen mode

Exit full screen mode

Edit the php.ini file

  • Press the letter “i” on your keyboard to enter INSERT mode in vim.
  • Remove the functions proc_open and proc_close from the list.
  • When you have finished editing the file, press the ESC key. This will exit INSERT mode and “– INSERT –” will disappear from the bottom left of your terminal.
  • To save the file, type “:wq”, which is the write & quit statement.

Restart the PHP engine

To load the new configurations, you must restart PHP. The instruction below should work on most servers. You may want to search Google for a specific operating system.

// Change the version of the PHP with what is in use in your machine
sudo systemctl restart php8.2-fpm.service
Go to full screen mode

Exit full screen mode

For more technical articles you can follow me on Linkedin or X.

Monitor your PHP application for free

Inspector is a code execution monitoring tool designed specifically for software developers. You don’t need to install anything at the server level, just install it Laravel or Symphony package and you are ready to go.

If you’re looking for HTTP monitoring, database query insights, and the ability to route alerts and notifications to your preferred messaging environment, try Inspector for free. Register your account.

Or read more on the website: https://inspector.dev

PHP application monitoring tool