备忘:在Slicehost VPS上安装Nginx+Fastcgi+eAccelerator+Zend Optimizer
Sunday, January 27th, 2008使用Slicehost的VPS(256M Slice)有一个月了,但相对国外的共享虚拟主机(比如我此前所用的Dreamhost)并没有感觉到速度上的优势,除了Wordpress本身性能较差外,恐怕与我并未对VPS做性能优化有关,因为Slicehost的VPS主机给了用户完全的操作权限,这意味着你可以随心所欲地安装软件、更改设置,但同时你也不再享有服务商事先做好各种的系统优化,比如安装Zend Optimizer--现在这些事情必须由你自己来完成。所以,作为一个半吊子Linux用户,我只好借助Google边查边干,所幸,整个过程还算顺利,目前在我的VPS上安装了Nginx、Fastcgi、eAccelerator和Zend Optimizer,Nginx+Fastcgi用以替换Apache,Zend Optimizer用来加速PHP性能,eAccelerator则利用缓存提高站点性能,经过这番优化,网站速度的确比以前大有改观,此文是整个安装过程的备忘,由于我选用的Linux发行版为Debian ETCH,所以在其他Linux上可能会有些不同,应酌情调整。
在安装Nginx之前,先装好Fastcgi:
Nginx是俄罗斯出品的一款Web服务器软件,相比Apache更加小巧,占用资源更少,速度更快,在我这台只有256M内存的VPS上,显然Nginx更适合.
- 安装fastcgi
- 下载Fastcgi的执行脚本:
- 给php-fastcgi加上执行权限
- 下载fastcgi的配置文件:
- 将php-fastcgi这个执行脚本添加的系统的启动服务中:
- 现在尝试启动fastcgi:
- 正常情况下,fastcgi应该成功启动了,可以用ps -aux查看是否有php-cgi进程,如果你更改了php.ini,必须重启fastcgi。
sudo apt-get install php5-cgi libfcgi0c2
cd /etc/init.d/
sudo wget -O php-fastcgi http://techpush.com/wp-content/uploads/php-fastcgiinit.txt #php-fastcgi也可改为其他名字
sudo chmod +x php-fastcgi
cd /etc/default/
sudo wget -O php-fastcgi http://techpush.com/wp-content/uploads/php-fastcgidefault.txt
sudo update-rc.d php-fastcgi defaults 19 21
sudo invoke-rc.d php-fastcgi start(或者sudo /etc/init.d/php-fastcgi start)
注意: 最好将php.ini中的memory_limit调大,否则可能会遇到内存不够的情况。
安装Nginx:
Debian或者Ubuntu的源中有nginx,但版本比较老了,所以还是从官方网站上下载最新版本安装,当然这需要编译才行
- 安装编译nginx所需的依赖环境:
- 下载最新版本的nginx(当前最新稳定版为0.5.35):
- 编译安装:
- 安装nginx的执行脚本:
- 使nginx支持fastcgi:
- 修改nginx的配置:
- 配置完毕后,启动nginx
sudo apt-get install zlib1g-dev libpcre3-dev libssl-dev libgcrypt11-dev
cd ~/src
wget -c http://sysoev.ru/nginx/nginx-0.5.35.tar.gz
tar zxvf nginx-0.5.35.tar.gz
./configure –sbin-path=/usr/sbin/nginx –conf-path=/etc/nginx/nginx.conf –pid-path=/var/run/nginx.pid –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –user=www-data –group=www-data –with-http_ssl_module –with-http_stub_status_module –with-http_realip_module –with-http_addition_module –with-http_sub_module –with-http_dav_module –with-http_flv_module –http-client-body-temp-path=/var/cache/nginx/client_body_temp –http-proxy-temp-path=/var/cache/nginx/proxy_temp –http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp –with-sha1=/usr/lib #此处的各项配置可根据你自己的实际需要来确定
make
sudo make install
sudo mkdir /var/cache/nginx
sudo chown www-data:www-data /var/cache/nginx
cd /etc/init.d/
sudo wget -O nginx http://techpush.com/wp-content/uploads/nginxinit.txt
sudo chmod +x nginx
sudo update-rc.d nginx defaults
cd /etc/nginx
sudo wget -O fastcgi_params http://techpush.com/wp-content/uploads/fastcgi_params.txt
如果你是严格按照上述流程操作的,那么nginx的配置文件应该在/etc/nginx/目录下,使用一个文本编辑器编辑nginx.conf
sudo vi nginx.conf
具体的配置方法可以参考官方wiki,或者我的配置示例文件
这里需要做说明的有两点:
虚拟主机的配置:
server {
listen 80;
server_name www.domain1.com;
rewrite ^/(.*) http://domain1.com permanent;}
server {
listen 80;
server_name domain1.com;access_log /home/demo/public_html/domain1.com/logs/access.log;
error_log /home/demo/public_html/domain1.com/logs/error.log;location / {
root /home/demo/public_html/domain1.com/public/;
index index.html index.php;}
}
由于nginx不支持apache的rewrite规则(.htaccess),因此对于Wordpress用户,如果想使用简洁的永久链接格式,需要在虚拟主机配置段中加入如下语句:
location / {
index index.php index.html;
if (!-e $request_filename) {
rewrite ^/(.+)$ /blog/index.php?q=$1 last;
}
}
sudo invoke-rc.d nginx start
下次更改nginx的配置,可以不用重启nginx而使配置生效(这点比apache强):
sudo kill -HUP `cat /var/run/nginx.pid`
安装Zend Optimizer
安装Zend Optimizer最为简单,到Zend官方网站下载相应CPU的版本,解压后运行./install.sh(如果你是通过远程终端安装,则运行./install-tty)按照屏幕提示操作即可,注意php.ini文件的位置在/etc/php5/cgi/目录中。
安装eAccelerator
在http://bart.eaccelerator.net/source/获取源代码,最新版本为0.9.5.2
-
编译eAccelerator需要autoconf,automake,没有的话自己先安装一下
- 编译安装:
- 完成后,手动编辑php.ini文件,在最后加上如下语句:
- 重启fastcgi:
- 对于wordpress用户,可以开启wordpress自己的cache功能提高性能:
phpize
./configure
make
sudo make install
zend_extension=”/usr/lib/php5/20060613/eaccelerator.so”
eaccelerator.shm_size=”16″
eaccelerator.cache_dir=”/tmp/eaccelerator”
eaccelerator.enable=”1″
eaccelerator.optimizer=”1″
eaccelerator.check_mtime=”1″
eaccelerator.debug=”0″
eaccelerator.filter=”"
eaccelerator.shm_max=”0″
eaccelerator.shm_ttl=”0″
eaccelerator.shm_prune_period=”0″
eaccelerator.shm_only=”0″
eaccelerator.compress=”1″
eaccelerator.compress_level=”9″
注意eaccelerator.so文件的位置要写对,另外要建立eaccelerator目录:
sudo mkdir /tmp/eaccelerator
sudo chmod 0777 /tmp/eaccelerator
sudo invoke-rc.d php-fastcgi restart
编辑wp-config.php,加上一句:
// Enable the WordPress Object Cache:
define(ENABLE_CACHE, true);
在wp-content目录下建立cache文件夹并赋予777权限
mkdir ./wp-content/cache
chmod 0777 ./wp-content/cache
至此大功告成!
本文参考了:
nginx + PHP + FastCGI:轻型网页伺服器« 豆腐!
eAccelerator的安装和性能比较
wordpress在nginx下的rewrite设置







