首页 > 代码库 > 37 java技术体系基础、tomcat核心概念及使用初步、tomcat配置与应用

37 java技术体系基础、tomcat核心概念及使用初步、tomcat配置与应用

02 tomcat核心概念及使用初步


配置环境

node1: CentOS 6.7 192.168.1.121


CentOS 6 部署Tomcat

[root@node1 ~]# rpm -ivh jdk-7u67-linux-x64.rpm 

[root@node1 jdk1.7.0_67]# vim /etc/profile.d/java.sh

export JAVA_HOME=/usr/java/latest

export PATH=$JAVA_HOME/bin:$PATH

[root@node1 jdk1.7.0_67]# . /etc/profile.d/java.sh

[root@node1 ~]# tar xf apache-tomcat-8.0.38.tar.gz -C /usr/local/

[root@node1 ~]# cd /usr/local/

[root@node1 local]# ln -s apache-tomcat-8.0.38/ tomcat

[root@node1 local]# cd tomcat/

[root@node1 tomcat]# vim /etc/profile.d/tomcat.sh

export CATALINA_HOME=/usr/local/tomcat

export PATH=$CATALINA_HOME/bin:$PATH

[root@node1 tomcat]# . /etc/profile.d/tomcat.sh

#启动Tomcat

[root@node1 tomcat]# catalina.sh start


03 tomcat配置与应用


配置环境

node1: CentOS 7.2 192.168.1.131


1、CentOS 7 部署Tomcat

[root@node1 ~]# yum install jdk-8u25-linux-x64.rpm 

[root@node1 ~]# vim /etc/profile.d/java.sh

export JAVA_HOME=/usr/java/latest

export PATH=$JAVA_HOME/bin:$PATH

[root@node1 ~]# . /etc/profile.d/java.sh

[root@node1 ~]# tar xf apache-tomcat-8.0.38.tar.gz -C /usr/local/

[root@node1 ~]# cd /usr/local/

[root@node1 local]# ln -s apache-tomcat-8.0.38 tomcat

[root@node1 local]# cd tomcat/

[root@node1 tomcat]# vim /etc/profile.d/tomcat.sh

export CATALINA_HOME=/usr/local/tomcat

export PATH=$CATALINA_HOME/bin:$PATH

[root@node1 tomcat]# . /etc/profile.d/tomcat.sh

#启动Tomcat

[root@node1 tomcat]# catalina.sh start


2、手动添加一个测试应用程序

[root@node1 webapps]# pwd

/usr/local/tomcat/webapps

[root@node1 webapps]# mkdir -p myapp/{lib,classes,WEB-INF,META-INF}

[root@node1 webapps]# tree myapp/

myapp/

├── classes

├── lib

├── META-INF

└── WEB-INF

[root@node1 webapps]# vim myapp/index.jsp

<%@ page language="java" %>

<%@ page import="java.util.*" %>

<html>

<head>

<title>JSP Test Page</title>

</head>

<body>

<% out.println("Hello,world."); %>

</body>

</html>


访问测试页:http://192.168.1.131:8080/myapp/

3、添加访问“Manager App”用户

[root@node1 tomcat]# cd conf/

[root@node1 conf]# vim tomcat-users.xml 

添加

 <role rolename="manager-gui"/>

 <user username="tomcat" password="tomcat" roles="manager-gui"/>

[root@node1 conf]# catalina.sh stop

[root@node1 conf]# catalina.sh start


4、添加访问“Host Manager”用户

[root@node1 conf]# vim tomcat-users.xml 

添加

 <role rolename="manager-gui"/>

 <role rolename="admin-gui"/>

 <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>  

[root@node1 conf]# catalina.sh stop

[root@node1 conf]# catalina.sh start 



[root@node1 conf]# cp server.xml{,.bak}

[root@node1 conf]# vim server.xml


04 tomcat配置与应用

配置环境

node1: CentOS 7.2 192.168.1.131

