Nginx 0.7.x + PHP 5.2.6(FastCGI)+ MySQL 5.1 在128M小內存VPS服務器上的配置優(yōu)化第1/2頁
更新時間:2008年12月06日 21:29:47 作者:
VPS(全稱Virtual Private Server)是利用最新虛擬化技術在一臺物理服務器上創(chuàng)建多個相互隔離的虛擬私有主機。它們以最大化的效率共享硬件、軟件許可證以及管理資源。
對其用戶和應用程序來講,每一個VPS平臺的運行和管理都與一臺獨立主機完全相同,因為每一個VPS均可獨立進行重啟并擁有自己的root訪問權限、用戶、IP地址、內存、過程、文件、應用程序、系統(tǒng)函數(shù)庫以及配置文件。
VPS服務器最重要的指標就是內存大小,多個VPS服務器可以共享一顆CPU,但不能共享同一塊內存。內存越大,價格越貴。
下面,以我的博客所在的VPS為例,介紹在128M內存下對 Nginx 0.7.x + PHP 5.2.6(FastCGI)+ MySQL 5.1 的優(yōu)化。
至于 Nginx + PHP + MySQL 的安裝配置,可參見:《Nginx 0.7.x + PHP 5.2.6(FastCGI)搭建勝過Apache十倍的Web服務器(第4版) 》
--------------------------------------------------------------------------------
優(yōu)化后的效果:
提供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服務器系統(tǒng)自身和其它程序也會使用一些內存,但128M內存的VPS已經(jīng)夠用??傮w而言,經(jīng)過優(yōu)化后,128M內存的VPS跑 Nginx + PHP + MySQL 效果不錯。當然,如果有Money購買更大內存的VPS,就更好了。
優(yōu)化項如下:
一、增加256M的swap交換文件
1、創(chuàng)建并激活swap交換文件
cd /var/
dd if=/dev/zero of=swapfile bs=1024 count=262144
/sbin/mkswap swapfile
/sbin/swapon swapfile
2、加到fstab文件中讓系統(tǒng)引導時自動啟動
vi /etc/fstab
在末尾增加以下內容:
引用
/var/swapfile swap swap defaults 0 0
詳見:
--------------------------------------------------------------------------------
二、Nginx 0.7.19 的主配置文件(nginx.conf)優(yōu)化
user www www;
#Nginx每個進程耗費10M~12M內存,這里只開啟一個Nginx進程,節(jié)省內存。
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;
#對網(wǎng)頁文件、CSS、JS、XML等啟動gzip壓縮,減少數(shù)據(jù)傳輸量,提高訪問速度。
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系統(tǒng)的Rewrite靜態(tài)化
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在高并發(fā)訪問下比Unix Socket穩(wěn)定,但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在高并發(fā)訪問下比Unix Socket穩(wěn)定,但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;
}
}
VPS服務器最重要的指標就是內存大小,多個VPS服務器可以共享一顆CPU,但不能共享同一塊內存。內存越大,價格越貴。
下面,以我的博客所在的VPS為例,介紹在128M內存下對 Nginx 0.7.x + PHP 5.2.6(FastCGI)+ MySQL 5.1 的優(yōu)化。
至于 Nginx + PHP + MySQL 的安裝配置,可參見:《Nginx 0.7.x + PHP 5.2.6(FastCGI)搭建勝過Apache十倍的Web服務器(第4版) 》
--------------------------------------------------------------------------------
優(yōu)化后的效果:
提供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服務器系統(tǒng)自身和其它程序也會使用一些內存,但128M內存的VPS已經(jīng)夠用??傮w而言,經(jīng)過優(yōu)化后,128M內存的VPS跑 Nginx + PHP + MySQL 效果不錯。當然,如果有Money購買更大內存的VPS,就更好了。
優(yōu)化項如下:
一、增加256M的swap交換文件
1、創(chuàng)建并激活swap交換文件
cd /var/
dd if=/dev/zero of=swapfile bs=1024 count=262144
/sbin/mkswap swapfile
/sbin/swapon swapfile
2、加到fstab文件中讓系統(tǒng)引導時自動啟動
vi /etc/fstab
在末尾增加以下內容:
引用
/var/swapfile swap swap defaults 0 0
詳見:
--------------------------------------------------------------------------------
二、Nginx 0.7.19 的主配置文件(nginx.conf)優(yōu)化
復制代碼 代碼如下:
user www www;
#Nginx每個進程耗費10M~12M內存,這里只開啟一個Nginx進程,節(jié)省內存。
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;
#對網(wǎng)頁文件、CSS、JS、XML等啟動gzip壓縮,減少數(shù)據(jù)傳輸量,提高訪問速度。
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系統(tǒng)的Rewrite靜態(tài)化
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在高并發(fā)訪問下比Unix Socket穩(wěn)定,但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在高并發(fā)訪問下比Unix Socket穩(wěn)定,但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;
}
}
相關文章
Linux XAMPP下啟用WordPress的自定義文件名(偽靜態(tài))功能
這篇文章主要介紹了Linux XAMPP下啟用WordPress的自定義文件名(偽靜態(tài))功能的相關資料,需要的朋友可以參考下2016-12-12tr命令在統(tǒng)計英文單詞出現(xiàn)頻率中的妙用
今天小編就為大家分享一篇關于tr命令在統(tǒng)計英文單詞出現(xiàn)頻率中的妙用,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03