类别:原创 服务器    
第一:说明,软件说明,和安装的目的        

架设基于linux下的高性能web服务器,使用Nginx + php + jsp      
关于nginx的详细手册,请参看:      
《高性能Linux服务器构建实战:运维监控、性能调优与集群应用》http://book.51cto.com/art/201202/314644.htm

本文的前提是已安装好了 mysql,php 和gd ,关于如何安装这些软件本文不再讨论,具体请看我其它的文章:《实战 linux 下 mysql+apache+php+gd 的安装和配置》http://jedy82.blog.51cto.com/425872/888815

如果源码安装的php 请确保打上了php-fpm补丁并加上 --enable-fastcgi  --enable-fpm 参数      
rpm方式安装的php 请确保安装了php-fpm包

查看 php 编辑参数的命令是:php -i | grep configure      
查看 nginx编辑参数的命令是:nginx -V

本文参照:      
《Linux下Nginx+PHP+MySQL配置(图)》http://www.nginx.name/view/1.html      
《tomcat与nginx的整合》http://blog.sina.com.cn/s/blog_5ce87d560100q5tw.html      
《PHP与PHP-FPM的安装及优化(1)》http://book.51cto.com/art/201202/314842.htm      
《PHP与PHP-FPM的安装及优化(2)》http://book.51cto.com/art/201202/314843.htm      
《搭建Nginx+Java环境》http://book.51cto.com/art/201202/314851.htm

第二:本例操作环境


所使用的系统环境为 Centos 5.6 64位操作系统      
[root@jedy ~]# uname -a      
Linux jedy.com 2.6.18-238.el5 #1 SMP Thu Jan 13 15:51:15 EST 2011 x86_64 x86_64 x86_64 GNU/Linux      
[root@jedy ~]# more /etc/redhat-release      
CentOS release 5.6 (Final)      
[root@jedy ~]#

第三:所需要的软件包如下


nginx-1.2.0.tar.gz http://nginx.org/

第四:安装


1.yum 或rpm方式安装      
[root@jedy ~]# wget http://nginx.org/packages/centos/5/noarch/RPMS/nginx-release-centos-5-0.el5.ngx.noarch.rpm -P /software      
[root@jedy ~]# wget  http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm -P /software

[root@jedy ~]# rpm -ivh /software/nginx-release-centos-5-0.el5.ngx.noarch.rpm   epel-release-5-4.noarch.rpm      
[root@jedy ~]# yum -y install nginx

2.源码安装  

使用源码安装时 要确保系统中安装全了所需的相关组件,如 gcc make工具,pcre-devel zlib-devle 等开发工具包

[root@jedy ~]# tar zxvf /software/nginx-1.2.0.tar.gz -C /usr/local/src/      
[root@jedy ~]# cd /usr/local/src/nginx-1.2.0/      
[root@jedy nginx-1.2.0]# echo "./configure --prefix=/usr/local/nginx-1.2.0 --conf-path=/etc/nginx/nginx.conf  --with-http_stub_status_module --with-http_gzip_static_module

" | tee configure.log | sh         // 将./configure 时所用的参数放到一个文档里,方便以后之前的编辑参数,这对以后重新编辑会很有好处      
[root@jedy nginx-1.2.0]# echo "make && make install" | tee -a configure.log | sh && mv configure.log /usr/local/nginx-1.2.0      
[root@jedy nginx-1.2.0]# ln -sv /usr/local/nginx-1.2.0 /usr/local/nginx      
[root@jedy nginx-1.2.0]#      
[root@jedy nginx-1.2.0]#

################################安装部分完成#################################

第五:编辑、配置nginx 及整合 nginx同php jsp

1.编辑主配置文件

[root@jedy nginx-1.2.0]#      
[root@jedy nginx-1.2.0]# mkdir /etc/nginx/conf.d      
[root@jedy nginx-1.2.0]#      
[root@jedy nginx-1.2.0]# vim /etc/nginx/nginx.conf                                \\ 编辑主配置文件,内容如下:      
################################脚本开始#################################      
user  nginx;      
worker_processes  1;

error_log  /var/log/nginx/error.log warn;      
pid        /var/run/nginx.pid;


events {      
    worker_connections  1024;      
}


