This topic describes how to manually deploy a Java web environment on an Elastic Compute Service (ECS) instance that runs Alibaba Cloud Linux 2. This topic is applicable to individual users who are new to website construction on ECS instances.
Background information
In this topic, the following instance type and software versions are used. Operation may vary based on your instance type and software versions.
- Instance type: ecs.c6.large
- Operating system: Alibaba Cloud Linux 2.1903 LTS 64-bit
- Java Development Kit (JDK): JDK 19
- Apache Tomcat: Apache Tomcat 8.5.85Note In this example, Apache Tomcat 8.5.85 is used. The source code is upgraded on a regular basis. You can manually obtain a version appropriate to your requirements.
Step 1: Make preparations
- Add inbound rules to the security group of the instance to allow traffic on the required ports. For more information, see Add a security group rule. In this example, inbound rules are added to allow traffic on SSH port 22, HTTP port 80, HTTPS port 443, and Apache Tomcat port 8080.
- Connect to the instance. For more information, see Connect to a Linux instance by using a password.
- Disable the firewall.
- Disable Security-Enhanced Linux (SELinux).
Step 2: Install JDK
- Create a directory in which to install JDK.
mkdir /usr/jdk
- Download the JDK installation package. In this example, the JDK 19 installation package is downloaded. You can visit the official Oracle website to view information about more JDK versions.
cd /usr/jdk wget https://download.oracle.com/java/19/latest/jdk-19_linux-x64_bin.tar.gz
- Decompress the JDK installation package to the specified folder.
tar xzf jdk-19_linux-x64_bin.tar.gz -C /usr/jdk/
- Configure environment variables.
- Check the JDK version.
java -version
JDK version information in the sample command output:[root@iZuspq7vnxm**** ~]# java -version java version "19.0.2" 2023-01-17 Java(TM) SE Runtime Environment (build 19.0.2+7-44) Java HotSpot(TM) 64-Bit Server VM (build 19.0.2+7-44, mixed mode, sharing)
Step 3: Install Apache Tomcat
- Download the Apache Tomcat 8 installation package.
wget --no-check-certificate https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.85/bin/apache-tomcat-8.5.85.tar.gz
Note The download address of Apache Tomcat may change. If the preceding download address is invalid, visit the official Apache Tomcat website to obtain the latest download address. - Decompress the Apache Tomcat 8 installation package.
tar -zxvf apache-tomcat-8.5.85.tar.gz
- Move the Apache Tomcat installation files to the /usr/local/tomcat/ directory.
mv apache-tomcat-8.5.85 /usr/local/tomcat/
- Access the /usr/local/tomcat/bin directory.
cd /usr/local/tomcat/bin
- Edit the setclasspath.sh script.
vi setclasspath.sh
- Press the I key to enter the edit mode, and add the following content to the end of the setclasspath.sh script.
export JAVA_HOME=/usr/jdk/jdk-19.0.2 export JRE_HOME=$JAVA_HOME
Note Set jdk-<Version number> based on the actual JDK version number. - Press the Esc key to exit the edit mode. Enter
:wq
and press the Enter key to save and close the script. - Start Apache Tomcat.
./startup.sh
- Check the Tomcat process.
If a command output similar to the following one is returned, Apache Tomcat is started:ps -ef | grep tomcat
root 2225 1 40 14:46 pts/0 00:00:04 /usr/jdk/jdk-19.0.2/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start root 2285 6446 0 14:46 pts/0 00:00:00 grep --color=auto tomcat
Step 4: Deploy and verify the test project
- If the deployed Java web project files are a WAR package, you must upload the WAR package to the /usr/local/tomcat/webapps/ directory. You can use a connection tool that has a file transfer feature or build an FTP site to upload the project files. After the project files are uploaded, you can enter
http://<Public IP address of the instance>:8080/<Project name>
in the address bar of a browser to access the files. - If only web pages need to be uploaded, you can upload the web pages to the /usr/local/tomcat/webapps/ROOT/ directory. After the web pages are uploaded, you can enter
http://<Public IP address of the instance>:8080
in the address bar of a browser to access the pages.
In example, a Tomcat test page is created in the /usr/local/tomcat/webapps/ROOT/ directory. You can enter http://<Public IP address of the instance>:8080
in the address bar of a browser on your computer to access the page.
- Create a test file.
echo Tomcat test > /usr/local/tomcat/webapps/ROOT/index.jsp
- Open your browser and enter
http://<Public IP address of the ECS instance>:8080
in the address bar to connect to the ECS instance.The page shown in the following figure indicates that Apache Tomcat is installed. If http://<Public IP address of the ECS instance>:8080 is inaccessible, check whether rules are added to the security group of the instance to allow access on Apache Tomcat port 8080. For information about how to add security group rules, see Add a security group rule.