分類  >  Web前端 >

優化LNMP架構採取「Website Baker」為小型公司創建高性能網站方案「

tags:    時間:2013-12-11 12:24:44
優化LNMP架構採用「Website Baker」為小型公司創建高性能網站方案「

優化LNMP架構採用「Website Baker」為小型公司創建高性能網站方案

參考圖片:
1.
一、安裝部署各種軟體:
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
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
#vi /usr/local/php/etc/php.ini 修改並保存
// 開啟以下幾項
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" >> /ect/rc.local
7.//為nginx增加fastcgi_params 文件
#vi /etc/nginx/fastcgi.conf    //修改後保存
// 將裡面內容替換為
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
##########################################
更新說明:2011-11-23
// 什麼原因就不解釋了,不能歸咎與nginx  ,因PHP PATH_INFO問題!
// 只採用方便的方式
// 在 fcgi.conf文件開頭,添加一下內容,便於多個虛擬主機引用!
if ($request_filename ~* (.*)\.php) {
    set $php_url $1;
}
if (!-e $php_url.php) {
    return 403;
}
##########################################
8.為nginx 優化linux內核參數
vi /etc/sysctl.conf                  // 增加一下內容,並是修改生效
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw.reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.ip_local_port_range = 1024 65535
##########################################
#sysctl -p                           //使其及時生效
--------------------------------------------------
9.編譯安裝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
10.編寫SystemV風格的啟動腳本(可在本文附件下載后直接使用)
//如果需要請參考: http://linuxxx.blog.51cto.com/1824808/705590 有關nginxd腳本的編寫
//使用說明,保存nginxd腳本,賦予執行許可權,添加服務和開機啟動
#chmod +x /etc/init.d/nginxd
#chkconfig --add nginxd
// 如果不支持,則添加一下2行即可
// # chkconfig: - 85 15                #為必須字元
// # description: nginx is a World Wide Web server. It is used to serve
#chkconfig --level 2345 nginxd on       
11.第一次啟動nginx
#service nginxd start    //沒問題的話就顯示,也會看到Welcome to nginx!
Starting nginx: [  OK  ]
在XP客戶端訪問一下 http://192.168.10.11   //伺服器地址
--------------------------------------------------
12.優化配置nginx.conf 文件
user nginx nginx;
worker_processes  8;                       //我的伺服器為雙核雙線程
pid        /usr/local/nginx/nginx.pid;     //PID 存在位置,和啟用nginx.pid管理
events {
    use epoll;                             //使用的網路IO模型,CeotOs或REAL推薦使用epoll
    worker_connections  1024;              //允許的連接數
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format mylog  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';     
                       //mylog為自定義log_format的name,log_format用於定義log的格式
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 60; //連接保持時間
    gzip on;                           //開啟gzip壓縮,為客戶端訪問節省更大的帶寬,提高訪問速度
    gzip_min_length 1k;    
    gzip_buffers 4 8k;                 //安裝原數據8K為單位的4倍申請內存
    gzip_http_version 1.1;             //http版本信息,目前為1.0和1.1
    gzip_comp_level 4;                 //gzip 壓縮比(與CPU處理速度相反)
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
   
    fastcgi_intercept_errors 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;
    server {
        listen       80;
        server_name  www.abc.com;
        access_log  logs/abc.access.log  mylog;   //用到了mylog,將日誌定義為log_format格式
        location / {
            root   /usr/local/nginx/html/abc;    
            index index.php index.html index.htm;
        }
       
        error_page  404 = /404.html;           //當訪問不存在的頁面時,提示用戶重新訪問主頁   
        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /usr/local/nginx/50x/50x.html;  //各種錯誤頁面
        location = /50x.html {
            root   html;
        }
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;                               //fastcgi 索引頁格式
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    server {
        listen       80;
        server_name  www.abcd.com;
        access_log  logs/abcd.access.log  mylog;
        location / {
            root  /usr/local/nginx/html/abcd;
            index index.php index.html index.htm;
            error_page  404 = /404.html;
            error_page   500 502 503 504  /usr/local/nginx/50x/50x.html;
        location = /50x.html {
       
            root   html;
        location ~ \.php$ {
            root   /usr/local/nginx/html/abcd/wb;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
        }
    }
}    
############### This is nginx.conf ###############
--------------------------------------------------
13.使用PID信號來控制管理nginx進程
// pid 存放位置  /usr/local/nginx/nginx.pid
// kill - 信號類型  cat `/usr/local/nginx/nginx.pid`
// 信號類型說明
// TERM,INT 快速關閉nginx , 如以此kill掉8個進程是不是很悲劇
// OUT 從容關閉
// HUP 平滑重啟,重新載入配置文件
// USR1 重新打開日誌文件,在自動切割日誌時比較使用
// USR2 平滑升級可執行程序
// WINCH 從容關閉進程,對於線上的nginx新舊版本更換
// 針對與上文的nginx.conf 修改此處使用HUP信號來控制
#kill -HUP cat `/usr/local/nginx/nginx.pid`  //讓修改立即生效
14.訪問簡單測試測試頁面
#vi /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
15.測試訪問資料庫
<?php
link=mysql_connect("localhost","root","hanfeng");
if(!$link) echo "ERROR!";
else echo "OK!";
phpinfo();
?>
--------------------------------------------------
二、安裝「Website Baker」
1.安裝部署Website Baker
// 採用nginx的虛擬主機為企業N個域名做網站部署
#unzip WebsiteBaker_2.8*.zip
#mv WebsiteBaker_2.8* /usr/local/nginx/html/abc
#cp -r /usr/local/nginx/html/abc /usr/local/nginx/html/abcd
#cd /usr/local/nginx/html/abc
#cp wb/modules/fckeditor/fckeditor/editor/filemanager/connectors/php/config.php ./
#cp config.php ../abcd/wb/
#chmod 777 /usr/local/nginx/html/abc
#chmod 777 /usr/local/nginx/html/abcd
2.為Website Baker的創建資料庫用戶
#/usr/local/mysql/bin/mysql -uroot -p
mysql> GRANT ALL PRIVILEGES ON abc.* TO 'abc'@'localhost' IDENTIFIED BY 'hanfeng';
mysql> GRANT ALL PRIVILEGES ON abcd.* TO 'abcd'@'localhost' IDENTIFIED BY 'hanfeng';
mysql>flush privileges;
3.通過瀏覽器訪問並install 和admin
IE: http://www.abcd.com/install  安裝  (abc.com同理)
//以下僅有圖片提供

IE:  http://www.abcd.com/admin     管理
 
//安裝完后刪除安裝文件和upgrade-script.php文件
#rm -rf /usr/local/nginx/html/{abc,abcd}/{upgrade-script.php,install}
// admin進入界面請見文章開始
4.通過訪問安裝無誤,證明基於LNMP下的「Website Baker」安裝成功!
// 根據後台admin管理網站即可了!
說明:
1.本文不只是為了構建LNMP安裝「Website Baker」網站,而是將最近學習到東西給朋友提供實際生產中的應用!
2.nignx 日誌切割、mysql數據備份已經配置為cron任務,並為編輯人員提供了bat文件自動上傳和伺服器狀態查看!基本不需要專業運維人員管理,直接託付給IDC即可!
3.LNMP編譯安裝優化多學自撫琴煮酒《構建高可用linux伺服器》和張宴《實戰Nginx取代Apache的高性能Web伺服器》並借鑒51CTO《運維趨勢之伺服器優化_13期》;
4.不在於編譯安裝LNMP,而是優化期架構在生產中的應用,根據最近觀察服務運行良好!
5.文章提供技術支持,如有好的建議或安裝問題 請 Send E-mail to linuxhzg#qq.com (請將#換為@) 如涉及隱私請通知寒楓(hanfeng)謝謝;
 

本文出自 「寒楓」 博客,請務必保留此出處http://linuxxx.blog.51cto.com/1824808/719972

推薦閱讀文章

Bookmark the permalink ,來源:互聯網