Home  >  Article  >  Backend Development  >  How to modify php.ini parameters

How to modify php.ini parameters

WBOY
WBOYOriginal
2023-05-07 14:47:101034browse

With the rapid development of Internet technology, PHP language, as a language widely used in Web development, is increasingly used. In the process of developing web applications using PHP language, in order to meet specific needs, we sometimes need to modify some parameters in the php.ini file. This article will introduce how to modify php.ini parameters.

1. Locate the php.ini file

The php.ini file is the PHP configuration file. It contains a series of configuration information for the PHP application, such as uploaded file size, memory limit, and time zone. Etc., this information will affect the performance and security of PHP applications. Therefore, before modifying the php.ini parameters, we need to first understand where the php.ini file is.

In Linux systems, you can find the location of the php.ini file by entering the command "php --ini" in the terminal. This command will output the path of php.ini and other loaded INI files, as shown in the following figure:

Configuration File (php.ini) Path: /usr/local/etc/php/7.4
Loaded Configuration File:         /usr/local/etc/php/7.4/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.4/conf.d
Additional .ini files parsed:      /usr/local/etc/php/7.4/conf.d/docker-php-ext-bcmath.ini,
/usr/local/etc/php/7.4/conf.d/docker-php-ext-opcache.ini,
/usr/local/etc/php/7.4/conf.d/docker-php-ext-pdo_mysql.ini,
/usr/local/etc/php/7.4/conf.d/docker-php-ext-redis.ini,
/usr/local/etc/php/7.4/conf.d/docker-php-ext-sodium.ini,
/usr/local/etc/php/7.4/conf.d/docker-php-ext-xdebug.ini,
/usr/local/etc/php/7.4/conf.d/docker-php-ext-zip.ini

In Windows systems, the php.ini file is usually located in the "php.ini" under the PHP installation directory. ” file, you can also see the path to php.ini in the output information of the phpinfo() function.

2. Back up the original php.ini file

Before starting to modify the php.ini file, it is recommended that you back up the php.ini file first to avoid incorrect modifications that may affect the performance or security of the PHP application. Problems with sex. There are two ways to back up the php.ini file:

  1. Copy the php.ini file, name the copied file "php.ini.bak" or another name you like, and save it in the same directory Just download it;
  2. Enter the following command in the command line to back up the original php.ini file as "php.ini.bak":

    cp /etc/php.ini /etc/php.ini.bak

    After the backup is completed, You can start modifying the php.ini file.

3. Modify the php.ini parameters

The parameters in the php.ini file are expressed in the format of "parameter name = parameter value". The type of parameter value and the specific Depends on the parameters, some parameters need to be filled in numbers, and some parameters need to be filled in strings.

  1. Modify the upload file size

When we want to upload large files, we often encounter the problem of exceeding the server upload file size limit. In order to solve this problem, we can increase the server upload file size limit by modifying the "upload_max_filesize" parameter and "post_max_size" parameter in the php.ini file.

In the php.ini file, we search for the "upload_max_filesize" and "post_max_size" keywords, find the corresponding parameter lines, and modify the parameter values. As shown below:

upload_max_filesize = 100M
post_max_size = 100M

The above parameters are set to 100MB. It should be noted that when modifying these parameters, they should be set to values ​​with reasonable sizes. Otherwise, values ​​that are too large or too small may cause the upload file function to fail or other problems.

  1. Modify memory limit

When a PHP script needs to process a large amount of data, insufficient memory may occur. To solve this problem, we can increase the memory limit of the PHP script to ensure that the PHP script can run properly.

In the php.ini file, we search for the "memory_limit" keyword, find the corresponding parameter line, and modify the parameter value. As shown below:

memory_limit = 512M

The above parameters are set to 512MB. It should be noted that setting the memory limit too high may strain the server, so set it according to the actual situation.

  1. Modify time zone

The processing and display of timestamps in PHP applications is time zone related. If the PHP application is deployed and maintained in different regions, issues with incorrect timestamps may occur. The solution to this problem is to modify the time zone of the PHP application.

In the php.ini file, we search for the "date.timezone" keyword, find the corresponding parameter line, and modify the parameter value. As shown below:

date.timezone = Asia/Shanghai

The above parameters are set to "Asia/Shanghai" to represent the Shanghai time zone. It should be noted that in order to ensure that the timestamp is accurate, the time zone must be set correctly.

4. Restart the Web server

After modifying the php.ini file, we need to restart the Web server to make the new settings take effect. Under Windows systems, you can double-click the php.ini file to restart; under Linux systems, you can restart through the following command.

Apache server restart command:

sudo systemctl restart apache2

Nginx server restart command:

sudo systemctl restart nginx

Summary

Modifying php.ini parameters is important in web application optimization In the first step, by modifying the parameters in the php.ini file, you can adjust the running environment of the PHP application to improve the performance and security of the application. However, it should be noted that you should be cautious when modifying the configuration file to avoid unexpected problems. This article briefly introduces three common parameters for modifying php.ini, hoping to help PHP developers better configure and optimize the PHP environment.

The above is the detailed content of How to modify php.ini parameters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn