Java教程

Tomcat环境部署

本文主要是介绍Tomcat环境部署,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录

1、安装 JDK 

2、部署 Tomcat

(1)配置 Tomcat 环境变量

(2)配置 Tomcat pid 文件和相关参数

(3)配置 Tomcat 服务脚本

(4)启动服务

(5)提升 Tomcat 启动速度

3、配置 Tomcat web 控制页面

4、Web 站点部署(Jpress)

(1)配置数据库

5、Tomcat 虚拟主机配置

(1)修改 server.xml

(2)虚拟主机目录及文件准备

(3)配置 hosts 解析

 (4)重启 Tomcat

(5)测试


1、安装 JDK 

[root@localhost ~]# ls
anaconda-ks.cfg  apache-tomcat-8.5.20.tar.gz  jdk-8u144-linux-x64.rpm
[root@localhost ~]# yum install -y jdk-8u144-linux-x64.rpm

2、部署 Tomcat

[root@localhost ~]# tar xf apache-tomcat-8.5.20.tar.gz -C /usr/local
[root@localhost ~]# cd /usr/local/
[root@localhost java]# ln -sv apache-tomcat-8.5.20/ tomcat
‘tomcat’ -> ‘apache-tomcat-8.5.20/’

(1)配置 Tomcat 环境变量

[root@localhost local]# vim /etc/profile.d/tomcat.sh 
# TOMCAT
export CATALINA_HOME=/usr/local/tomcat   
# PATH
export PATH=$PATH:$CATALINA_HOME

(2)配置 Tomcat pid 文件和相关参数

[root@localhost local]# vim /usr/local/tomcat/bin/setenv.sh 
# add tomcat pid
CATALINA_PID="$CATALINA_HOME/tomcat.pid"
# add java opts
JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=512m -Xms512M -Xmx512M -XX:MaxNewSize=256m"

(3)配置 Tomcat 服务脚本

[root@localhost local]# vim /usr/lib/systemd/system/tomcat.service 
[Unit]
Description=Apache Tomcat Web Application 
ContainerAfter=syslog.target network.target

[Service]
Type=forking
PIDFile=/usr/local/tomcat/tomcat.pid
ExecStart=/usr/local/tomcat/bin/catalina.sh start
ExecReload=/bin/kill -s HUP MAINPID
ExecStop=/bin/kill−s QUIT MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

(4)启动服务

[root@localhost local]# systemctl daemon-reload
[root@localhost local]# systemctl status tomcat.service
[root@localhost local]# systemctl enable tomcat.service
[root@localhost local]# systemctl is-enabled tomcat.service
enabled
[root@localhost local]# systemctl start tomcat.service

# 查看 Tomcat 开启端口状态
[root@localhost ~]# netstat -lnutp | grep :8080
tcp6       0      0 :::8080                 :::*                    LISTEN      3060/java 
# 查看当前运行进程
[root@localhost ~]# ps -ef | grep java
root       3060      1  0 13:16 ?        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 -server -XX:PermSize=256M -XX:MaxPermSize=512m -Xms512M -Xmx512M -XX:MaxNewSize=256m -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -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       3910   2183  0 13:32 pts/0    00:00:00 grep --color=auto java

(5)提升 Tomcat 启动速度

# 查看是否有足够的熵来用于产生随机数
[root@localhost local]# cat /proc/sys/kernel/random/entropy_avail 
1101

# 安装rngd服务(熵服务,增大熵池)
[root@localhost ~]# yum install rng-tools
# 开机自启动服务
[root@localhost ~]# systemctl enable --now rngd 

# 再次查看
[root@localhost local]# cat /proc/sys/kernel/random/entropy_avail 
3245

3、配置 Tomcat web 控制页面

注意:不要在生产环境使用

# 添加如下内容
[root@localhost local]# vim /usr/local/tomcat/conf/tomcat-users.xml
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-jmx"/>
<role rolename="manager-script"/>
<role rolename="manager-status"/>
<user username="tomcat" password="tomcat" roles="admin-gui,manager-gui,managerjmx,manager-script,manager-status,admin-script"/>

