🗂️ What is the PHP.ini File and Where is it Located? Understanding Key Parameters and How to Edit It
- PixelHost
- 14 ago 2024
- 5 Min. de lectura
The PHP.ini file is a configuration file that contains the PHP settings for your web server. Every time PHP starts, your system looks for and executes this file to enable the scripting rules for your site.
While it comes pre-configured, you may need to change the default PHP settings to suit your needs. For example, you might modify the session cookie name and duration to enable the "remember me" feature on your site.
In this article, we’ll explain the location of the PHP.ini file and its important parameters. You’ll also learn how to edit the file to change PHP settings.
🔍 What is PHP.ini?
PHP.ini is a configuration file that contains the PHP settings for your web server.
It allows you to control your site's PHP-related rules, such as setting the file upload size limit and hiding error messages.
The location of the PHP configuration file varies depending on the web server. To change its settings, edit the file and change the parameter values using a text editor.
📍 Where is the PHP.ini File Located?
The location of the PHP.ini file varies depending on your server, PHP version, and operating system. When PHP runs, the system looks for the file in the following locations:
The specific location of the Server API (SAPI) module.
The runtime configuration environment variable (PHPRC).
The current working directory, which can be found using the getcwd() function.
The installed PHP or web server directory.
Instead of manually searching for your INI files through your hosting’s file managers, users can do it automatically.
Among several methods, creating a new PHP file within your domain’s root directory is the easiest. For this tutorial, we’ll show you how to do it via hPanel:
Go to hPanel → Hosting → Manage.
Click on File Manager.
Navigate to your site’s root directory. In our case, it’s /home/user/public_html.
Select the + File icon. Enter a file name with a .php extension, such as phpinfo.php. Then, click Create new file.
Right-click on the new file → Edit and enter the following code:
php
Copy code
<?php phpinfo(); ?>
Click Save changes.
Enter yourdomain.tld/phpinfo.php in your web browser and press Enter.
Look for the Loaded Configuration File entry. The path to the PHP.ini file should look something like this:
Path to the PHP.ini file in PHP info output.
This method also works in cPanel to locate the PHP.ini file on a Virtual Private Server (VPS). Alternatively, connect via SSH and run the following command:
bash
php -i | grep 'Configuration File'
Your SSH client will display the directory, which is usr/local/lib for most web servers. Besides finding the location, you can move and edit the PHP.ini file with the SSH client.
🔑 Important PHP.ini Parameters
In this section, we’ll explain the most important parameters in the PHP.ini file, including their values and purposes. These parameters are also known as directives.
1. display_errors
Determines whether PHP error messages are displayed to users during script execution, using the values on and off. For security reasons, you should only use this directive during the development of your site.
2. error_reporting
Sets which error messages are shown to users when display_errors is enabled. The error_reporting parameter takes various constants to display different errors.
You can use multiple constants and exclude specific errors. For example, to show all errors except the deprecation warning, use the following:
php
E_ALL & ~E_DEPRECATED
3. error_log
Specifies the file where PHP will log errors for troubleshooting. Before enabling it, make sure the web server users have permission to write to the file.
4. file_uploads
Sets whether HTTP file uploads are enabled or not. The value on will allow users to upload files to your site, while off will disable it.
5. upload_max_filesize
This parameter determines the maximum upload file size that PHP allows on your site. Since the default value is 2 MB, you may want to increase the maximum upload size limit to allow users to upload larger files.
Learn more about this topic in our tutorial on how to fix the upload_max_filesize error.
6. post_max_size
The maximum size of POST data that PHP can collect from HTML forms on your site. The value should be greater than the maximum file size, as it handles the POST function.
7. allow_url_fopen
Allows a PHP script to access remote files from another server. It’s disabled by default, as enabling it could expose your server to a code injection attack.
8. allow_url_include
This directive functions similarly to allow_url_fopen, but it uses the include function. To enable it, allow_url_fopen must be set to on.
9. session.name
This directive sets the current session name used in cookies and URLs. You can change the default PHPSESSID value to any descriptive name with alphanumeric characters.
10. session.auto_start
Chooses whether a PHP session starts automatically or on request when users access your site. If set to 0, the session will start manually via the session_start script.
11. session.cookie_lifetime
Specifies the session cookie duration in your site visitors' browsers. By default, the value is set to 0 seconds, meaning your site deletes visitors’ session data after they close their browsers.
12. memory_limit
Sets the maximum amount of RAM that a PHP script can use. Be careful when increasing the memory limit, as incorrect settings can lead to slow sites or server crashes.
13. max_execution_time
Determines the maximum execution time of a script. You can change the default maximum execution time from 30 seconds to any value, but setting it too high could cause performance issues.
Learn more in our guide on how to fix the Fatal Error: max_execution_time exceeded error if you encounter this code.
14. max_input_time
Sets how long a script can parse data collected from HTML forms on your site via a POST or GET method. The more data your site collects, the higher the max_input_time value should be.
15. upload_temp_dir
Specifies the temporary directory for storing uploaded files. All users should be able to write to the specified directory, or PHP will use the system default.
16. realpath_cache_ttl
Sets the duration for your system to cache realpath information. We recommend increasing the value for systems with rarely changing files.
17. arg_separator.output
Use this data handling directive to separate arguments in URLs generated by PHP. Its default value is an ampersand (&).
18. arg_separator.input
Sets the separator that PHP uses to parse input URLs into variables. By default, it’s an ampersand, but you can change it to other symbols like a semicolon.
✏️ How to Edit the PHP.ini File
Before editing the PHP.ini file, check your server’s PHP settings using phpinfo.php. If PHP is set up as an Apache module, change PHP settings through your .htaccess file.
Important: Back up your PHP.ini file before editing it. This allows you to quickly restore the file, as incorrect settings can cause performance issues or downtime.
For PHP CGI, edit the INI file directly using cPanel’s MultiPHP INI Editor to configure the settings.
If you’re using hPanel, simply download and edit the file with a text editor.
For cPanel users, here are the steps:
Access your cPanel.
Go to the Software section → MultiPHP INI Editor.
Click on the Basic Mode tab. For deeper customization, select Editor Mode instead.
From the drop-down menu, select the home directory or domain path. This will configure your domain-specific PHP.ini file.
Change the settings for each PHP directive and click Apply to save the changes.
If your server does not support the editor tool, use the PHP Selector instead.
Alternatively, download the PHP.ini file and change the configuration directives using a text editor.
For VPS users, configure PHP settings through an SSH client. Ensure you’ve installed the Nano text editor and run this command:
bash
nano usr/local/lib/php.ini
Change the directory to the path of your PHP.ini file on your VPS.
🏁 Conclusion
PHP.ini is a configuration file that stores your web server’s PHP settings. You can edit it to change your site’s PHP-related rules, such as restricting actions or limiting the size of uploaded data.
Since its location varies, create a PHP info file in your domain’s root directory and load it in your browser to quickly find the PHP.ini file. In the PHP info, the location is in the Loaded Configuration File section.
The PHP.ini file contains several parameters for configuring error handling, file uploads, session settings, and other rules.
Best Regards,
João @PixelHost.
Comments