[文章作者:张宴 本文版本:v1.0 最后修改:2008.10.28 转载请注明原文链接:http://blog.zyan.cc/post/375/]

  VPS(全称Virtual Private Server)是利用最新虚拟化技术在一台物理服务器上创建多个相互隔离的虚拟私有主机。它们以最大化的效率共享硬件、软件许可证以及管理资源。对其用户和应用程序来讲,每一个VPS平台的运行和管理都与一台独立主机完全相同,因为每一个VPS均可独立进行重启并拥有自己的root访问权限、用户、IP地址、内存、过程、文件、应用程序、系统函数库以及配置文件。

  VPS服务器最重要的指标就是内存大小,多个VPS服务器可以共享一颗CPU,但不能共享同一块内存。内存越大,价格越贵。

  下面,以我的博客所在的VPS为例,介绍在128M内存下对 Nginx 0.7.x + PHP 5.2.6(FastCGI)+ MySQL 5.1 的优化。

  至于 Nginx + PHP + MySQL 的安装配置,可参见:《Nginx 0.7.x + PHP 5.2.6(FastCGI)搭建胜过Apache十倍的Web服务器(第4版)



  优化后的效果:

  提供HTTP服务的1个Nginx进程占用11M物理内存,5个php-cgi进程每个占用8M左右物理内存,1个MySQL服务器占用7M物理内存,加上两个占用内存不大的Nginx和php-cgi父进程,Nginx + PHP + MySQL 系列总共只占用47.7%的物理内存,即62M物理内存(128M * 47.7% ≈ 62M)。

  点击在新窗口中浏览此图片

  另外,VPS服务器系统自身和其它程序也会使用一些内存,但128M内存的VPS已经够用。总体而言,经过优化后,128M内存的VPS跑 Nginx + PHP + MySQL 效果不错。当然,如果有Money购买更大内存的VPS,就更好了。



  优化项如下:

  一、增加256M的swap交换文件
  1、创建并激活swap交换文件
cd /var/
dd if=/dev/zero of=swapfile bs=1024 count=262144
/sbin/mkswap swapfile
/sbin/swapon swapfile


  2、加到fstab文件中让系统引导时自动启动
vi /etc/fstab

在末尾增加以下内容:
引用
/var/swapfile swap swap defaults 0 0

  详见:http://blog.zyan.cc/post/374.htm



  二、Nginx 0.7.19 的主配置文件(nginx.conf)优化
引用
user  www www;


#Nginx每个进程耗费10M~12M内存,这里只开启一个Nginx进程,节省内存。
worker_processes 1;

error_log  /data1/logs/nginx_error.log  crit;

pid        /usr/local/webserver/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
{
use epoll;
worker_connections 51200;
}

http
{
include       mime.types;
default_type  application/octet-stream;

#charset  gb2312;
    
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
    
sendfile on;
tcp_nopush     on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

#对网页文件、CSS、JS、XML等启动gzip压缩,减少数据传输量,提高访问速度。
gzip on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;


#limit_zone  crawler  $binary_remote_addr  10m;

server
{
   listen       80;
   server_name  blog.zyan.cc www.zyan.cc zyan.cc *.zyan.cc;
   index index.html index.htm index.php;
   root  /data0/htdocs/blog;

   #limit_conn   crawler  20;    

   #针对Bo-Blog系统的Rewrite静态化
   rewrite ^/post/([0-9]+).htm$ /read.php?$1 last;
   rewrite ^/post/([0-9]+)_([0-9]+).htm$ /read.php?$1&page=$2 last;
   rewrite ^/post/([0-9]+)_([0-9]+)_([0-9]+).htm$ /read.php?$1&page=$2&part=$3 last;
   rewrite ^/index_([0-9]+)_([0-9]+).htm$ /index.php?mode=$1&page=$2 last;
   rewrite ^/star_([0-9]+)_([0-9]+).htm$ /star.php?mode=$1&page=$2 last;
   rewrite ^/category_([0-9]+).htm$ /index.php?go=category_$1 last;
   rewrite ^/category_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=category_$1&mode=$2&page=$3 last;
   rewrite ^/archive_([0-9]+)_([0-9]+).htm$ /index.php?go=archive&cm=$1&cy=$2 last;
   rewrite ^/archive_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=archive&cm=$1&cy=$2&mode=$3&page=$4 last;
   rewrite ^/showday_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=showday_$1-$2-$3 last;
   rewrite ^/showday_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=showday_$1-$2-$3&mode=$4&page=$5 last;

   location ~ .*\.(php|php5)?$
   {
     #将Nginx与FastCGI的通信方式由TCP改为Unix Socket。TCP在高并发访问下比Unix Socket稳定,但Unix Socket速度要比TCP快。
     fastcgi_pass  unix:/tmp/php-cgi.sock;
     #fastcgi_pass  127.0.0.1:9000;

     fastcgi_index index.php;
     include fcgi.conf;
   }

   location ~ /read.php
   {
     #将Nginx与FastCGI的通信方式由TCP改为Unix Socket。TCP在高并发访问下比Unix Socket稳定,但Unix Socket速度要比TCP快。
     fastcgi_pass  unix:/tmp/php-cgi.sock;
     #fastcgi_pass  127.0.0.1:9000;

     fastcgi_index index.php;
     include fcgi.conf;
   }
  
   #博客的图片较多,更改较少,将它们在浏览器本地缓存15天,可以提高下次打开我博客的页面加载速度。
   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
   {
     expires      15d;
   }


   #博客会加载很多JavaScript、CSS,将它们在浏览器本地缓存1天,访问者在看完一篇文章或一页后,再看另一篇文件或另一页的内容,无需从服务器再次下载相同的JavaScript、CSS,提高了页面显示速度。
   location ~ .*\.(js|css)?$
   {
     expires      1d;
   }  


   log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
   access_log  /data1/logs/access.log  access;
   }
}




  三、PHP 5.2.6(FastCGI)的配置优化
  1、php.ini 配置文件中关于eAcelerator的优化。只使用1M共享内存,删除所有在最后3600秒内无法存取的脚本缓存,用磁盘辅助进行缓存。
引用
[eaccelerator]
zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="1"
eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"

eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.keys = "disk_only"
eaccelerator.sessions = "disk_only"
eaccelerator.content = "disk_only"


  2、php-fpm.conf 的配置优化
  修改两项,一是修改以下一行,将启动的php-cgi进程数由原来的128个改为5个:
引用
<value name="max_children">5</value>

  二是修改以下一行,将TCP模式改为Unix Socket模式:
引用
<value name="listen_address">/tmp/php-cgi.sock</value>




  四、MySQL 5.1.26 配置优化
  1、使用以下参数编译安装的 MySQL 5.1 默认支持4种存储引擎:CSV、MRG_MYISAM、MEMORY、MyISAM,不支持InnoDB存储引擎。由于内存有限,而InnoDB耗费的内存较大,这里推荐使用MyISAM存储引擎。
./configure --prefix=/usr/local/webserver/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile
make && make install


  2、MySQL 5.1 配置文件(my.cnf)优化
引用
[client]
port    = 3306
socket  = /tmp/mysql.sock

[mysql]
prompt="(\u:s135:)[\d]> "
no-auto-rehash

[mysqld]
user    = mysql
port    = 3306
socket  = /tmp/mysql.sock
basedir = /usr/local/webserver/mysql
datadir = /usr/local/webserver/mysql/data
open_files_limit    = 600
back_log = 20
max_connections = 100
max_connect_errors = 200
table_cache = 60
external-locking = FALSE
max_allowed_packet = 16M
sort_buffer_size = 128K
join_buffer_size = 128K
thread_cache_size = 10
thread_concurrency = 8
query_cache_size = 0M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default_table_type = MyISAM
thread_stack = 192K
transaction_isolation = READ-UNCOMMITTED
tmp_table_size = 512K
max_heap_table_size = 32M
/usr/local/webserver/mysql/data/slow.log
/usr/local/webserver/mysql/data/error.log
long_query_time = 1
log_long_format
server-id = 1
#log-bin = /usr/local/mysql/data/binlog
binlog_cache_size = 2M
max_binlog_cache_size = 4M
max_binlog_size = 512M
expire_logs_days = 7
key_buffer_size = 4M
read_buffer_size = 1M
read_rnd_buffer_size = 2M
bulk_insert_buffer_size = 2M
myisam_sort_buffer_size = 4M
myisam_max_sort_file_size = 10G
myisam_max_extra_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover

[mysqldump]
quick
max_allowed_packet = 16M




Tags: , , , ,



技术大类 » Web服务器 | 评论(101) | 引用(0) | 阅读(186908)
kiki
2009-3-5 03:10
090305 02:25:08 mysqld_safe Starting mysqld daemon with databases from /usr/local/webserver/mysql/data
090305  2:25:08 [ERROR] /usr/local/webserver/mysql/libexec/mysqld: unknown option '--skip-federated'
090305  2:25:08 [ERROR] Aborting

mysql,一直起不来,不明白为什么了。晕
kiki
2009-3-5 14:07
rewrite "^(.*)/date/([0-9]{6})/([0-9]+)/$" /index.php?action=index&setdate=$1&page=$2 last;
  rewrite ^(.*)/page/([0-9]+)/$ index.php?page=$1 last;
  rewrite ^(.*)/category/([0-9]+)/([0-9]+)/$ /index.php?cid=$1&page=$2 last;
  rewrite ^(.*)/category/(.+)/([0-9]+)/$ /index.php?curl=$1&page=$2 last;
  rewrite ^(.*)/(archives|search|reg|login|index|links|aboutus)/$ /index.php?action=$1 last;
  rewrite ^(.*)/(comments|tagslist|index)/([0-9]+)/$ /index.php?action=$1&page=$2 last;
  rewrite ^(.*)/tag/(.+)/([0-9]+)/$ /index.php?action=tags&item=$1&page=$2 last;
  rewrite ^(.*)/archives/([0-9]+)/([0-9]+)/$ /index.php?action=show&id=$1&page=$2 last;
  rewrite ^(.*)/(.+)/([0-9]+)/$ /index.php?action=show&alias=$1&page=$2 last;
  rewrite ^(.*)/user/(.+)/$ /index.php?action=finduser&user=$1 last;
  rewrite ^(.*)/uid/([0-9]+)/$ /index.php?action=finduser&uid=$1 last;

帮我看看有什么问题吗?死活不行
firesk
2009-3-6 13:50
kiki 2009-3-5 03:10
090305 02:25:08 mysqld_safe Starting mysqld daemon with databases from /usr/local/webserver/mysql/data
090305  2:25:08 [ERROR] /usr/local/webserver/mysql/libexec/mysqld: unknown option '--skip-federated'
090305  2:25:08 [ERROR] Aborting

mysql,一直起不来,不明白为什么了。晕

希望你还在,我用的时候也是出现了这样的问题,解决方案如下:
sudo vi /usr/local/webserver/mysql/my.cnf(这里换成你的cnf文件所在)
往下翻,就能找到一个skip-federated的项,然后用#注释掉,就可以了,其它的按照错误提示一般都能解决。
ik
2009-3-6 22:19
中文tags都是错的,多了%25,怎么回事啊。谢谢
Justin Homepage
2009-3-18 21:22
PHP make install后执行strip /usr/local/webserver/php/bin/php-cgi会有更好的表现cool
handsome
2009-4-3 10:09
为什么要单独设置read.php呢?
引用

   location ~ /read.php
   {
     #将Nginx与FastCGI的通信方式由TCP改为Unix Socket。TCP在高并发访问下比Unix Socket稳定,但Unix Socket速度要比TCP快。
     fastcgi_pass  unix:/tmp/php-cgi.sock;
     #fastcgi_pass  127.0.0.1:9000;
     fastcgi_index index.php;
     include fcgi.conf;
   }
wgwds
2009-4-10 04:35
不能修改 worker_connections,它说最多不能超过64  输多点就报错
use epoll 也不能用 显示invalid event type epoll  真郁闷!望张老师看看。
GNU
2009-5-19 18:12
GNU hosting helper
有没有文档!
供我们新手学习,

  大师有劳啦。
过客
2009-8-8 14:06
请问:我按照文章进行优化后,php-cgi进程开启3个,但是,每个进程内存居然还是占用15M以上,应该如何解决?128M内存实在吃不消了……
plum
2009-8-11 17:01
张老师你好
我的VPS也是128M的内存,我想请问一下你的那篇完整文章中涉及下面的修改是不是适用于这个小内存的VPS?
vi /etc/sysctl.conf

# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024  65535


我感觉有些数值开的太恐怖了,,不是很适合
王猛 Email Homepage
2009-8-28 21:35
博主,可以将你的Bo-Blog 2.1以上的重写规则公开吗?
我看你已经用了,谢谢你了。
hnws Email Homepage
2009-11-14 14:34
nginx     5963  8.6 21.1 330140 55564 ?        S    14:27   0:03 /usr/bin/php-cgi
nginx     5964  7.7 20.0 323392 52520 ?        S    14:27   0:02 /usr/bin/php-cgi

請問在默認php配置的情況下,我的php-cgi進程為什麼會佔用這麼多的MEM呢?都到達了20%。是256MB內存的VPS。
如能回答我的問題,非常感謝。
dd
2009-11-21 19:30
VPS上好像不能加载TCMlloc不知道张宴试了没有,我没有成功,系统是Centos5.3 32位,Mysql5.1
skywing Email Homepage
2010-11-3 20:01
在执行 /sbin/mkswap swapfile 的时候返回
mkswap: swapfile: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 262140 KiB
no label, UUID=5cb931ca-eeb6-4cf5-a1db-b1022a3bab83
执行 /sbin/swapon swapfile 返回错误信息
swapon: swapfile: swapon failed: Operation not permitted
请问下老师这个应该怎么解决?
kalso Email Homepage
2011-4-25 11:35
这个不能大于75s吧
fastcgi_connect_timeout

syntax: fastcgi_connect_timeout time

default: fastcgi_connect_timeout 60

context: http, server, location

Directive sets timeout period for connection with FastCGI-server. It should be noted that this value can't exceed 75 seconds.
louis vuitton uk Email Homepage
2011-11-23 09:05
This louis vuitton uk for sale belongs to the sounding just what are termed as Louis Vuitton vintage best sellers, many other products and services for the reason that range appearing companies.You will easily notice the unfold zippers of this coach outlet store online. That is the decoration. There are some inside pockets for you as well. They are easy to match your clothes and to carry.Let us inspire your inner beauty with fine christian louboutin sale. Purse the elegance in bridal wedding. Enjoy the fashion.
www.rijigu.com Email Homepage
2011-11-24 16:59
用上了 在此表示感谢!
welpher.yu Email Homepage
2011-12-27 14:43
这篇文章够老了,我参考了一个博主这篇文章,发现nginx 才2M的样子,php-cgi一个就20M了 mysql 4M的样子。当然了,我用的比较新的版本了。
分页: 2/6 第一页 上页 1 2 3 4 5 6 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]