nginx0.8 + php-5.3.4 + memcached #-- 編碼轉換庫 Unicode和其他的傳統編碼之間轉換的需求 --# wget http://mozbuildtools.googlecode.com/files/libiconv-1.13.1.tar.gz tar -zxvf libiconv-1.13.1.tar.gz cd libiconv-1.13.1 ./configure --prefix=/usr/local && make && make install #-- 修改動態鏈接載入的目錄查找文件 --# echo "/usr/local/lib" >> /etc/ld.so.conf /sbin/ldconfig #-- libmcrypt是加密算法擴展庫 --# wget http://www.sfr-fresh.com/unix/privat/libmcrypt-2.5.8.tar.gz tar -zxvf libmcrypt-2.5.8.tar.gz cd libmcrypt-2.5.8/ ./configure && make && make install /sbin/ldconfig cd libltdl/ ./configure --enable-ltdl-install && make && make install #-- 支持多種哈稀演算法的函數庫(php使用哈稀) --# wget http://cdnetworks-kr-1.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz tar -zxvf mhash-0.9.9.9.tar.gz cd mhash-0.9.9.9 ./configure && make && make install ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config #-- mcrypt是一個雙向加密算法 函數庫 (php) --# wget http://www.sfr-fresh.com/unix/privat/mcrypt-2.6.8.tar.gz tar -zxvf mcrypt-2.6.8.tar.gz cd mcrypt-2.6.8 LD_LIBRARY_PATH=/usr/local/lib ./configure && make && make install *********************************************************** #-- libevent作爲一個事件與回調框架,它提供了一些接口,同時也提供了一些處理IO與信號的流程 --# wget http://monkey.org/~provos/libevent-2.0.9-rc.tar.gz tar -zxvf libevent-2.0.9-rc.tar.gz cd libevent-2.0.9-rc ./configure --prefix=/usr/local/libevent && make && make install出的。。 wget http://jaist.dl.sourceforge.net/project/buluoos/0.1/src/jpegsrc.v8b.tar.gz tar xzvf jpegsrc.v8b.tar.gz cd jpegsrc.v8b ./configure --enable-static --enable-shared && make && make install wget http://www.sfr-fresh.com/unix/misc/libpng-1.2.44.tar.gz tar xzvf libpng-1.2.44.tar.gz cd libpng-1.2.44 ./configure && make && make install 安裝報錯:沒有找到libpng.so文件時,需要安裝libpng-devel和freetype-devel兩個包 wget http://www.libgd.org/releases/gd-2.0.35.tar.gz tar xzvf gd-2.0.35.tar.gz cd gd-2.0.35 ./configure --with-png --with-freetype --with-jpeg make (如果出錯,就執行make clean,然後再make) make install #-- memcached (memcache 服務端)--# wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz tar -zxvf memcached-1.4.5.tar.gz cd memcached-1.4.5 ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent && make && make install ln -s /usr/local/libevent/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5 /usr/local/memcached/bin/memcached -d -m 128 -l 0.0.0.0 -p 11211 -u root #啓動memcached ***** ( -d 以守護程序(daemon)方式運行 memcached; #啓動參數說明 -m 設置 memcached 可以使用的內存大小,單位爲 M; -M 設置memcached 不LRU -l 設置監聽的 IP 地址,如果是本機的話,通常可以不設置此參數; -p 設置監聽的端口,默認爲 11211,所以也可以不設置此參數; -u 指定用戶,如果當前爲 root 的話,需要使用此參數指定用戶。 -P 記錄memcache運行的PID到文件/usr/local/bin/memcached.pid -n 設置初始chunk的大小 -f chunk size factor的增長因子,默認是1.25 ) #--nginx pcre 包含了perl兼容的正規表達式庫,些在執行正規表達式模式匹配時用 --# wget http://www.sfr-fresh.com/unix/misc/pcre-8.11.tar.gz tar -zxvf pcre-8.11.tar.gz cd pcre-8.11 ./configure && make && make install #-- nginx --# wget http://nginx.org/download/nginx-0.8.54.tar.gz tar -zxvf nginx-0.8.54.tar.gz cd nginx-0.8.54 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make && make install 附加:如果nginx安裝有錯誤,就是zlib庫、openssl庫、pcre庫沒有安裝,需要安裝後纔開始安裝nginx,可以選擇tar包安裝,也可以進行yum安裝,執行yum語句:yum -y install pcre-devel yum -y install openssl openssl-devel yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel yum -y install gcc gcc-c++ autoconf automake #-- libjpeg --# locate libjpeg.so cd /usr/lib/ ln -s /usr/lib/libjpeg.so.62 /usr/lib/libjpeg.so ln -s /usr/lib64/libjpeg.so.62 /usr/lib64/libjpeg.so #-- php php-5.3.3 或 以上已經包含了php-fpm--# wget http://at2.php.net/distributions/php-5.3.4.tar.gz tar -zxvf php-5.3.4.tar.gz cd php-5.3.4 ./configure \ --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --with-curl \ --with-curlwrappers \ --enable-gd-native-ttf \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-iconv-dir=/usr/local/libiconv \ --with-libxml-dir=/usr/local/ \ --with-mhash \ --with-mcrypt \ --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-openssl \ --with-xmlrpc \ --with-zlib \ --disable-debug \ --disable-rpath \ --enable-bcmath \ --enable-fpm \ --enable-inline-optimization \ --enable-mbregex \ --enable-mbstring \ --enable-pcntl \ --enable-safe-mode \ --enable-shmop \ --enable-soap \ --enable-sockets \ --enable-sysvsem \ --enable-xml \ --enable-zip \ --with-libdir=lib64 \ --without-pear #-- --with-libdir=lib64 64位系統添加此項 否則去掉 --# 安裝報錯:沒有找到libpng.so文件時,需要安裝libpng-devel和freetype-devel兩個包 可以利用yum來安裝 make ZEND_EXTRA_LIBS='-liconv' && make install cp ./php.ini-production /usr/local/php/etc/php.ini #-- zlib 數據壓縮庫 --# wget http://www.imagemagick.org/download/delegates/zlib-1.2.5.tar.gz tar zxvf zlib-1.2.5.tar.gz cd zlib-1.2.5 ./configure --prefix=/usr/local/zlib --64 --libdir=/usr/lib make && make install #-- php 擴展memcache (memcache 客戶端) --# ##### wget http://pecl.php.net/get/memcache-3.0.5.tgz tar -zxvf memcache-3.0.5.tgz cd memcache-3.0.5 /usr/local/php/bin/phpize #(如果沒有configure文件,就執行:yum -y install autoconf) ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir=/usr/local/zlib make && make install 會出現下面的消息: (Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/) #-- apc 用於加速php APC提供兩種緩存功能 提供一些接口將用戶數據駐留在內存中 --# wget http://pecl.php.net/get/APC-3.1.4.tgz tar -zxvf APC-3.1.4.tgz cd APC-3.1.4 /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make && make install cp /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/apc.so /usr/local/php/lib/php/extensions/apc.so # xxxxno-debug-non-zts-20090626 是編譯apc 成功後輸出信息 chmod 755 /usr/local/php/lib/php/extensions/apc.so #-- 安裝完畢 開始配置 --# #-- php.ini 配置 --# vi /usr/local/php/etc/php.ini #extension_dir = "ext" 在這行下添加 以下 路徑是 extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/" ### extension=apc.so extension=imagick.so extension=memcache.so #再查找output_buffering=Off #修改爲output_buffering=On 再查找;cgi.fix_pathinfo=1 修改爲cgi.fix_pathinfo=0,防止Nginx文件類型錯誤解析漏洞 # 按 shift+g 鍵 跳到文件未尾 添加以下 [APC] apc.enabled=1 apc.ttl=7200 apc.shm_size=128M #每個共享內存塊的大小(以MB爲單位,建議值爲128~256)。 #要開啓上傳文件功能時要添加以下參數 apc.cache_by_default = on #是否默認對所有文件啓用緩衝 apc.shm_segments = 1 #爲編譯器緩衝區分配的共享內存塊數量(建議值爲1)。 apc.ttl = 600 #緩存條目在垃圾回收表中能夠存在的秒數 apc.user_ttl = 600 #類似於apc.ttl,只是針對每個用戶而言,建議值爲7200~36000 apc.num_files_hint = 0 #Web服務器上可能被包含或被請求的不同源文件的大致數量(建議值爲1024~4096)。能確定,則設爲 0 apc.write_lock = On #是否啓用寫入鎖 apc.max_file_size = 1000M #設置apc所支持上傳文件的大小 upload_max_filesize = 1000M # #-- php-fpm 配置 --# cd /usr/local/php/etc/ cp php-fpm.conf.default php-fpm.conf vi php-fpm.conf ;emergency_restart_threshold = 0 -> emergency_restart_threshold = 10 // 錯誤的php進程線 ;emergency_restart_interval = 0 -> emergency_restart_interval = 1m // 表示在(60)秒內出現SIGSEGV或者SIGBUS錯誤的php-cgi進程數如果超過emergency_restart_threshold個,php-fpm就會重啓 ;process_control_timeout = 0 -> process_control_timeout = 5s //請求處理超時時間 ;daemonize = yes -> daemonize = yes //yes 後臺運行,no 前臺運行 listen = 127.0.0.1:9000 user = nobody -> user = www //更改運行用戶爲www group = nobody -> group = www pm = dynamic //設置進程管理器如何管理子進程,static - 子進程的數量是固定的,dynamic - 子進程的數量在下面配置的基礎上動態設置 pm.max_children = 200 //子進程的數量 1個略佔2MB內存,pm 設置爲 static 時表示創建的,pm 設置爲 dynamic 時表示最大可創建的 pm.start_servers = 50 //設置啓動時創建的子進程數目. 僅在 pm 設置爲 dynamic 時使用 pm.min_spare_servers = 30 //設置空閒服務進程的最低數目,pm 設置爲 dynamic 時使用 pm.max_spare_servers = 100 //設置空閒服務進程的最大數目 pm.max_requests = 1024 //每個子進程重生之前服務的請求數,對於可能存在內存泄漏的第三方模塊來說是非常有用的,如果設置爲 '0' 則一直接受請求 #500 request_terminate_timeout = 5m //設置單個請求的超時中止時間. 該選項可能會對php.ini設置中的'max_execution_time'因爲某些特殊原因沒有中止運行的腳本有用 request_slowlog_timeout = 30s //當一個請求該設置的超時時間後,就會將對應的PHP調用堆棧信息完整寫入到慢日誌中 rlimit_files = 65535 //設置文件打開描述符的rlimit限制,默認值: 系統定義值 rlimit_core = 0 //設置核心rlimit最大限制值. 可用值: 'unlimited' 、0或者正整數. 默認值: 系統定義值 ;chdir = /var/www //設置啓動目錄,啓動時會自動Chdir到該目錄. 所定義的目錄需要是絕對路徑 在這裏不需要 slowlog = log/$pool.log.slow (新添加的) #------------------------- end -----------------------# /usr/local/php/sbin/php-fpm start #啓動PHP ******* #-------- nginx.conf 配置 -----------------------------# vi /usr/local/nginx/conf/nginx.conf user www www; #運行用戶和組 worker_processes 8; #進程的數量,一般推薦爲處理器的個數. 可以適當增加,以避免進程在堵塞在IO等待中 error_log logs/error.log; #錯誤日誌 pid logs/nginx.pid; #pid文件位置 worker_rlimit_nofile 65535; events { use epoll; #Linux 下機能最好的 event 模式 #指定 nginx 處理進程的個數,根據硬件調整,和前面工作進程配合起來用,儘量大,但是別把cpu跑到100%就行 #其與總處理量的關係用公式表達如下: #MaxClient = worker_processes * worker_connections #因此這兩個數的乘積若大於系統最大可用tcp/ip棧數是沒有意義 . worker_connections 4096; } #-------- HTTP 請求設置 ---------# http { include mime.types; #載入mime類型 default_type application/octet-stream; #默認類型 #------------- 日誌的格式 -----------------# #log_format main '$remote_addr - $remote_user [$time_local] $request ' # '"$status" $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #-- 設定請求緩衝 --# server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay 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; #-- 開啓gzip模塊 --# gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; limit_zone crawler $binary_remote_addr 10m; #限制寬帶 10MB limit_conn crawler 10; #限制單個IP 併發數 10 proxy_set_header X-Real-IP $remote_addr; #獲取真實ip #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #獲取代理者的真實ip #---- 服務器設置 ---# server { listen 80; #監聽端口 server_name localhost; #WEB服務主機名(域名) #charset utf-8; access_log logs/localhost.access.log access; #訪問日誌 #-- 請求規則 默認請求 --# location / { root /home/www/www; #WEB根目錄 index index.html index.htm index.php; #默認索引文件名 #保留用戶真實信息 include proxy.conf; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 404 /404.html; #頁面不存在處理 error_page 500 502 503 504 /50x.html; #服務器錯誤定向 location = /50x.html { root /home/www/www; } #--- PHP 腳本請求全部轉發到 FastCGI處理. 使用FastCGI默認配置 ---# location ~ \.php$ { root /home/www/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name; 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 REQUEST_URI $request_uri; #保留用戶真實信息 include proxy.conf; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; include fastcgi_params; } # jsp java location ~* \.(jsp|do)$ { #auth_basic "this is sports center"; #auth_basic_user_file /usr/local/www/nginx_passwd; apache 密碼認證 nginx_passwd apache 密碼文件 proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 50m; client_body_buffer_size 256k; proxy_connect_timeout 30; proxy_send_timeout 30; proxy_read_timeout 60; proxy_buffer_size 256k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; proxy_max_temp_file_size 128m; proxy_pass http://localhost:8080/; } #------ 禁止訪問 .htxxx 文件 ------# location ~ /\.ht { deny all; } #------ 禁止IP訪問 ------# server{ server_name _; return 404; } #---------------------------- end -----------------------------------------------------------------# #----- 這個是反向代理的例子 ---# server { listen 80; server_name www.vinbet.com vinbet.com; location / { proxy_pass http://www.vinbet.com/; # vi /etc/host serverIP www.vinbet.com proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/proxy.access.log ; } #------- 靜態文件,nginx自己處理 -------# location ~ ^/(images|javascript|js|css|flash|media|static)/ { root /var/www/virtual/big.server.com/htdocs; #過期30天,靜態文件不怎麼更新,過期可以設大一點,如果頻繁更新,則可以設置得小一點。 expires 30d; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 1h; } #---- 把請求轉發給後臺web服務器,反向代理和fastcgi的區別是,反向代理後面是web服務器,fastcgi後臺是fasstcgi監聽進程,當然,協議也不一樣 --# location / { proxy_pass http://127.0.0.1:8080; } } #-- upstream的負載均衡,weight是權重,可以根據機器配置定義權重。據說nginx可以根據後臺響應時間調整。後臺需要多個web服務器 --# upstream big_server_com { server 127.0.0.3:8000 weight=5; server 127.0.0.3:8001 weight=5; server 192.168.0.1:8000; server 192.168.0.1:8001; } server { listen 80; server_name big.server.com; access_log logs/big.server.access.log main; #--- Nginx 狀況監控設置裝備擺設 ---# location ~ ^/NginxStatus/ { stub_status on; access_log off; } #--- 禁止訪問SEB-INF 目錄 ---# location ~ ^/(WEB-INF)/ { deny all; } location / { proxy_pass http://big_server_com; } } #------- upstream的負載均衡 end ----------------------# #------ Nginx 監控 ---------------------# #--- Nginx 狀況監控 ---# location ~ ^/NginxStatus/ { stub_status on; access_log off; } http://localhost/NginxStatus/ 就可以監控到 Nginx 的運行信息,表現的內容如下 Active connections: 70 server accepts handled requests 14553819 14553819 19239266 Reading: 0 Writing: 3 Waiting: 67 NginxStatus 表現的內容意思如下: •active connections – 當前 Nginx 正處置的勾當毗連數。 •server accepts handled requests — 統共處置了 14553819 個毗連 , 樂成建立 14553819 次握手 ( 證實中心沒有失敗的 ), 統共處置了 19239266 個懇求 ( 均勻每次握手處置了 1.3 個數據懇求 )。 •reading — nginx 讀取到客戶真個 Header 信息數。 •writing — nginx 返回給客戶真個 Header 信息數。 •waiting — 開啓 keep-alive 的環境下,這個值即是 active – (reading + writing),意思便是 Nginx 已經處置完正在等待下一次懇求指令的駐留毗連。 *****注意文件的權限問題*****
nginx0.8 + php-5.3.4 + memcached
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.