VPS(Virtual Private Server)是一台真实的物理服务器上通过最新的虚拟化技术创建多个相互隔离的虚拟似有主机,特点就是独立CPU资源,内存也是相互独立的,所以从一些著名的主机提供商(VPS这种服务对于像我这样的学生来说价格不菲啊!)购买VPS主机的时候内存大小越大价格也就越高。下面针对128MB独立内存的LNMP进行优化。

增加swap交换文件

      如果VPS提供商没有划分SWAP交换分区,可以自动创建SWAP交换文件来代替交互分区。

cd /var/

dd if=/dev/zero of=swapfile bs=1024 count=262144

/sbin/mkswap swapfile

/sbin/swapon swapfile然后加到fstab,开机自启动

vi /etc/fstab

/var/swapfile swap swap defaults 0 0

nginx配置文件(nginx.conf)的配置

user www www;

wordker_processes 1; //开启一个进程

error_log /data/logs/nginx_error.log crit;

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

worker_rlimit_nofile 51200;

events {

use epoll;

worker_connections 51200;

}

http {

include mime.types;

default_type application/octet-stream;

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_tiomeout 300;

fastcgi_read_timeout 300;

fastcgi_send_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

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;

server {

listen 80;

server_name www.domain.com;

index index.html index.htm index.php;

root /data/htdocs/blog;

#将nginx与fastcgi的通信方式改为unix socket,tcp在高并发访问下比unix socket稳定,单unix socket比TCP快。

location ~ .*.(php|php5)?$

{

fastcgi_pass unix:/tmp/php-cgi.sock;

#fastcgi_pass http://127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

location ~ .*.(jpg|jpeg|gif|bmp|png|swf)$

{

expires 15d;

}

location ~ .*.(js|css)?$

{

expires 1d;

}

log_format log_format_name ‘$remote_addr – $remote_user [$time_local] “request”‘$status $body_bytes_sent “$http_referer”’“$http_user_agent” $http_x_forwarded_for’;

access_log /data/logs/access.log access;

}

}
PHP优化

php.ini中eaccelerator的优化。只使用1MB共享内存,删除所有在最后3600秒内无法读取的脚本缓存,用磁盘补助进行缓存。

[eaccelerator]

zend_extension=“/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so”   //eaccelerator的扩展库

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”

php-fpm.conf的配置

将启动的php-cgi进程数改为5个

<value name=“max_children”>5</value>

TCP模式改为unix socket

<value name=“listen_address”>/tmp/php-cgi.sock</value>

MYSQL的配置优化

[client]

port = 3306

socket = /tmp/mysql.sock

[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 = 0

thread_cache_limit = 2M

quety_cache_min_res_unit = 2k

default_table_type = MyISAM //使用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

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 = 2M

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

哟?不错哎 订阅小米的RSS Feeds 回去慢慢看^_^