tomcat的安装配置以及使用
简介
tomcat 是 apache 软件基金会的核心项目,由 apache、SUN 等公司来一起研发。
tomcat是JSP+servlet的运行平台。
tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。实际上Tomcat 部分是Apache 服务器的扩展,但它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。
优点:
(1)免费的
(2)占用系统资源比较小
(3)跨平台性好,有JAVA环境就行
安装
一. 部署JAVA环境
# tar xf jdk-7u67-linux-x64.tar.gz
# mv jdk1.7.0_67/ /usr/local/java
# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/java
export PATH=$JAVA_HOME/bin:$PATH
# source /etc/profile.d/java.sh
# java -version
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)
二. 安装tomcat
# tar xf apache-tomcat-7.0.73.tar.gz
# mv apache-tomcat-7.0.73 /usr/local/tomcat
# useradd -d /usr/local/tomcat tomcat
# chown tomcat.tomcat /usr/local/tomcat/ -R
# cd /usr/local/tomcat/
bin 命令:shutdown.sh 关闭tomcat;startup.sh启动tomcat
lib java的库 .jar
logs 日志
temp 临时文件
work 运行JSP脚本需要的class文件
conf 配置文件
webapps DocumentRoot == /var/www/html/
三. 启动tomcat
方法一:
# cd bin/# ./startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
# netstat -tanp |grep 8080
tcp 0 0 :::8080 :::* LISTEN 2045/java
方法二: system-V脚本
# vim /etc/init.d/tomcat
#!/bin/bash
# Init file for Tomcat server daemon
JAVA_OPTS='-Xms64m -Xmx128m'
JAVA_HOME=/usr/local/java
CATALINA_HOME=/usr/local/tomcat
export JAVA_OPTS JAVA_HOME CATALINA_HOME
exec $CATALINA_HOME/bin/catalina.sh $1
配置文件
# vim /usr/local/tomcat/conf/server.xml
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
##Server中的port监听关闭tomcat的请求,shutdown指定向端口发送的命令串
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
##监听8080端口,负责建立HTTP连接。在通过浏览器访问Tomcat服务器的Web应用时,使用的就是这个连接器
connectionTimeout="20000"
##连接超时时间数(毫秒)
redirectPort="8443" />
##指定服务器正在处理http请求时收到一个SSL传输请求后重定向的端口号
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
##监听8009端口,负责和apache服务器建立连接。在把Tomcat与apache集成时,需要用到这个连接器
<Engine name="Catalina" defaultHost="localhost">
##defaultHost默认连接到哪个主机
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
##指定的主机名或ip,应用程序目录
unpackWARs="true" autoDeploy="true">
##是否自动解压war文件 ,是否自动发布
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
##定义日志文件及格式
</Host>
</Engine>
</Service>
</Server>
虚拟主机
一、修改配置文件
# vim /usr/local/tomcat/conf/server.xml
<Engine name="Catalina" defaultHost="localhost"> \\这里可以定义输入IP地址默认进入哪个网页
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps" \\虚拟主机1
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<Host name="www.test1.com" appBase="/myweb/test1" > \\虚拟主机2
<Context path="" docBase="ROOT" />
</Host>
<Host name="www.test2.com" appBase="/myweb/test2" > \\虚拟主机3
<Context path="" docBase="ROOT" />
</Host>
</Engine>
二、创建网页发布目录及测试网页
# mkdir -p /myweb/test{1,2}/ROOT
# vim /myweb/test1/ROOT/index.jsp
<html>
<head>
<title>JSP test page</title>
</head>
<body>
<% out.println("test1"); %>
</body>
</html>
三、重启服务
四、客户端解析,测试
多实例
作用:
- 多实例运行不同的应用(类似虚拟主机)
- 多实例运行相同的应用(实现负载均衡,支持高并发处理,session问题)
?
tomcat单机多实例配置
Tomcat:/usr/local/tomcat/{bin,lib}
baidu: /usr/local/tomcat/baidu/{conf,logs,temp,work,webapps} 8081
163: /usr/local/tomcat/163/{conf,logs,temp,work,webapps} 8082
google: /usr/local/tomcat/google/{conf,logs,temp,work,webapps} 8083
webapps: /webapps/ROOT
一、tomcat安装
二、配置多实例目录
# tree -d -L 3 /usr/local/tomcat/
/usr/local/tomcat/
├── bin
├── lib
── 163
│ ├── conf
│ ├── logs
│ ├── temp
│ ├── webapps
│ └── work
├── baidu
│ ├── conf
│ ├── logs
│ ├── temp
│ ├── webapps
│ └── work
├── google
├── conf
├── logs
├── temp
├── webapps
└── work
三、配置示例server.xml
# vim /usr/local/tomcat/baidu/conf/server.xml
<!-- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> -->
<Server port="8091" shutdown="SHUTDOWN">
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Host name="localhost" appBase="/webapps"
unpackWARs="true" autoDeploy="true">
四、写启动脚本
# vim /usr/local/tomcat/baidu/tomcat-baidu.sh
#!/bin/bash
#baidu
export CATALINA_HOME="/usr/local/tomcat"
export CATALINA_BASE="/usr/local/tomcat/baidu"
case "$1" in
tart)
$CATALINA_HOME/bin/startup.sh;;
stop)
$CATALINA_HOME/bin/shutdown.sh;;
restart)
$CATALINA_HOME/bin/shutdown.sh
$CATALINA_HOME/bin/startup.sh;;
esac
五、启动测试
# /usr/local/tomcat/baidu/tomcat-baidu.sh start
# /usr/local/tomcat/163/tomcat-163.sh start
# /usr/local/tomcat/163/tomcat-google.sh start
# netstat -tanp |grep 80
tcp 0 0 :::8081 :::* LISTEN 1587/java
tcp 0 0 :::8082 :::* LISTEN 1560/java tcp 0 0 :::8083 :::* LISTEN 1752/java tcp 0 0 ::ffff:127.0.0.1:8091 :::* LISTEN 1587/java tcp 0 0 ::ffff:127.0.0.1:8092 :::* LISTEN 1560/java tcp 0 0 ::ffff:127.0.0.1:8093 :::* LISTEN 1752/java tcp 0 0 :::8009 :::* LISTEN 1560/java
nginx+tomcat
Nginx + Tomcat 多实例实现网站的负载均衡
# vim /usr/local/nginx/conf/nginx.conf
http {
upstream tomcat {
server 192.168.122.43:8081;
server 192.168.122.43:8082;
}
server {
location / {
proxy_pass http://tomcat;
}
}
}
原文地址:https://www.cnblogs.com/we9999/p/11757541.html