How to use PHP-FPM optimization to improve the performance of Symfony applications
Introduction:
Symfony is a popular PHP framework that is widely adopted by many developers. However, the performance of Symfony applications may be affected under high traffic and large concurrent requests. PHP-FPM is a common solution for managing PHP processes, providing better performance and scalability. This article will introduce how to use PHP-FPM to optimize and improve the performance of Symfony applications, and provide specific code examples.
1. Install and configure PHP-FPM
First, make sure the PHP-FPM extension is installed. On Ubuntu, you can install it with the following command:
sudo apt-get install php-fpm
After the installation is complete, some configuration adjustments are required. Locate the php-fpm.conf file, usually located at /etc/php/7.4/fpm/php-fpm.conf. Find the following configuration key in the file and modify it to the appropriate value for your server:
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm. max_spare_servers = 15
These values control the size of the PHP-FPM process pool. Depending on the server's configuration and resources, adjustments can be made as needed.
2. Configure the Symfony application
Next, you need to make some adjustments to the Symfony application to work properly with PHP-FPM.
location ~ ^/index.php(/|$) {
fastcgi_split_path_info ^(. .php)(/.*)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off ;
}
This will ensure that the request is passed to PHP-FPM correctly.
framework:
cache:
pools: app.cache.php_fpm: adapter: cache.app.php_filesystem default_lifetime: 0 reset_lifetime: 0 directory: '%kernel.cache_dir%/php_fpm'
This will create a separate cache directory File system pool to store cached data for Symfony applications.
3. Testing and Optimization
After completing the above configuration, you can start testing and optimizing the performance of the Symfony application.
ab -n 1000 -c 100 http://example.com/
This will send 1000 requests, each with 100 concurrent request. Based on the results obtained, the performance of the application can be evaluated.
$em = $this->getDoctrine()->getManager();
$em->getConfiguration()->setResultCacheImpl($this-> container->get('cache.app.php_fpm'));
$em->createQueryBuilder()
->select('p')
->from('AppBundle: Post', 'p')
->where('p.published = :published')
->setParameter('published', true)
->getQuery()
->useResultCache(true)
->getResult();
Using query cache can avoid repeated queries to the database and improve response speed.
opcache.enable=1
opcache.enable_cli=1
Make sure these values are set to 1 to enable OPcache.
Conclusion:
By installing and configuring PHP-FPM, optimizing the caching mechanism of Symfony applications, and performing performance testing and optimization, the performance of Symfony applications can be significantly improved. Performance can be further improved by judicious use of the database query cache and PHP's OPcache. Depending on the server configuration and resource conditions, some parameters may need to be adjusted to suit the needs of Symfony applications.
The above is the detailed content of How to use PHP-FPM optimization to improve the performance of Symfony applications. For more information, please follow other related articles on the PHP Chinese website!