在Linux上安装JDK和Tomcat可以通过命令行进行。以下是基于CentOS的示例步骤:
- 安装Java JDK
首先,检查是否已经安装了Java:
java -version
如果没有安装,可以通过以下步骤安装OpenJDK:
sudo yum update
sudo yum install java-1.8.0-openjdk
这将安装Java 8。安装完成后,验证安装:
java -version
- 安装Tomcat
首先,添加Tomcat用户:
sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat
下载Tomcat(以Tomcat 9为例):
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.62/bin/apache-tomcat-9.0.62.tar.gz
解压缩Tomcat:
sudo tar xf apache-tomcat-*tar.gz -C /opt/tomcat --strip-components=1
更新权限:
cd /opt/tomcat
sudo chown -R tomcat: /opt/tomcat
创建setenvsh文件:
sudo nano /opt/tomcat/bin/setenv.sh
添加以下内容:
export CATALINA_PID="$CATALINA_BASE/tomcat.pid"
export CATALINA_HOME="$CATALINA_BASE"
export CATALINA_BASE="/opt/tomcat"
export CATALINA_OPTS="-Xms512M -Xmx1024M -XX:MaxPermSize=256m"
给setenv.sh文件添加执行权限:
sudo chmod +x /opt/tomcat/bin/setenv.sh
创建systemd服务文件:
sudo nano /etc/systemd/system/tomcat.service
添加以下内容:
[Unit]
Description=Tomcat 9 servlet container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/jre"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_BASE=/opt/tomcat"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
Environment="JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
启动Tomcat服务并设置开机自启:
sudo systemctl start tomcat
sudo systemctl enable tomcat
检查Tomcat是否运行:
sudo systemctl status tomcat
现在,Tomcat应该已经安装并运行在CentOS上了。可以通过浏览器访问 http://your_server_ip:8080
来验证。