node12: CentOS 7.2 192.168.1.132




1、定义虚拟主机1

[root@node1 tomcat]# vim conf/server.xml

添加Host段如下:

 <Host name="web1.magedu.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="false">

<Context path="" docBase="/data/webapps" reloadable="true" />        

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"

  prefix="web1_access_log" suffix=".txt"

  pattern="%h %l %u %t &quot;%r&quot; %s %b" />

 </Host>

[root@node1 tomcat]# mkdir -p /data/{webapps,logs}

[root@node1 tomcat]# cp -r /usr/local/tomcat/webapps/myapp/* /data/webapps/

[root@node1 tomcat]# catalina.sh stop

[root@node1 tomcat]# catalina.sh start

[root@node2 ~]# curl web1.magedu.com:8080/index.jsp


2、定义虚拟主机2

[root@node1 tomcat]# cd /data/webapps/

[root@node1 webapps]# ls

classes  index.jsp  lib  META-INF  WEB-INF

[root@node1 webapps]# mkdir ROOT

[root@node1 webapps]# mv classes/ index.jsp lib/ META-INF/ WEB-INF/ ROOT

[root@node1 ~]# cd /usr/local/tomcat/conf/

[root@node1 conf]# vim server.xml

添加Host段如下:

 <Host name="web1.magedu.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true">

<Context path="" docBase="ROOT" reloadable="true" />

<Context path="/shop" docBase="shopxx" reloadable="true" />

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"

  prefix="web1_access_log" suffix=".txt"

  pattern="%h %l %u %t &quot;%r&quot; %s %b" />

 </Host>

[root@node1 conf]# mkdir /data/webapps/shopxx  

[root@node1 conf]# catalina.sh stop

[root@node1 conf]# catalina.sh start

[root@node2 ~]# curl web1.magedu.com:8080


3、一个简单的示例

[root@node1 ~]# unzip shopxx-a5-Beta.zip 

[root@node1 ~]# cd shopxx-v3.0-Beta/

[root@node1 shopxx-v3.0-Beta]# mv shopxx-3.0Beta/ /data/webapps/

[root@node1 shopxx-v3.0-Beta]# cd /data/webapps/

[root@node1 webapps]# rm -rf shopxx

[root@node1 webapps]# ln -s shopxx-3.0Beta shopxx

[root@node1 webapps]# catalina.sh stop

[root@node1 webapps]# catalina.sh start

[root@node1 webapps]# yum -y install mariadb-server

[root@node1 webapps]# systemctl start mariadb.service

[root@node1 webapps]# mysql

MariaDB [(none)]> grant all on shopxx.* to shopuser@‘localhost‘ identified by ‘shopuserpass‘;

MariaDB [(none)]> grant all on shopxx.* to shopuser@‘127.0.0.1‘ identified by ‘shopuserpass‘;

MariaDB [(none)]> flush privileges;

[root@node1 webapps]# cd shopxx

[root@node1 shopxx]# mv install/ backup

[root@node1 shopxx]# catalina.sh stop

[root@node1 shopxx]# catalina.sh start


4、拒绝某主机对网站的访问

[root@node1 ~]# vim /usr/local/tomcat/conf/server.xml

修改Host段的内容为(修改的内容为Context段)

 <Host name="web1.magedu.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true">

<Context path="" docBase="ROOT" reloadable="true">

<Valve className="org.apache.catalina.valves.RemoteAddrValve"

deny="192\.168\.56\.97"/>

 </Context>

<Context path="/shop" docBase="shopxx" reloadable="true" />

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"

  prefix="web1_access_log" suffix=".txt"

  pattern="%h %l %u %t &quot;%r&quot; %s %b" />

 </Host>

[root@node1 ~]# catalina.sh stop

[root@node1 ~]# catalina.sh start

测试成功


5、测试tomcat支持静态图片

[root@node1 ~]# cd /data/webapps/ROOT/

