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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了37 java技术体系基础tomcat核心概念及使用初步tomcat配置与应用相关的知识,希望对你有一定的参考价值。

02 tomcat核心概念及使用初步


配置环境

node1: CentOS 6.7 192.168.1.121


CentOS 6 部署Tomcat

[[email protected] ~]# rpm -ivh jdk-7u67-linux-x64.rpm 

[[email protected] jdk1.7.0_67]# vim /etc/profile.d/java.sh

export JAVA_HOME=/usr/java/latest

export PATH=$JAVA_HOME/bin:$PATH

[[email protected] jdk1.7.0_67]# . /etc/profile.d/java.sh

[[email protected] ~]# tar xf apache-tomcat-8.0.38.tar.gz -C /usr/local/

[[email protected] ~]# cd /usr/local/

[[email protected] local]# ln -s apache-tomcat-8.0.38/ tomcat

[[email protected] local]# cd tomcat/

[[email protected] tomcat]# vim /etc/profile.d/tomcat.sh

export CATALINA_HOME=/usr/local/tomcat

export PATH=$CATALINA_HOME/bin:$PATH

[[email protected] tomcat]# . /etc/profile.d/tomcat.sh

#启动Tomcat

[[email protected] tomcat]# catalina.sh start


03 tomcat配置与应用


配置环境

node1: CentOS 7.2 192.168.1.131


1、CentOS 7 部署Tomcat

[[email protected] ~]# yum install jdk-8u25-linux-x64.rpm 

[[email protected] ~]# vim /etc/profile.d/java.sh

export JAVA_HOME=/usr/java/latest

export PATH=$JAVA_HOME/bin:$PATH

[[email protected] ~]# . /etc/profile.d/java.sh

[[email protected] ~]# tar xf apache-tomcat-8.0.38.tar.gz -C /usr/local/

[[email protected] ~]# cd /usr/local/

[[email protected] local]# ln -s apache-tomcat-8.0.38 tomcat

[[email protected] local]# cd tomcat/

[[email protected] tomcat]# vim /etc/profile.d/tomcat.sh

export CATALINA_HOME=/usr/local/tomcat

export PATH=$CATALINA_HOME/bin:$PATH

[[email protected] tomcat]# . /etc/profile.d/tomcat.sh

#启动Tomcat

[[email protected] tomcat]# catalina.sh start


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

[[email protected] webapps]# pwd

/usr/local/tomcat/webapps

[[email protected] webapps]# mkdir -p myapp/{lib,classes,WEB-INF,META-INF}

[[email protected] webapps]# tree myapp/

myapp/

├── classes

├── lib

├── META-INF

└── WEB-INF

