服務(wù)器的大用戶量的承載方案 一、前言
二、編譯安裝 三、 安裝MySQL、memcache 四、 安裝Apache、PHP、eAccelerator、php-memcache 五、 安裝Squid 六、后記 一、前言,準(zhǔn)備工作 當(dāng)前,LAMP開發(fā)模式是WEB開發(fā)的首選,如何搭建一個高效、可靠、穩(wěn)定的WEB服務(wù)器一直是個熱門主題,本文就是這個主題的一次嘗試。 我們采用的架構(gòu)圖如下: 引用
-------- ---------- ------------- --------- ------------
| 客戶端 | ===> |負(fù)載均衡器| ===> |反向代理/緩存| ===> |WEB服務(wù)器| ===> |數(shù)據(jù)庫服務(wù)器| -------- ---------- ------------- --------- ------------ Nginx Squid Apache,PHP MySQL eAccelerator/memcache 引用
服務(wù)器: Intel(R) Xeon(TM) CPU 3.00GHz * 2, 2GB mem, SCISC 硬盤
操作系統(tǒng):CentOs4.4,內(nèi)核版本2.6.9-22.ELsmp,gcc版本3.4.4 軟件: Apache 2.2.3(能使用MPM模式) PHP 5.2.0(選用該版本是因為5.2.0的引擎相對更高效) eAccelerator 0.9.5(加速PHP引擎,同時也可以加密PHP源程序) memcache 1.2.0(用于高速緩存常用數(shù)據(jù)) libevent 1.2a(memcache工作機(jī)制所需) MySQL 5.0.27(選用二進(jìn)制版本,省去編譯工作) Nginx 0.5.4(用做負(fù)載均衡器) squid-2.6.STABLE6(做反向代理的同時提供專業(yè)緩存功能) 二、編譯安裝 一、) 安裝Nginx 1.) 安裝 Nginx發(fā)音為[engine x],是由俄羅斯人Igor Sysoev建立的項目,基于BSD許可。據(jù)說他當(dāng)初是F5的成員之一,英文主頁:http://。俄羅斯的一些大網(wǎng)站已經(jīng)使用它超過兩年多了,一直表現(xiàn)不凡。 Nginx的編譯參數(shù)如下: [root@localhost]#./configure --prefix=/usr/local/server/nginx --with-openssl=/usr/include \ --with-pcre=/usr/include/pcre/ --with-http_stub_status_module --without-http_memcached_module \ --without-http_fastcgi_module --without-http_rewrite_module --without-http_map_module \ --without-http_geo_module --without-http_autoindex_module 在這里,需要說明一下,由于Nginx的配置文件中我想用到正則,所以需要 pcre 模塊的支持。我已經(jīng)安裝了 pcre 及 pcre-devel 的rpm包,但是 Ngxin 并不能正確找到 .h/.so/.a/.la 文件,因此我稍微變通了一下: [root@localhost]#mkdir /usr/include/pcre/.libs/ [root@localhost]#cp /usr/lib/libpcre.a /usr/include/pcre/.libs/libpcre.a [root@localhost]#cp /usr/lib/libpcre.a /usr/include/pcre/.libs/libpcre.la 然后,修改 objs/Makefile 大概在908行的位置上,注釋掉以下內(nèi)容: ./configure --disable-shared 接下來,就可以正常執(zhí)行 make 及 make install 了。 2.) 修改配置文件 /usr/local/server/nginx/conf/nginx.conf 以下是我的 nginx.conf 內(nèi)容,僅供參考: #運行用戶 user nobody nobody; #啟動進(jìn)程 worker_processes 2; #全局錯誤日志及PID文件 error_log logs/error.log notice; pid logs/nginx.pid; #工作模式及連接數(shù)上限 events { use epoll; worker_connections 1024; } #設(shè)定http服務(wù)器,利用它的反向代理功能提供負(fù)載均衡支持 http { #設(shè)定mime類型 include conf/mime.types; default_type application/octet-stream; #設(shè)定日志格式 log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" ' '"$gzip_ratio"'; log_format download '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" ' '"$http_range" "$sent_http_content_range"'; #設(shè)定請求緩沖 client_header_buffer_size 1k; large_client_header_buffers 4 4k; #開啟gzip模塊 gzip on; gzip_min_length 1100; gzip_buffers 4 8k; gzip_types text/plain; output_buffers 1 32k; postpone_output 1460; #設(shè)定access log access_log logs/access.log main; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; #設(shè)定負(fù)載均衡的服務(wù)器列表 upstream mysvr { #weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大 #本機(jī)上的Squid開啟3128端口 server 192.168.8.1:3128 weight=5; server 192.168.8.2:80 weight=1; server 192.168.8.3:80 weight=6; } #設(shè)定虛擬主機(jī) server { listen 80; server_name 192.168.8.1 www.; charset gb2312; #設(shè)定本虛擬主機(jī)的訪問日志 access_log logs/www..access.log main; #如果訪問 /img/*, /js/*, /css/* 資源,則直接取本地文件,不通過squid #如果這些文件較多,不推薦這種方式,因為通過squid的緩存效果更好 location ~ ^/(img|js|css)/ { root /data3/Html; expires 24h; } #對 "/" 啟用負(fù)載均衡 location / { proxy_pass http://mysvr; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } #設(shè)定查看Nginx狀態(tài)的地址 location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus"; auth_basic_user_file conf/htpasswd; } } } 備注:conf/htpasswd 文件的內(nèi)容用 apache 提供的 htpasswd 工具來產(chǎn)生即可,內(nèi)容大致如下: 3.) 查看 Nginx 運行狀態(tài) 輸入地址 http://192.168.8.1/NginxStatus/,輸入驗證賬號密碼,即可看到類似如下內(nèi)容: Active connections: 328 server accepts handled requests 9309 8982 28890 Reading: 1 Writing: 3 Waiting: 324 第一行表示目前活躍的連接數(shù) 第三行的第三個數(shù)字表示Nginx運行到當(dāng)前時間接受到的總請求數(shù),如果快達(dá)到了上限,就需要加大上限值了。 第四行是Nginx的隊列狀態(tài) 三、 安裝MySQL、memcache 1.) 安裝MySQL,步驟如下: [root@localhost]#tar zxf mysql-standard-5.0.27-linux-i686.tar.gz -C /usr/local/server [root@localhost]#mv /usr/local/server/mysql-standard-5.0.27-linux-i686 /usr/local/server/mysql [root@localhost]#cd /usr/local/server/mysql [root@localhost]#./scripts/mysql_install_db --basedir=/usr/local/server/mysql \ --datadir=/usr/local/server/mysql/data --user=nobody [root@localhost]#cp /usr/local/server/mysql/support-files/my-large.cnf \ /usr/local/server/mysql/data/my.cnf 2.) 修改 MySQL 配置,增加部分優(yōu)化參數(shù),如下: [root@localhost]#vi /usr/local/server/mysql/data/my.cnf 主要內(nèi)容如下: [mysqld] basedir = /usr/local/server/mysql datadir = /usr/local/server/mysql/data user = nobody port = 3306 socket = /tmp/mysql.sock wait_timeout = 30 long_query_time=1 #log-queries-not-using-indexes = TRUE log-slow-queries=/usr/local/server/mysql/slow.log log-error = /usr/local/server/mysql/error.log external-locking = FALSE key_buffer_size = 512M back_log = 400 table_cache = 512 sort_buffer_size = 2M join_buffer_size = 4M read_buffer_size = 2M read_rnd_buffer_size = 4M myisam_sort_buffer_size = 64M thread_cache_size = 32 query_cache_limit = 2M query_cache_size = 64M thread_concurrency = 4 thread_stack = 128K tmp_table_size = 64M binlog_cache_size = 2M max_binlog_size = 128M max_binlog_cache_size = 512M max_relay_log_size = 128M bulk_insert_buffer_size = 8M myisam_repair_threads = 1 skip-bdb #如果不需要使用innodb就關(guān)閉該選項 #skip-innodb innodb_data_home_dir = /usr/local/server/mysql/data/ innodb_data_file_path = ibdata1:256M;ibdata2:256M:autoextend innodb_log_group_home_dir = /usr/local/server/mysql/data/ innodb_log_arch_dir = /usr/local/server/mysql/data/ innodb_buffer_pool_size = 512M innodb_additional_mem_pool_size = 8M innodb_log_file_size = 128M innodb_log_buffer_size = 8M innodb_lock_wait_timeout = 50 innodb_flush_log_at_trx_commit = 2 innodb_file_io_threads = 4 innodb_thread_concurrency = 16 innodb_log_files_in_group = 3 以上配置參數(shù)請根據(jù)具體的需要稍作修改。運行以下命令即可啟動 MySQL 服務(wù)器: /usr/local/server/mysql/bin/mysqld_safe \ --defaults-file=/usr/local/server/mysql/data/my.cnf & 由于 MySQL 不是安裝在標(biāo)準(zhǔn)目錄下,因此必須要修改 mysqld_safe 中的 my_print_defaults 文件所在位置,才能通過 mysqld_safe 來啟動 MySQL 服務(wù)器。 3.) memcache + libevent 安裝編譯安裝: [root@localhost]#cd libevent-1.2a [root@localhost]#./configure --prefix=/usr/ && make && make install [root@localhost]#cd ../memcached-1.2.0 [root@localhost]#./configure --prefix=/usr/local/server/memcached --with-libevent=/usr/ [root@localhost]#make && make install 備注:如果 libevent 不是安裝在 /usr 目錄下,那么需要把 libevent-1.2a.so.1 拷貝/鏈接到 /usr/lib 中,否則 memcached 無法正常加載。運行以下命令來啟動 memcached: [root@localhost]#/usr/local/server/memcached/bin/memcached \ -l 192.168.8.1 -d -p 10000 -u nobody -m 128 表示用 daemon 的方式啟動 memcached,監(jiān)聽在 192.168.8.1 的 10000 端口上,運行用戶為 nobody,為其分配 128MB 的內(nèi)存。 四、 安裝Apache、PHP、eAccelerator、php-memcache 四、) 安裝Apache、PHP、eAccelerator、php-memcache由于Apache 2下的php靜態(tài)方式編譯十分麻煩,因此在這里采用動態(tài)模塊(DSO)方式。1.) 安裝Apache 2.2.3 [root@localhost]#./configure --prefix=/usr/local/server/apache --disable-userdir --disable-actions \ --disable-negotiation --disable-autoindex --disable-filter --disable-include --disable-status \ --disable-asis --disable-auth --disable-authn-default --disable-authn-file --disable-authz-groupfile \ --disable-authz-host --disable-authz-default --disable-authz-user --disable-userdir \ --enable-expires --enable-module=so 備注:在這里,取消了一些不必要的模塊,如果你需要用到這些模塊,那么請去掉部分參數(shù)。 2.) 安裝PHP 5.2.0 [root@localhost]#./configure --prefix=/usr/local/server/php --with-mysql \ --with-apxs2=/usr/local/server/apache/bin/apxs --with-freetype-dir=/usr/ --with-png-dir=/usr/ \ --with-gd=/usr/ --with-jpeg-dir=/usr/ --with-zlib --enable-magic-quotes --with-iconv \ --without-sqlite --without-pdo-sqlite --with-pdo-mysql --disable-dom --disable-simplexml \ --enable-roxen-zts [root@localhost]#make && make install 備注:如果不需要gd或者pdo等模塊,請自行去掉。 3.) 安裝eAccelerator-0.9.5 [root@localhost]#cd eAccelerator-0.9.5 [root@localhost]#export PHP_PREFIX=/usr/local/server/php [root@localhost]#$PHP_PREFIX/bin/phpize [root@localhost]#./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config [root@localhost]#make && make install 4.) 安裝memcache模塊 [root@localhost]#cd memcache-2.1.0 [root@localhost]#export PHP_PREFIX=/usr/local/server/php [root@localhost]#$PHP_PREFIX/bin/phpize [root@localhost]#./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config [root@localhost]#make && make install 5.) 修改 php.ini 配置然后修改 php.ini,修改/加入類似以下內(nèi)容: extension_dir = "/usr/local/server/php/lib/" extension="eaccelerator.so" eaccelerator.shm_size="32" ;設(shè)定eaccelerator的共享內(nèi)存為32MB eaccelerator.cache_dir="/usr/local/server/eaccelerator" eaccelerator.enable="1" eaccelerator.optimizer="1" eaccelerator.check_mtime="1" eaccelerator.debug="0" eaccelerator.filter="*.php" eaccelerator.shm_max="0" eaccelerator.shm_ttl="0" eaccelerator.shm_prune_period="3600" eaccelerator.shm_only="0" eaccelerator.compress="1" eaccelerator.compress_level="9" eaccelerator.log_file = "/usr/local/server/apache/logs/eaccelerator_log" eaccelerator.allowed_admin_path = "/usr/local/server/apache/htdocs/ea_admin" extension="memcache.so" 在這里,最好是在apache的配置中增加默認(rèn)文件類型的cache機(jī)制,即利用apache的expires模塊,新增類似如下幾行: ExpiresActive On ExpiresByType text/html "access plus 10 minutes" ExpiresByType text/css "access plus 1 day" ExpiresByType image/jpg "access 1 month" ExpiresByType image/gif "access 1 month" ExpiresByType image/jpg "access 1 month" ExpiresByType application/x-shockwave-flash "access plus 3 day" 這么設(shè)置是由于我的這些靜態(tài)文件通常很少更新,因此我選擇的是"access"規(guī)則,如果更新相對比較頻繁,可以改用"modification"規(guī)則;或者也可以用"access"規(guī)則,但是在文件更新的時候,執(zhí)行一下"touch"命令,把文件的時間刷新一下即可。 五、 安裝Squid 五、) 安裝Squid [root@localhost]#./configure --prefix=/usr/local/server/squid --enable-async-io=100 --disable-delay-pools --disable-mem-gen-trace --disable-useragent-log --enable-kill-parent-hack --disable-arp-acl --enable-epoll --disable-ident-lookups --enable-snmp --enable-large-cache-files --with-large-files [root@localhost]#make && make install 或使用如下安裝方法: [root@localhost]#yum install squid 如果是2.6的內(nèi)核,才能支持epoll的IO模式,舊版本的內(nèi)核則只能選擇poll或其他模式了;另外,記得帶上支持大文件的選項,否則在access log等文件達(dá)到2G的時候就會報錯。設(shè)定 squid 的配置大概如下內(nèi)容: #設(shè)定緩存目錄為 /var/cache1 和 /var/lib/squid,每次處理緩存大小為128MB,當(dāng)緩存空間使用達(dá)到95%時 #新的內(nèi)容將取代舊的而不直接添加到目錄中,直到空間又下降到90%才停止這一活動 #/var/cache1 最大1024MB,/var/lib/squid 最大 5000MB,都是 16*256 級子目錄 cache_dir aufs /var/cache1 1024 16 256 cache_dir aufs /var/lib/squid 5000 16 256 cache_mem 128 MB cache_swap_low 90 cache_swap_high 95 #設(shè)置存儲策略等 maximum_object_size 4096 KB minimum_object_size 0 KB maximum_object_size_in_memory 80 KB ipcache_size 1024 ipcache_low 90 ipcache_high 95 cache_replacement_policy lru memory_replacement_policy lru #設(shè)置超時策略 forward_timeout 20 seconds connect_timeout 15 seconds read_timeout 3 minutes request_timeout 1 minutes persistent_request_timeout 15 seconds client_lifetime 15 minutes shutdown_lifetime 5 seconds negative_ttl 10 seconds #限制一個ip最大只能有16個連接 acl OverConnLimit maxconn 16 http_access deny OverConnLimit #限制baidu spider訪問 #acl AntiBaidu req_header User-Agent Baiduspider #http_access deny AntiBaidu #常規(guī)設(shè)置 visible_hostname cache. cache_mgr webmaster@ client_persistent_connections off server_persistent_connections on cache_effective_user nobody cache_effective_group nobody tcp_recv_bufsize 65535 bytes half_closed_clients off #設(shè)定不緩存的規(guī)則 hierarchy_stoplist cgi-bin acl QUERY urlpath_regex cgi-bin cache deny QUERY #不要相信ETag 因為有g(shù)zip acl apache rep_header Server ^Apache broken_vary_encoding allow apache #設(shè)置access log,并且令其格式和apache的格式一樣,方便awstats分析 emulate_httpd_log on logformat apache %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs % 初始化和啟動squid [root@localhost]#/usr/local/server/squid/sbin/squid -z [root@localhost]#/usr/local/server/squid/sbin/squid 第一條命令是先初始化squid緩存哈希子目錄,只需執(zhí)行一次即可。 六、后記 六、后記一、)想要啟用squid所需的改變想要更好的利用squid的cache功能,不是把它啟用了就可以的,我們需要做以下幾個調(diào)整: 1、啟用apache的 mod_expires 模塊,修改 httpd.conf,加入以下內(nèi)容: #expiresdefault "modification plus 2 weeks"expiresactive onexpiresbytype text/html "access plus 10 minutes"expiresbytype image/gif "modification plus 1 month"expiresbytype image/jpeg "modification plus 1 month"expiresbytype image/png "modification plus 1 month"expiresbytype text/css "access plus 1 day"expiresbytype application/x-shockwave-flash "access plus 3 day" 以上配置的作用是規(guī)定各種類型文件的cache規(guī)則,對那些圖片/flash等靜態(tài)文件總是cache起來,可根據(jù)各自的需要做適當(dāng)調(diào)整。 2、修改 php.ini 配置,如下: session.cache_limiter = nocache 以上配置的作用是默認(rèn)取消php中的cache功能,避免不正常的cache產(chǎn)生。 3、修改應(yīng)用程序例如,有一個php程序頁面static.php,它存放著某些查詢數(shù)據(jù)庫后的結(jié)果,并且數(shù)據(jù)更新并不頻繁,于是,我們就可以考慮對其cache。只需在static.php中加入類似如下代碼: header('Cache-Control: max-age=86400 ,must-revalidate');header('Pragma:');header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT' );header("Expires: " .gmdate ('D, d M Y H:i:s', time() + '86400' ). ' GMT'); 以上代碼的意思是,輸出一個http頭部信息,讓squid知道本頁面默認(rèn)緩存時長為一天。 二、)squidclient簡要介紹 *取得squid運行狀態(tài)信息: squidclient -p 80 mgr:info *取得squid內(nèi)存使用情況: squidclient -p 80 mgr:mem *取得squid已經(jīng)緩存的列表: squidclient -p 80 mgr:objects. use it carefully,it may crash *取得squid的磁盤使用情況: squidclient -p 80 mgr:diskd *強(qiáng)制更新某個url:squidclient -p 80 -m PURGE http://www./static.php *更多的請查看:squidclient-h 或者 squidclient -p 80 mgr: |
|