优化LNMP架构采用“Website Baker”为小型公司创建高性能网站方

一、安装部署各种软件:

1.系统环境:
硬件:HP 360G4/2G
System:CentOS 5.4 
Web:nginx-1.1.6.tar.gz  
FastCGI:php-5.3.8.tar.gz
DB:mysql-5.5.3-m3.tar.gz
 
脚本下载地址:

具体下载目录在 /2012年资料/2月/3日/优化LNMP架构采用“Website Baker”为小型公司创建高性能网站方案/

 
2. 安装编译环境
#yum -y install gcc gcc-c++ autoconf automake openssl-devel zlib-devel pcre-devel   //pcre让nginx支持正则
#yum -yt groupinstall "Developement Tools" "Development Libraries"
1.编译安装mysql
//a.编译安装mysql的优点:源码包的默认编译参数默认以dubgu模式)给mysql的性能带来很大的损失,
// 此处使用 --without-debug参数禁用debug模式
//b.以静态方式编译(比动态方式提高5%-10%的性能)
// 此处使用 --with-mysqld-ldflgs=--all-staic 和 --with-client-ldflds=--all-staic参数
//c.使用unix套接字链接提高7.5%性能
// 此处使用--with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock
//d.允许使用汇编模式
// 此处使用 --enable-assembler  (优化性能)
//e.至于 --pgcc 目前没有在生产上使用过(据说可以提高1%)
//f.编译安装,可以在同一台服务器上安装多个mysql(前提要保证存放位置、sock不同)
#groupadd mysql                             //为mysql创建用户组
#useradd -g mysql mysql                     //将mysql用户添加到mysql组中
#tar zxvf mysql-5.5.3-m3.tar.gz            
#cd mysql-5.5.3-m3  
./configure \
--prefix=/usr/local/mysql \
--without-debug \                           //取消debug模式编译
--enable-assembler \
--with-mysqld-ldflags=-all-static \
--with-client-ldflags=-all-static \
--with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock \
--with-extra-charsets=utf8,gbk
//如果没有问题则会显示
// Thank you for choosing MySQL!
// 以下时间将为很长,请为mysql创建配置文件或数据目录
#make && make install                           //如果编译安装没有问题则NEXT
#chmod 755 /usr/local/mysql            
#chown -R mysql:mysql /usr/local/mysql          //修改安装目录的用户和用户组为mysql
#mkdir -pv /mysql_data/{data,binlog,relaylog}   //为mysql提供数据存放目录
#/usr/local/mysql/bin/mysql_install_db \        //推荐是用其它新分区,避免IO重读/分区
--basedir=/usr/local/mysql \
--datadir=/mysql_data --user=mysql
----------------------------------------------
//创建并优化my.cnf文件
#vi /mysql_data/my.cnf                            //创建后保存
[client]
character-set-server = utf8
port    = 3306
socket  = /usr/local/mysql/tmp/mysql.sock
[mysqld]
character-set-server = utf8
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-ignore-db = information_schema
user    = mysql
port    = 3306
socket  = /usr/local/mysql/tmp/mysql.sock                
basedir = /usr/local/mysql
datadir = /mysql_data/data
log-error = /mysql_data/mysql.error.log
pid-file = /mysql_data/mysql.pid
skip-locking                                    //避免mysql外部锁定
open_files_limit = 10240
back_log = 384                                  //推荐小于512
max_connections = 5000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 300
query_cache_size = 512M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
long_query_time = 3
log-slave-updates
log-bin = /mysql_data/binlog/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 1G
relay-log-index = /mysql_data/relaylog/relaylog
relay-log-info-file = /mysql_data/relaylog/relaylog
relay-log = /mysql_data/relaylog/relaylog
expire_logs_days = 30
key_buffer_size = 256M            //索引缓冲区为256M 或(4G内存可512M)
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
interactive_timeout = 120
wait_timeout = 120
skip-name-resolve                //避免做DNS 解析,提供IP地址连接
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
server-id = 1
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_data_file_path = ibdata1:256M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
[mysqldump]
quick
max_allowed_packet = 32M
#######################################
2.漫长的等待后启动mysql(SysV格式的脚本见附件)
#/usr/local/webserver/mysql/bin/mysql start
//管理mysql并修改密码和给新用户创建权限
#/usr/local/mysql/bin/mysql -u root -p -S /usr/local/mysql/tmp/mysql.sock
enter password: <Enter>              //默认为空 
#/usr/local/mysql/bin/mysqladmin -uroot -p paswword 'hanfeng'  //创建新密码为hanfeng
--------------------------------------------------
3.编译安装PHP
// 为PHP安装 事件触发网络库
#tar zxvf libevent-2.0.15-stable.tar.gz
#cd libevent-2.0.15-stable
#./configure && make && make install
// 为PHP安装 字符编码转换
#tar zxvf  libconv-1.13.1.tar.gz
#cd libconv-1.13.1
#./configure && make && make install
// 安装FastCGIのPHP
// 此处采用
#tar zxvf php-5.3.8.tar.gz
#cd  php-5.3.8
#./configure \
--prefix=/usr/local/php \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config  \
--with-openssl \
--enable-fpm \                                 //一定要有
--with-libevent-dir=/usr/local/lib \
--enable-mbstring \
--disable-debug \                              //取消掉debug,为增加性能
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--with-iconv-dir=/usr/local
--------------------------------------------------
4.随后将会看到
// Thank you for using PHP
#ln -s /usr/local/lib/libiconv.so.2 /usr/lib/libiconv.so.2        //做个连接防止make时缺少libiconv.so.2
#ln -s /usr/local/lib/libiconv.so.2 /usr/local/libiconv.so.2
#make ZEND_EXTRA_LIBS='-liconv'                                   //iconv库问题
5.漫长时间后将会看到
// Build complete. Don't forget to run 'make test'
#make install
#cp php-5.3.8/php.ini-production /usr/local/php/etc/php.ini
//新增说明:2011-12-01
//隐藏php版本信息,将 php.ini中
// expose_php = On  改为 expose_php = Off  重启php-fpm即可
#cp /usr/local/php-5.3.8/etc/php-fpm.conf.default /usr/local/php-5.3.8/etc/php-fpm.conf
#vi /usr/local/php-5.3.8/etc/php-fpm.conf  修改并保存
// 开启以下几项
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
6. 使用nohup在后台启动 php-fpm  为防止tty终端关闭导致php-fpm 挂掉!推荐生产环境使用nohup将命令放于后台运行
#nohup /usr/local/php/sbin/php-fpm &
[1] 18198
nohup: appending output to `nohup.out'
[1]+  Done    nohup /usr/local/php/sbin/php-fpm
//将命令加到开始启动
#echo "nohup /usr/local/php/sbin/php-fpm & > /dev/null" >> /etc/rc.local
--------------------------------------------------
7.编译安装nginx
#useradd -s /sbin/nologin -M nginx       //添加nginx用户
#tar zxvf nginx-1.1.6.tar.gz
#cd nginx-1.1.6
#./configure \
  --prefix=/usr/local/nginx \                             //安装路径
  --sbin-path=/usr/sbin/nginx \                           //可执行文件路径
  --conf-path=/etc/nginx/nginx.conf \                     //最好定义到/etc下
  --pid-path=/usr/local/nginx/nginx.pid  \                  //pid文件存放位置,后面管理时将会用到
  --lock-path=/var/lock/nginx.lock \          
  --user=nginx \
  --group=nginx \
  --with-http_stub_status_module \                        //以取得一些网页的运行状态
  --with-http_ssl_module \                                //支持https加密连接
  --with-http_gzip_static_module \                        //静态缓存、压缩模块
  --with-http_realip_module  \                            //让nginx透明获取客户端IP
  --http-client-body-temp-path=/var/tmp/nginx/client/ \   //指定http客户端请求缓存文件存放目录
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \          //指定http反向代理缓存文件存放目录
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/           //指定FastCGI缓存文件存放目录
#make && make install                                     //安装nginx
--------------------------------------------------

相关推荐