歡迎您光臨本站 註冊首頁

nginx 的安裝及與php、jsp 的整合

←手機掃碼閱讀     火星人 @ 2014-03-08 , reply:0

類別:原創 伺服器
第一:說明,軟體說明,和安裝的目的

架設基於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/310392
如果源碼安裝的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 ~]# rpm -ivh /software/nginx-release-centos-5-0.el5.ngx.noarch.rpm
[root@jedy ~]# yum –y install nginx

2.源碼安裝

[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" | 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"%>




首頁



################################腳本結束#################################

[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的安裝配置全部結束.

本文出自 「jedy 網路技術&linux學習」 博客,請務必保留此出處http://jedy82.blog.51cto.com/425872/867898


[火星人 ] nginx 的安裝及與php、jsp 的整合已經有671次圍觀

http://coctec.com/docs/linux/show-post-45953.html