squid+nginx+apache

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

  1. ./configure   \
  2. "--prefix=/usr/local/server/nginx" \
  3. "--with-openssl=/usr/include" \
  4. "--with-pcre=/usr/include/pcre/" \
  5. "--with-http_stub_status_module" \
  6. "--without-http_memcached_module" \
  7. "--without-http_fastcgi_module" \
  8. "--without-http_rewrite_module" \
  9. "--without-http_map_module" \
  10. "--without-http_geo_module" \
  11. "--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

  1. #運行用戶
  2. user nobody nobody;
  3. #啓動進程
  4. worker_processes 2;
  5. #全局錯誤日誌及PID文件
  6. error_log logs/error.log notice;
  7. pid logs/nginx.pid;
  8. #工做模式及鏈接數上限
  9. events {
  10. use epoll;
  11. worker_connections 1024;
  12. }
  13. #設定http服務器,利用它的反向代理功能提供負載均衡支持
  14. http {
  15. #設定mime類型
  16. include conf/mime.types;
  17. default_type application/octet-stream;
  18. #設定日誌格式
  19. log_format main '$remote_addr - $remote_user [$time_local] '
  20. '"$request" $status $bytes_sent '
  21. '"$http_referer" "$http_user_agent" '
  22. '"$gzip_ratio"';
  23. log_format download '$remote_addr - $remote_user [$time_local] '
  24. '"$request" $status $bytes_sent '
  25. '"$http_referer" "$http_user_agent" '
  26. '"$http_range" "$sent_http_content_range"';
  27. #設定請求緩衝
  28. client_header_buffer_size 1k;
  29. large_client_header_buffers 4 4k;
  30. #開啓gzip模塊
  31. gzip on;
  32. gzip_min_length 1100;
  33. gzip_buffers 4 8k;
  34. gzip_types text/plain;
  35. output_buffers 1 32k;
  36. postpone_output 1460;
  37. #設定access log
  38. access_log logs/access.log main;
  39. client_header_timeout 3m;
  40. client_body_timeout 3m;
  41. send_timeout 3m;
  42. sendfile on;
  43. tcp_nopush on;
  44. tcp_nodelay on;
  45. keepalive_timeout 65;
  46. #設定負載均衡的服務器列表
  47. upstream mysvr {
  48. #weigth參數表示權值,權值越高被分配到的概率越大
  49. #本機上的Squid開啓3128端口
  50. server 192.168.8.1:3128 weight=5;
  51. server 192.168.8.2:80 weight=1;
  52. server 192.168.8.3:80 weight=6;
  53. }
  54. #設定虛擬主機
  55. server {
  56. listen 80;
  57. server_name 192.168.8.1 www.yejr.com;
  58. charset gb2312;
  59. #設定本虛擬主機的訪問日誌
  60. access_log logs/www.yejr.com.access.log main;
  61. #若是訪問 /img/*, /js/*, /css/* 資源,則直接取本地文件,不經過squid
  62. #若是這些文件較多,不推薦這種方式,由於經過squid的緩存效果更好
  63. location ~ ^/(img|js|css)/ {
  64. root /data3/Html;
  65. expires 24h;
  66. }
  67. #對 "/" 啓用負載均衡
  68. location / {
  69. proxy_pass http://mysvr;
  70. proxy_redirect off;
  71. proxy_set_header Host $host;
  72. proxy_set_header X-Real-IP $remote_addr;
  73. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  74. client_max_body_size 10m;
  75. client_body_buffer_size 128k;
  76. proxy_connect_timeout 90;
  77. proxy_send_timeout 90;
  78. proxy_read_timeout 90;
  79. proxy_buffer_size 4k;
  80. proxy_buffers 4 32k;
  81. proxy_busy_buffers_size 64k;
  82. proxy_temp_file_write_size 64k;
  83. }
  84. #設定查看Nginx狀態的地址
  85. location /NginxStatus {
  86. stub_status on;
  87. access_log on;
  88. auth_basic "NginxStatus";
  89. auth_basic_user_file conf/htpasswd;
  90. }
  91. }
  92. }
複製代碼