http {      
    include       /etc/nginx/mime.types;      
    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"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;      
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;      
    include /etc/nginx/conf.d/*.conf;                                         \\引用其它的配置文件,这样看起来结构会更加清晰      
               }      
################################脚本结束#################################

2.编辑各子配置文件

[root@jedy nginx-1.2.0]#      
[root@jedy nginx-1.2.0]# vim /etc/nginx/conf.d/default.conf                         \\ 编辑默认文件,内容如下:      
################################脚本开始#################################

server {      
    listen       88;                                                  \\ 端口配置      
    server_name  localhost;

    #charset koi8-r;      
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {      
        root   /usr/share/nginx/html;                   \\ 默认的网站主页面位置      
        index  index.html index.htm;      
    }      
############此处为增加cacti对nginx的监控配置##################      
#    location /ns {      
#        stub_status   on;      
#        access_log  off;      
#        allow all;      
#    }      
##############################################################      
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html      
    #      
    error_page   500 502 503 504  /50x.html;      
    location = /50x.html {      
        root   /usr/share/nginx/html;      
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80      
    #      
    #location ~ \.php$ {      
    #    proxy_pass   http://127.0.0.1;      
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000      
    #      
    # location ~ \.php$ {      
    # location ~ .*\.(php|php5)?$ {      
    #   root            /data/webapps/jedy;      
    #   fastcgi_pass   127.0.0.1:9000;      
    #   fastcgi_index  index.php;      
    #   fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;      
    #   fastcgi_param  SCRIPT_FILENAME  /data/webapps/jedy$fastcgi_script_name;      
    #     include        fastcgi_params;      
    #}

    # deny access to .htaccess files, if Apache's document root      
    # concurs with nginx's one      
    #      
    #location ~ /\.ht {      
    #    deny  all;      
    #}      
}


################################脚本结束#################################

[root@jedy nginx-1.2.0]#      
[root@jedy nginx-1.2.0]#vim /etc/nginx/conf.d/jedy.conf        \\ 虚拟主机配置文件,内容如下:      
################################脚本开始#################################      
server{      
    listen    88;      
    server_name    www.jedy.com jedy.com;                                    \\ 域名配置      
    index    index.html index.htm index.php index.jsp;                   \\ 增加对index.php index.isp的支持      
    root /data/webapps/jedy;                                                              \\ 网页主目录      
    charset    gb2312;      
    location ~ .*\.(php|php5)?$                                                            \\ php 支持设置段,此处我们使用php-fpm功能支持php      
    {  
                root /data/webapps/jedy;      
                fastcgi_pass  127.0.0.1:9000;                                              \\ php-fpm 端口      
                fastcgi_index index.php;      
                fastcgi_param  SCRIPT_FILENAME  /data/webapps/jedy$fastcgi_script_name;      
                include fastcgi_params;      
    }      
    location ~ .*\.(jsp|action|do)$                                      \\ jsp 支持设置段 使用代理方式支持jsp,此后会指定proxy.conf的内容      
    {                                                                                          \\ 匹配以jsp action do结尾的,此处根据实际情况      
        index    index.jsp;      
        proxy_pass    http://127.0.0.1:8080;                        \\ jsp 端口 此处应该是tomcat的端口 当然tomcat得启动了      
        }      
}

################################脚本结束#################################

[root@jedy nginx-1.2.0]#      
[root@jedy nginx-1.2.0]# vim /etc/nginx/conf.d/proxy.conf     \\ 设置代理使用配置文件,用于整合jsp,内容如下:      
################################脚本开始#################################      
proxy_redirect          off;      
proxy_set_header        Host $host;      
proxy_set_header        X-Real-IP $remote_addr; #获取真实IP      
#proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for; #获取代理者的真实ip      
client_max_body_size    10m;      
client_body_buffer_size 128k;      
proxy_connect_timeout   90;      
proxy_send_timeout      90;      
proxy_read_timeout      90;      
proxy_buffer_size       4k;      
proxy_buffers           4 32k;      
proxy_busy_buffers_size 64k;      
proxy_temp_file_write_size 64k;      
################################脚本结束#################################

[root@jedy nginx-1.2.0]#      
[root@jedy nginx-1.2.0]# vim /etc/nginx/conf.d/ssl.conf       \\ 设置ssl配置文件,一般很少用,内容如下      
################################脚本开始#################################      
# HTTPS server      
#      
#server {      
#    listen       443;      
#    server_name  localhost;

#    ssl                  on;      
#    ssl_certificate      /etc/nginx/cert.pem;      
#    ssl_certificate_key  /etc/nginx/cert.key;

#    ssl_session_timeout  5m;

#    ssl_protocols  SSLv2 SSLv3 TLSv1;      
#    ssl_ciphers  HIGH:!aNULL:!MD5;      
#    ssl_prefer_server_ciphers   on;

#    location / {      
#        root   /usr/share/nginx/html;      
#        index  index.html index.htm;      
#    }      
#}

################################脚本结束#################################      
[root@jedy nginx-1.2.0]#      
[root@jedy nginx-1.2.0]# /usr/local/nginx/sbin/nginx                           \\ 手动启动Nginx

第六:使用服务器进程启动Nginx

1.新建nginx 服务器进程      
[root@jedy nginx-1.2.0]#      
[root@jedy nginx-1.2.0]# vim /etc/init.d/nginx      
################################脚本开始#################################      
#!/bin/sh      
#      
# nginx        Startup script for nginx      
#      
# chkconfig: - 85 15      
# processname: nginx      
# config: /etc/nginx/nginx.conf      
# config: /etc/sysconfig/nginx      
# pidfile: /var/run/nginx.pid      
# description: nginx is a HTTP and reverse proxy server      
#      
### BEGIN INIT INFO      
# Provides: nginx      
# Required-Start: $local_fs $remote_fs $network      
# Required-Stop: $local_fs $remote_fs $network      
# Default-Start: 2 3 4 5      
# Default-Stop: 0 1 6      
# Short-Description: start and stop nginx      
### END INIT INFO

# Source function library.      
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/nginx ]; then      
    . /etc/sysconfig/nginx      
fi

prog=nginx      
nginx=${NGINX-/usr/local/nginx/sbin/nginx}                      \\ nginx程序的位置      
conffile=${CONFFILE-/etc/nginx/nginx.conf}                      \\ nginx主配置文件      
lockfile=${LOCKFILE-/var/lock/subsys/nginx}      
pidfile=${PIDFILE-/var/run/nginx.pid}                           \\ pid文件      
SLEEPMSEC=100000      
RETVAL=0

start() {      
    echo -n $"Starting $prog: "

    daemon --pidfile=${pidfile} ${nginx} -c ${conffile}      
    RETVAL=$?      
    echo      
    [ $RETVAL = 0 ] && touch ${lockfile}      
    return $RETVAL      
}

stop() {      
    echo -n $"Stopping $prog: "      
    killproc -p ${pidfile} ${prog}      
    RETVAL=$?      
    echo      
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}      
}

reload() {      
    echo -n $"Reloading $prog: "      
    killproc -p ${pidfile} ${prog} -HUP      
    RETVAL=$?      
    echo      
}

upgrade() {      
    oldbinpidfile=${pidfile}.oldbin

    configtest -q || return 6      
    echo -n $"Staring new master $prog: "      
    killproc -p ${pidfile} ${prog} -USR2      
    RETVAL=$?      
    echo      
    /bin/usleep $SLEEPMSEC      
    if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then      
        echo -n $"Graceful shutdown of old $prog: "      
        killproc -p ${oldbinpidfile} ${prog} -QUIT      
        RETVAL=$?      
        echo      
    else      
        echo $"Upgrade failed!"      
        return 1      
    fi      
}

configtest() {      
    if [ "$#" -ne 0 ] ; then      
        case "$1" in      
            -q)      
                FLAG=$1      
                ;;      
            *)      
                ;;      
        esac      
        shift      
    fi      
    ${nginx} -t -c ${conffile} $FLAG      
    RETVAL=$?      
    return $RETVAL      
}

rh_status() {      
    status -p ${pidfile} ${nginx}      
}

# See how we were called.      
case "$1" in      
    start)      
        rh_status >/dev/null 2>&1 && exit 0      
        start      
        ;;      
    stop)      
        stop      
        ;;      
    status)      
        rh_status      
        RETVAL=$?      
        ;;      
    restart)      
        configtest -q || exit $RETVAL      
        stop      
        start      
        ;;      
    upgrade)      
        upgrade      
        ;;      
    condrestart|try-restart)      
        if rh_status >/dev/null 2>&1; then      
            stop      
            start      
        fi      
        ;;      
    force-reload|reload)      
        reload      
        ;;      
    configtest)      
        configtest      
        ;;      
    *)      
        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"      
        RETVAL=2      
esac

exit $RETVAL

################################脚本结束#################################


2.新建php-fpm 服务器进程      
[root@jedy nginx-1.2.0]#      
[root@jedy nginx-1.2.0]# vim /etc/init.d/php-fpm      
################################脚本开始#################################      
#!/bin/sh      
#      
# php-fpm Start/Stop the php-fpm server      
#      
# chkconfig:   - 85 15      
# description: php-fpm server      
#      
### BEGIN INIT INFO      
# Provides: nginx      
# Required-Start: $local_fs $remote_fs $network      
# Required-Stop: $local_fs $remote_fs $network      
# Default-Start: 2 3 4 5      
# Default-Stop: 0 1 6      
# Short-Description: start and stop nginx      
### END INIT INFO


# Source function library.      
. /etc/rc.d/init.d/functions

exec="/usr/bin/php-cgi"                                 \\ php-cgi 位置      
prog="php-fpm"      
config="/etc/sysconfig/php-fpm"                         \\ php-fpm主配置文件位置

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/$prog

start() {      
    [ -x $exec ] || exit 5      
    [ -f $config ] || exit 6      
    echo -n $"Starting $prog: "      
    daemon $exec $OPTIONS      
    retval=$?      
    echo      
    [ $retval -eq 0 ] && touch $lockfile      
    return $retval      
}

stop() {      
    echo -n $"Stopping $prog: "      
    killproc $prog      
    retval=$?      
    echo      
    [ $retval -eq 0 ] && rm -f $lockfile      
    return $retval      
}

restart() {      
    stop      
    start      
}

reload() {      
    restart      
}

force_reload() {      
    restart      
}

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)      
        $1      
        ;;      
    reload)      
        rh_status_q || exit 7      
        $1      
        ;;      
    force-reload)      
        force_reload      
        ;;      
    status)      
        rh_status      
        ;;      
    condrestart|try-restart)      
        rh_status_q || exit 0      
        restart      
        ;;      
    *)      
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"      
        exit 2      
esac      
exit $?

################################脚本结束#################################

[root@jedy nginx-1.2.0]# chkconfig --add nginx                       \\ 注册nginx为服务,会自动设置在2345模式自动启动的      
[root@jedy nginx-1.2.0]# chmod +x /etc/init.d/nginx      
[root@jedy nginx-1.2.0]# chkconfig --add php-fpm                      \\ 注册nginx为服务,会自动设置在2345模式自动启动的      
[root@jedy nginx-1.2.0]# chmod +x /etc/init.d/php-fpm

 
第七:测试连接

[root@jedy nginx-1.2.0]# service nginx restart                            \\ 启动nginx服务      
[root@jedy nginx-1.2.0]# service php-fpm restart                          \\ 启动php-fpm服务      
[root@jedy nginx-1.2.0]# netstart -tlnp | grep nginx 如果有88、9000端口 说明服务启动成功      
[root@jedy nginx-1.2.0]# mkdir -pv /data/webapps/jedy      
[root@jedy nginx-1.2.0]#      
[root@jedy nginx-1.2.0]# vim /data/webapps/jedy/index.php      
################################脚本开始#################################      
<?      
phpinfo()      
?>      
################################脚本结束#################################

[root@jedy nginx-1.2.0]#      
[root@jedy nginx-1.2.0]# vim /data/webapps/jedy/index.jsp      
################################脚本开始#################################      
<%@ page language="java" contentType="text/html; charset=UTF-8"      
    pageEncoding="UTF-8"%>      
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">      
<html>      
<head>      
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">      
<title>首页</title>      
</head>      
<body>      
    首页      
</body>      
</html>      
################################脚本结束#################################

[root@jedy nginx-1.2.0]#      
[root@jedy nginx-1.2.0]#

在网页中输入 http://www.jedy.com:88/index.php 应该能得到 phpinfo的信息      
在网页中输入 http://www.jedy.com:88/index.jsp 应该能得到 首页字样

至此 关于nginx的安装配置全部结束。