快捷搜索:   nginx

Nginx 0.7.x + PHP 5.2.6(FastCGI)+ MySQL 5.1 在128M小内存VPS服务器上的

对其用户和应用程序来讲,每一个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
详见:
--------------------------------------------------------------------------------
二、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.s135.com www.s135.com s135.com *.s135.com;
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;
}
}

顶(1)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论