VPS默认 装完APACHE2 后是 prefork 模式. 简单来说就是一种比较线程安全的管理模式, 每次来一个请求 都会开一个APACHE2进程来服务. mod_php 模块就是线程安全的, 但是这种模式下并不能很好的扩展, 在大量并发的情况下 内存效率就很差.
今天了解到 APACHE2 的 worker 模式, 也就是一个APACHE进程可以同时处理多个线程, 来一个请求的时候 可以把空余的线程分配给它, 这种方式效率较高 而且省内存 并发情况下好. 但是不是线程安全 所以不能使用 mod_php 模块. 需要把 PHP当成 CGI模式来调用. 简单的来说就是 APACHE 把 PHP请求让外部的程序 如 php5-fpm 来处理并取得返回结果.
查看什么模式
可以通过命令:
1 | apache2ctl -V | grep -i mpm |
apache2ctl -V | grep -i mpm
来查看是 prefork 还是 worker.
从 prefork 转成 worker
这里有一个麻烦的地方就是 PHP. 可能你会遇到这样的错误:
Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP.
需要先安装几个需要的部件:
1 2 | sudo apt-get install apache2-mpm-worker sudo apt-get install libapache2-mod-fastcgi php5-fpm |
sudo apt-get install apache2-mpm-worker sudo apt-get install libapache2-mod-fastcgi php5-fpm
停止prefork, 启用 worker:
1 2 | sudo a2dismod php5 mpm_prefork sudo a2enmod actions fastcgi alias mpm_worker |
sudo a2dismod php5 mpm_prefork sudo a2enmod actions fastcgi alias mpm_worker
配置 php5 文件
1 2 | sudo touch /usr/lib/cgi-bin/php5.fcgi sudo chown -R www-data:www-data /usr/lib/cgi-bin |
sudo touch /usr/lib/cgi-bin/php5.fcgi sudo chown -R www-data:www-data /usr/lib/cgi-bin
配置全启 PHP5 文件
1 | sudo nano /etc/apache2/conf-available/php5-fpm.conf |
sudo nano /etc/apache2/conf-available/php5-fpm.conf
然后加入以下内容:
1 2 3 4 5 6 7 8 9 | <IfModule mod_fastcgi.c> AddHandler php5.fcgi .php Action php5.fcgi /php5.fcgi Alias /php5.fcgi /usr/lib/cgi-bin/php5.fcgi FastCgiExternalServer /usr/lib/cgi-bin/php5.fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization -idle-timeout 3600 <Directory /usr/lib/cgi-bin> Require all granted </Directory> </IfModule> |
<IfModule mod_fastcgi.c> AddHandler php5.fcgi .php Action php5.fcgi /php5.fcgi Alias /php5.fcgi /usr/lib/cgi-bin/php5.fcgi FastCgiExternalServer /usr/lib/cgi-bin/php5.fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization -idle-timeout 3600 <Directory /usr/lib/cgi-bin> Require all granted </Directory> </IfModule>
启用配置:
1 | sudo a2enconf php5-fpm |
sudo a2enconf php5-fpm
重启 APACH2 服务器:
1 | sudo service apache2 restart && sudo service php5-fpm restart |
sudo service apache2 restart && sudo service php5-fpm restart
如果你这时候通过 phpinfo(); 就能发现 “FPM/FastCGI” 这个关键输出.
worker 模式配置文件
这个文件在 /etc/apache2/mods-enable/mpm-worker.conf
1 2 3 4 5 6 7 8 9 10 | <IfModule mpm_worker_module> ServerLimit 250 StartServers 10 MinSpareThreads 75 MaxSpareThreads 250 ThreadLimit 64 ThreadsPerChild 32 MaxClients 8000 MaxRequestsPerChild 10000 </IfModule> |
<IfModule mpm_worker_module> ServerLimit 250 StartServers 10 MinSpareThreads 75 MaxSpareThreads 250 ThreadLimit 64 ThreadsPerChild 32 MaxClients 8000 MaxRequestsPerChild 10000 </IfModule>
这样APACHE2就可以处理大量的并发了. 所以worker 模式是比较推荐 常用的配置, 特别是在小硬件 VPS下 更需要这样的配置, 否则内存消费很大. 当然你也可以换更轻便 为并发做优化的 nginx 或者是 lighttpd. 不过APACHE2的功能会更强大些.
更新: 后来了解到 在 apache2.4 中 event 模式已经成熟, event 模式更适合于高负载 大并发 有一个专门的线程池. 可以通过以下命令启用 event 模式.
1 2 3 | a2dismod mpm_worker a2enmod mpm_event service apache2 restart |
a2dismod mpm_worker a2enmod mpm_event service apache2 restart
配置参数语法 和 worker 一样.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # StartServers: initial number of server processes to start # MinSpareThreads: minimum number of worker threads which are kept spare # MaxSpareThreads: maximum number of worker threads which are kept spare # ThreadsPerChild: constant number of worker threads in each server process # MaxRequestWorkers: maximum number of worker threads # MaxConnectionsPerChild: maximum number of requests a server process serves StartServers 2 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxRequestWorkers 150 MaxConnectionsPerChild 0 |
# StartServers: initial number of server processes to start # MinSpareThreads: minimum number of worker threads which are kept spare # MaxSpareThreads: maximum number of worker threads which are kept spare # ThreadsPerChild: constant number of worker threads in each server process # MaxRequestWorkers: maximum number of worker threads # MaxConnectionsPerChild: maximum number of requests a server process serves StartServers 2 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxRequestWorkers 150 MaxConnectionsPerChild 0
配置完强烈推荐用 命令 apache2ctl configtest 来进行语法检查和配置设置 有可能你的设置存在一些问题.
loading...
上一篇: 英国博士毕业答辩是怎样一个过程?
下一篇: 超人气减肥茶 BOOTEA
为什么不用更好的 event 模式?
嗯, 了解到 event 和 worker 有点类似.不过网上讨论得比较多的就是 worker..所以还是随大众, 参考资料比较多.
Event 是 2.4 新出的特性,worker 讨论得比较多只是因为它比较老了.
哦, 原来是这样,
HHVM好呢, 还是APACHE+EVENT?
HHVM 相当于 PHP-FPM,即使用了 HHVM,Apache 还是要配置的,和用 worker 还是 event 没关系.
嗯 之前配置过HHVM 后来不知道什么原因就没继续用了.
已经根据你的建议改成 event.
暂时没发现任何区别….