PS:这篇 blog 是我第一次尝试安装 nginx 时候写的,有不少配置不合理的地方,更好的配置请参考这篇文章。
由于最近谷奥的负载实在比较大,究其根本除去 WordPress 本身程序的问题外,很大一部分原因在于 Apache 的低效。虽然谷奥更换了更强劲的 Xeon x3370 CPU,但是还是未雨绸缪试了试传说中可以承受 Apache 10 倍负载的 Nginx,安装环境为 运行 Debian Lenny 的 VPS。
1,安装 Nginx:
aptitude install nginx
搞定了。。。打开 80 端口看一下吧,应该已经 Welcome to nginx 了。
2,安装 php5:
aptitude install php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
如果你还需要什么变态模块的话尽管往上加好了
修改 php.ini
vi /etc/php5/cgi/php.ini
在最后加入一行
cgi.fix_pathinfo = 1
接下来要让 php-cgi 以一个 daemon 形式运行,基本有以下几个方法:
- php-cgi 本身使用 -b 参数
- lighthttpd 自带的 spawn-fcgi
- Python 脚本 supervisord
- PHP-FPM(Fast Process Manager),很多高负载服务器上使用的是这个,即将进入 PHP 的 Trunk。
这里有 php-cgi, spawn-fcgi 和 PHP-FPM 的一些细节比较。
这里我们使用 spawn-fcgi。
借用 lighthttpd:
aptitude install lighttpd
安装的最后启动 lighthttpd 的时候会提示你启动失败,80 端口已经被占用,这是正常的,因为 nginx 正在那里跑着,所以不用管它。安装完成后将 lighthttpd 从 rc 里面删除
update-rc.d -f lighttpd remove
我们可以用如下命令启动 spawn-fcgi
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid
可以将其加入 rc.local,以后每次开机自动启动
编辑一下 nginx 默认虚拟主机的配置文件,加入 php 支持
vi /etc/nginx/sites-available/default
修改成
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } }
重新启动 ngingx
/etc/init.d/nginx restart
写个 phpinfo() 放到 /var/www/nginx-default/ 里面看看吧,应该已经可以显示了。
参考:
我的做法是用nginx做反向proxy, php分流给apache, 其它静态内容由nginx处理. 相对简单, 效果也很好.
http://raynix.info/archives/803
我看默认的配置文件好像就是这样的,也有好多人推荐,但是问题在于 wordpress 这变态玩意根本就没啥静态页面。。。囧
Pingback: nginx / IPv6 在 Linux 下访客 IP 问题的解决 | gkp's post