文章标签 ‘tomcat’
201512 月20

Nginx+Tomcat负载均衡、绑定多域名、设置开机启动的安装步骤

1、JDK安装
/data1/soft/java/
>>chmod 777 jdk-6u45-linux-x64-rpm.bin
>>jdk-6u45-linux-x64-rpm.bin
>>gedit /etc/profile
JAVA_HOME=/data1/soft/java/jdk1.6.0_45

CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME
export CLASSPATH
export PATH
>>source /etc/profile

2、TOMCAT安装
将原windows系统中的tomcat主要文件拷贝到linux(conf、lib、bin),并新建空文件夹(logs、temp、work)
/data1/soft/tomcat1 多网站的那个tomcat
/data1/soft/tomcat2 少网站的那个tomcat
分别修改两个tomcat下的server.xml文件,将项目路径分别替换为 /data1/www/websties1 和 /data1/www/websties2
>>/data1/soft/tomcat/bin/startup.sh //启动tomcat
>>/data1/soft/tomcat/bin/shutdown.sh //停止tomcat

开机启动:
http://jingyan.baidu.com/article/6525d4b1382f0aac7d2e9421.html
【tomcat1、tomcat2分别设置为启动项】
除了百度经验之外,要在catalina.sh 里加上下面三行,否则在自启动的时候找不到jdk
export JAVA_HOME=/data1/soft/java/jdk1.6.0_45
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH

3、Nginx安装
>>rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
>>yum -y install nginx
通过命令安装后
配置文件:/etc/nginx/nginx.conf
域名配置:/etc/nginx/conf.d/ 一个域名一个配置文件,不要的域名可以备份后删除

server {
listen 80;
server_name test.wizzer.cn;
location / {
proxy_pass http://test.wizzer.cn:8101;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

日志文件路径:/var/logs/nginx
服务启动或通知
>>service nginx stop
>>service nginx start
设置为随机启动
>>chkconfig nginx on

4、Nginx代理权限设置
若通过域名+端口号可访问,但80端口显示 502 Bad Gateway ,日志/var/logs/nginx/error.log报
2015/12/19 18:02:03 [crit] 9978#0: *8 connect() to 61.132.139.155:8101 failed (13: Permission denied) while connecting to upstream, client: 124.73.13.21, server: test.wizzer.cn, request: “GET / HTTP/1.1”, upstream: “http://61.132.139.155:8101/”, host: “test.wizzer.cn”
则需要设置selinux权限(宽容模式):
>>setenforce 0
>>setsebool -P httpd_can_network_connect 1

5、防火墙配置
>>vi /etc/sysconfig/iptables
#########################################################
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 21 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A INPUT -j REJECT –reject-with icmp-host-prohibited
-A FORWARD -j REJECT –reject-with icmp-host-prohibited
COMMIT
#########################################################

>>/etc/init.d/iptables restart #重启防火墙使配置生效,或者命令 service iptables restart

20145 月16

Linux:解决多个Tomcat Session冲突的问题

<Context path=”” docBase=”/usr/local/tomcat8080/webapps/ROOT” reloadable=”true” sessionCookiePath=”/” sessionCookieName=”BB_SESSION”/>

更改SESSION名称。

 

201211 月8

Apache + Tomcat 负载均衡单机部署实例

一、准备工作

Tomcat6 : http://tomcat.apache.org/download-60.cgi

下载:apache-tomcat-6.0.36.exe

apache httpd server 2.2: http://www.fayea.com/apache-mirror//httpd/binaries/win32/

下载:httpd-2.2.22-win32-x86-no_ssl.msi

apache tomcat connector: http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.31/

下载:mod_jk-1.2.31-httpd-2.2.3.so

二、安装配置

安装路径:

E:\Apache2.2

E:\apache-tomcat-6.0.36-1

E:\apache-tomcat-6.0.36-2

项目路径:

E:\work\demo

1、Apache 配置

修改 E:\Apache2.2\conf\httpd.conf  文件。

1)、加载外部配置文件:

文件最后一行加上

include conf/mod_jk.conf

2)配置项目路径:

<IfModule alias_module>
    Alias /demo "E:/work/demo"
    ScriptAlias /cgi-bin/ "E:/Apache2.2/cgi-bin/"
</IfModule>

3)配置目录权限:

<Directory "E:/work/demo"> 
   Order Deny,Allow 
   Allow from all 
</Directory>

4)配置默认首页:
增加 index.jsp

<IfModule dir_module>
    DirectoryIndex index.jsp index.html
</IfModule>

5)增加 E:\Apache2.2\conf\mod_jk.conf 文件内容:

LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so
JkWorkersFile conf/workers.properties
#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名
JkMount /*.jsp controller

同时将 mod_jk-1.2.31-httpd-2.2.3.so 文件放入 E:\Apache2.2\modules 文件夹下。

6)增加 E:\Apache2.2\conf\workers.properties 文件内容:

#server
worker.list = controller
#========tomcat1========
worker.tomcat1.port=10009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
#========tomcat2========
worker.tomcat2.port=11009
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1

#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2
worker.controller.sticky_session=false
worker.controller.sticky_session_force=1
#worker.controller.sticky_session=1

2、Tomcat 配置

1)Tomcat-1 配置

E:\apache-tomcat-6.0.36-1\conf\server.xml

需要修改端口的地方:

<Server port="10005" shutdown="SHUTDOWN">
<Connector port="10080" URIEncoding="GBK" protocol="HTTP/1.1" 
connectionTimeout="20000" keepAliveTimeout="15000" maxKeepAliveRequests="1" 
redirectPort="8443" />
<Connector port="10009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
   <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

1)Tomcat-2 配置

E:\apache-tomcat-6.0.36-2\conf\server.xml

需要修改端口的地方:

<Server port="11005" shutdown="SHUTDOWN">
<Connector port="11080" URIEncoding="GBK" protocol="HTTP/1.1" 
connectionTimeout="20000" keepAliveTimeout="15000" maxKeepAliveRequests="1" 
redirectPort="8443" />
<Connector port="11009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
   <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

三、项目测试

项目 web.xml 文件需在 <web-app>下增加<distributable/>。

test.jsp

<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="java.util.*"%>
<html>
	<head>
		<title>Cluster App Test</title>
	</head>
	<body>
		Server Info:
		<%
		out.println(request.getLocalAddr() + " : " + request.getLocalPort() + "<br>");
		%>
		<%
			out.println("<br> ID " + session.getId() + "<br>");
			// 如果有新的 Session 属性设置
			String dataName = request.getParameter("dataName");
			if (dataName != null && dataName.length() > 0) {
				String dataValue = request.getParameter("dataValue");
				session.setAttribute(dataName, dataValue);
			}
			out.println("<b>Session 列表</b><br>");
			System.out.println("============================");
			Enumeration e = session.getAttributeNames();
			while (e.hasMoreElements()) {
				String name = (String) e.nextElement();
				String value = session.getAttribute(name).toString();
				out.println(name + " = " + value + "<br>");
				System.out.println(name + " = " + value);
			}
		%>
		<form action="test.jsp" method="POST">
			名称:
			<input type=text size=20 name="dataName">
			<br>
			值:
			<input type=text size=20 name="dataValue">
			<br>
			<input type=submit>
		</form>
	</body>
</html>

先启动Apache2服务,之后依次启动两个tomcat。
分别访问:
http://127.0.0.1:10080/test.jsp
http://127.0.0.1:11080/test.jsp
http://127.0.0.1/test.jsp
接下来测试你懂得,三者 seesion 内容一致即配置成功。

四、注意事项

1、若测试结果不成功,可以查看日志看看报什么错误,是否配置疏忽了什么环节,apache的权限有没有配置等,注意版本;
2、放在session里的对象需要序列化,即类 implements Serializable。