備註:conf/htpasswd 文件的內容用 apache 提供的 htpasswd 工具來產生便可,內容大體以下:
3.) 查看 Nginx 運行狀態
輸入地址 http://192.168.8.1/NginxStatus/,輸入驗證賬號密碼,便可看到相似以下內容:
html

  1. Active connections: 328
  2. server accepts handled requests
  3. 9309 8982 28890
  4. 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

  1. [mysqld]
  2. basedir = /usr/local/server/mysql
  3. datadir = /usr/local/server/mysql/data
  4. user = nobody
  5. port = 3306
  6. socket = /tmp/mysql.sock
  7. wait_timeout = 30
  8. long_query_time=1
  9. #log-queries-not-using-indexes = TRUE
  10. log-slow-queries=/usr/local/server/mysql/slow.log
  11. log-error = /usr/local/server/mysql/error.log
  12. external-locking = FALSE
  13. key_buffer_size = 512M
  14. back_log = 400
  15. table_cache = 512
  16. sort_buffer_size = 2M
  17. join_buffer_size = 4M
  18. read_buffer_size = 2M
  19. read_rnd_buffer_size = 4M
  20. myisam_sort_buffer_size = 64M
  21. thread_cache_size = 32
  22. query_cache_limit = 2M
  23. query_cache_size = 64M
  24. thread_concurrency = 4
  25. thread_stack = 128K
  26. tmp_table_size = 64M
  27. binlog_cache_size = 2M
  28. max_binlog_size = 128M
  29. max_binlog_cache_size = 512M
  30. max_relay_log_size = 128M
  31. bulk_insert_buffer_size = 8M
  32. myisam_repair_threads = 1
  33. skip-bdb
  34. #若是不須要使用innodb就關閉該選項
  35. #skip-innodb
  36. innodb_data_home_dir = /usr/local/server/mysql/data/
  37. innodb_data_file_path = ibdata1:256M;ibdata2:256M:autoextend
  38. innodb_log_group_home_dir = /usr/local/server/mysql/data/
  39. innodb_log_arch_dir = /usr/local/server/mysql/data/
  40. innodb_buffer_pool_size = 512M
  41. innodb_additional_mem_pool_size = 8M
  42. innodb_log_file_size = 128M
  43. innodb_log_buffer_size = 8M
  44. innodb_lock_wait_timeout = 50
  45. innodb_flush_log_at_trx_commit = 2
  46. innodb_file_io_threads = 4
  47. innodb_thread_concurrency = 16
  48. 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

  1. ./configure   \
  2. "--prefix=/usr/local/server/apache" \
  3. "--disable-userdir" \
  4. "--disable-actions" \
  5. "--disable-negotiation" \
  6. "--disable-autoindex" \
  7. "--disable-filter" \
  8. "--disable-include" \
  9. "--disable-status" \
  10. "--disable-asis" \
  11. "--disable-auth" \
  12. "--disable-authn-default" \
  13. "--disable-authn-file" \
  14. "--disable-authz-groupfile" \
  15. "--disable-authz-host" \
  16. "--disable-authz-default" \
  17. "--disable-authz-user" \
  18. "--disable-userdir" \
  19. "--enable-expires" \
  20. "--enable-module=so" \
複製代碼

備註:在這裏,取消了一些沒必要要的模塊,若是你須要用到這些模塊,那麼請去掉部分參數。
2.) 安裝PHP 5.2.0
[root@localhost]#
linux

  1. ./configure \
  2. "--prefix=/usr/local/server/php" \
  3. "--with-mysql" \
  4. "--with-apxs2=/usr/local/server/apache/bin/apxs" \
  5. "--with-freetype-dir=/usr/" \
  6. "--with-png-dir=/usr/" \
  7. "--with-gd=/usr/" \
  8. "--with-jpeg-dir=/usr/" \
  9. "--with-zlib" \
  10. "--enable-magic-quotes" \
  11. "--with-iconv" \
  12. "--without-sqlite" \
  13. "--without-pdo-sqlite" \
  14. "--with-pdo-mysql" \
  15. "--disable-dom" \
  16. "--disable-simplexml" \
  17. "--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

  1. extension_dir = "/usr/local/server/php/lib/"
  2. extension="eaccelerator.so"
  3. eaccelerator.shm_size="32" ;設定eaccelerator的共享內存爲32MB
  4. eaccelerator.cache_dir="/usr/local/server/eaccelerator"
  5. eaccelerator.enable="1"
  6. eaccelerator.optimizer="1"
  7. eaccelerator.check_mtime="1"
  8. eaccelerator.debug="0"
  9. eaccelerator.filter="*.php"
  10. eaccelerator.shm_max="0"
  11. eaccelerator.shm_ttl="0"
  12. eaccelerator.shm_prune_period="3600"
  13. eaccelerator.shm_only="0"
  14. eaccelerator.compress="1"
  15. eaccelerator.compress_level="9"
  16. eaccelerator.log_file = "/usr/local/server/apache/logs/eaccelerator_log"
  17. eaccelerator.allowed_admin_path = "/usr/local/server/apache/htdocs/ea_admin"
  18. extension="memcache.so"
複製代碼

在這裏,最好是在apache的配置中增長默認文件類型的cache機制,即利用apache的expires模塊,新增相似以下幾行:web

  1. ExpiresActive On
  2. ExpiresByType text/html "access plus 10 minutes"
  3. ExpiresByType text/css "access plus 1 day"
  4. ExpiresByType image/jpg "access 1 month"
  5. ExpiresByType image/gif "access 1 month"
  6. ExpiresByType image/jpg "access 1 month"
  7. ExpiresByType application/x-shockwave-flash "access plus 3 day"
複製代碼

