本次實驗是為了解決單台的web伺服器通路壓力過大而進行的,這裡用雙web伺服器加單獨的Mysql伺服器來實作;平台是RedHat Linux 5.8系統。
具體實作方案:

上圖中的web1上将安裝:Nginx、DNS、NFS;web2隻安裝Nginx;Mysql上安裝Mysql資料庫和PHP。
當某用戶端發送通路請求時,DNS會将請求輪詢到web1或web2伺服器上,DNS上的算法會計算出該将請求發給哪個伺服器響應,基本上是平均的,這樣就減小了web伺服器的通路壓力;
通路請求到達web伺服器後,web伺服器會檢測用戶端請求的是什麼内容,如果是靜态頁面檔案,就直接響應用戶端;如果是動态的網頁内容,就會通過接口送至Mysql伺服器上的PHP進行解析,PHP再通路資料庫的資料,并将結果傳回web伺服器,web伺服器就響應用戶端。
步驟:
首先每台伺服器都要先關閉RedHat的SELinux。
- 檢視SELinux的目前狀态
- #getenforce
- 如果是處于'ermissive',就不用再執行下面的指令了
- #setenfoce 0 關閉
配置web1
1.編譯安裝Nginx
- # yum -y install pcre-devel 解決依賴關系
- # groupadd -r nginx
- # useradd -r -g nginx -s /bin/false -M nginx
- # tar xf nginx-1.2.2.tar.gz
- # cd nginx-1.2.2
- # ./configure \
- --prefix=/usr \
- --sbin-path=/usr/sbin/nginx \
- --conf-path=/etc/nginx/nginx.conf \
- --error-log-path=/var/log/nginx/error.log \
- --http-log-path=/var/log/nginx/access.log \
- --pid-path=/var/run/nginx/nginx.pid \
- --lock-path=/var/lock/nginx.lock \
- --user=nginx \
- --group=nginx \
- --with-http_ssl_module \
- --with-http_flv_module \
- --with-http_stub_status_module \
- --with-http_gzip_static_module \
- --http-client-body-temp-path=/var/tmp/nginx/client/ \
- --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
- --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
- --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
- --http-scgi-temp-path=/var/tmp/nginx/scgi \
- --with-pcre
- # make
- # maek install
- 建立SysV腳本,使之支援服務啟動指令
- # vim /etc/rc.d/init.d/nginx 編輯如下内容
- #!/bin/sh
- #
- # nginx - this script starts and stops the nginx daemon
- #
- # chkconfig: - 85 15
- # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
- # proxy and IMAP/POP3 proxy server
- # processname: nginx
- # config: /etc/nginx/nginx.conf
- # config: /etc/sysconfig/nginx
- # pidfile: /var/run/nginx.pid
- # Source function library.
- . /etc/rc.d/init.d/functions
- # Source networking configuration.
- . /etc/sysconfig/network
- # Check that networking is up.
- [ "$NETWORKING" = "no" ] && exit 0
- nginx="/usr/sbin/nginx"
- prog=$(basename $nginx)
- NGINX_CONF_FILE="/etc/nginx/nginx.conf"
- [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
- lockfile=/var/lock/subsys/nginx
- make_dirs() {
- # make required directories
- user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
- options=`$nginx -V 2>&1 | grep 'configure arguments:'`
- for opt in $options; do
- if [ `echo $opt | grep '.*-temp-path'` ]; then
- value=`echo $opt | cut -d "=" -f 2`
- if [ ! -d "$value" ]; then
- # echo "creating" $value
- mkdir -p $value && chown -R $user $value
- fi
- fi
- done
- }
- start() {
- [ -x $nginx ] || exit 5
- [ -f $NGINX_CONF_FILE ] || exit 6
- make_dirs
- echo -n $"Starting $prog: "
- daemon $nginx -c $NGINX_CONF_FILE
- retval=$?
- echo
- [ $retval -eq 0 ] && touch $lockfile
- return $retval
- }
- stop() {
- echo -n $"Stopping $prog: "
- killproc $prog -QUIT
- retval=$?
- echo
- [ $retval -eq 0 ] && rm -f $lockfile
- return $retval
- }
- restart() {
- configtest || return $?
- stop
- sleep 1
- start
- }
- reload() {
- configtest || return $?
- echo -n $"Reloading $prog: "
- killproc $nginx -HUP
- RETVAL=$?
- echo
- }
- force_reload() {
- restart
- }
- configtest() {
- $nginx -t -c $NGINX_CONF_FILE
- }
- rh_status() {
- status $prog
- }
- rh_status_q() {
- rh_status >/dev/null 2>&1
- }
- case "$1" in
- start)
- rh_status_q && exit 0
- $1
- ;;
- stop)
- rh_status_q || exit 0
- $1
- ;;
- restart|configtest)
- $1
- ;;
- reload)
- rh_status_q || exit 7
- $1
- ;;
- force-reload)
- force_reload
- ;;
- status)
- rh_status
- ;;
- condrestart|try-restart)
- rh_status_q || exit 0
- ;;
- *)
- echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
- exit 2
- esac
- 添加到服務清單并使開機啟動
- # chkconfig --add nginx
- # chkconfig nginx on
啟動nginx并測試一下是否正常工作。
修改nginx的配置檔案
- worker_processes 2;
- events {
- worker_connections 50000;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- server {
- listen 172.16.3.110:80;
- server_name localhost;
- location / {
- root html;
- index index.html index.htm index.php;
- }
- location ~ \.php$ {
- root html;
- fastcgi_pass 172.16.3.112:9000;
- fastcgi_index inedx.php;
- fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- include fastcgi_params;
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- }
編輯/etc/nginx/fastcgi_params,将内容替換
- 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;
配置NFS共享
- # vim /etc/exports
- /etc/nginx/mnt 172.16.0.0/16 (rw,no_root_squash,)
編譯安裝DNS
- # yum -y install bind97 bind97-devel bind97-libs bind97-utils
修改主配置檔案/etc/named.conf
- 修改檔案中的zone部分
- zone "web1.com" IN {
- type master;
- file "web1.com.zone";
- allow-update{ none; };
- };
- zone "web2.com" IN {
- type master;
- file "web2.com.zone";
- allow-update{ none; };
- };
- zone "mp.com" IN {
- type master;
- file "mp.com.zone";
- allow-update{ none; };
- }
添加兩條A記錄
- $TTL 600
- @ IN SOA ns.web1.com. admin.web1.com. (
- 0 ; serial
- 1D ; refresh
- 1H ; retry
- 1W ; expire
- 3H ) ; minimum
- IN NS ns.web.com
- ns IN A 172.16.3.110
- www IN A 172.16.3.110
- www IN A 172.16.3.111
2.配置web2
這裡安裝Nginx與web1相同,隻是蟹蓋配置檔案時有些不同
- worker_processes 2;
- events {
- worker_connections 50000;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- server {
- listen 172.16.3.111:80;
- server_name localhost;
- location / {
- root html;
- index index.html index.htm index.php;
- }
- location ~ \.php$ {
- root html;
- fastcgi_pass 172.16.3.112:9000;
- fastcgi_index inedx.php;
- fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- include fastcgi_params;
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- }
3.配置Mysql伺服器
編譯安裝Mysql資料庫
将下載下傳好的Mysql源碼包進行編譯安裝
- # tar xf mysql-5.5.24-linux2.6-i686.tar.gz -C /usr/local
- # cd /usr/local/
- # ln -sv mysql-5.5.24-linux2.6-i686 mysql
- # cd mysql
- # chown -R mysql:mysql .
- # scripts/mysql_install_db --user=mysql --datadir=/mydata/data
- # chown -R root .
修改配置檔案
- # cd /usr/local/mysql
- # cp support-files/my-large.cnf /etc/my.cnf 軟體提供的樣本
- 修改
- thread_concurrency = 2
- datadir = /mydata/data
為Mysql提供服務啟動腳本
- # cd /usr/local/mysql
- # cp support-files/mysql.server /etc/rc.d/init.d/mysqld
- # chkconfig --add mysqld
- # chkconfig mysqld on
這樣Mysql就可以啟動了,但是因為是編譯安裝,是以僅僅這樣很不符合系統使用規範,我們就把它做的規範些
- 輸出man手冊
- # vim /etc/man.config 添加
- MANPATH /usr/local/mysql/man
- 輸出頭檔案到指定的位置
- # ln -sv /usr/local/mysql/include /usr/include/mysql
- 輸出庫檔案的路徑
- # echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
- 設定PATH的環境變量
- # vim /etc/profile
- PATH=/usr/local/mysql/bin:#PATH
然後重新載入下:#ldconfig
編譯安裝PHP
- # tar xf php-5.4.4.tar.bz2
- # cd php-5.4.4
- # ./configure --prefix=/usr/local/php4nginx
- --with-mysql=/usr/local/mysql --with-openssl --enable-fpm
- --enable-sockets --enable-sysvshm
- --with-mysqli=/usr/local/mysql/bin/mysql_config
- --enable-mbstring --with-freetype-dir --with-jpeg-dir
- --with-png-dir --with-zlib-dir --with-libxml-dir=/usr
- --enable-xml --with-mhash --with-mcrypt --with-config-file-path=/etc
- --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl
- # make # make test # make intall
為PHP提供配置檔案和服務啟動腳本
- # cp php.ini-production /etc/php.ini
- # cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
- # chmod +x /etc/rc.d/init.d/php-fpm
- # chkconfig --add php-fpm
- # chkconfig php-fpm on
設定php-fpm參數
- 首先把PHP提供的配置檔案樣本複制并重命名
- # cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
- # vim /usr/local/php/etc/php-fpm.conf
- pm.max_children = 50
- pm.start_servers = 5
- pm.min_spare_servers = 2
- pm.max_spare_servers = 8
- pid = /usr/local/php/var/run/php-fpm.pid
- 啟動并驗證
- # service php-fpm start
- # ps aux | grep php-fpm
PHP算是配置好了,可是為了系統更給力的工作,給PHP安裝xcache加速器吧
- # tar xf xcache-2.0.0.tar.gz
- # cd xcache-2.0.0
- # /usr/local/php/bin/phpize
- # ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
- # make && make install
- 安裝完成後,會出現一個
- Installing shared extensions: ..............的字元串,把冒号後面的複制下備用
- 編輯php.ini就可以将exache跟php整合到一塊了
- 把xcache提供的樣例配置導入php.ini
- # mkdir /etc/php.d
- # cp /xcache/xcache.ini /etc/php.d
- 編輯/etc/php.d,找到zend_extension開頭行,将複制的字元串粘進去
- 然後重新開機php-fpm
- # service php-fpm restart