标签归档:JDK环境

Linux Apache Tomcat 8.5 安装与配置

生产环境:

Server version: Apache Tomcat/8.5.16
CentOS Linux release 7.3.1611 (Core) x86-64

1.Apache Tomcat/8.5.16依赖jre7及以上版本,不过jdk高版本已经包含jre,关于jdk&jre的安装请查看《Linux JAVA JDK JRE 环境变量安装与配置

2.创建tomcat专有用户

# groupadd tomcat
# useradd -g tomcat -s /bin/false tomcat
或
# useradd -g tomcat -s /sbin/nologin tomcat

注意

-g tomcat用户隶属于tomcat组
-s /bin/false 禁用shell访问

3.Apache Tomcat 8.5下载安装与配置&设置用户组权限并创建软连接

# cd /tmp
# wget //apache.fayea.com/tomcat/tomcat-8/v8.5.16/bin/apache-tomcat-8.5.16.tar.gz
# tar zxvf apache-tomcat-8.5.16.tar.gz
# mv apache-tomcat-8.5.16 /usr/local/
# cd /usr/local/
# chown -hR tomcat:tomcat apache-tomcat-8.5.16
# ln -s apache-tomcat-8.5.16 tomcat

4.添加tomcat自启动systemd服务单元文件

# vim /lib/systemd/system/tomcat.service

[Unit]
Description=Apache Tomcat 8
After=syslog.target network.target

[Service]
Type=forking
User=tomcat
Group=tomcat

Environment=JAVA_HOME=/usr/local/jdk/jre
Environment=CATALINA_PID=/usr/local/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat
Environment=CATALINA_BASE=/usr/local/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx4096M -server -XX:+UseParallelGC'
Environment='CATALINA_OPTS=-Dfile.encoding=UTF-8 -server -Xms2048m -Xmx2048m -Xmn1024m -XX:SurvivorRatio=10 -XX:MaxTenuringThreshold=15 -XX:NewRatio=2 -XX:+DisableExplicitGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target

5.重载systemd服务单元,给予软连接目录权限,启动Apache Tomcat服务并设置Tomcat为开机自启动

# systemctl daemon-reload
# cd /usr/local/
# chown -hR tomcat:tomcat tomcat
# systemctl start tomcat
# systemctl enable tomcat

6.配置Apache Tomcat用户实现远程登录

在tomcat-users.xml文件<tomcat-users></tomcat-users>中间添加;

# vim /usr/local/tomcat/conf/tomcat-users.xml
<role rolename="manager-gui"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>

tomcat8.5之后的版本,已经增强远程登录安全过滤规则,默认不支持远程登录,需要修改配置文件。

修改文件:

/host-manager/META-INF/context.xml
/manager/META-INF/context.xml

默认值:

<Valve className="org.apache.catalina.valves.RemoteAddrValve"
 allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

修改为:

<Valve className="org.apache.catalina.valves.RemoteAddrValve"
 addConnectorPort="true"
 allow="127\.\d+\.\d+\.\d+;\d*|::1;\d*|0:0:0:0:0:0:0:1;\d*|.*;8080"/>

详情可查看 Valve 文档

7.配置Firewalld防火墙

如果不放行8080端口,就无法在外部使用8080进行访问,现在将端口放行并重载firewall服务

# firewall-cmd --zone=public --add-port=8080/tcp --permanent
# firewall-cmd --reload
# firewall-cmd --list-ports
# firewall-cmd --list-services

目前为止Apache Tomcat 8.5.16已经成功安装。现在您可以使用IP:port进行测试访问。

Linux JAVA JDK JRE 环境变量安装与配置

一般安装JDK JAVA环境有2种方式,下面我逐个说明。

生产环境:

$ hostnamectl
 Static hostname: localhost.localdomain
 Icon name: computer-vm
 Chassis: vm
 Machine ID: 769ef9902d8743958a40cb81db696433
 Boot ID: ce70591a91b64e73926682db72441f7b
 Virtualization: microsoft
 Operating System: CentOS Linux 7 (Core)
 CPE OS Name: cpe:/o:centos:centos:7
 Kernel: Linux 3.10.0-514.2.2.el7.x86_64
 Architecture: x86-64

一、rpm Tool安装方式(相对简单)

1.下载对应的JAVA JDK RPM包 64位:

//www.oracle.com/technetwork/java/javase/downloads/index.html

2.开始安装:

# cd /tmp
# rpm -ivh jdk-8u144-linux-x64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:jdk1.8.0_144-2000:1.8.0_144-fcs  ################################# [100%]
Unpacking JAR files...
        tools.jar...
        plugin.jar...
        javaws.jar...
        deploy.jar...
        rt.jar...
        jsse.jar...
        charsets.jar...
        localedata.jar...

3.RPM包安装完成,验证是否安装成功,如果有输出JDK相关版本说明已经安装成功。

# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

4.添加JAVA JDK环境变量:

# vim /etc/profile.d/java.sh

JAVA_HOME=/usr/java/jdk1.8.0_144
JRE_HOME=/usr/java/jdk1.8.0_144/jre
CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME PATH CLASSPATH

5.永久使环境变量生效:

# source /etc/profile.d/java.sh

RPM安装方式已经完成。

二、JAVA JDK源码Tarball包安装方式(推荐)

1.先卸载之前通过rpm或yum安装的包,如果输出无,说明没安装,反之已安装。

# rpm -qa | grep -E '^open[jre|jdk]|j[re|dk]'
libbasicobjects-0.1.1-27.el7.x86_64
openjpeg-libs-1.5.1-16.el7_3.x86_64
gobject-introspection-1.42.0-1.el7.x86_64
openjpeg-1.5.1-16.el7_3.x86_64
jdk1.8.0_131-1.8.0_131-fcs.x86_64
pygobject3-base-3.14.0-3.el7.x86_64
openjpeg-devel-1.5.1-16.el7_3.x86_64

2.卸载已安装的JDK:

# yum -y remove jdk1.8.0_131-1.8.0_131-fcs.x86_64

3.下载对应的JDK版本Tarball包 64位并解压:

//www.oracle.com/technetwork/java/javase/downloads/index.html

# cd /mnt
# tar zxvf jdk-8u144-linux-x64.tar.gz
# mv jdk-8u144-linux-x64 /usr/local/
# cd /usr/local/
# ln -s jdk-8u144-linux-x64 jdk

4.添加JAVA JDK环境变量:

# vim /etc/profile.d/jdk.sh

JAVA_HOME=/usr/local/jdk
JRE_HOME=/usr/local/jdk/jre
CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME PATH CLASSPATH

5.永久使JAVA JDK环境变量生效:

# source /etc/profile.d/jdk.sh

6.查看JAVA JDK版本输出:

# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

7.至此java jdk环境安装配置已经完成。

JAVA JDK JRE环境安装有多种方式,每一种方式都可以使用,找到适合自己的,如果要我推荐,我建议选择Tarball构建。