Wordpress Nginx Php-cgi Mysql在128m内存下的vps安装和优化手记[原创]

[ 文章作者:陈臻 本文版本:v1.0 最后修改:2009.8.2 转载请注明原文链接:http://www.54chen.com/688-wordpress-nginx-php-cgi-mysql-memory-in-the-128m-to-install-and-optimize-the-vps-notes/ ]

选取CentOS,因为它是号称最安全及性能都相对较好的Linux系统。系统内存128m,系统用掉30m,有100m左右可用(如图1所示),swap已经有256M,硬盘为5G,除去系统后大约有4G可供捣腾。

图1。

 

首先,在空白的系统上使用下面的命令,安装gcc等一堆工具和后面php会用到的一些包:

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

yum这一堆东西会有点慢,这里正好插播一下,mysql的编译在128的内存下非常慢,所以呆会儿我们会采取直接yum,php、nginx都下源码编译,经过以往的经验eAccelerator、Xcache和Zend Optimizer这三者,最好是用eAccelerator搭配Zend Optimizer能给php加速得到最佳效果,当然了,Zend Optimizer需要Zend Guard来搭配,而后者是收费的,伟大的人有伟大的破解。

我执行的时候耗时32分钟左右。

言归正传,继续: wget http://sysoev.ru/nginx/nginx-0.7.61.tar.gz
wget http://www.php.net/get/php-5.2.10.tar.gz/from/this/mirror
wget http://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.10-fpm-0.5.11.diff.gz
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.9.tar.gz [nginx rewrite使用]

【安装MySQLyum install -y mysql-server
启动MySQL:
service mysqld start
设置mysql数据库root帐号密码:
mysqladmin -u root password 'newpassword' [引号内填密码]
让mysql更安全:
mysql -u root -p [此时会要求你输入刚刚设置的密码,输入后回车即可]

mysql> DROP DATABASE test; [删除test数据库]
mysql> DELETE FROM mysql.user WHERE user = ''; [删除匿名帐户]
mysql>DELETE FROM mysql.user WHERE password = '';[删除无密码帐户]
mysql> FLUSH PRIVILEGES; [重载权限]

【优化MySQL】
此时的mysql直接吃掉20m左右的内存,如图2所示。我们要对其进行优化,关掉innodb

图2。

 

 

 

 

vi /etc/my.cnf
在后面增加如下内容:

skip-innodb
[mysql]
no-auto-rehash

[mysqld]
user = mysql
port = 3306
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
long_query_time = 1
log_long_format
server-id = 1
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

【重启MySQL】
service mysqld restart
再看mysql吃掉的内存,已经降低了四分之一,如图3所示: 图3。

 

 

 

 

【安装php】
首先使用yum安装上mysql的开发包,供php扩展mysql使用: yum -y install mysql-devel
安装patch工具:
yum -y install patch
tar zxvf php-5.2.10.tar.gz
gzip -cd php-5.2.10-fpm-0.5.11.diff.gz | patch -d php-5.2.10 -p1
cd php-5.2.10
./configure --prefix=/opt/php --with-config-file-path=/opt/php/etc --with-mysql=/usr/share/mysql --with-mysqli=/usr/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-gd --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --without-pear
make && make install
把pear手动装上(这个是5.2.10的一个bug,后面可能已经打补丁了):
curl http://pear.php.net/go-pear | /opt/php/bin/php
cp php.ini-dist /opt/php/etc/php.ini

【安装eaccelerator】 tar jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3
/opt/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/opt/php/bin/php-config
make && make install
mkdir -p /opt/eaccelerator_cache
vi /opt/php/etc/php.ini

添加下面的内容: [eaccelerator]
zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="64"
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"

 

【安装Zend Optimizer】
要去zend.com上注册用户名后进download页面下载,这里太弱智了,不知道zend是否有专门的产品人员,这种注册下载有什么意义,只是一堆死账号而已。
下载ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz tar -zxvf ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz
cd ZendOptimizer-3.3.3-linux-glibc23-i386
./install

运行过程中会要求你写些安装路径啥的,照着写就是。
如果出现类似下面的错误:
./install-tty: line 139: ./php: cannot execute binary file
那说明你下错了包了,这种情况是因为你32位的系统下了64位的包或者是反过来。

【安装nginx】 tar -zxvf pcre-7.9.tar.gz
cd pcre-7.9
./configure
make && make install
cd ..

tar zxvf nginx-0.7.61.tar.gz
cd nginx-0.7.61
./configure --user=www --group=www --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install

增加www用户:
groupadd www
useradd -g www www

【优化php\nginx】
1.优化php-fpm.conf vi /opt/php/etc/php-fpm.conf
修改log level为error:
error
修改max_children:
5 修改listen_address为unix socket方式运行:
/tmp/php-cgi.sock

2.优化nginx.conf
vi /opt/nginx/conf/nginx.conf
在events中增加: use epoll;
在http中增加下面的代码,打开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;
在使用php的server中使用unix socket通信方式:
fastcgi_pass unix:/tmp/php-cgi.sock;

【设置开机启动】
/sbin/chkconfig --add mysqld [在服务清单中添加mysql服务]
/sbin/chkconfig mysqld on [设置mysql服务开机启动]

vi /etc/rc.locale
加入两行:
/opt/php/sbin/php-fpm start
/opt/nginx/sbin/nginx

【误区提示】
XEN更像物理服务器,会尽量把剩余的内存当成buffer和cache,所以看到下图的时候不要惊慌,其实内存都在cache里了。 图4

 

 

【wordpress专项优化】
使用wp-super-cache插件,将页面生成html,省去php的开销,性能有提升。
对照access log,刷新页面,看堵在什么地方

【几个建议】
合并css js个数 并且压缩 速度提升明显
wordpress没有任何插件的时候,效率是很不错的,但是有些不负责任的插件会导致整体看起来很慢,下面是一些常用的插件的问题:
1.weibo tools插件:用来从后端取weibo数据的,这个插件搞了一个js一个css在head里,并且都是通过php生成的,两个php请求,让首页很慢。建议去掉(可能会影响自动化?没来得及分析细节)。
2.wp-spamfree插件:用来做antispam的,这是一个动态生成的js,不知道为什么非常慢。建议换别的插件。

【结果展示】
在完成上述一系列的作后,https://www.54chen.com 我是陈科学院的打开速度已经非常迅速了,基本上一秒钟就能全部显示。内存还有40m空闲,如图5所示: 图5


原创文章如转载,请注明:转载自五四陈科学院[http://www.54chen.com]

捐款订阅54chen
捐赠说明