==系统架构==
OS: Linux 2.6.18-194.el5PAE i386 GNU/Linux
Disk: 20GB Memory:4G Swap:5G
==用户/组规划==
mkdir /app
groupadd app
useradd tomcat -u 505 -G app -b /app
useradd nginx -u 506 -G app -b /app
==基础服务==
Nginx + Tomcat
版本:
Tengine version: Tengine/1.4.6 (nginx/1.2.9) http://tengine.taobao.org/
[root@ceshi1 ~]# /app/tomcat/bin/version.sh
Using CATALINA_BASE: /app/tomcat
Using CATALINA_HOME: /app/tomcat
Using CATALINA_TMPDIR: /app/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.7.0_17
Using CLASSPATH: /app/tomcat/bin/bootstrap.jar:/app/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.40
Server built: May 5 2013 08:54:06
Server number: 7.0.40.0
OS Name: Linux
OS Version: 2.6.32-358.6.2.el6.x86_64
Architecture: amd64
JVM Version: 1.7.0_17-b02
JVM Vendor: Oracle Corporation
==依赖服务==
Pcre(支持正则的模块) JDK (Java 开发工具包)
版本: Pcre:pcre-8.32 JDK:1.7.0_17
==服务安装==
1. 32bit JDK:
a)wget http://219.239.26.10/download/38564427/54254163/3/gz/53/228/1362527298357_484/jdk-7u17-linux-i586.tar.gz
b)tar zxf jdk-7u17-linux-i586.tar.gz
c) mv jdk1.7.0_17 /usr/local
d) 配置环境变量
1. vim /etc/profile
PATH="/usr/local/jdk1.7.0_17/bin:$PATH:$HOME/bin"
JAVA_OPTS='-Xms2048m -Xmx2048m -XX:PermSize=128M -XX:MaxPermSize=256M'
CATALINA_OPTS='-Djava.awt.headless=true'
JAVA_HOME="/usr/local/jdk1.7.0_17"
CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
CATALINA_HOME="/app/tomcat"
export PATH JAVA_HOME JAVA_OPTS CATALINA_OPTS CLASS_PATH CATALINA_HOME
2. source /etc/profile
2. Tomcat:
a) wget http://apache.dataguru.cn/tomcat/tomcat-7/v7.0.40/bin/apache-tomcat-7.0.40.tar.gz
b) tar zxf apache-tomcat-7.0.40.tar.gz
c) mv apache-tomca-7.0.40/* /app/tomcat/
3. Pcre
a) wget http://sourceforge.net/projects/pcre/files/pcre/8.32/pcre-8.32.tar.bz2/download
b) tar xf pcre-8.32.tar.bz2
c) cd pcre-8.32
d) ./configure
e) make && make install
4. nginx
a) wget http://nginx.org/download/nginx-1.4.6.tar.gz
b) tar zxf nginx-1.4.6.tar.gz
c) cd nginx-1.4.6
d) ./configure --prefix=/app/nginx --user=nginx --group=app --prefix=/app/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_concat_module --with-http_upstream_check_module --with-http_sub_module --with-http_realip_module
--with-http_stub_status_module:该模块可以为Tengine提供主动式后端服务器健康检查的功能 http://tengine.taobao.org/document_cn/http_upstream_check_cn.html
--with-http_concat_module :该模块类似于apache中的mod_concat模块,用于合并多个文件在一个响应报文中http://tengine.taobao.org/document_cn/http_concat_cn.html
--without-http_upstream_consistent_hash_module: 这个模块提供一致性hash作为负载均衡算法 http://tengine.taobao.org/document_cn/http_upstream_consistent_hash_cn.html
ngx_http_upstream_session_sticky_module 该模块是一个负载均衡模块,通过cookie实现客户端与后端服务器的会话保持, 在一定条件下可以保证同一个客户端访问的都是同一个后端服务器 http://tengine.taobao.org/document_cn/http_upstream_session_sticky_cn.html
e) make && make install
f) 如果遇到报错:./configure: error: SSL modules require the OpenSSL library 解决办法:yum -y install openssl openssl-devel
==服务配置==
Nginx 配置
监听端口:80/8080
----
vim /app/nginx/conf/nginx.conf
< pre>
worker_rlimit_nofile 65535;
events {
worker_connections 10240;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 1000m;
server_tokens off;
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 65;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css text/javascript application/xml;
gzip_vary on;
include ../conf.d/www.tets.com; (添加虚拟主机)
}
vim /app/nginx/conf.d/www.test.com
server {
listen 80;
server_name www.test.com;
charset utf-8;
location ~* {
proxy_pass http://127.0.0.1:8080;(指定代理)
proxy_set_header Host www.test.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ /\.ht {deny all;}
}
Tomcat 配置
vim /app/tomcat/conf/server.xml (添加主机、指定网站目录)
<Host name=www.test.com appBase="webapps">
<Context path="" docBase="/app/web/www.test.com/ROOT" debug="0" reloadable="true"/>
</Host>
==Shell 脚本==
1.tomcat启动脚本 (此脚本适合同一台服务器部署多个tomcat的情况 )
vim /etc/init.d/tomcat
#!/bin/bash
#
# httpd Startup script for the Tomcat HTTP Server
#
# chkconfig: - 92 92
# description: The web container tomcat is 365wos
# HTML files and CGI.
# processname: java
# config: /app/tomcat_bapp/conf/server.xml
TOMCAT="tomcat_bapp"
TOMCATPID=`/usr/bin/pgrep -f /app/$TOMCAT/conf/logging.properties`
tomcatup ()
{
/usr/bin/pgrep -f /app/$TOMCAT/conf/logging.properties &> /dev/null
if [ $? -eq 0 ];then
echo "The $TOMCAT is started..."
elif [ $USER = 'tomcat' ];then
/app/$TOMCAT/bin/startup.sh
elif [ $USER = 'root' ];then
su - tomcat << EOF
/app/$TOMCAT/bin/startup.sh
EOF
else
echo "Please use tomcat user !!!"
fi
}
tomcatdown ()
{
/app/$TOMCAT/bin/shutdown.sh
kill -9 $TOMCATPID &> /dev/null
}
tomcatstatus ()
{
cat=$TOMCATPID
if [ -z $cat ]
then
echo -e "$TOMCAT stoping... \t\t\t [OK] "
else
echo -e "$TOMCAT running... \t\t\t [OK] "
fi
}
case $1 in
start)
if tomcatup
then
echo -e "tomcat starting... \t\t\t [OK]";
else
echo -e "tomcat starting... \t\t\t [Error]";
fi
;;
stop)
if tomcatdown
then
echo -e "tomcat shutdown... \t\t\t [OK]";
else
echo -e "tomcat shutdown... \t\t\t [Error]";
fi
;;
restart)
if tomcatdown
then
echo -e "tomcat shutdown... \t\t\t [OK]";
else
echo -e "tomcat shutdown... \t\t\t [Error]";
fi
if tomcatup
then
echo -e "tomcat starting... \t\t\t [OK]";
else
echo -e "tomcat starting... \t\t\t [Error]";
fi
;;
status)
tomcatstatus
;;
*)
echo "Error, Please enter start|stop|restart|status"
esac
tomcat 脚本2(此脚本适合单Tomcat时使用)
vim /etc/init.d/tomcat
# Source function library.
. /etc/rc.d/init.d/functions
start(){
if [ -z $(/sbin/pidof java) ]; then
echo "Starting tomcat"
/app/tomcat/bin/startup.sh &>/dev/null
touch /var/lock/subsys/tomcat
else
echo "tomcat allready running"
fi
}
stop(){
if [ ! -z $(/sbin/pidof java) ]; then
echo "Shutting down tomcat"
/app/tomcat/bin/shutdown.sh &>/dev/null
kill -9 $(/bin/ps -ef | grep "/app/tomcat/conf/logging.properties" | grep -v grep | awk '$1 ~ '\/$USER\/'{print $2}') &> /dev/null
until [ -z $(/sbin/pidof java) ]; do :; done
rm -f /var/lock/subsys/tomcat
else
echo "tomcat not running"
fi
}
status(){
cat=$(ps -ef | grep "/app/tomcat/conf/logging.properties"| wc -l)
if [ $cat -ge 2 ]
then
echo -e "Tomcat running... \t\t\t [OK] "
else
echo -e "Tomcat stoping... \t\t\t [NO] "
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
# /app/tomcat/bin/catalina.sh version
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
esac
exit 0
2.nginx 启动脚本
vim /etc/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: /app/nginx/conf/nginx.conf
# pidfile: /app/nginx/logs/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="/app/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/app/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
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
killall -9 nginx
}
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
3.日志查看脚本
vim /bin/log
#!/bin/bash
case $1 in
1|nginx_access)
tail -f /app/nginx/logs/access.log -n 100
;;
2|nginx_error)
tail -f /app/nginx/logs/error.log -n 200
;;
3|tomcat)
tail -f /app/tomcat/logs/catalina.out -n 200
;;
*)
echo 输入错误!!
echo "命令方法"
echo "/bin/log tomcat"
echo ""
echo "或者"
echo "/bin/log nginx_access"
echo ""
echo "或者"
echo "/bin/log nginx_error"
echo ""
;;
esac
==目录权限设置==
创建网站目录:
mkdir -p /app/web/www.test.com/ROOT/
设置权限:
chown -R nginx:app /app/nginx
chown -R tomcat:app /app/web/www.test.com
chown -R tomcat:app /app/tomcat
chmod 755 /bin/log /etc/init.d/tomcat /app/tomcat/bin/mainsh/tomcatstatus.sh /etc/init.d/nginx
==设置开机自启动==
chkconfig --level 35 nginx on
chkconfig --level 35 tomcat on
==异常处理==
Starting nginx: /app/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file:
No such file or directory [FAILED]
pcre.libpcre.so.1 => not found 并没有找到
解决办法:
find / -name libpcre.so.1
/usr/local/lib/libpcre.so.1
ln -s /usr/local/lib/libpcre.so.1 /lib
总结:上面的问题其实是pcre的动态库路径不对,无需编译
Error: failed /usr/local/jdk/jre/lib/i386/client/libjvm.so, because /usr/local/jdk/jre/lib/i386/client/libjvm.so: cannot restore segment prot after reloc: Permission denied
解决办法:
1 打开/etc/selinux/config
2 把selinux=enforcing改为selinux=disabled
3 重启linux