星期二, 九月 18, 2012

用nginx/php-fpm/postgresql在低配VPS上建站

前贴减少小内存低配VPS上CentOS 6.2系统内存使用尝试优化LAMP服务器的内存使用,事实上,由于设计原因,apache和mysql都是耗内存大户,再优化在256MB以下的VPS上运行也不会很宽松。所以本篇尝试用nginx/php-fpm/postgresql的组合在低配VPS上建站。这里使用Debian 6系统。

以下操作需要root权限,有底纹的文字为代码或者配置文件内容
1. 添加必要的源
vi /etc/apt/sources.list
加入以下内容
deb http://packages.dotdeb.org stable all
导入dotdeb源密钥
wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | sudo apt-key add -
rm dotdeb.gpg
调整源优先级,减小第三方源对系统稳定性的影响(参考:调整第三方软件源优先级减小对系统影响
cat > /etc/apt/preferences.d/dotdeb-org << EOF
Package: *
Pin: origin packages.dotdeb.org
Pin-Priority: 200
EOF
为了安装dotdeb上的php-fpm及其依赖包,需要以下命令为php相关软件包调整优先级,参考:
http://howto.biapy.com/en/debian-gnu-linux/system/software/setup-the-dotdeb-apt-repository-on-debian
#获得php相关包列表
PACKAGES=$(command wget "http://packages.dotdeb.org/dists/squeeze/php5/binary-$(command dpkg --print-architecture)" \
    --quiet --output-document=- \
    | command grep "href=" | command grep -v "h1" | command grep -v "\.\./" \
    | command sed -e 's/^[^>]*>\([^_]*\)_.*$/\1/' | command tr "\n" " ")
PECL_PACKAGES=$(command wget "http://packages.dotdeb.org/dists/squeeze/php5-pecl/binary-$(command dpkg --print-architecture)" \
    --quiet --output-document=- \
    | command grep "href=" | command grep -v "h1" | command grep -v "\.\./" \
    | command sed -e 's/^[^>]*>\([^_]*\)_.*$/\1/' | command tr "\n" " ")
#设置php相关包优先级
command echo "Package: ${PACKAGES} \\
    ${PECL_PACKAGES}
Pin: origin packages.dotdeb.org
Pin-Priority: 500" \
    > '/etc/apt/preferences.d/dotdeb-org-php5'
更新源
apt-get update
apt-get upgrade

2. 安装需要的软件包
apt-get install nginx php5 php5-apc php5-fpm php5-pgsql postgresql

3. 配置nginx
编辑/etc/nginx/nginx.conf,调整性能和安全性
#设置工作进程数,根据cpu数或者内核数调整(可以用cat /proc/cpuinfo看到)
worker_processes 1;
#关闭nginx版本提示,提高安全性
server_tokens off;
#为除ie6以外的浏览器打开数据压缩
gzip on;
gzip_disable "msie6";
编辑/etc/nginx/sites-enabled/default为默认站点添加php支持
location ~ \.php$ {
        root /var/www;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
}

4. 配置php
编辑/etc/php5/fpm/php.ini
cgi.fix_pathinfo=0
编辑/etc/php5/fpm/pool.d/www.conf
#确定监听地址和端口
listen = 127.0.0.1:9000
#根据需要调整进程数

pm = dynamic                                     #dynamic:自动调节子进程数(建议,性能稍好),ondemand:只在需要时启动子进程(更省内存)
pm.max_children = 5
pm.start_servers = 1                           #ondemand未用
pm.min_spare_servers = 1                  #ondemand未用
pm.max_spare_servers = 1                 #ondemand未用
pm.max_requests = 500

pm.process_idle_timeout = 120s;        #dynamic未用

5. 配置postgresql,适当减小内存使用,编辑/etc/postgresql/8.4/main/postgresql.conf
shared_buffers = 8MB
根据需要同时可以修改temp_buffers, work_mem, maintenance_work_mem, max_stack_depth

6. 替换/bin/sh为dash,而不使用bash,这样可以减少shell脚本执行时的资源占用
apt-get install dash
dpkg-reconfigure dash
cd /bin
#用ll /bin/sh确认/bin/sh现在指向,如果不是dash用下面两个命令手动修改
ln -sf dash sh
ln -sf dash sh.distrib

7. 关闭mail daemon,dns server等非必要的服务,用syslog-ng替换rsyslog,其它轻量级的软件包替换原来的软件包等。

做完这些步骤后一个openvz上的debian 6的服务器运行时内存占用约为30M,如下图

没有评论: