一、编译安装nginx

    cd /usr/local/src

    wget http://nginx.org/download/nginx-1.6.3.tar.gz

    tar -zxvf nginx-1.6.3.tar.gz

    cd nginx-1.6.3

    ./configure --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_p_w_picpath_filter_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_degradation_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre

    make && make install


二、安装apache的httpd服务器

    wget apache.fayea.com/apr/apr-1.5.2.tar.gz

    wget apache.fayea.com/apr/apr-util-1.5.4.tar.gz

    wget apache.fayea.com/httpd/httpd-2.4.16.tar.gz

    #安装apache的时候必须先安装apr,在安装apr-util,最后安装httpd


(1)安装apr

    tar -zxvf apr-1.5.2.tar.gz

    cd apr-1.5.2

    ./cponfigure --prefix=/usr/local/apr

    make && make install

    

(2)安装apr-util

    tar -zxvf apr-util-1.5.4.tar.gz

    cd apr-util-1.5.4

    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/

    make && make install


(3)安装httpd

    tar -zxvf httpd-2.4.16.tar.gz

    cd httpd-2.4.16

    ./configure --prefix=/usr/local/http --sysconfdir=/etc/httpd --enable-rewrite --enable-so --enable-ssl --enable-cgi --enable-cgid --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/

    make && make install



    编译httd报错,在文档写好后的两个星期,搭建另一个httpd服务器遇到的,error message:exports.c:1131:note: previous definition of 'ap_hack_apr_version_string' was here 

make[2]: *** [exports.lo] 错误1

make[1]:*** [all-recursive] 错误1


原因:with-apr-utils写错了,应该是with-apr-util,本文已经更正。。。百度了一番没有解决,在这里卡了几个小时。郁闷。。。

    #安装完成之后为httpd提供启动脚本

    vim /etc/init.d/httpd

    

#!/bin/bash

#

# httpd                 Startup script for the Apache HTTP Server

#

# chkconfig: - 85 15

# description: Apache is a World Wide Web server.   It is used to serve \

#          HTML files and CGI.

# processname: httpd

# config: /etc/httpd/conf/httpd.conf

# config: /etc/sysconfig/httpd

# pidfile: /var/run/httpd.pid


# Source function library.

. /etc/rc.d/init.d/functions


if [ -f /etc/sysconfig/httpd ]; then

        . /etc/sysconfig/httpd

fi


# Start httpd in the C locale by default.

HTTPD_LANG=${HTTPD_LANG-"c"}


# This will prevent initlog from swallowing up a pass-phrase prompt if

# mod_ssl needs a pass-phrase from the user.

INITLOG_ARGS=""


# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server

# with the thread-based "worker" MPM; BE WARNED that some modules may not

# work correctly with a thread-based MPM; notably PHP will refuse to start.


# Path to the apachectl script, server binary, and short-form for messages.

apachectl=/usr/local/http/bin/apachectl

httpd=${HTTPD-/usr/local/http/bin/httpd}

prog=httpd

pidfile=${PIDFILE-/var/run/httpd.pid}

lockfile=${LOCKFILE-/var/lock/subsys/httpd}

RETVAL=0


start() {

        echo -n $"Starting $prog: "

        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && touch ${lockfile}

        return $RETVAL

}


stop() {

        echo -n $"Stopping $prog: "

        killproc -p ${pidfile} -d 10 $httpd

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

}


reload() {

        echo -n $"Reloading $prog: "

        if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then

                RETVAL=$?

                echo $"not reloading due to configuration systax error"

                failure $"not reloading $httpd due to configuration syntax error"

        else

                killproc -p ${pidfile} $httpd -HUP

                RETVAL=$?

        fi

        echo

}


# See how we were called.

case "$1" in

    start)

        start

        ;;

    stop)

        stop

        ;;

    status)

            status -p ${pidfile} $httpd

        RETVAL=$?

        ;;

    restart)

        stop

        start

        ;;

    condrestart)

        if [ -f ${pidfile} ] ; then

            stop

            start

        fi

        ;;

    reload)

            reload

        ;;

    graceful|help|configtest|fullstatus)

        $apachectl $@

        RETVAL=$?

        ;;

    *)

        echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"

        exit 1

esac


exit $RETVAL



    chmod +x /etc/init.d/httpd   #赋予可执行权限

    vim /etc/profile.d/httpd.sh  #开机加入path变量httpd的安装路径

    


三、安装php

    wget mirrors.sohu.com/php/php-5.3.29.tar.gz

    tar -zxvf php-5.3.29.tar.gz

    cd php-5.3.29

    ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/http/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts --with-mhash --with-gd --with-iconv-dir --enable-magic-quotes --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-gd-native-ttf --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-exif --enable-dom                        #重要的是with-apxs2=/usr/local/http/bin/apxs 选项,代表把php编译成httpd的模块使用

make && make install

cp /usr/local/src/php-5.3.29/php.ini-production /etc/php.ini

vim /etc/httpd/httpd.conf   #修改httpd配置文件,在大概158行左右去掉注释,使apache加载php的模块


LoadModule php5_module        modules/libphp5.so


