Home >Backend Development >PHP Tutorial >The key to improving website performance: PHP-FPM optimization practical guide

The key to improving website performance: PHP-FPM optimization practical guide

王林
王林Original
2023-10-05 09:28:411075browse

The key to improving website performance: PHP-FPM optimization practical guide

The key to improving website performance: PHP-FPM optimization practical guide

With the rapid development of the Internet, websites play an increasingly important role. For website operators, improving website performance is crucial, not only to improve user experience, but also to improve search engine rankings. PHP-FPM (FastCGI Process Manager), as the process manager for PHP running, plays a vital role in improving website performance. This article will provide you with a practical guide to PHP-FPM optimization, including specific code examples.

1. Install and configure PHP-FPM

First, you need to make sure that PHP is installed on the server. You can check it with the following command:

php -v

If PHP is not installed, please use the following command to install it (taking CentOS as an example):

yum install php

After the installation is completed, you need to modify the php.ini file, Enable PHP-FPM support. Find the following two lines of code and modify them:

cgi.fix_pathinfo=0
; FastCGI dynamic process spawning
; Set to 0 if you're not having permission errors when running PHP as a CGI.
; http://php.net/cgi.fix-pathinfo

Modify the first line of code to:

cgi.fix_pathinfo=1

Modify the second line of code to:

;cgi.fix_pathinfo=0

Save the changes and restart Start the PHP-FPM service for the modifications to take effect.

service php-fpm restart

2. Adjust the configuration parameters of PHP-FPM

Before optimizing PHP-FPM, we need to first understand some important parameters of PHP-FPM:

  • pm.max_children: Specifies the maximum number of processes, which is the maximum number of PHP processes that PHP-FPM can create. When this value is exceeded, new connections will be queued.
  • pm.start_servers: Specify the number of PHP processes created at startup.
  • pm.min_spare_servers: Specify the minimum number of idle processes that PHP-FPM needs to maintain.
  • pm.max_spare_servers: Specify the maximum number of idle processes that PHP-FPM needs to maintain.
  • pm.max_requests: Specifies the maximum number of requests processed by each PHP process. After exceeding this value, the process will be restarted.

According to the server configuration and website traffic, these parameters can be adjusted appropriately to improve the performance of PHP-FPM. For example, if the server configuration is low, you can set pm.max_children to a smaller value, such as 20. If the number of visits to the website is not high, you can set pm.min_spare_servers and pm.max_spare_servers to smaller values, such as 5.

You can modify these parameters by editing the php-fpm.conf file:

vi /etc/php-fpm.conf

Find the following lines of code to modify:

pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pm.max_requests = 500

After saving the modifications, restart PHP- FPM service for modifications to take effect.

service php-fpm restart

3. Enable PHP’s OPcache extension

OPcache is an accelerator introduced after PHP version 5.5. It can cache compiled PHP scripts into memory to reduce the time of repeated compilation. . The OPcache extension can be enabled by following these steps:

  1. Open the php.ini file:
vi /etc/php.ini
  1. Find the following code:
;zend_extension = <path_to_opcache.so>
  1. Remove the preceding ";" and replace "" with the specific OPcache extension path, usually /usr/lib64/php/modules/opcache.so.
  2. After saving the changes, restart the PHP-FPM service to make OPcache take effect.
service php-fpm restart

4. Turn on the Slow Log function of PHP-FPM

The Slow Log function of PHP-FPM can record requests whose execution time exceeds the specified threshold into a log file for subsequent use Analysis and optimization. You can enable the Slow Log function through the following steps:

  1. Open the php-fpm.conf file:
vi /etc/php-fpm.conf
  1. Find the following code:
;slowlog = /var/log/php-fpm/www-slow.log
;request_slowlog_timeout = 0
  1. Remove the preceding ";" and modify "request_slowlog_timeout" to the required threshold, in seconds. For example, modifying it to 1 means that requests that take more than 1 second to execute will be logged.
  2. After saving the modification, restart the PHP-FPM service to make the Slow Log function take effect.
service php-fpm restart

5. Use reverse proxy servers such as Nginx

Using reverse proxy servers such as Nginx can forward static resource requests to Nginx for processing, thereby reducing the complexity of PHP-FPM load and improve website performance.

In the Nginx configuration file, you can forward the request for static resources to Nginx through the following code:

location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
    proxy_pass http://yourdomain.com;
    proxy_set_header Host $host;
}

The above code will jpg, jpeg, png, gif, ico, css, js Requests for other suffixes are forwarded to http://yourdomain.com for processing.

6. Use the caching mechanism

In the business logic of the website, the caching mechanism can be used to reduce database access and the generation of dynamic pages, thereby improving the performance of the website. This can be achieved using caching technologies such as Redis and Memcached.

You can use the Redis cache through the following code example:

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$key = 'user_info_' . $user_id;
if ($redis->exists($key)) {
    $user_info = $redis->get($key);
} else {
    $user_info = getUserInfoFromDatabase($user_id);
    $redis->setex($key, 3600, $user_info);
}

The above code first checks whether the user information exists in the cache. If it exists, it is obtained directly from the cache; if it does not exist, it is obtained from the database. Obtain user information from , store it in the cache, and set the expiration time to 3600 seconds.

Summary:

By optimizing PHP-FPM, we can improve the performance of the website and enhance the user experience. This article provides a practical guide to PHP-FPM optimization and gives specific code examples for your reference. In actual applications, it can also be adjusted and optimized according to specific needs to achieve the best performance.

The above is the detailed content of The key to improving website performance: PHP-FPM optimization practical guide. 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