部分内容可以参考我这篇文章
:Windows11与CentOS7下配置与检测JDK与Maven环境变量 中的 2.2 安装jdk-8u371-linux-x64.tar.gz和配置环境变量/etc/profile
//1、安装redhat-lsb
yum install -y redhat-lsb
//2、查看系统版本信息
lsb_release -a
//3、查看系统位数
getconf LONG_BIT
cat /etc/redhat-release
参考我这篇文章
:查看CentOS版本及系统位数与设置CentOS 7.9 2009 防火墙配置放开端口的命令与过程
//查看yum源中JDK版本
yum list java*
//使用yum安装JDK1.8.0
yum -y install java-1.8.0-openjdk*
//检查Java版本,查看是否安装成功
java -version
//配置环境变量
//查看JDK安装的路径
find /usr/lib/jvm -name 'java-1.8.0-openjdk-1.8.0*'
//编辑配置文件/etc/profile
//按i进入编辑模式
vim /etc/profile
//添加以下信息
//按下Esc键,输入:wq并回车以保存并关闭文件。
#2023-9-2 23:28:12 djc配置JDK环境变量
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.382.b05-1.el7_9.x86_64
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME CLASSPATH PATH
--------------------注意-----------------------
//在本文目录5.1 NO such file or directory 中需要在tomcat脚本文件(即Tomcat-init脚本文件)中,
//需要配置使用JAVA_HOME
//之前执行了移动并重命名Tomcat-init的命令:mv Tomcat-init /etc/init.d/tomcat
//详情见 本文目录5.1 NO such file or directory 中
#Location of JAVA_HOME (bin files)
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.382.b05-1.el7_9.x86_64
//tomcat脚本文件(即Tomcat-init脚本文件)中的PATH的值,
//是该项目作者默认配置成这样了export PATH=$JAVA_HOME/bin:$PATH
//,与JDK环境变量中的PATH=$PATH:$JAVA_HOME/bin 就是顺序颠倒了,作用应该是一样的
#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH
-------------------------------------------
//最后刷新环境变量
source /etc/profile
修改环境变量后,可能会导致执行其他命令时,出现类似-bash: chmod: command not found这样的问题,
执行export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin即可。
查看yum源中JDK版本
:yum list java*
使用yum安装JDK1.8.0
:yum -y install java-1.8.0-openjdk*
检查Java版本,查看是否安装成功
:java -version
[root@freedom ~]# java -version
openjdk version "1.8.0_382"
OpenJDK Runtime Environment (build 1.8.0_382-b05)
OpenJDK 64-Bit Server VM (build 25.382-b05, mixed mode)
[root@freedom ~]#
查看JDK安装的路径
:find /usr/lib/jvm -name 'java-1.8.0-openjdk-1.8.0*'
[root@freedom ~]# find /usr/lib/jvm -name 'java-1.8.0-openjdk-1.8.0*'
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.382.b05-1.el7_9.x86_64
[root@freedom ~]#
编辑配置文件/etc/profile
:
添加以下JDK环境变量信息
:
#2023-9-2 23:28:12 djc配置JDK环境变量
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.382.b05-1.el7_9.x86_64
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME CLASSPATH PATH
添加完JDK环境变量信息的/etc/profile
:
[root@freedom ~]# cat /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
# ksh workaround
EUID=`/usr/bin/id -u`
UID=`/usr/bin/id -ru`
fi
USER="`/usr/bin/id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022
fi
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
unset i
unset -f pathmunge
#2023-9-2 23:28:12 djc配置JDK环境变量
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.382.b05-1.el7_9.x86_64
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME CLASSPATH PATH
刷新环境变量
:source /etc/profile
修改环境变量后
,可能会导致执行其他命令时
,出现类似-bash: chmod: command not found
这样的问题,执行export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
即可。
Tomcat官网
点击左侧的Download
中的Tomcat 8
:
选中右击后点击-复制链接地址
然后再服务器中使用wget命令,下载Tomcat包:
//链接修改成需要的版本
wget --no-check-certificate https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.96/bin/apache-tomcat-8.5.93.tar.gz
或者直接在浏览器中点击下载链接
,下载完成后再将apache-tomcat-8.5.93.tar.gz
,利用xftp
等工具上传到服务器中
。
//解压 apache-tomcat-8.5.93.tar.gz
tar -zxvf apache-tomcat-8.5.93.tar.gz
//移动并改名到Tomcat目录
mv apache-tomcat-8.5.93 /usr/local/tomcat/
移动并改名到Tomcat目录
:
在/usr/local/tomcat/
目录下:
bin
:存放Tomcat的一些脚本文件,包含启动和关闭Tomcat服务脚本。
conf
:存放Tomcat服务器的各种全局配置文件,其中最重要的是server.xml和web.xml。
webapps
:Tomcat的主要Web发布目录,默认情况下把Web应用文件放于此目录。
logs
:存放Tomcat执行时的日志文件。
为保证系统安全性
,建议创建一般用户
来运行Tomcat
。
//创建一般用户djc
useradd djc
//运行命令,将文件的所属用户设置为djc
chown -R djc.djc /usr/local/tomcat/
//运行命令,切换到/usr/local/tomcat/conf/目录。
cd /usr/local/tomcat/conf/
//运行以下命令,重命名server.xml文件
//server.xml_bk相当于把server.xml初始文件的备份
mv server.xml server.xml_bk
//新建一个server.xml文件
//会创建并打开一个server.xml文件
//i键编辑,ESC键退出,:wq保存并退出
vi server.xml
server.xml的内容
:
"1.0" encoding="UTF-8"?>
"8006" shutdown="SHUTDOWN">
"org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
"org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
"org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
"org.apache.catalina.core.AprLifecycleListener"/>
"UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml"/>
"Catalina">
"8080"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxThreads="1000"
minSpareThreads="20"
acceptCount="1000"
maxHttpHeaderSize="65536"
debug="0"
disableUploadTimeout="true"
useBodyEncodingForURI="true"
enableLookups="false"
URIEncoding="UTF-8"/>
"Catalina" defaultHost="localhost">
"org.apache.catalina.realm.LockOutRealm">
"org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
"localhost" appBase="/data/wwwroot/default" unpackWARs="true" autoDeploy="true">
"" docBase="/data/wwwroot/default" debug="0" reloadable="false" crossContext="true"/>
"org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
i键
编辑,ESC键
退出,:wq
保存并退出
cat server.xml
:
/usr/local/tomcat/conf/
server.xml
#四、设置JVM内存参数
//运行命令,创建并打开/usr/local/tomcat/bin/setenv.sh文件。
//i键编辑,ESC键退出,:wq保存并退出
vi /usr/local/tomcat/bin/setenv.sh
//setenv.sh的内容
//指定JAVA_OPTS参数,用于设置JVM的内存信息以及编码格式。
JAVA_OPTS='-Djava.security.egd=file:/dev/./urandom -server -Xms256m -Xmx496m -Dfile.encoding=UTF-8'
Tomcat-init
//获取Tomcat自启动脚本 Tomcat-init
//或者使用git clone(从Windows里clone完成后再上传到linux服务器)
//也可以在服务器中直接wget
wet https://github.com/oneinstack/oneinstack/blob/main/init.d/Tomcat-init
//移动并重命名Tomcat-init
mv Tomcat-init /etc/init.d/tomcat
//为/etc/init.d/tomcat添加可执行权限
chmod +x /etc/init.d/tomcat
//设置启动脚本JAVA_HOME
//重要:脚本中JDK的版本信息必须与您安装的JDK版本信息一致,否则Tomcat会启动失败。
sed -i 's@^export JAVA_HOME=.*@export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.342.b07-1.el7_9.x86_64@' /etc/init.d/tomcat
Tomcat-init脚本
的内容:
#!/bin/bash
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: tomcat-server daemon
# Description: tomcat-server daemon
### END INIT INFO
#
# chkconfig: - 95 15
# description: Tomcat start/stop/status script
#Location of JAVA_HOME (bin files)
export JAVA_HOME=
#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH
#CATALINA_HOME is the location of the configuration files of this instance of Tomcat
CATALINA_HOME=/usr/local/tomcat
#TOMCAT_USER is the default user of tomcat
TOMCAT_USER=www
#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"
#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20
tomcat_pid() {
echo `ps -ef | grep java | grep $CATALINA_HOME/ | grep -v grep | tr -s " "|cut -d" " -f2`
}
start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]; then
echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
else
echo -e "\e[00;32mStarting tomcat\e[00m"
if [ `user_exists $TOMCAT_USER` = "1" ]; then
su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
else
$CATALINA_HOME/bin/startup.sh
fi
status
fi
return 0
}
status() {
pid=$(tomcat_pid)
if [ -n "$pid" ]; then
echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
else
echo -e "\e[00;31mTomcat is not running\e[00m"
fi
}
stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ]; then
echo -e "\e[00;31mStoping Tomcat\e[00m"
$CATALINA_HOME/bin/shutdown.sh
let kwait=$SHUTDOWN_WAIT
count=0;
until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
do
echo -n -e "\e[00;31mwaiting for processes to exit\e[00m\n";
sleep 1
let count=$count+1;
done
if [ $count -gt $kwait ]; then
echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
kill -9 $pid
fi
else
echo -e "\e[00;31mTomcat is not running\e[00m"
fi
return 0
}
user_exists() {
if id -u $1 >/dev/null 2>&1; then
islogin=`cat /etc/passwd | grep ^${TOMCAT_USER}: | grep nologin$`
if [ "${islogin: -7}" = "nologin" ]; then
echo "0"
else
echo "1"
fi
else
echo "0"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo -e $TOMCAT_USAGE
;;
esac
exit 0
上传自启动脚本Tomcat-init
:
注意这里的etc的init.d文件夹是个快捷方式链接:
也就是说init.d
实际指向rc.d下的init.d文件夹
里面有刚刚使用mv命令移动并重命名的
Tomcat-init脚本文件,即tomcat脚本文件
:
设置启动脚本JAVA_HOME:
为/etc/init.d/tomcat添加可执行权限
(chmod +x /etc/init.d/tomcat
),字体变色变粗(可以用来识别命令是否生效
)
//设置Tomcat开机自启动
chkconfig --add tomcat
chkconfig tomcat on
//启动Tomcat
service tomcat start
启动Tomcat
:
报错
:tomcat-NO such file or directory:
这是因为Tomcat-init
脚本文件,也就是/etc/init.d/tomcat
脚本文件 (之前执行了移动并重命名Tomcat-init脚本文件的命令
:mv Tomcat-init /etc/init.d/tomcat
)这个文件中需要配置参数JAVA_HOME
与TOMCAT_USER=www
修改为TOMCAT_USER=djc
(TOMCAT_USER is the default user of tomcat),
所以此处的tomcat
实际上是一个脚本文件的名称
,即Tomcat-init
脚本文件。
cat tomcat脚本文件
(即Tomcat-init
脚本文件)
#Location of JAVA_HOME (bin files)
export JAVA_HOME
=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.382.b05-1.el7_9.x86_64
的值:
#CATALINA_HOME is the location of the configuration files of this instance of Tomcat
CATALINA_HOME=/usr/local/tomcat
的值:
修改后的tomcat脚本
(即原来的Tomcat-init脚本
,mv Tomcat-init /etc/init.d/tomcat):
#H {
echo `ps -ef | grep java | grep $CATALINA_HOME/ | grep -v grep | tr -s " "|cut -d" " -f2`
}
start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]; then
echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
else
echo -e "\e[00;32mStarting tomcat\e[00m"
if [ `user_exists $TOMCAT_USER` = "1" ]; then
su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
else
$CATALINA_HOME/bin/startup.sh
fi
status
fi
return 0
}
status() {
pid=$(tomcat_pid)
if [ -n "$pid" ]; then
echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
else
echo -e "\e[00;31mTomcat is not running\e[00m"
fi
}
stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ]; then
echo -e "\e[00;31mStoping Tomcat\e[00m"
$CATALINA_HOME/bin/shutdown.sh
let kwait=$SHUTDOWN_WAIT
count=0;
until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
do
echo -n -e "\e[00;31mwaiting for processes to exit\e[00m\n";
sleep 1
let count=$count+1;
done
if [ $count -gt $kwait ]; then
echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
kill -9 $pid
fi
else
echo -e "\e[00;31mTomcat is not running\e[00m"
fi
return 0
}
user_exists() {
if id -u $1 >/dev/null 2>&1; then
islogin=`cat /etc/passwd | grep ^${TOMCAT_USER}: | grep nologin$`
if [ "${islogin: -7}" = "nologin" ]; then
echo "0"
else
echo "1"
fi
else
echo "0"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo -e $TOMCAT_USAGE
;;
esac
exit 0
原因
:shell脚本是在Windows
下编写,通过工具软件传输到CentOS
中。而Windows下
行结尾符号为\r\n
,linux下
行结尾时\n,两者不同,所以无法识别,需要转换一下
。
参考我这篇文章
:CentOS: $‘\r‘: command not found
Tomcat-init
脚本文件,也就是/etc/init.d/tomcat
脚本文件 (之前执行了移动并重命名Tomcat-init脚本文件的命令
:mv Tomcat-init /etc/init.d/tomcat
)
所以此处的tomcat
实际上是一个脚本文件的名称
,即Tomcat-init
脚本文件,内容见5.1 NO such file or directory
中的【修改后的tomcat脚本(即原来的Tomcat-init脚本,mv Tomcat-init /etc/init.d/tomcat):
】
//执行sh deploy.sh base,报错$'\r': command not found
//sh deploy.sh base
//这里修改为service tomcat start
service tomcat start
//1、dos2unix 脚本名,此处是dos2unix deploy.sh
//如果执行dos2unix deploy.sh报-bash:dos2unix:command not found,就使用yum install -y dos2unix 安装dos2unix
//如果执行成功,会报一个dos2unix: converting file deploy.sh to Unix format ...
//dos2unix deploy.sh
//这里改为dos2unix tomcat
dos2unix tomcat
//2、安装dos2unix
yum install -y dos2unix
//3、再次执行dos2unix deploy.sh命令
//如果dos2unix deploy.sh执行成功,会报一个dos2unix: converting file deploy.sh to Unix format ...
//如果dos2unix deploy.sh执行成功后,再次执行sh deploy.sh base还不行,依旧报 $'\r': command not found,就执行下面的sed命令
//这里改为再次执行dos2unix tomcat
dos2unix tomcat
//4、sed -i 's/\r//' 脚本名
//sed -i 's/\r//' deploy.sh
//这里改为sed -i 's/\r//' tomcat
sed -i 's/\r//' tomcat
//5、再次执行sh deploy.sh base
//sh deploy.sh base
//这里改为 service tomcat start
service tomcat start
[root@freedom init.d]# service tomcat start
Starting tomcat
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.382.b05-1.el7_9.x86_64
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
Tomcat is running with pid: 852
【ECS生长万物之开源】手动部署Java Web环境(CentOS 7)
Tomcat官网
Windows11与CentOS7下配置与检测JDK与Maven环境变量
CentOS: $‘\r‘: command not found