[root@node1 ROOT]# mkdir images

[root@node1 ROOT]# cd images/

[root@node1 images]# ls #上传两个图片

1.jpg  2.jpg

访问:

http://web1.magedu.com:8080/images/1.jpg

能正常访问

[root@node1 ~]# vim /usr/local/tomcat/conf/server.xml

[root@node1 ~]# cd /data/webapps/

[root@node1 webapps]# mv shopxx-3.0Beta/ /root/

[root@node1 webapps]# rm shopxx 

[root@node1 webapps]# mkdir shopxx

[root@node1 webapps]# catalina.sh stop

[root@node1 webapps]# catalina.sh start


6、LNMT

(1)用nginx反向解析tomcat全部内容

[root@node2 ~]# yum -y install nginx

[root@node2 ~]# cd /etc/nginx/

[root@node2 nginx]# vim nginx.conf

修改location /段的内容为

location / {

proxy_pass http://192.168.1.131:8080/;

}

[root@node2 nginx]# nginx -t

[root@node2 nginx]# systemctl start nginx.service

(2)用nginx反向解析tomcat指定网站(web1.magedu.com)

[root@node2 nginx]# vim /etc/hosts

添加:

192.168.1.131   web1.magedu.com

[root@node2 nginx]# vim nginx.conf

修改location /段的内容为:

location / {

proxy_pass http://web1.magedu.com:8080/;

}

[root@node2 nginx]# systemctl reload nginx.service


(3)动态内容发往tomcat主机(node1),静态内容留在本机(node2)

[root@node2 nginx]# vim nginx.conf

添加 

index   index.jsp index.html;

修改location /段的内容为:

location / {

}

location ~* \.(jsp|do)$ {

proxy_pass http://web1.magedu.com:8080;

}

[root@node2 nginx]# systemctl reload nginx.service

[root@node2 nginx]# mkdir -p /data/webpics/images

[root@node2 nginx]# cd /data/webpics/images/

[root@node2 images]# ls #下载两张图片

1.jpg  2.jpg

[root@node2 images]# vim /etc/nginx/nginx.conf

修改 

root         /usr/share/nginx/html

root         /data/webpics;

[root@node2 images]# systemctl reload nginx


7、LAMT

(1)proxy_module_http方式

[root@node2 images]# systemctl stop nginx.service

[root@node2 images]# yum -y install httpd

[root@node2 images]# cd /etc/httpd/

[root@node2 httpd]# vim conf/httpd.conf

注释

#DocumentRoot "/var/www/html"

[root@node2 httpd]# vim conf.d/vhosts.conf

添加以下内容:

<VirtualHost *:80>

ServerName web1.magedu.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost On

<Proxy *>

Require all granted

</Proxy>

ProxyPass / http://192.168.1.131:8080/

ProxyPassReverse / http://192.168.1.131:8080/

<Location />

Require all granted

</Location>

</VirtualHost>

[root@node2 httpd]# httpd -t

[root@node2 httpd]# systemctl start httpd.service 

(2)proxy_module_ajp:

[root@node2 httpd]# vim conf.d/vhosts.conf

<VirtualHost *:80>

ServerName web1.magedu.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost On

<Proxy *>

Require all granted

</Proxy>

ProxyPass / ajp://192.168.1.131:8009/

ProxyPassReverse / ajp://192.168.1.131:8009/

<Location />

Require all granted

</Location>

</VirtualHost>

[root@node2 httpd]# systemctl start httpd.service

(3)status不使用反向解析

[root@node2 httpd]# vim conf.d/vhosts.conf

在ProxyPass前面添加

ProxyPass /status !

[root@node2 httpd]# vim conf/httpd.conf 


本文出自 “追梦” 博客,请务必保留此出处http://sihua.blog.51cto.com/377227/1864908

37 java技术体系基础、tomcat核心概念及使用初步、tomcat配置与应用