在Ubuntu上提升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
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
use Swoole\Http\Server;
$server = new Server("127.0.0.1", 9501);
$server->on("Request", function ($request, $response) {
$response->end("Hello World\n");
});
$server->start();
$mysqli = new mysqli('p:localhost', 'user', 'password', 'database');
$memcached = new Memcached();
$memcached->addServer('127.0.0.1', 11211);
$data = $memcached->get('key');
if (!$data) {
$data = fetchDataFromDatabase();
$memcached->set('key', $data, 3600);
}
server {
listen 80;
server_name example.com;
location / {
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 PATH_INFO $fastcgi_path_info;
}
}
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost:9000
</VirtualHost>
通过以上方法,可以显著提升PHP在Ubuntu上的并发处理能力。根据具体应用场景和需求,选择合适的优化策略。