[[email protected] 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”用户

[[email protected] tomcat]# cd conf/

[[email protected] conf]# vim tomcat-users.xml 

添加

 <role rolename="manager-gui"/>

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

[[email protected] conf]# catalina.sh stop

[[email protected] conf]# catalina.sh start


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

[[email protected] conf]# vim tomcat-users.xml 

添加

 <role rolename="manager-gui"/>

 <role rolename="admin-gui"/>

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

[[email protected] conf]# catalina.sh stop

[[email protected] conf]# catalina.sh start 



[[email protected] conf]# cp server.xml{,.bak}

[[email protected] conf]# vim server.xml


04 tomcat配置与应用

配置环境

node1: CentOS 7.2 192.168.1.131

node12: CentOS 7.2 192.168.1.132




1、定义虚拟主机1

[[email protected] 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>

[[email protected] tomcat]# mkdir -p /data/{webapps,logs}

[[email protected] tomcat]# cp -r /usr/local/tomcat/webapps/myapp/* /data/webapps/

[[email protected] tomcat]# catalina.sh stop

[[email protected] tomcat]# catalina.sh start

[[email protected] ~]# curl web1.magedu.com:8080/index.jsp


2、定义虚拟主机2

[[email protected] tomcat]# cd /data/webapps/

[[email protected] webapps]# ls

classes  index.jsp  lib  META-INF  WEB-INF

[[email protected] webapps]# mkdir ROOT

[[email protected] webapps]# mv classes/ index.jsp lib/ META-INF/ WEB-INF/ ROOT

[[email protected] ~]# cd /usr/local/tomcat/conf/

[[email protected] 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>

[[email protected] conf]# mkdir /data/webapps/shopxx  

[[email protected] conf]# catalina.sh stop

[[email protected] conf]# catalina.sh start

[[email protected] ~]# curl web1.magedu.com:8080


3、一个简单的示例

[[email protected] ~]# unzip shopxx-a5-Beta.zip 

[[email protected] ~]# cd shopxx-v3.0-Beta/

[[email protected] shopxx-v3.0-Beta]# mv shopxx-3.0Beta/ /data/webapps/

[[email protected] shopxx-v3.0-Beta]# cd /data/webapps/

[[email protected] webapps]# rm -rf shopxx

[[email protected] webapps]# ln -s shopxx-3.0Beta shopxx

[[email protected] webapps]# catalina.sh stop

[[email protected] webapps]# catalina.sh start

[[email protected] webapps]# yum -y install mariadb-server

[[email protected] webapps]# systemctl start mariadb.service

[[email protected] webapps]# mysql

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

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

MariaDB [(none)]> flush privileges;

[[email protected] webapps]# cd shopxx

[[email protected] shopxx]# mv install/ backup

[[email protected] shopxx]# catalina.sh stop

[[email protected] shopxx]# catalina.sh start


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

[[email protected] ~]# 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>

[[email protected] ~]# catalina.sh stop

[[email protected] ~]# catalina.sh start

测试成功


5、测试tomcat支持静态图片

[[email protected] ~]# cd /data/webapps/ROOT/

[[email protected] ROOT]# mkdir images

[[email protected] ROOT]# cd images/

[[email protected] images]# ls #上传两个图片

1.jpg  2.jpg

访问:

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

能正常访问

[[email protected] ~]# vim /usr/local/tomcat/conf/server.xml

[[email protected] ~]# cd /data/webapps/

[[email protected] webapps]# mv shopxx-3.0Beta/ /root/

[[email protected] webapps]# rm shopxx 

[[email protected] webapps]# mkdir shopxx

[[email protected] webapps]# catalina.sh stop

[[email protected] webapps]# catalina.sh start


6、LNMT

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

[[email protected] ~]# yum -y install nginx

[[email protected] ~]# cd /etc/nginx/

[[email protected] nginx]# vim nginx.conf

修改location /段的内容为

location / {

proxy_pass http://192.168.1.131:8080/;

}

[[email protected] nginx]# nginx -t

[[email protected] nginx]# systemctl start nginx.service

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

[[email protected] nginx]# vim /etc/hosts

添加:

192.168.1.131   web1.magedu.com

[[email protected] nginx]# vim nginx.conf

修改location /段的内容为:

location / {

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

}

[[email protected] nginx]# systemctl reload nginx.service


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

[[email protected] nginx]# vim nginx.conf

添加 

index   index.jsp index.html;

修改location /段的内容为:

location / {

}

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

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

}

[[email protected] nginx]# systemctl reload nginx.service

[[email protected] nginx]# mkdir -p /data/webpics/images

[[email protected] nginx]# cd /data/webpics/images/

[[email protected] images]# ls #下载两张图片

1.jpg  2.jpg

[[email protected] images]# vim /etc/nginx/nginx.conf

修改 

root         /usr/share/nginx/html

root         /data/webpics;

[[email protected] images]# systemctl reload nginx


7、LAMT

(1)proxy_module_http方式

[[email protected] images]# systemctl stop nginx.service

[[email protected] images]# yum -y install httpd

[[email protected] images]# cd /etc/httpd/

[[email protected] httpd]# vim conf/httpd.conf

注释

#DocumentRoot "/var/www/html"

[[email protected] 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>

[[email protected] httpd]# httpd -t

[[email protected] httpd]# systemctl start httpd.service 

(2)proxy_module_ajp:

[[email protected] 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>

[[email protected] httpd]# systemctl start httpd.service

(3)status不使用反向解析

[[email protected] httpd]# vim conf.d/vhosts.conf

在ProxyPass前面添加

ProxyPass /status !

[[email protected] httpd]# vim conf/httpd.conf 


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

以上是关于37 java技术体系基础tomcat核心概念及使用初步tomcat配置与应用的主要内容,如果未能解决你的问题,请参考以下文章

Linux基础进程的概念及fork初识

Docker 实战核心概念及常用命令

Docker 实战核心概念及常用命令

机器学习概念及决策树

Zookeeper分布式过程协同技术 - 概念及基础

[笔记]《游戏架构设计与策划基础》第三章 游戏概念及原型设计