PHPMatters Help You Better Hosting Your PHP-based Sites
How to Change PHP Settings with Apache and CGI

How to Change PHP Settings with Apache and CGI

PHP is one of the most popular scripting languages that are used in developing websites. It is open source, and you can customize it by changing the PHP settings by yourself. In general, there are 2 ways to run PHP on Apache. One way is to run PHP as an Apache module, and the other way is to run it as a CGI module. No matter which module the PHP is running as on your server, you should need to claim some changes to your PHP settings to make it more customizable. Thus, we have written this post to offer you some references about configuring your PHP settings with Apache module and CGI module.

How about Apache Module and CGI Module

cgi and apacheBefore learning how to change PHP settings with Apache and CGI, we would like to introduce you something about Apache module and CGI module. Actually, each of the 2 modes has its strong points.

On the one hand, Apache module is a little faster and more stable than CGI module in managing the requests and resources. On the other hand, CGI module is of more security, because under this mode, the server can exert some restrictions on the access to the binaries. That is to say, when PHP runs as a CGI module, nobody but you and your PHP scripts can read your database passwords that have been placed in a certain file.

You may wonder how you can find out whether your PHP runs as an Apache module or a CGI module. As a matter of fact, it is quite an easy task to see the PHP settings. You can view the “Server API” value using a PHP info page, which can be created with File Manager without effort. When the “Server API” lists “Apache”, then you are running PHP as an Apache module. However, if the value of “Server API” is “CGI”, then a CGI module is configured.

Generally speaking, to update the PHP settings with Apache module, you can use .htaccess file, but if PHP is running as a CGI module, then you can change the PHP settings using php.ini file.

Change PHP Settings with Apache Module

apacheAs stated above, when PHP is running as Apache module, .htaccess file is needed to change the PHP settings. Let’s learn something about .htaccess file first. htaccess is the abbreviation of Hypertext Access. This file is a configuration file that is supported by several web servers that are based on Apache. Usually, files that begin with a dot (.) such as .htaccess file are hidden files. To see your .htaccess file, you should open the File Manager from your control panel like cPanel, and choose Show Hidden Files (dotfiles). However, there is a possibility that you are not allowed to edit the .htaccess file by your web host. In this case, you can create a file and name it .htaccess.

By editing the .htaccess file, you can modify most of your PHP settings, such as memory_limit, register_globals, max_post_size, max_input_time and many more settings. To declare changes to these settings with Apache module, you should use the basic syntax below in your .htaccess file:

change PHP settings in Apache

For instance, if you want to modify the memory_limit from 60M to 120M, then the following need to be used in your .htaccess file:

change PHP settings in Apache example

To put the changes that have been made into effect, you need to refresh the PHP info page.

Change PHP Settings with CGI Module

apacheTo modify PHP settings when your server is running PHP as a CGI module, you need to edit the php.ini file. Similarly, let’s have a basic understanding of php.ini file in the very beginning. php.ini file is a PHP configuration file that can be used to declare some specific changes to PHP, such as modifying the max_execution_time value, max_input_time value, memory_limit value, and many more values. Sometimes, php.ini file may not exist in your account. In this case, you can ask one from your hosting company or simply create a new php.ini file by yourself.

Again, let’s take the memory_limit value as an example. You can open your File Manager and search for publich_html, and then find the php.ini file. After that, you should search for memory_limit and then you can see the following:

memory limit coding

If you want to configure memory_limit value from 60M to 120M, you can change 60M to 120M directly. After this, you should save the file. Remember to refresh your PHP info page, because only in this way the changes you have declared can be seen.