This document describes how to deploy Java Web projects on CentOS and is suitable for new individual users of Tencent Cloud.
The versions of software tools used in this document are as follows, which may be different from your software versions during actual operations.
After purchasing the CVM, you can click Login on the CVM details page to log in to your CVM instance where you can enter your username and password to set up the Java web environment. For more information on how to create a CVM instance, please see CVM - Creating Instance.
Enter the following command:
mkdir /usr/java # Create a `java` folder
cd /usr/java # Enter the `java` folder
# Upload JDK installation package (recommended)
You are recommended to use tools such as WinSCP to upload the JDK installation package to the above `java` folder and then decompress it.
Or
# Use a command (you are recommended to upload the installation package): run `wget` to download the package, which cannot be decompressed because a downloaded package declines the Oracle BSD License by default. Please go to https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html to accept the license agreement and obtain the download link with your cookies.
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.tar.gz
# Decompress
chmod +x jdk-8u201-linux-x64.tar.gz
tar -xzvf jdk-8u201-linux-x64.tar.gz
/etc/profile
file.vi /etc/profile
# set java environment
export JAVA_HOME=/usr/java/jdk1.8.0_201
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH
:wq
to save and close the file.source /etc/profile
Run the java -version
command. If the JDK version information is displayed, JDK has been successfully installed.
Enter the following commands:
# The mirror address may change and the Tomcat version may be continuously upgraded. If the download link expired, please go to [Tomcat official website](https://tomcat.apache.org/download-80.cgi) and select an appropriate installation package address.
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.39/bin/apache-tomcat-8.5.39.tar.gz
tar -xzvf apache-tomcat-8.5.39.tar.gz
mv apache-tomcat-8.5.39 /usr/local/tomcat/
The following files are in the /usr/local/tomcat/
directory:
server.xml
and web.xml
.Note:If the download link expired, please replace it with the latest link at Tomcat's official website.
# Add a general user `www` to run Tomcat
useradd www
# Create a website root directory
mkdir -p /data/wwwroot/default
# Upload the Java web project file (WAR package) to the website root directory and modify the file permission under the directory to `www`. This example shows how to create a Tomcat test page in the website root directory:
echo Hello Tomcat! > /data/wwwroot/default/index.jsp
chown -R www.www /data/wwwroot
/usr/local/tomcat/bin/setenv.sh
script file.vi /usr/local/tomcat/bin/setenv.sh
JAVA_OPTS='-Djava.security.egd=file:/dev/./urandom -server -Xms256m -Xmx496m -Dfile.encoding=UTF-8'
:wq
to save and exit./usr/local/tomcat/conf/
directory.cd /usr/local/tomcat/conf/
server.xml
file.mv server.xml server_default.xml
server.xml
file.vi server.xml
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8006" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<Listener className="org.apache.catalina.core.AprLifecycleListener"/>
<GlobalNamingResources>
<Resource name="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"/>
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="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"/>
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="/data/wwwroot/default" unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/data/wwwroot/default" debug="0" reloadable="false" crossContext="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
:wq
to save and exit.Enter the bin
directory of the Tomcat server and run the ./startup.sh
command to start the Tomcat server.
cd /usr/local/tomcat/bin
./startup.sh
The execution result is as follows:
service tomcat start
.wget https://github.com/lj2007331/oneinstack/raw/master/init.d/Tomcat-init
mv Tomcat-init /etc/init.d/tomcat
chmod +x /etc/init.d/tomcat
JAVA_HOME
startup script.sed -i 's@^export JAVA_HOME=.*@export JAVA_HOME=/usr/java/jdk1.8.0_201@' /etc/init.d/tomcat
chkconfig --add tomcat
chkconfig tomcat on
# Start Tomcat
service tomcat start
# View Tomcat server status
service tomcat status
# Stop Tomcat
service tomcat stop
The execution result is as follows:cd /usr/local
chmod -R 777 tomcat
http://public IP:port
(where the port is the connector port set in server.xml
) in the address bar of the browser. If the following page appears, the installation is successful.In case of access failure, check the security group. As shown in the above example, the connector port is 8080 in server.xml
, so you need to open TCP:8080 to the internet in the security group bound to the corresponding CVM instance.
Was this page helpful?