#在http配置文件大概385行左右的位置加入下面的配置让httpd支持php

    AddType application/x-httpd-php .php

    AddType p_w_picpath/x-icon .ico

    AddType application/x-http-php-source .phps

保存退出


重启httpd

service httpd restart


apachectl -M 查看是否加载了php

到httpd的网页目录下创建一个index.php文件,在浏览器打开。如果可以访问就是可以解析php程序了。

#index.php文件内容:

phpinfo();

?>


到此安装结束。下面是配置负载均衡和动静分离


一、nginx配置

先说说架构,前端一个nginx(192.168.10.100)实现轮询算法的负载均衡,所有请求由前端的nginx接受根据轮询的方式交给后端的应用服务器(192.168.10.233、192.168.10.234),静态请求由后端的nginx响应,而动态请求通过转发至后端的httpd服务器进行处理之后在返回给nginx。由nginx把结果返回给客户。我用两个虚拟主机,分别是a.pl.com和img.pl.com

(1)前端负载nginx配置(192.168.10.100)定义后端的应用服务器集群和负载方式

    vim /usr/local/nginx/conf/nginx.conf

    

    upstream a.pl.com {

        server 192.168.10.233:80 weight=50 max_fails=5 fail_timeout=20s;

        server 192.168.10.234:80 weight=50 max_fails=5 fail_timeout=20s;

    }

    upstream b.pl.com {

        server 192.168.10.233:80 weight=50 max_fails=5 fail_timeout=20s;

        server 192.168.10.234:80 weight=50 max_fails=5 fail_timeout=20s;

    }


 


___________________________________________________________________________________________

(2)后端nginx服务器配置(192.168.10.233)

    #定义两个虚拟主机,在每个server段中配置一个location,所有以.php结尾的页面全部交给apache来处理



    server {

        listen       80;

        server_name a.pl.com;

        index index.html index.php;

        root  /data/wwwroot/polyguide;

        access_log  /var/log/nginx/access_cd.polyguide.com.cn.log;

        location / {

                if (!-e $request_filename)

                {

                        rewrite ^/(.*\.(js|ico|gif|jpg|png|css|bmp|html|xls)$) /$1 last;

                        rewrite ^/(.*) /index.php?$1 last;

                        rewrite ^.*$ /index.php last;

                }

        }


        location ~ .*\.php$ {

                proxy_set_header Host $host;

                proxy_set_header X-Real-IP $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                proxy_pass  http://127.0.0.1:8080;

        }

}


    server {

        listen       80;

        server_name  img.pl.com;

        access_log  /var/log/nginx/img_access.log;

        location / {

            root   /data/fastdfs;

            index  index.html index.htm;

        }


        location ~* /group1/M00/ {

                        root /data/fastdfs;

                        ngx_fastdfs_module;

        }

        location ~ .*\.php$ {

                proxy_set_header Host $host;

                proxy_set_header X-Real-IP $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                proxy_pass  http://127.0.0.1:8080;

        }


}       



二、后端apache应用服务器(192.168.10.233)


(1)编辑httpd主配置文件

    vim /etc/httpd/httpd.conf

___________________________________________________________________________________________

    大概224行左右,注释掉DocumentRoot

    DocumentRoot "/data/wwwroot/polyguide"


    大概477行左右,去掉注释,使apache开启虚拟主机功能

    Include /etc/httpd/extra/httpd-vhosts.conf


(2)编辑httpd虚拟主机配置文件

    vim /etc/httpd/extra/httpd-vhosts.conf

    

                #设置虚拟主机的监听地址和端口,由于httpd不直接向外通信,只是接收nginx转发的动态请求,所以监听在本地地址。

        ServerName a.pl.com                 #基于域名的虚拟主机

        DocumentRoot "/data/wwwroot/polyguide"

   

        Options none    

        AllowOverride none

        Require all granted                 #允许所有用户访问

   

   

        RewriteEngine on

        RewriteRule ^/static/(.*) /static/$1 [L]

        RewriteRule ^/uploadtmp/(.*) /uploadtmp/$1 [L]

        RewriteRule ^/ueditor/(.*) /ueditor/$1 [L]

        RewriteRule ^/crontab/(.*) /crontab/$1 [L]

        RewriteRule ^/robots/(.*) /robots/$1 [L]

        #RewriteRule ^(.*)$ /uploadtmp/$1 [L]            #should use this

        RewriteRule ^(.*)$ /uploadtmp/index2.php [L]     #this is test rewrite rules

   

        ErrorLog "/var/log/http/a.pl-error_log"            #httpd错误日志的位置

        CustomLog "/var/log/http/a.pl-access_log" combined    #访问日志位置

    


                    #第二个虚拟主机

        ServerName img.pl.com                   

        DocumentRoot "/data/fastdfs"            #网页文件的主目录

   

        Options none

        AllowOverride none

        Require all granted

   

   

        sethandler fastdfs                       #这个是FastDFS的设置

   

        ErrorLog "/var/log/http/img.pl-error_log"

        CustomLog "/var/log/http/img.pl-access_log" combined

    


另一台配置就不写了,到此,配置完毕,现在分别访问在nginx上的静态页面和apache上的动态页面测试。新手~写的不好。请各位大神勿喷,有什么错误和需要改正的请留言。谢谢~