首页
学
【学】PHP
【学】前端
【学】Linux
【学】杂学
【学】支付
【学】Docker
享
【享】文件
【享】工具
日记
Me
【Me】我
【Me】留言
老郭博客-程序员客栈
文章模型
下载模型
首页
学
【学】PHP
【学】前端
【学】Linux
【学】杂学
【学】支付
【学】Docker
享
【享】文件
【享】工具
日记
Me
【Me】我
【Me】留言
首页
学
【学】Linux
Centos7搭建JDK+Tomcat
Centos7搭建JDK+Tomcat
发布时间:
3年前
作者:
老郭
热度:
1147 ℃
评论数:
# Centos7搭建JDK+Tomcat ### 一、软件准备 本篇采用tar.gz安装,当然也可以采用rpm方式安装或yum方式安装,至于后两种安装方式感兴趣的请自行查阅相关文章。 JDK文件:jdk-8u131-linux-x64.tar.gz 官方下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html Tomcat文件:apache-tomcat-8.5.15.tar.gz 官方下载地址:http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.15/bin/apache-tomcat-8.5.15.tar.gz 或:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 注意:因为在前两篇文章中都是以CentOS7 x64来介绍的,所以这里下载的压缩包也都是x64的。 ### 二、安装Oracle JDK ##### 1、卸载OpenJDK 采用前面的方式安装CentOS7时,默认安装了OpenJDK1.8的,OpenJDK其实就是也是在Java规范上的另一种实现,就如同Mono也是按照微软的.NET规范实现的一样,在一个平台上编译后的文件可以直接在另一个平台上运行,反正几年前我就干过在Windows上用Microsoft Visual Studio编写ASP.NET程序然后放在Linux系统上借助Apache+Mono+xsp运行。 因为后面还有更多内容依赖于Java,所以这里就卸载OpenJDK而安装Oracle JDK了。 检查当前系统是否安装JDK,运行命令: ```shell rpm -qa | grep jdk ``` 得到结果如下: ```shell java-1.8.0-openjdk-headless-1.8.0.131-2.b11.el7_3.x86_64 java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64 copy-jdk-configs-1.2-1.el7.noarch ``` 卸载OpenJDK,运行命令: ```shell yum remove java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64 yum remove java-1.8.0-openjdk-headless-1.8.0.131-2.b11.el7_3.x86_64 ``` 此时如果再运行` rpm -qa | grep jdk` 就会看到`java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64`被卸载了。 ##### 2、安装Oracle JDK 按照本文提供的地址将`jdk-8u131-linux-x64.tar.gz`下载到宿主机,然后借助SSH客户端软件SSHSecureShellClient上传到/root目录。 解压文件: ```shell tar –zxf /root/ jdk-8u131-linux-x64.tar.gz ``` 解压后会在/root目录下多出一个`jdk1.8.0_131`文件夹来,然后通过下面的命令将其移动到`/usr/local`目录下: ```shell mv ./jdk1.8.0_131/ /usr/local ``` 注意:`/usr/local/jdk1.8.0_131`这个目录在设置JDK环境变量时会用到。 >下面就需要配置环境变量了,告诉系统Java的相关信息,如安装路径、类库路径等。 这里有两种配置方式:一种是为指定用户配置,一种是为所有用户配置,如果只为某个用户配置那么只需在该用户目录下的`.bashrc`文件中设置即可,如果需要给所有用户设置,那么就需要在`/etc/profile`中设置,这里以全局设置为例,打开`/etc/profile`文件在最后添加如下内容: ```shell export JAVA_HOME=/usr/local/jdk1.8.0_131 export JRE_HOME=/usr/local/jdk1.8.0_131 export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$PATH:$JAVA_HOME/bin ``` 然后使配置生效,执行如下命令: ```shell source /etc/profile ``` 现在可以检查Java环境变量是否配置正确了,通过`java –version`命令,在本人机器上得到如下输出: ```shell [root@localhost ~]# java -version java version "1.8.0_131" Java(TM) SE Runtime Environment (build 1.8.0_131-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode) ``` 至此,Oracle JDK已正确安装。 ### 三、换一种姿势安装JDK(推荐) ##### 1、卸载 查看已经安装的jdk: ```shell [root@bogon jre]# rpm -qa|grep jdk java-1.8.0-openjdk-headless-1.8.0.65-3.b17.el7.x86_64 java-1.7.0-openjdk-1.7.0.91-2.6.2.3.el7.x86_64 java-1.7.0-openjdk-headless-1.7.0.91-2.6.2.3.el7.x86_64 java-1.8.0-openjdk-1.8.0.65-3.b17.el7.x86_64 ``` 卸载命令: ```shell [root@bogon jre]# yum -y remove java-1.8.0-openjdk-headless-1.8.0.65-3.b17.el7.x86_64 ``` 卸载完成之后Java命令不被识别: ```shell [root@bogon lib]# java -version bash: java: command not found... ``` ##### 2、安装 去官网下载jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 解压到安装目录: ```shell [root@bogon software]# tar -zxvf jdk-8u101-linux-x64.tar.gz -C /usr/local/java/ ``` 安装完毕之后在`/etc/profile`文件末尾添加: ```shell [root@bogon software]# vim /etc/profile export JAVA_HOME=/usr/local/java/jdk1.8.0_161 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH ``` 使`/etc/profile`生效: ```shell [root@bogon jdk1.8.0_101]# source /etc/profile ``` 检测安装是否成功: ```shell [root@bogon jdk1.8.0_101]# java -version java version "1.8.0_101" Java(TM) SE Runtime Environment (build 1.8.0_101-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode) [root@bogon jdk1.8.0_101]# javac Usage: javac <options> <source files> where possible options include: -g Generate all debugging info -g:none Generate no debugging info -g:{lines,vars,source} Generate only some debugging info -nowarn Generate no warnings -verbose Output messages about what the compiler is doing -deprecation Output source locations where deprecated APIs are used -classpath <path> Specify where to find user class files and annotation processors -cp <path> Specify where to find user class files and annotation processors -sourcepath <path> Specify where to find input source files -bootclasspath <path> Override location of bootstrap class files -extdirs <dirs> Override location of installed extensions -endorseddirs <dirs> Override location of endorsed standards path -proc:{none,only} Control whether annotation processing and/or compilation is done. -processor <class1>[,<class2>,<class3>...] Names of the annotation processors to run; bypasses default discovery process -processorpath <path> Specify where to find annotation processors -parameters Generate metadata for reflection on method parameters -d <directory> Specify where to place generated class files -s <directory> Specify where to place generated source files -h <directory> Specify where to place generated native header files -implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files -encoding <encoding> Specify character encoding used by source files -source <release> Provide source compatibility with specified release -target <release> Generate class files for specific VM version -profile <profile> Check that API used is available in the specified profile -version Version information -help Print a synopsis of standard options -Akey[=value] Options to pass to annotation processors -X Print a synopsis of nonstandard options -J<flag> Pass <flag> directly to the runtime system -Werror Terminate compilation if warnings occur @<filename> Read options and filenames from file ``` ### 四、安装Tomcat8 按照本文提供的地址将`apache-tomcat-8.5.15.tar.gz`下载至本地宿主机(注:也可在能访问互联网的虚拟机上直接用wget下载到虚拟机上),然后借助SSH客户端软件SSHSecureShellClient上传到/root目录。 解压文件: ```shell tar –zxf /root/ jdk-8u131-linux-x64.tar.gz ``` 解压后会在/root目录下多出一个`apache-tomcat-8.5.15`文件夹来,然后通过下面的命令将其移动到`/usr/local`目录下: ```shell mv ./apache-tomcat-8.5.15/ /usr/local ``` 注意:`/usr/local/ apache-tomcat-8.5.15`这个目录在设置Tomcat环境变量时会用到。当然,也可以不设置Tomcat相关的环境变量,使用相关脚本对Tomcat进行操作时会自动检测这些环境变量。 至此,Tomcat已经安装完成。 通过如下命令启动Tomcat: ```shell /usr/local/ apache-tomcat-8.5.15/bin/startup.sh ``` 如果要关闭Tomcat也很简单,执行如下命令: ```shell /usr/local/ apache-tomcat-8.5.15/bin/stop.sh ``` 注意:使用`/usr/local/ apache-tomcat-8.5.15/bin/catalina.sh`加上参数也可以启动或者停止Tomcat。 如 `/usr/local/ apache-tomcat-8.5.15/bin/catalina.sh start`来启动Tomcat,`/usr/local/ apache-tomcat-8.5.15/bin/catalina.sh stop`来停止Tomcat,除此之外还可以使用`debug/run/configtest/version`参数,不过没有restart参数来重启Tomcat。 可以在虚拟机打开`http://localhost:8080` 观看那个属性的Tomcat起始页了。 在宿主机中则要以虚拟机的IP地址来访问了(前提是虚拟机至少有一个网卡是桥接模式),在本人机器上虚拟机有个网卡是桥接的,IP地址为:`192.168.60.198`,与宿主机在一个网段,因此可以以`http://192.168.60.198:8080`来查看了,如下图所示:  注意:如果可以通过ssh连接虚拟机但不能打开`http://192.168.60.198:8080`,那么很有可能是防火墙问题,执行如下命令关闭防火墙: ```shell systemctl disable firewalld ``` 重启后即可访问。 ### 五、在CentOS7为Tomcat添加服务(使用systemctl来操作Tomcat) 为了能让Tomcat能够随CentOS7启动,其实就是能够通过`systemctl start/stop/restart`来控制Tomcat。 在网上有不少在CentOS7以前版本中的例子,都是通过在`/etc/rc.d/init.d`或`/etc/init.d`目录中添加脚本来实现的,在CentOS7中提供了另外一种方式,使用systemctl来替代了service,具体就是在`/usr/lib/systemd/system`或`/etc/systemd/system/`目录添加控制命令。 ##### 1、Systemctl用法 ```shell 在CentOS7中有关系统服务的命令有: systemctl list-units(等同于systemctl):列出激活的服务 systemctl --failed:列出运行失败的服务 systemctl daemon-reload:扫描新的或有变动的服务 systemctl status A.serivice:查看A这个服务的启动信息(注意在运行时需要将A这个名称改为具体的service的名字,下同,如SSH服务对应的sshd.service),这个命令在启动服务失败时查看原因非常有用 systemctl enable A.service:设置A这个service随系统一起启动 systemctl disable A.service:禁止A这个service随系统一起启动 systemctl start A.service:启动A这个 servicesystemctl stop A.service:停止A这个 servicesystemctl restart A.service:重新启动A这个service ``` 注意:在实际操作中,上述命令中A.service可简化为A,如`systemctl enable A.service`可简化为`systemctl enable A`。 ##### 2、给Tomcat增加启动参数 前面的讲述了如何安装Oracle JDK8和Tomcat8.5,并展示了使用Tomcat安装位置下的bin目录中的`startup.sh/stop.sh`或`catalina.sh start/catalina.sh stop`来启动或者关闭Tomcat。 但在实际开发中我们还是希望Tomcat能够在系统启动的时候自动启动,而不是手工通过命令行来启动,下面就讲述如何将Tomcat添加到CentOS7的启动服务中。 首先我们记住以下三点: 1.Oracle JDK8安装目录:`/usr/local/jdk1.8.0_131` 2.Tomcat8安装目录:`/usr/local/apache-tomcat-8.5.15` 3.并且在/etc/profile中配置了Java相关的环境变量: ```shell export JAVA_HOME=/usr/local/jdk1.8.0_131 export JRE_HOME=/usr/local/jdk1.8.0_131 export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$PATH:$JAVA_HOME/bin ``` 首先需要在Tomcat的安装目录下的bin目录下增加setenv.sh文件,可以通过`vim /usr/local/ apache-tomcat-8.5.15/bin/setenv.sh`来创建文件,并将如下内容粘贴进去后保存: ```shell #set JAVA_HOME and JRE_HOME and CATALINA_HOME and CATALINA_BASE export JAVA_HOME=/usr/local/jdk1.8.0_131 export JRE_HOME=/usr/local/jdk1.8.0_131/jre export CATALINA_HOME=/usr/local/apache-tomcat-8.5.15 export CATALINA_BASE=/usr/local/apache-tomcat-8.5.15 #set JAVA_HOME and JRE_HOME and CATALINA_HOME and CATALINA_BASE #add tomcat pid CATALINA_PID="$CATALINA_BASE/tomcat.pid" #add Java opts JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m" ``` 也就是设置`JAVA_HOME /JRE_HOME/CATALINA_HOME/CATALINA_BASE/CATALINA_PID`五个环境变量。 注意: 1.刚刚建立的setenv.sh文件会在通过执行catalina.sh来启动Tomcat时被自动调用。 2.之前学习时在网上看到有不少文章提到仅设置`CATALINA_HOME/CATALINA_BASE/CATALINA_PID`三个环境变量的,本人一直没成功,通过`systemctl status`命令查看说是缺少`JAVA_HOME /JRE_HOME`配置信息,配置`JAVA_HOME /JRE_HOME`之后就可以成功启动。前面也说了,本人在`/etc/profile`中也配置了`JAVA_HOME /JRE_HOME`的。 3.另外因为配置了tomcat.pid文件,所以在启动Tomcat时会在Tomcat根目录自动生成tomcat.pid文件,停止Tomcat之后tomcat.pid文件会自动被删除。使用tomcat.pid可以做到防止多次执行catalina.sh或startup.sh启动Tomcat时会启动多个实例的情形。 ##### 3、创建tomcat.service文件 接着增加`tomcat.service`文件,可以通过`/usr/lib/systemd/system/tomcat.service`来创建文件并下以下内容粘贴进去,然后保存: ```shell [Unit] Description=Apache Tomcat 8.5.15 After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/apache-tomcat-8.5.15/tomcat.pid ExecStart=/usr/local/apache-tomcat-8.5.15/bin/startup.sh ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target ``` 然后可以执行如下命令来检验是否配置正确: ```shell systemctl enable tomcat.service #设置tomcat这个service随系统一起启动 systemctl disable tomcat.service #禁止tomcat这个service随系统一起启动 systemctl start tomcat.service #启动tomcat这个service systemctl stop tomcat.service #停止tomcat这个service systemctl restart tomcat.service #重新启动tomcat这个service ``` ##### 4、tomcat.service文件结构说明 上面讲述的tomcat.service文件包含[unit]、[service]、[install]三部分,其中: [unit]配置了tomcat服务的描述和启动顺序,在本实例中规定了在network启动之后执行 [service]配置tomcat服务的pid,服务的启动,停止,重启命令 [install]配置了tomcat运行时的使用用
Java,JDK,Tomcat,Centos7
上一篇:
Centos7搭建Git及安装使用
下一篇:
Centos7安装Redis
栏目导航
【学】PHP
【学】前端
【学】Linux
【学】杂学
【学】支付
【学】Docker
相关文章
Centos7中 ntp 定时时间同步
3608 ℃
Centos7搭建LAMP
1169 ℃
Centos7搭建Git及安装使用
1514 ℃
Centos7搭建JDK+Tomcat
1147 ℃
Centos7安装Redis
1344 ℃
Centos7.3搭建LNMP
1843 ℃
Centos7.3搭建LNMP(文章二)
1621 ℃
Centos7搭建SVN服务器
2006 ℃
Centos 6.x 或 7.x yum安装php5.6.X(最新版)
1461 ℃
Nginx-一个IP配置多个站点(无域名)
3506 ℃
Centos7使用FireWall开放8888端口
2102 ℃
Centos7.4编译安装Nginx_1.13.8、php_7.2.1、mysql_5....
1237 ℃
Cenots7下添加Php的Redis扩展
1259 ℃
Redis引起的删库跑路加勒索事件
3301 ℃
创业耗费百万,为何DDoS如此要命Part 1
2051 ℃
记一次被黑后的经历
3274 ℃
(最新)CentOS7.4搭建LN(1.14.0)M(MariaDB)P(7....
2893 ℃
微信小程序
手机扫码访问