這麼設置是因爲個人這些靜態文件一般不多更新,所以我選擇的是"access"規則,若是更新相對比較頻繁,能夠改用"modification"規則;或者也能夠用"access"規則,可是在文件更新的時候,執行一下"touch"命令,把文件的時間刷新一下便可。

5、 安裝Squid 

5、) 安裝Squid
[root@localhost]#
sql

  1. ./configure \
  2. "--prefix=/usr/local/server/squid" \
  3. "--enable-async-io=100" \
  4. "--disable-delay-pools" \
  5. "--disable-mem-gen-trace" \
  6. "--disable-useragent-log" \
  7. "--enable-kill-parent-hack" \
  8. "--disable-arp-acl" \
  9. "--enable-epoll" \
  10. "--disable-ident-lookups" \
  11. "--enable-snmp" \
  12. "--enable-large-cache-files" \
  13. "--with-large-files" \
複製代碼

[root@localhost]#make && make install
或使用以下安裝方法:
[root@localhost]#yum install squid
若是是2.6的內核,才能支持epoll的IO模式,舊版本的內核則只能選擇poll或其餘模式了;另外,記得帶上支持大文件的選項,不然在access
log等文件達到2G的時候就會報錯。設定 squid 的配置大概以下內容:
數據庫

  1. #設定緩存目錄爲 /var/cache1 和 /var/lib/squid,每次處理緩存大小爲128MB,當緩存空間使用達到95%時
  2. #新的內容將取代舊的而不直接添加到目錄中,直到空間又降低到90%才中止這一活動
  3. #/var/cache1 最大1024MB,/var/lib/squid 最大 5000MB,都是 16*256 級子目錄
  4. cache_dir aufs /var/cache1 1024 16 256
  5. cache_dir aufs /var/lib/squid 5000 16 256
  6. cache_mem 128 MB
  7. cache_swap_low 90
  8. cache_swap_high 95
  9. #設置存儲策略等
  10. maximum_object_size 4096 KB
  11. minimum_object_size 0 KB
  12. maximum_object_size_in_memory 80 KB
  13. ipcache_size 1024
  14. ipcache_low 90
  15. ipcache_high 95
  16. cache_replacement_policy lru
  17. memory_replacement_policy lru
  18. #設置超時策略
  19. forward_timeout 20 seconds
  20. connect_timeout 15 seconds
  21. read_timeout 3 minutes
  22. request_timeout 1 minutes
  23. persistent_request_timeout 15 seconds
  24. client_lifetime 15 minutes
  25. shutdown_lifetime 5 seconds
  26. negative_ttl 10 seconds
  27. #限制一個ip最大隻能有16個鏈接
  28. acl OverConnLimit maxconn 16
  29. http_access deny OverConnLimit
  30. #限制baidu spider訪問
  31. #acl AntiBaidu req_header User-Agent Baiduspider
  32. #http_access deny AntiBaidu
  33. #常規設置
  34. visible_hostname cache.yejr.com
  35. cache_mgr webmaster@yejr.com
  36. client_persistent_connections off
  37. server_persistent_connections on
  38. cache_effective_user nobody
  39. cache_effective_group nobody
  40. tcp_recv_bufsize 65535 bytes
  41. half_closed_clients off
  42. #設定不緩存的規則
  43. hierarchy_stoplist cgi-bin
  44. acl QUERY urlpath_regex cgi-bin
  45. cache deny QUERY
  46. #不要相信ETag 由於有gzip
  47. acl apache rep_header Server ^Apache
  48. broken_vary_encoding allow apache
  49. #設置access log,而且令其格式和apache的格式同樣,方便awstats分析
  50. emulate_httpd_log on
  51. 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,加入如下內容:

  1. #expiresdefault "modification plus 2 weeks"expiresactive
  2. onexpiresbytype text/html "access plus 10 minutes"expiresbytype
  3. image/gif "modification plus 1 month"expiresbytype image/jpeg "modification
  4. plus 1 month"expiresbytype image/png "modification plus 1
  5. month"expiresbytype text/css "access plus 1 day"expiresbytype
  6. application/x-shockwave-flash "access plus 3 day"
複製代碼

以上配置的做用是規定各類類型文件的cache規則,對那些圖片/flash等靜態文件老是cache起來,可根據各自的須要作適當調整。
二、修改 php.ini 配置,以下:

  1. session.cache_limiter = nocache
複製代碼

以上配置的做用是默認取消php中的cache功能,避免不正常的cache產生。
三、修改應用程序例如,有一個php程序頁面static.php,它存放着某些查詢數據庫後的結果,而且數據更新並不頻繁,因而,咱們就能夠考慮對其cache。只需在static.php中加入相似以下代碼:

  1. header('Cache-Control: max-age=86400
  2. ,must-revalidate');header('Pragma:');header('Last-Modified: ' .
  3. gmdate('D, d M Y H:i:s') . ' GMT' );header("Expires: " .gmdate ('D, d M Y
  4. 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 mgrbjects. 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:

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息