1、前言
2、編譯安裝
3、安裝MySQL、memcache
4、安裝Apache、PHP、eAccelerator、php-memcache
5、安裝Squid
6、後記
1、前言,準備工做
當前,LAMP開發模式是WEB開發的首選,如何搭建一個高效、可靠、穩定的WEB服務器一直是個熱門主題,本文就是這個主題的一次嘗試。
咱們採用的架構圖以下:
引用
---------- ------------- ---------------- -------------
| 客戶端 | ===> |負載均衡器| ===> |反向代理/緩存| ===> |WEB服務器|
---------- ------------- ---------------- -------------
---------------
===> |數據庫服務器|
---------------
Nginx.squid,Apache,PHP,MySQL
eAccelerator/memcache準備工做:
引用服務器: Intel(R) Xeon(TM) CPU 3.00GHz * 2, 2GB mem, SCISC 硬盤
操做系統:CentOs4.4,內核版本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(用於高速緩存經常使用數據)
libevent 1.2a(memcache工做機制所需)
MySQL 5.0.27(選用二進制版本,省去編譯工做)
Nginx 0.5.4(用作負載均衡器)
squid-2.6.STABLE6(作反向代理的同時提供專業緩存功能)
2、編譯安裝
1、) 安裝Nginx
1.) 安裝
Nginx發音爲[engine x],是由俄羅斯人Igor Sysoev創建的項目,基於BSD許可。聽說他當初是F5的成員之一,英文主頁:http://nginx.net。俄羅斯的一些大網站已經使用它超過兩年多了,一直表現不凡。
Nginx的編譯參數以下:
[root@localhost]#php
- ./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 模塊的支持。我已經安裝了 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行的位置上,註釋掉如下內容:
./configure --disable-shared
接下來,就能夠正常執行 make 及 make install 了。
2.) 修改配置文件 /usr/local/server/nginx/conf/nginx.conf
如下是個人 nginx.conf 內容,僅供參考:css
- #運行用戶
- user nobody nobody;
- #啓動進程
- worker_processes 2;
- #全局錯誤日誌及PID文件
- error_log logs/error.log notice;
- pid logs/nginx.pid;
- #工做模式及鏈接數上限
- events {
- use epoll;
- worker_connections 1024;
- }
- #設定http服務器,利用它的反向代理功能提供負載均衡支持
- http {
- #設定mime類型
- include conf/mime.types;
- default_type application/octet-stream;
- #設定日誌格式
- 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"';
- #設定請求緩衝
- 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;
- #設定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;
- #設定負載均衡的服務器列表
- upstream mysvr {
- #weigth參數表示權值,權值越高被分配到的概率越大
- #本機上的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;
- }
- #設定虛擬主機
- server {
- listen 80;
- server_name 192.168.8.1 www.yejr.com;
- charset gb2312;
- #設定本虛擬主機的訪問日誌
- access_log logs/www.yejr.com.access.log main;
- #若是訪問 /img/*, /js/*, /css/* 資源,則直接取本地文件,不經過squid
- #若是這些文件較多,不推薦這種方式,由於經過squid的緩存效果更好
- location ~ ^/(img|js|css)/ {
- root /data3/Html;
- expires 24h;
- }
- #對 "/" 啓用負載均衡
- 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;
- }
- #設定查看Nginx狀態的地址
- location /NginxStatus {
- stub_status on;
- access_log on;
- auth_basic "NginxStatus";
- auth_basic_user_file conf/htpasswd;
- }
- }
- }
複製代碼
備註:conf/htpasswd 文件的內容用 apache 提供的 htpasswd 工具來產生便可,內容大體以下:
3.) 查看 Nginx 運行狀態
輸入地址 http://192.168.8.1/NginxStatus/,輸入驗證賬號密碼,便可看到相似以下內容:html
- Active connections: 328
- server accepts handled requests
- 9309 8982 28890
- Reading: 1 Writing: 3 Waiting: 324
複製代碼
第一行表示目前活躍的鏈接數
第三行的第三個數字表示Nginx運行到當前時間接受到的總請求數,若是快達到了上限,就須要加大上限值了。
第四行是Nginx的隊列狀態
3、 安裝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 配置,增長部分優化參數,以下:
[root@localhost]#vi /usr/local/server/mysql/data/my.cnf
主要內容以下:node
- [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就關閉該選項
- #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
複製代碼
以上配置參數請根據具體的須要稍做修改。運行如下命令便可啓動 MySQL 服務器:
/usr/local/server/mysql/bin/mysqld_safe --defaults-file=/usr/local/server/mysql/data/my.cnf &
因爲 MySQL 不是安裝在標準目錄下,所以必需要修改 mysqld_safe 中的 my_print_defaults 文件所在位置,才能經過
mysqld_safe 來啓動 MySQL 服務器。
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,監聽在 192.168.8.1 的 10000 端口上,運行用戶爲 nobody,爲其分配128MB 的內存。
4、 安裝Apache、PHP、eAccelerator、php-memcache
因爲Apache2下的php靜態方式編譯十分麻煩,所以在這裏採用動態模塊(DSO)方式。
1.) 安裝Apache 2.2.3
[root@localhost]#mysql
- ./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" \
複製代碼
備註:在這裏,取消了一些沒必要要的模塊,若是你須要用到這些模塊,那麼請去掉部分參數。
2.) 安裝PHP 5.2.0
[root@localhost]#linux
- ./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,修改/加入相似如下內容:nginx
- extension_dir = "/usr/local/server/php/lib/"
- extension="eaccelerator.so"
- eaccelerator.shm_size="32" ;設定eaccelerator的共享內存爲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的配置中增長默認文件類型的cache機制,即利用apache的expires模塊,新增相似以下幾行:web
- 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"
複製代碼
這麼設置是因爲個人這些靜態文件一般不多更新,所以我選擇的是"access"規則,若是更新相對比較頻繁,能夠改用"modification"規則;或者也能夠用"access"規則,可是在文件更新的時候,執行一下"touch"命令,把文件的時間刷新一下便可。
5、 安裝Squid
5、) 安裝Squid
[root@localhost]#sql
- ./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的內核,才能支持epoll的IO模式,舊版本的內核則只能選擇poll或其餘模式了;另外,記得帶上支持大文件的選項,不然在access
log等文件達到2G的時候就會報錯。設定 squid 的配置大概以下內容:數據庫
- #設定緩存目錄爲 /var/cache1 和 /var/lib/squid,每次處理緩存大小爲128MB,當緩存空間使用達到95%時
- #新的內容將取代舊的而不直接添加到目錄中,直到空間又降低到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
- #設置存儲策略等
- 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
- #設置超時策略
- 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
- #常規設置
- visible_hostname cache.yejr.com
- cache_mgr webmaster@yejr.com
- 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
- #設定不緩存的規則
- hierarchy_stoplist cgi-bin
- acl QUERY urlpath_regex cgi-bin
- cache deny QUERY
- #不要相信ETag 由於有gzip
- acl apache rep_header Server ^Apache
- broken_vary_encoding allow apache
- #設置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緩存哈希子目錄,只需執行一次便可。
6、後記
6、後記1、)想要啓用squid所需的改變想要更好的利用squid的cache功能,不是把它啓用了就能夠的,咱們須要作如下幾個調整:
一、啓用apache的 mod_expires 模塊,修改 httpd.conf,加入如下內容:
- #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"
複製代碼
以上配置的做用是規定各類類型文件的cache規則,對那些圖片/flash等靜態文件老是cache起來,可根據各自的須要作適當調整。
二、修改 php.ini 配置,以下:
- session.cache_limiter = nocache
複製代碼
以上配置的做用是默認取消php中的cache功能,避免不正常的cache產生。
三、修改應用程序例如,有一個php程序頁面static.php,它存放着某些查詢數據庫後的結果,而且數據更新並不頻繁,因而,咱們就能夠考慮對其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知道本頁面默認緩存時長爲一天。
2、)squidclient簡要介紹
*取得squid運行狀態信息: squidclient -p 80 mgr:info
*取得squid內存使用狀況: squidclient -p 80 mgr:mem
*取得squid已經緩存的列表: squidclient -p 80 mgr
bjects. use it carefully,it may crash
*取得squid的磁盤使用狀況: squidclient -p 80 mgr:diskd
*強制更新某個url:squidclient -p 80 -m PURGE http://www.abc.com/static.php*更多的請查看:squidclient-h 或者 squidclient -p 80 mgr: