Java Web is a technology stack that integrates the Java programming language with various technologies and frameworks to develop dynamic web applications. It utilizes the robust features of the Java platform, allowing developers to build complex, high-performance web applications for cross-platform deployment. Apache Tomcat is an open-source web server frequently used in Java Web development for deploying and running Java web applications. This topic describes the steps to deploy a Java web environment on an Elastic Compute Service (ECS) instance.
Quick deployment
You can click Run with one click to access Terraform Explorer, where you can view and execute Terraform code to automatically deploy a Java web environment (Tomcat) on an ECS instance.
Preparations
Before deploying a Java web environment, ensure the ECS instance satisfies the following criteria:
-
The instance has a Java environment deployed on it. For more information, see Deploying a Java Environment.
-
An ECS instance is either assigned a static public IP address or linked to an Elastic IP Address (EIP). For more information, see Elastic IP Address.
-
For Linux instances, ensure a security group rule is added to allow inbound traffic on ports 22 and 8080.
For Windows instances, ensure a security group rule is added to allow inbound traffic on ports 3389 and 8080.
For additional details, please refer to how to add a security group rule.
-
If your instance is running on a Linux operating system, ensure that both the system firewall and Security-Enhanced Linux (SELinux) are disabled. For more information, see how to enable or disable the Linux system firewall and how to enable or disable SELinux.
Ensure that the system firewall is disabled on your instance if it is running a Windows operating system. For more information, see how to manage the Windows system firewall.
Procedure
Follow these steps based on your instance's operating system.
Linux
This example uses Apache Tomcat 9.0.91. If you install a different version or use other directories, replace the version and directories in the commands accordingly.
-
Log on to a Linux server. For additional details, refer to Overview of ECS Remote Connection Methods.
To download and extract the Apache Tomcat 9.0.91 installation package, execute the following commands.
NoteThe download URLs for Apache Tomcat may change. If the link provided below is not working, please visit the official Apache Tomcat website to retrieve the latest version.
Should you require a different version, the official Apache Tomcat website provides download URLs which you can use to update the URL in the command accordingly.
wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.91/bin/apache-tomcat-9.0.91.tar.gz --no-check-certificate tar -zxvf apache-tomcat-9.0.91.tar.gz
To relocate the Apache Tomcat installation files, execute the command below to move them to the /usr/local/tomcat/ directory.
sudo mv apache-tomcat-9.0.91 /usr/local/tomcat/
-
(Optional) Configure the server.xml file.
To modify Apache Tomcat configurations, follow these steps:
-
Open the
/usr/local/tomcat/conf/server.xml
file by running the following command:vim /usr/local/tomcat/conf/server.xml
-
Press the
i
key to enter edit mode. By default, Apache Tomcat uses standard configurations. However, you can modify these settings to meet your specific business needs. For instance:-
The default port for Apache Tomcat is 8080. To change the port number, modify the
port
property value.NoteRemember to allow inbound traffic on the new port by adding a security group rule to your instance. For more information, see add security group rule.
-
The default website root directory for Apache Tomcat is
webapps
. By placing a web application, such as a WAR file or a directory containing the WEB-INF folder, into thewebapps
directory, Apache Tomcat will automatically deploy the application. To change the website root directory in Apache Tomcat, you can alter the value of theappBase
property.
-
-
Once modifications are complete, press the Esc key, type
:wq
, and press Enter to save the changes and exit the file.
-
-
(Optional) Configure JVM memory parameters.
To optimize the performance and stability of Apache Tomcat based on your business needs, you can configure the JVM memory parameters by following these steps.
-
Execute the command below to create and open the
/usr/local/tomcat/bin/setenv.sh
file.vim /usr/local/tomcat/bin/setenv.sh
-
Press the
i
key to enter edit mode and insert the following content.Set the
JAVA_OPTS
parameter to adjust JVM memory settings and specify the encoding format, which in this example is UTF-8.NoteYou can modify the content below to better suit your business requirements for JVM memory configuration.
JAVA_OPTS='-Djava.security.egd=file:/dev/./urandom -server -Xms512m -Xmx512m -Dfile.encoding=UTF-8'
-
Press the
Esc
key, type:wq
, and hit theEnter
key to save the changes and exit the file. -
Assign execute permissions to the file.
sudo chmod +x /usr/local/tomcat/bin/setenv.sh
-
-
Set up a script to automatically start Apache Tomcat during system boot.
-
Download the script by executing the command below.
wget https://help-static-aliyun-doc.aliyuncs.com/software/Tomcat-init.sh
-
Use the following command to relocate and rename
Tomcat-init.sh
.sudo mv Tomcat-init.sh /etc/init.d/tomcat
-
Assign execute permissions to
/etc/init.d/tomcat
by running the command below.chmod +x /etc/init.d/tomcat
-
Determine the installation path of the Java Development Kit (JDK). Execute the command below to find the directory where the Java binary is located; this directory's parent is the JDK installation path.
readlink -f $(which java)
NoteFor instance, if the command output is
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.432.b06-2.0.2.1.al8.x86_64/jre/bin/java
, then the JDK installation path is/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.432.b06-2.0.2.1.al8.x86_64
. -
Configure the startup script's
JAVA_HOME
by executing the command below.ImportantEnsure that the JDK path in the script matches the actual JDK installation path to enable Apache Tomcat to start properly.
sudo sed -i 's@^export JAVA_HOME=.*@export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.432.b06-2.0.2.1.al8.x86_64@' /etc/init.d/tomcat
-
-
To ensure Apache Tomcat starts automatically upon system boot and to initiate Apache Tomcat, execute the following commands in sequence:
sudo systemctl daemon-reload sudo systemctl start tomcat sudo systemctl enable tomcat
Once started, you can check the current status of Apache Tomcat by running
sudo systemctl status tomcat
. If the output showsactive (running)
, Apache Tomcat has been successfully launched. -
Verify the environment configuration.
Enter
http://public IP:8080
in your local browser's address bar. If the Tomcat welcome page appears, the environment configuration is successful.Note-
Should the page fail to display after entering
http://public IP:8080
, verify that the security group settings allow inbound traffic on port 8080. -
If you have modified the Apache Tomcat port number, use the new port number instead of 8080 and ensure the security group permits inbound traffic on that port.
-
To test a local project, upload the WAR package to Apache Tomcat's website root directory (default is
/usr/local/tomcat/webapps
) and navigate tohttp://public IP:8080/package name
in your browser. For more information, see how to upload or download files (Linux).
-
Windows
This example uses Apache Tomcat 9.0.97.
Log on to a Windows server. For more information, see the referenced document.
Download Apache Tomcat.
Visit the official Apache Tomcat website, select the appropriate version, and download the installation package.
Extract the Apache Tomcat installation package.
Unzip the downloaded package to your desired installation directory, for example,
C:\Program Files
.Locate the Apache Tomcat installation directory.
Navigate to the directory where you unzipped the package and find the installation directory of Apache Tomcat in the address bar.
Set up environment variables.
Right-click This PC and select Properties.
Scroll to the bottom of the About page and click Advanced System Settings.
Click Environment Variables.
In the System Variables section, you can click New to create a system variable. Once created, click OK. Name the variable
CATALINA_HOME
and set its value to the Apache Tomcat installation directory, such asC:\Program Files\apache-tomcat-9.0.97
.Locate the
Path
variable under System Variables, select it, and click Edit.In the Edit Environment Variables window, click New and add
%CATALINA_HOME%\bin
to the list.Click OK twice to save the changes to the environment variables.
Launch Apache Tomcat.
Navigate to the bin directory within the Apache Tomcat installation folder.
Type
cmd
in the address bar and press Enter to open a command prompt window.Run the
startup.bat
command to start Apache Tomcat. A new command prompt window will open displaying the startup log. Keep this window open.
Verify Apache Tomcat installation.
Open a local browser and enter
http://Public IP:8080
to access the Apache Tomcat welcome page. If the page displays, the installation is successful.NoteShould the page fail to load upon entering
http://Public IP:8080
, consider the following troubleshooting steps:Ensure the instance's security group allows inbound traffic on port 8080.
Ensure that the command prompt window remains open after executing the
startup.bat
command to start Apache Tomcat. If the window has closed, re-execute thestartup.bat
command to reopen it.
To test a local project, upload the WAR file to the Apache Tomcat webapps directory (e.g.,
C:\Program Files\apache-tomcat-9.0.97\webapps
) and navigate tohttp://Public IP:8080/Package Name
in your browser. For more information, see the referenced document.