Nginx 0.7.x + PHP 5.2.6(FastCGI)+ MySQL 5.1 在128M小内存VPS服务器上的配置优化[原创]
[ 2008-10-28 16:55 | by 张宴 ]
[文章作者:张宴 本文版本: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交换文件
2、加到fstab文件中让系统引导时自动启动
在末尾增加以下内容:
详见:http://blog.zyan.cc/post/374.htm
二、Nginx 0.7.19 的主配置文件(nginx.conf)优化
三、PHP 5.2.6(FastCGI)的配置优化
1、php.ini 配置文件中关于eAcelerator的优化。只使用1M共享内存,删除所有在最后3600秒内无法存取的脚本缓存,用磁盘辅助进行缓存。
2、php-fpm.conf 的配置优化
修改两项,一是修改以下一行,将启动的php-cgi进程数由原来的128个改为5个:
二是修改以下一行,将TCP模式改为Unix Socket模式:
四、MySQL 5.1.26 配置优化
1、使用以下参数编译安装的 MySQL 5.1 默认支持4种存储引擎:CSV、MRG_MYISAM、MEMORY、MyISAM,不支持InnoDB存储引擎。由于内存有限,而InnoDB耗费的内存较大,这里推荐使用MyISAM存储引擎。
2、MySQL 5.1 配置文件(my.cnf)优化
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
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;
}
}
#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"
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
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
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
hotmail login
2020-3-27 10:42
This is a great article, with a lot of information in it, These types of articles make users interested in your site. Keep sharing more of the better posts! Thanks!hotmail email login | gmail sign up
sarahemi
2020-5-18 15:31
I like to read this blog it gives you many new ideas to think and carry forward to other people . Flyer design uae is help people to make awarness about the new product. It is good to like to guide audience .
Religion Assignment Writing Services
2020-7-30 20:47
It is important to seek religion essay writing services and theology & religion research paper services since students find help when they visit Religion Assignment Writing Services.
escortgirls
2020-12-3 14:51
I was waiting for such a post, I read your post, the content in it is very unique, thank you, I will be reading your post even further, good luck.Mumbai call girlsMumbai call girlMumbai escort serviceMumbai EscortsEscort Service in MumbaiOur Service Partners :Digital Marketing AgencyExporter and ImporterSatta KingGAS STOVE REPAIR
pg slot
2021-3-26 15:10
The are thousands of job interviews conducted daily in different countries, the first step is looking for available job openings and applying for them depending o your experience and your country of residence.The casino website pin pg slotpgslot
jangdong
2021-6-2 14:15
I gotta favorite this website. I truly appreciate it. I’ll stick a link of this entry in my site. friday night funkin slope unblocked
MIKLAS
2021-8-28 16:12
I am really happy to visit your blog. Now I have found what I really want. I check your blog everyday and try to learn something from your blog. Thank you and waiting for your new post.cookie clicker
s
2021-10-3 01:29
Thanks for the post สล็อต roma x
s
2021-10-3 01:29
Thanks for the post สมัครสล็อตโรม่าx
s
2021-10-3 01:29
Thanks for the post ทดลองเล่นสล็อตเว็บตรง
s
2021-10-3 01:30
Thanks for the post เล่นสล็อตค่าย jili
lokosa
2021-12-22 12:27
It’s hard to come by experienced people in this particular topic, but you sound like you know what you’re talking about! Thanks driving directions mapquest
kkgreece
2022-1-22 00:40
best website to play online game shell shockers https://www.talentcreation.org/shell-shockers-unblocked-games/
The impossible game
2022-1-25 17:07
Have you ever encountered a problem that seemed out of your control? And how do you deal with them? Have you really focused on entertainment? Or do you just take a break from work and still think about it? The Impossible Game will be the solution to help you forget everything to have a time of real entertainment, and also a way for you to know where your limits are!
Miklasf
2022-4-19 12:39
I have read your blog, it is very helpful for me. I want to say thank you to you. I have bookmarked your site for future updates. I will be back in the near future!poppy playtime
guxico
2022-4-23 12:46
Thank you for sharing this information; it was quite helpful to me. moto x3m
run 3
2022-5-19 12:29
This is a great site, there are very insightful and useful articles, I will visit often to find the necessary information, thank you
Kareem Abdul-Jabbar
2022-5-19 12:31
I love blogging, and I come here often to collect useful information and knowledge, wish the blog to grow more and more <a href="https://bubbleshootergame.online/">bubble shooter</a>
pg
2022-6-6 17:33
pg demo ให้คุณได้ลองเล่นเกม สล็อต ได้พัฒนาฝีมือก่อนลงสนามจริง ทำให้ทุกการลงทุนของคุณมีความหมาย พร้อมรับโบนัสฟรีแล้ววันนี้ สมัครเลย
bevis
2022-6-14 15:42
Thanks for your post, it's very helpful I hope in the future you will provide more information. I will visit and support the article for youClick on drift hunters to play the hot game
分页: 4/6 1 2 3 4 5 6