Tomcat 简介
Tomcat是Apache软件基金会的Jakarta项目中开发的一个Servlet容器,实现了对Servlet和JavaServerPage(JSP)的支持,Tomcat本身也是一个HTTP服务器,Tomcat主要用来解析JSP语言,html格式的网页页面。
Tomcat安装
JDK下载
JDK下载地址:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
获取下载JDK临时登陆oracle用户:http://bugmenot.com/view/oracle.com
解压JDK
root@localhost ~]# rz -y # 将本地下载好的JDK软件上传到Linux服务器上rz waiting to receive.Starting zmodem transfer. Press Ctrl+C to cancel.Transferring jdk-8u261-linux-i586.tar.gz... 100% 140096 KB 15566 KB/sec 00:00:09 0 Errors[root@localhost ~]# yum install glibc.i686 -y[root@localhost ~]# cd /usr/local[root@localhost local]# mkdir {java,tomcat}[root@localhost local]# cd ~[root@localhost ~]# tar -xf jdk-8u261-linux-i586.tar.gz[root@localhost ~]# mv jdk1.8.0_261/ /usr/local/java
配置Java环境变量
[root@localhost local]# vim /etc/profile #在 /etc/profile配置文末添加Java路径和环境变量export JAVA_HOME=/usr/local/java/jdk1.8.0_261export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATHexport CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib [root@localhost ~]# [root@localhost local]# source /etc/profile # 使Java环境变量立即生效[root@localhost local]# which java/usr/local/java/jdk1.8.0_261/bin/java[root@localhost local]# java -version # 查看Java版本java version "1.8.0_261"Java(TM) SE Runtime Environment (build 1.8.0_261-b12)Java HotSpot(TM) Client VM (build 25.261-b12, mixed mode)[root@localhost local]#
java环境变量常见报错
问题:[root@localhost ~]# java -version -bash: /usr/local/java/bin/java: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory解决方案:[root@localhost ~]# yum install glibc.i686 -y
下载Tomcat软件
[root@localhost local]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.57/bin/apache-tomcat-8.5.57.tar.gz--2020-09-05 16:28:15-- https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.57/bin/apache-tomcat-8.5.57.tar.gzResolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 10379806 (9.9M) [application/x-gzip]Saving to: ‘apache-tomcat-8.5.57.tar.gz’100%[================================================>] 10,379,806 1.13MB/s in 9.3s2020-09-05 16:28:24 (1.06 MB/s) - ‘apache-tomcat-8.5.57.tar.gz’ saved [10379806/10379806][root@localhost local]#
安装Tomcat
[root@localhost local]# tar -xf apache-tomcat-8.5.57.tar.gz[root@localhost ~]# mv apache-tomcat-8.5.57 /usr/local/tomcat # 配置 Tomcat 环境变量[root@localhost local]# echo 'export TOMCAT_HOME=/usr/local/tomcat' >> /etc/profile[root@localhost local]# source /etc/profile[root@localhost local]#[root@localhost ~]# tail -4 /etc/profileexport JAVA_HOME=/usr/local/java/jdk1.8.0_261export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATHexport CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/libexport TOMCAT_HOME=/usr/local/tomcat[root@localhost ~]#
启动Tomcat
[root@localhost ~]# /usr/local/tomcat/apache-tomcat-8.5.57/bin/startup.shUsing CATALINA_BASE: /usr/local/apache-tomcat-8.5.57Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.57Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.5.57/tempUsing JRE_HOME: /usr/local/jdkUsing CLASSPATH: /usr/local/apache-tomcat-8.5.57/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.57/bin/tomcat-juli.jarTomcat started.[root@localhost bin]# [root@localhost bin]# ps -ef | grep javaroot 110329 104564 0 16:56 pts/1 00:00:00 grep --color=auto java[root@localhost bin]# ps -ef | grep tomcatroot 110331 104564 0 16:56 pts/1 00:00:00 grep --color=auto tomcat
在Firewalld上添加8080端口,关闭SELinux
[root@localhost ~]# firewall-cmd --zone=public --add-port=8080/tcp --permanentsuccess[root@localhost ~]# setenforce 0[root@localhost ~]# getenforcePermissive[root@localhost ~]#
打开浏览器输入IP地址:端口,访问tomcat默认页面
开启和关闭Tomcat
[root@localhost ~]# /usr/local/tomcat/apache-tomcat-8.5.57/bin/shutdown.sh # 关闭 Tomcat[root@localhost ~]# /usr/local/tomcat/apache-tomcat-8.5.57/bin/startup.sh # 启动 Tomcat
配置 server.xml文件 及 虚拟主机
server 模块: 代表整个容器,相当于整个tomcat软件,port指定Tomcat监听shutdown命令端口
service 模块:相当于JVM,是容器类组件,包含其他组件
connector 模块:可指定port监听端口来接收请求,protocol指定连接器使用的协议,支持http与AJP,默认两个协议都开一个端口,一般不用AJP,注释掉
engine 模块: 容器组件,默认引擎为catalina,defaultHost指定默认虚拟主机
host模块:容器组件,表示一个虚拟主机,name表示主机名/域名,appBase表示网页根目录,一般为webapps,也可写绝对路径,unpackWARs为true则自动解压war包,否则直接从war文件中运行应用程序,autoDeploy="true"则在tomcat启动时,自动部署此主机
[root@localhost ~]# cd /usr/local/tomcat/apache-tomcat-8.5.57/conf/[root@localhost conf]# lsCatalina context.xml logging.properties tomcat-users.xsdcatalina.policy jaspic-providers.xml server.xml web.xmlcatalina.properties jaspic-providers.xsd tomcat-users.xml[root@localhost conf]# vim server.xml # 在server.xml配置文件末尾添加如下内容<Context path="/" docBase="/data/webapps/www" roloadable="true"/>
在 /data/webapps/www目录下,创建自己的html页面,重启Tomcat即可访问
[root@localhost ~]# cd /usr/local/tomcat/apache-tomcat-8.5.57/webapps/[root@localhost webapps]# lsdocs examples host-manager manager ROOT[root@localhost webapps]# cp -a examples /data/webapps/[root@localhost webapps]# cd /data/webapps/[root@localhost webapps]# mv examples www[root@localhost webapps]# cd www[root@localhost www]# lsindex.html jsp servlets WEB-INF websocket[root@localhost www]# vim index.html "en">"UTF-8"></head>
Welcome to Apache Tomcat
H3>
</p>
></html>[root@localhost ~]# /usr/local/tomcat/apache-tomcat-8.5.57/bin/shutdown.sh[root@localhost ~]# /usr/local/tomcat/apache-tomcat-8.5.57/bin/startup.sh
打开浏览器,访问Tomcat虚拟主机
基于端口的虚拟主机
[root@localhost www]# vim index.html<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /><Connector port="81" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
通过Tomcat部署zrlog博客网站
zrlog是一款开源的java博客系统
官网:https://www.zrlog.com/
下载 zrlog 软件
[root@localhost ~]# wget http://dl.zrlog.com/release/zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war&ref=index[1] 6665--2020-09-08 14:38:28-- http://dl.zrlog.com/release/zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.warResolving dl.zrlog.com (dl.zrlog.com)... [root@localhost ~]# 113.113.73.41, 14.152.86.41Connecting to dl.zrlog.com (dl.zrlog.com)|113.113.73.41|:80... connected.HTTP request sent, awaiting response... 200 OKLength: 10316293 (9.8M) [application/zip]Saving to: ‘zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war’100%[=============================================>] 10,316,293 9.19MB/s in 1.1s 2020-09-08 14:38:30 (9.19 MB/s) - ‘zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war’ saved [10316293/10316293][1]+ Done wget http://dl.zrlog.com/release/zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war[root@localhost ~]# [root@localhost ~]# ls | grep *.warzrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war
将zrlog软件包移动到webapps目录下时会自动解压
[root@localhost ~]# cd /usr/local/tomcat/apache-tomcat-8.5.57/webapps[root@localhost webapps]# cd [root@localhost ~]# mv zrlog-2.1.3-b5f0d63-release.war\?attname\=ROOT.war /usr/local/tomcat/apache-tomcat-8.5.57/webapps/zrlog.war[root@localhost ~]# cd /usr/local/tomcat/apache-tomcat-8.5.57/webapps/[root@localhost webapps]# lsdocs examples host-manager manager ROOT zrlog zrlog.war
将zrlog目录更名为ROOT
[root@localhost webapps]# mv ROOT ROOT.bak[root@localhost webapps]# mv zrlog ROOT
安装Mariadb数据库
为 zrlog博客网站创建数据库
[root@localhost ~]# yum install mariadb mariadb-server.............省略部分安装过程..............Updated: mariadb.x86_64 1:5.5.65-1.el7 mariadb-server.x86_64 1:5.5.65-1.el7 Dependency Updated: mariadb-libs.x86_64 1:5.5.65-1.el7 Complete![root@localhost ~]#
启动并初始化Mariadb数据库
[root@localhost ~]# systemctl start mariadb[root@localhost ~]# systemctl enable mariadb[root@localhost ~]# mysql_secure_installation # 初始化数据库Enter current password for root (enter for none): # 敲回车OK, successfully used password, moving on...Set root password? [Y/n] y # 配置数据库登陆密码New password: Re-enter new password: Password updated successfully!Reloading privilege tables.. ... Success!Remove anonymous users? [Y/n] y # 删除匿名用户 ... Success!Disallow root login remotely? [Y/n] y # 禁止root远程登陆 ... Success!Remove test database and access to it? [Y/n] y # 删除 test数据库 - Dropping test database... ... Success! - Removing privileges on test database... ... Success!Reload privilege tables now? [Y/n] y # 刷新授权表 ... Success!Cleaning up...All done! If you've completed all of the above steps, your MariaDBinstallation should now be secure.Thanks for using MariaDB![root@localhost ~]#
在Firewall上添加MySQL,并重启Firewall
[root@localhost ~]# firewall-cmd --permanent --add-service=mysqlsuccess[root@localhost ~]# firewall-cmd --reloadsuccess[root@localhost ~]#
登陆Mariadb数据库,创建zrlog数据库和zrlog用户并给用户授权
[root@localhost ~]# mysql -uroot -p123456Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 10Server version: 5.5.65-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> create database zrlog; # 创建 zrlog数据库Query OK, 1 row affected (0.05 sec) # 创建数据库用户并授权MariaDB [(none)]> grant all on zrlog.* to 'zrlog'@'192.168.203.129' identified by '123456';Query OK, 0 rows affected (0.03 sec)MariaDB [(none)]>
用户:zrlog ,密码:123456
授权host主机:192.168.203.129
打开浏览器访问:http://192.168.203.129:80(tomcat端口)
配置管理员账号信息
登陆zrlog博客后台网站
在浏览器新开一个页面,输入:http://192.168.203.129/admin
使用管理员admin账号登陆后台
使用管理员登录后台就可以进行各种操作了
至此,Linux系统下安装Tomcat并完成部署网站!