# 允许访问 Manager App
[root@localhost local]# vim /usr/local/tomcat/webapps/host-manager/META-INF/context.xml
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"  allow="^.*$" />

# 允许访问 Host Manager
[root@localhost local]# vim /usr/local/tomcat/webapps/manager/META-INF/context.xml
  <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" />

# 重启 Tomcat 服务
[root@node01 local]# systemctl restart tomcat.service

在浏览器访问:http://192.168.3.99:8080/

4、Web 站点部署(Jpress)

[root@localhost ~]# ll
total 192620
-rw-r--r--. 1 root root      2831 Jan  4 21:49 1
-rw-------. 1 root root      1409 Jan  2 12:07 anaconda-ks.cfg
-rw-r--r--. 1 root root   9433364 Jan  2 13:07 apache-tomcat-8.5.20.tar.gz
-rw-r--r--. 1 root root 169971490 Jan  2 13:05 jdk-8u144-linux-x64.rpm
drwxr-xr-x. 2 root root         6 Jan  4 16:38 JspRun!_6.0.0_UTF8
-rw-r--r--. 1 root root  17825359 Jan  4 16:30 JspRun6.0.0_UTF8_1.zip
[root@localhost ~]#mv jpress-web-newest.war /usr/local/tomcat/webapps/

(1)配置数据库

[root@localhost bbs]# vim config.properties
dbhost = localhost
dbport=3306
dbuser = bbs
dbpw = 123456
dbname = jsprun
pconnect = 0
tablepre = jrun_

database = mysql
dbcharset =
charset = utf-8
headercharset = 0
tplrefresh = 1
version=5.1

[root@localhost local]# yum -y install mariadb-server
[root@localhost local]# systemctl start mariadb.service
[root@localhost local]# mysql -e "create database jsprun DEFAULT CHARACTER SET utf8;"
或者
MariaDB [(none)]> create database jsprun character set utf8;

[root@localhost local]# mysql -e "grant all on jsprun.* to bbs@'localhost' identified by '123456';"
或者
MariaDB [(none)]> grant all on jsprun.* to bbs@localhost identified by '123456';

浏览器访问:http://192.168.3.99:8080/jpress-web-newest/install

 

5、Tomcat 虚拟主机配置

(1)修改 server.xml

[root@localhost ~]# systemctl stop tomcat.service
[root@localhost bbs]# cd /usr/local/tomcat/conf/
[root@localhost conf]# cp server.xml{,.bak}
# 添加虚拟主机配置
[root@localhost conf]# vim server.xml
      </Host>
      <Host name="www.a.com" appBase="webapps"
            unpackWARs="true" autoDeploy="true">
      <Context docBase="/web/a" path="" reloadable="flase" />
      <Valve className="org.apache.catalina.valves.AccessLogValve"
             directory="logs"
             prefix="www.a.com_access_log" suffix=".txt"
             pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
      <Host name="www.b.com" appBase="webapps"
            unpackWARs="true" autoDeploy="true">
      <Context docBase="/web/b" path="" reloadable="flase" />
      <Valve className="org.apache.catalina.valves.AccessLogValve"
             directory="logs"
             prefix="www.b.com_access_log" suffix=".txt"
             pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>

(2)虚拟主机目录及文件准备

[root@localhost conf]# mkdir -p /web/{a,b}
[root@localhost conf]# vim /web/a/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP a page</title>
</head>
<body>
<% out.println("Welocome to test site,http://www.a.com");%>
</body>
</html>

[root@localhost conf]# vim /web/b/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP a page</title>
</head>
<body>
<% out.println("Welocome to test site,http://www.b.com");%>
</body>
</html>

(3)配置 hosts 解析

[root@localhost conf]# vim /etc/hosts
# 添加:
192.168.3.99 www.a.com www.b.com

 (4)重启 Tomcat

[root@localhost conf]# systemctl restart tomcat.service

(5)测试

[root@localhost conf]# curl www.a.com
这篇关于Tomcat环境部署的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!