What is PHP Scripting?
PHP is a scripting language used primarily used to run web publishing software like WordPress, Joomla etc. It is available by default on shared, VPS, and Dedicated hosting servers for a majority of the web hosts. This article will guide you on how to change PHP settings using .htaccess & php.ini file. This guide is very much useful to all the users using PHP to run their website, especially those who are running WordPress.
If you are serious about your website, we recommend you to purchase a good web hosting service with updated PHP support from any of the service providers listed below:
- Bluehost
- SiteGround
- Hostinger
- GoDaddy
- Interserver
- A2 Hosting
- iPage
- InMotion
You may purchase an account from any hosting of your choice. But these hosting service providers are having very good customer support and uptime even for shared hosting.
PHP is extremely customizable and has a lot of settings that you can modify yourself. This article will guide you on how to change default PHP settings using the .htaccess or php.ini file depending on the mode of PHP your server is running on.
How to View Current PHP Settings?
PHP can run in two different modes in the server: Apache Module & CGI Module. Before trying to change your local PHP settings you must know in which mode it is running because they follow different approaches to change the values. To view this, you must create a phpinfo page and access it. If you are not sure how create a phpinfo page, please see our 5 minutes guide on: How to view PHP settings of Your Web Server?
So we know that a Server can run on any of the below PHP Setup:
- Apache Module
- CGI Module
Now the question is that how you can find which more your PHP enabled server is running.
To do that just access the phpinfo page you have created (refer to the above guide) and Search for “Server API”. When you find “Server API” just check what value it has.
If the “Server API” shows the value as “Apache” then you server is running the PHP as “Apache Module”. Refer the below image:
If the “Server API” shows the value as “CGI” then your Server is running the PHP as “CGI Module”. Refer to the below image:
Now when you know the PHP Module your Server is running on, proceed to the relevant sections accordingly.
[Note: If you change the PHP version through the “Select PHP Version” button in cPanel, then the value of Server API can also change. Typically Native PHP version runs as the Apache Module, but If you change it to any other version it will run as CGI Module. So check the “Server API” before proceeding with any configuration change]
How to Change PHP Settings in Server API - Apache Module:
If your server is running the PHP as an "Apache Module", you can change the local PHP settings via the .htaccess file. A .htaccess file is a configuration file that you can create and use to change a lot of server settings, including local PHP settings. If you are using WordPress then you don't need to create one, as your master .htaccess file is already created by WordPress in the root directory.
[If you are not able to find .htaccess file then follow our guide How to Fix the .htaccess File Missing Problem in WordPress?]
The basic syntax you can use to change PHP settings through the .htaccess file is as follows:
php_value directive value
For example, if you wanted to change the max_execution_time from 20 seconds to 30 seconds, add the following code to your .htaccess file:
php_value max_execution_time 30
[Caution: Be sure to take a backup of your .htaccess file before making any change. .htaccess file is very critical for WordPress and any unauthorized change can break your site completely]
After editing the .htaccess and saving it, refresh your PHP info page, and you would see that the local value of that PHP settings has changed. Refer to the below image:
If you receive a 500 Server Error then double check the syntax, directive, and value you have used for any potential spelling errors. Also, double check the Server API if it is running in Apache Module. If nothing works then restore the .htaccess file from backup and start following this tutorial from beginning.
If you are not using WordPress and don't have a .htaccess file in the root directory, you can simply create a blank new file named .htaccess. As the name of .htaccess file begins with a dot (.), it is considered as a hidden file by majority of FTP clients or File Managers. If the file does not show up, then consider to selecting the option to "Show Hidden Files" option to make it visible.
How to Change PHP Settings in Server API - CGI Module:
If your server is running the PHP as a CGI module, you can change your local PHP settings by creating or modifying a php.ini file in the root folder.
A php.ini is a PHP configuration file which lists a lot of PHP directives/variables and their values. In this example, we will modify the same max_execution_time value.
The syntax to change the PHP settings through php.ini is as follows:
Directive = value
So the syntax to change the value of max_execution_time from 20 to 30 is to be written as:
max_execution_time = 30
The process is just to create or modify an existing php.ini and write the desired line into the file. Then save the file. The change reflects immediately, and if you refresh the phpinfo page you will see that the local value has changed.
Common Problems While Changing PHP Settings with php.ini:
Running PHP as a CGI module is a solution most of the web hosts provide, to enable you to choose your own PHP Version. But as you are not running the native server level PHP, it has some limitations and problems associated with it. Some of the most common problems associated with CGI module and their solution are given below:
I don't have a php.ini file! What Should I do?
Only users running PHP as CGI Module would have a php.ini file, if you are running PHP as Apache Module then php.ini is not required. First, check your Server API to know the PHP module you are using.
It is also a fairly common problem while you have changed the default server PHP version by using the “Select PHP Version” option of cPanel. In either case, there would be no php.ini file in your root folder.
In that case, you just need to create a php.ini file manually (using any text editor) and place that in the servers default folder (public_html or htdocs whatever is available).
I made changes to the php.ini file in root directory, but I do not see the changes!
It is also a very common problem, and the cause of this problem is that the php.ini file is not recursive by default. It only changes the PHP settings on the local folder.
Changing the value within the php.ini stored in "public_html/php.ini" or "htdocs/php.ini" will not affect PHP files in subfolders of them. For example, it will not affect the PHP settings of "public_html/finance" or "htdocs/finance". A solution to this issue is to make your php.ini file recursive so that it affects all subfolders.
To make the php.ini file of the root directory recursively affects all subfolders, you must put the below command in the .htaccess file:
suPHP_ConfigPath /home/username/public_html
Here you must replace the username with the one you use to login to your cPanel.
[Caution: Be sure to take a backup of your .htaccess file before making any change. .htaccess file is very critical for WordPress and any unauthorized change can break your site completely]
We hope that you have enjoyed the above article about How to Change PHP settings using .htaccess & php.ini. Be with us to explore free training on Leading Technologies and Certifications.
Leave us some comments if you have any question or facing issues while changing PHP settings.
If you like our articles please like our facebook and twitter page to receive notifications on recent and updated contents.
I have created a small plugin which helps users change the php settings directly from the WordPress administration pages: https://wordpress.org/plugins/custom-php-settings/