在CentOS上提升PHP的并发处理能力,可以通过以下几个方面来实现:
max_execution_time:减少脚本的最大执行时间,避免长时间运行的脚本占用资源。max_execution_time = 30
memory_limit:根据需要适当增加内存限制。memory_limit = 256M
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
sudo yum install php-fpm
/etc/php-fpm.d/www.conf文件,调整以下参数:pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
mod_proxy和mod_proxy_fcgi模块。<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ProxyPass / fcgi://127.0.0.1:9000/
ProxyPassReverse / fcgi://127.0.0.1:9000/
</VirtualHost>
$memcached = new Memcached();
$memcached->addServer('127.0.0.1', 11211);
$data = $memcached->get('key');
if (!$data) {
$data = fetchDataFromDatabase();
$memcached->set('key', $data, 3600); // 缓存1小时
}
通过以上这些方法,可以显著提升CentOS上PHP的并发处理能力。根据具体应用场景和需求,选择合适的优化策略。