本次实验是为了解决单台的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 关闭
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
这里安装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;
- }
- }
- }
编译安装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
(对与写博客,也就是对平常知识的总结,这是需要下大功夫的,并且很有必要;也不是说不会总结,只是,像这种写成文章来,还真是不好搞,写着写着就乱了,也看了很多写的好的文章、博客,跟人家一比较还真不好意思拿出来让大家看了;想想这个也只有多练了。不唠叨了,苦练吧!)