1. tomcat简介
Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。由于有了Sun 的参与和支持,最新的Servlet 和JSP 规范总是能在Tomcat 中得到体现,Tomcat 5支持最新的Servlet 2.4 和JSP 2.0 规范。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应HTML(标准通用标记语言下的一个应用)页面的访问请求。实际上Tomcat是Apache 服务器的扩展,但运行时它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。
诀窍是,当配置正确时,Apache 为HTML页面服务,而Tomcat 实际上运行JSP 页面和Servlet。另外,Tomcat和IIS等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Apache服务器。目前Tomcat最新版本为9.0。
tomcat就是传说中的中间件之一,tomcat本身是一个容器,专门用来运行java程序,java语言开发的网页.jsp就应该运行于tomcat中。而tomcat本身的运行也依赖于jdk环境。
tomcat应用场景:lnmt
2. tomcat项目部署
2.1 java环境安装
//安装jdk环境
[root@localhost ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
.....安装过程略
//查看安装的版本
[root@localhost ~]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
2.2 tomcat部署
上官网下载tomcat
//下载tomcat
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.16/bin/apache-tomcat-9.0.16.tar.gz
//解压部署
[root@localhost src]# ls
apache-tomcat-9.0.16.tar.gz debug kernels
[root@localhost src]# tar xf apache-tomcat-9.0.16.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ln -s apache-tomcat-9.0.16/ tomcat
[root@localhost local]# ll
总用量 0
drwxr-xr-x 9 root root 220 3月 7 14:58 apache-tomcat-9.0.16
drwxr-xr-x. 2 root root 6 3月 10 2016 bin
drwxr-xr-x. 2 root root 6 3月 10 2016 etc
drwxr-xr-x. 2 root root 6 3月 10 2016 games
drwxr-xr-x. 2 root root 6 3月 10 2016 include
drwxr-xr-x. 2 root root 6 3月 10 2016 lib
drwxr-xr-x. 2 root root 6 3月 10 2016 lib64
drwxr-xr-x. 2 root root 6 3月 10 2016 libexec
drwxr-xr-x. 2 root root 6 3月 10 2016 sbin
drwxr-xr-x. 5 root root 49 3月 2 23:09 share
drwxr-xr-x. 2 root root 6 3月 10 2016 src
lrwxrwxrwx 1 root root 21 3月 7 14:59 tomcat -> apache-tomcat-9.0.16/
//写一个hello world的java页面
[root@localhost ~]# vim index.jsp
[root@localhost ~]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("Hellow World");
%>
</body>
</html>
[root@localhost ~]# mkdir /usr/local/tomcat/webapps/test
[root@localhost ~]# cp index.jsp /usr/local/tomcat/webapps/test/
//启动tomcat
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@localhost ~]# ps -ef|grep tomcat
root 21582 1 24 20:33 pts/0 00:00:05 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 21632 10293 0 20:34 pts/0 00:00:00 grep --color=auto tomcat
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
LISTEN 0 100 :::8009
默认首页
查看server status
修改配置文件
//修改为所有人都可以访问
[root@localhost local]# vim tomcat/webapps/manager/META-INF/context.xml
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|\d+\.\d+\.\d+\.\d+" />
//在</tomcat-users>前面添加如下配置
[root@localhost local]# vim tomcat/conf/tomcat-users.xml
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="tomcat" password="tomcat" roles="manager-gui,manager-script,manager-jmx,manager-status"/>
访问host-manage会出现如下错误
修改配置文件
//修改为所有人都可以访问
[root@localhost local]# vim tomcat/webapps/host-manager/META-INF/context.xml
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|\d+\.\d+\.\d+\.\d+" />
//增加一个admin-gui role
[root@localhost local]# vim tomcat/conf/tomcat-users.xml
<role rolename="admin-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui"/>
3.部署lnmt
在服务端配置多个tomcat
[root@localhost local]# cp -a apache-tomcat-9.0.8/ apache-tomcat2
[root@localhost local]3 ln -s apache-tomcat2 tomcat2
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 9 root root 160 3月 7 20:26 apache-tomcat2
drwxr-xr-x. 9 root root 160 3月 7 20:26 apache-tomcat-9.0.8
drwxr-xr-x. 2 root root 6 3月 10 2016 bin
drwxr-xr-x. 2 root root 6 3月 10 2016 etc
drwxr-xr-x. 2 root root 6 3月 10 2016 games
drwxr-xr-x. 2 root root 6 3月 10 2016 include
drwxr-xr-x. 2 root root 6 3月 10 2016 lib
drwxr-xr-x. 2 root root 6 3月 10 2016 lib64
drwxr-xr-x. 2 root root 6 3月 10 2016 libexec
drwxr-xr-x. 2 root root 6 3月 10 2016 sbin
drwxr-xr-x. 5 root root 49 3月 5 03:11 share
drwxr-xr-x. 2 root root 6 3月 10 2016 src
lrwxrwxrwx. 1 root root 20 3月 7 20:27 tomcat -> apache-tomcat-9.0.8/
lrwxrwxrwx. 1 root root 14 3月 7 20:46 tomcat2 -> apache-tomcat2
修改端口为8081
[root@localhost local]# vim tomcat2/conf/server.xml
<Server port="8006" shutdown="SHUTDOWN">
修改这个port=”8006”,原来默认的为:8005,使得它的关闭端口和另一个关闭端口不发生冲突。
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改port=”8081”,原来默认的为“8080”,使得它的连接端口和另一个不冲突。
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
修改这个port=”8010”,原来默认的为:8009,AJP 1.3 Connector定义的地方。
设置test目录下的默认首页
//8080端口
[root@localhost local]# tomcat/webapps/
[root@localhost local]# mkdir test
[root@localhost local]# cd test/
[root@localhost local]# vim index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("192.168.110.30:8080");
%>
</body>
</html>
//8081端口
[root@localhost local]# tomcat2/webapps/
[root@localhost local]# mkdir test
[root@localhost local]# cd test/
[root@localhost local]# vim index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("192.168.110.30:8081");
%>
</body>
</html>
8081端口验证
nginx配置添加如下内容
[root@server ~]# vim /etc/nginx/nginx.conf
upstream lwf.com{ //实现简单负载均衡
server 192.168.110.30:8080;
server 192.168.110.30:8081;
}
location / {
root html;
index index.html index.htm;
proxy_pass http://lwf.com; // 反向代理
}
配置文件
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
upstream lwf.com{
server 192.168.110.30:8080;
server 192.168.110.30:8081;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root html;
index index.html index.htm;
proxy_pass http://lwf.com;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
效果
刷新几下