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 suitable for users who are new to website building on ECS instances.
Background information
The following instance type and software versions are used in this topic. Operations may vary based on your software versions.
Instance type: ecs.c6.large
Operating system: Alibaba Cloud Linux 2.1903 LTS 64-bit
Java Development Kit (JDK): JDK 17
Apache Tomcat: 8.5.88
NoteIn the example, Apache Tomcat 8.5.88 is used. The source code is updated on a regular basis. You can manually obtain a version that suits your requirements.
Step 1: Make preparations
Add inbound rules to the security groups of the instance to allow traffic on 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 or key.
Disable the firewall.
Run the systemctl status firewalld command to check the status of the firewall.
If the firewall is in the inactive state, the firewall is disabled.
If the firewall is in the active state, the firewall is enabled. In this example, the firewall is in the active state and must be disabled.
Disable the firewall. Skip this step if the firewall is disabled.
To temporarily disable the firewall, run the following command:
systemctl stop firewalld
NoteAfter you run the preceding command, the firewall is disabled. When you restart the Linux operating system, the firewall is enabled and re-enters the active state.
To permanently disable the firewall, perform the following steps:
Run the following command to stop the firewall:
systemctl stop firewalld
Run the following command to disable the firewall and prevent the firewall from starting on instance startup:
systemctl disable firewalld
NoteAfter you run the preceding commands, the firewall is disabled. When you restart the instance, the firewall remains disabled. You can re-enable the firewall. For more information, see Firewalld documentation.
Disable Security-Enhanced Linux (SELinux).
Check the status of SELinux.
getenforce
The following figure shows an example command output:
If SELinux is in the Disabled state, SELinux is disabled.
If SELinux is in the Enforcing state, SELinux is enabled. In this example, SELinux is in the Enforcing state and must be disabled.
Disable SELinux. Skip this step if SELinux is disabled.
To temporarily disable SELinux, run the following command:
setenforce 0
NoteAfter you run the preceding command, SELinux is disabled. When you restart the instance, SELinux is enabled and re-enters the Enforcing state.
To permanently disable SELinux, run the following command to open the SELinux configuration file:
vi /etc/selinux/config
In the /etc/selinux/config file, move the pointer on the
SELINUX=enforcing
line and press the I key to enter the edit mode. SetSELINUX
to disabled and press the Esc key to exit the edit mode. Enter:wq
and press the Enter key to save and close the file.NoteYou can re-enable SELinux after it is disabled. For more information, see Enable or disable SELinux.
Restart the instance to apply the settings.
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 17 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/17/latest/jdk-17_linux-x64_bin.tar.gz
Decompress the JDK installation package to the specified folder.
tar xzf jdk-17_linux-x64_bin.tar.gz -C /usr/jdk/
Configure environment variables.
View the path where JDK is installed.
find /usr/jdk -name 'jdk-17.*'
A command output similar to the following one is returned:
/usr/jdk/jdk-17.0.8
Open the configuration file.
vim /etc/profile
At the end of the configuration file, press the I key to enter Insert mode.
Add the following information.
NoteThe
JAVA_HOME
value is the path where JDK is installed. In this example, JDK is installed in the /usr/jdk/jdk-17.0.8 path.#set java environment export JAVA_HOME=/usr/jdk/jdk-17.0.7 export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib export PATH=$JAVA_HOME/bin:$PATH
Press the Esc key to exit Insert mode. Enter
:wq
and press the Enter key to save and close the file.Apply the environment variables.
source /etc/profile
Check the JDK version.
java -version
JDK version information in the sample command output:
[root@iZuspq7vnxm**** ~]# java -version java version "17.0.8" 2023-04-18 LTS Java(TM) SE Runtime Environment (build 17.0.7+8-LTS-224) Java HotSpot(TM) 64-Bit Server VM (build 17.0.7+8-LTS-224, mixed mode, sharing)
Step 3: Install Apache Tomcat
Run the following commands to download and decompress the Apache Tomcat 8 installation package:
wget --no-check-certificate https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.88/bin/apache-tomcat-8.5.88.tar.gz tar -zxvf apache-tomcat-8.5.88.tar.gz
NoteThe download URLs of Apache Tomcat may change. If the preceding download URL is invalid, visit the official Apache Tomcat website to obtain the latest download URL.
Run the following command to move the Apache Tomcat installation files to the /usr/local/tomcat/ directory:
mv apache-tomcat-8.5.88 /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 Insert mode, and add the following content to the end of the setclasspath.sh script:
export JAVA_HOME=/usr/jdk/jdk-17.0.8 export JRE_HOME=$JAVA_HOME
NoteSet jdk-<Version number> based on the actual JDK version number.
Press the Esc key to exit Insert mode. Enter
:wq
and press the Enter key to save and close the file.Start Apache Tomcat.
./startup.sh
Check the Tomcat process.
ps -ef | grep tomcat
If Apache Tomcat is started, a command output similar to the following one is returned:
root 2225 1 40 14:46 pts/0 00:00:04 /usr/jdk/jdk-17.0.8/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 this 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 instance>:8080
in the address bar to connect to the instance.The page shown in the following figure indicates that Apache Tomcat is installed. If http://<Public IP address of the instance>:8080 is inaccessible, check whether rules are added to the security groups 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.