CentOS6安装nginx+Tomcat7集群并实现自启动
环境:CentOS6.0
? ? ? Nginx1.1.15+Tomcat7.0.22
?
步骤:1.首先安装JDK,Tomcat
?2.安装nginx前先安装pcre,我安装的是最新的pcre-8.30.tar.gz,这个直接tar 然后configure,make,make install 就ok了
3.安装nginx1.1.15:
更多的安装配置?
./configure --prefix=/usr/local/nginx?
--with-openssl=/usr/include (启用ssl)?
--with-pcre=/usr/include/pcre/ (启用正规表达式)?
--with-http_stub_status_module (安装可以查看nginx状态的程序)?
--with-http_memcached_module (启用memcache缓存)?
--with-http_rewrite_module (启用支持url重写)
make ,make install
?
4.修改nginx.conf配置文件如下:
?
#user ?nobody;
worker_processes ?8;
error_log ?logs/error.log;
#error_log ?logs/error.log ?notice;
#error_log ?logs/error.log ?info;
#pid ? ? ? ?/var/run/nginx.pid;
worker_rlimit_nofile 102400;
events {
? ? use epoll;
? ? worker_connections ?102400;
}
http {
? ? include ? ? ? mime.types;
? ? default_type ?application/octet-stream;
? ? charset ?utf-8;
? ? server_names_hash_bucket_size 128;
? ? client_header_buffer_size 2k;
? ? large_client_header_buffers 4 4k;
? ? client_max_body_size 8m;
? ? #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 ?logs/access.log ?main;
? ?
? ? sendfile ? ? ? ?on;
? ? #tcp_nopush ? ? on;
? ? #keepalive_timeout ?0;
? ? keepalive_timeout ?60;
? ? #gzip ?on;
? ? #设定负载均衡列表 ?
? ? upstream ?backend
? ? { ?
? ? ?server 127.0.0.1:8080;
? ? ?server 127.0.0.1:8081;
? ? ?server 127.0.0.1:8082;
? ? ?}
? ? server {
? ? ? ? listen ? ? ? 80;
? ? ? ? server_name ?api.com;
? ? ? ? #access_log ?logs/host.access.log ?main;
#所有jsp的页面均交由tomcat处理
location ~ \.(action|jsp)?$ {
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移
proxy_next_upstream http_502 http_504 error timeout invalid_header;
#保留用户真实信息
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://backend; ? ? ?
} ? ?
? ? ? ? location / {
? ? ? ? ? ?root /app/webapp;
? ? ? ? ? ? index ?index.jsp;
? ? ? ? }
? ? ? ? #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 ? html;
? ? ? ? }
? ? }
}
5.设置nginx自启动脚本:#vi /etc/init.d/nginx#!/bin/bash## chkconfig: - 85 15# description: Nginx is a World Wide Web server.# processname: nginx
nginx=/usr/local/nginx/sbin/nginxconf=/usr/local/nginx/conf/nginx.conf
case $1 in? ? ? ?start) ? ? ?echo -n "Starting Nginx" ? ? ?$nginx -c $conf ? ? ?echo " done"? ? ? ?;;
? ? ? ?stop) ? ? ?echo -n "Stopping Nginx" ? ? ?killall -9 nginx ? ? ?echo " done"? ? ? ?;;
? ? ? ?test) ? ? ?$nginx -t -c $conf? ? ? ?;;
?reload) ? ? ?echo -n "Reloading Nginx" ? ? ?ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP ? ? ?echo " done"? ? ? ?;;
?restart)? $0 stop? $0 start? ? ? ?;;
? ? ? ?show) ? ? ?ps -aux|grep nginx? ? ? ?;;
? ? ? ?*) ? ? ?echo -n "Usage: $0 {start|restart|reload|stop|test|show}"? ? ? ?;;
esac
Nginx 自动启动脚本/重启脚本
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
?? ?[ -x $nginx ] || exit 5
?? ?[ -f $NGINX_CONF_FILE ] || exit 6
?? ?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
?? ?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
保存退出
第二步
#chmod +x /etc/init.d/nginx
第三步
#/sbin/chkconfig nginx on
检查一下
#sudo /sbin/chkconfig --list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
?
6.设置tomcat自启动
?在/etc/rc.d/rc.local文件中加入startup.sh
这里有点要说明,rc.local先于/etc/profile执行,所以会得不到JAVA环境变量,所以在startup.sh前加入代码:source /etc/profile
这样就可以了
?