Java Web is a technology stack that integrates the Java programming language and various technologies and frameworks to facilitate the development of dynamic web applications. Developers can use Java Web to develop complex, high-performance web applications that can be deployed across platforms. Apache Tomcat is the most popular web server environment used to deploy and run Java web applications. This topic describes how to deploy a Java web environment on an Elastic Compute Service (ECS) instance.
Prerequisites
Before you deploy a Java web environment on an ECS instance, make sure that the instance meets the following requirements:
The ECS instance is assigned a static public IP address (also called system-assigned or auto-assigned public IP address) or associated with an elastic IP address (EIP). For more information, see EIPs.
If your instance runs a Linux operating system, make sure that a rule is added to a security group of the instance to allow inbound traffic on ports 22 and 8080.
If your instance runs a Windows operating system, make sure that a rule is added to a security group of the instance to allow inbound traffic on ports 3389 and 8080.
For information about how to add a security group rule, see Add a security group rule.
If your instance runs a Linux operating system, make sure that the system firewall and Security-Enhanced Linux (SELinux) are disabled. For more information, see Enable or disable the system firewall on a Linux ECS instance and Enable or disable SELinux.
If your instance runs a Windows operating system, make sure that the system firewall is disabled. For more information, see Enable or disable the Windows firewall.
A Java environment is deployed on the instance. For more information, see Deploy a Java environment.
Procedure
Perform the following operations based on the operating system of your instance.
Linux
In this example, Apache Tomcat 9.0.91 is used. If you install another version of Apache Tomcat or use other directories, replace the version and directories in the following commands with the actual version and directories.
Connect to your Linux instance. For more information, see Connect to an instance.
Run the following commands to download and decompress the Apache Tomcat 9.0.91 installation package.
NoteThe download URLs of Apache Tomcat may change. If the following download URL is invalid, visit the official Apache Tomcat website to obtain the latest download URL.
If you want to install another version of Apache Tomcat, visit the official Apache Tomcat website to obtain the download URL and replace the URL in the following wget command with the URL that you obtained.
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
Run the following command to move the Apache Tomcat installation files to the /usr/local/tomcat/ directory:
sudo mv apache-tomcat-9.0.91 /usr/local/tomcat/
(Optional) Configure the server.xml file.
If you want to modify Apache Tomcat configurations, perform the following steps:
Run the following command to open the
/usr/local/tomcat/conf/server.xml
file:vim /usr/local/tomcat/conf/server.xml
Press the
I
key to enter Insert mode. In this example, the default Apache Tomcat configurations are used. You can also modify the Apache Tomcat configurations based on your business requirements. Examples:By default, Apache Tomcat uses port 8080. If you want to change the port number, change the value of the
port
parameter.NoteAfter you change the port number, you must allow the new port in a security group of the instance. For more information, see Add a security group rule.
By default, the website root directory of Apache Tomcat is
webapps
. When you transfer a web application, such as a WAR file or a folder that contains the WEB-INF directory, to thewebapps
directory, Apache Tomcat automatically deploys the application. If you want to change the website root directory of Apache Tomcat, change the value of theappBase
parameter.
Press the Esc key, enter
:wq
, and then press the Enter key to save and close the file.
(Optional) Configure Java Virtual Machine (JVM) memory parameters.
If you want to configure the JVM memory parameters of Apache Tomcat based on your business requirements to optimize the performance and stability of Apache Tomcat, perform the following steps:
Run the following command 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 Insert mode and add the following content.Specify the
JAVA_OPTS
parameter to configure JVM memory information and the encoding format. In this example, the encoding format is UTF-8.NoteYou can adjust the following content to configure JVM memory parameters based on your business requirements:
JAVA_OPTS='-Djava.security.egd=file:/dev/./urandom -server -Xms512m -Xmx512m -Dfile.encoding=UTF-8'
Press the
Esc
key to exit Insert mode. Enter:wq
and press theEnter
key to save and close the file.Run the following command to grant the execute permissions on the file:
sudo chmod +x /usr/local/tomcat/bin/setenv.sh
Configure a script that enables Apache Tomcat to automatically start on system startup.
Run the following command to download the script.
ImportantThe following script is maintained by the community and is provided only for reference. Alibaba Cloud does not provide warranty of any kind, expressed or implied, with regard to the performance and reliability of the script or the potential impacts of the script on operations. If you cannot download the script by running the wget command, access
https://raw.githubusercontent.com/oneinstack/oneinstack/master/init.d/Tomcat-init
in your browser to obtain the script content.wget https://raw.githubusercontent.com/oneinstack/oneinstack/master/init.d/Tomcat-init
Run the following command to move and rename the
Tomcat-init
file:sudo mv Tomcat-init /etc/init.d/tomcat
Run the following command to grant the execute permissions on the
/etc/init.d/tomcat
file:chmod +x /etc/init.d/tomcat
Obtain the path in which Java Development Kit (JDK) is installed. Run the following command to obtain the actual path in which the Java binary file is stored. The parent directory of the Java binary file is the path in which JDK is installed.
readlink -f $(which java)
NoteFor example, if
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.432.b06-2.0.2.1.al8.x86_64/jre/bin/java
is returned after the command is run, JDK is installed in the/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.432.b06-2.0.2.1.al8.x86_64
path.Run the following command to specify the
JAVA_HOME
parameter in the script.ImportantThe JDK path specified in the JAVA_HOME parameter must be the path in which JDK is installed. Otherwise, Apache Tomcat cannot start.
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
Run the following commands in sequence to enable Apache Tomcat to automatically start on system startup and start Apache Tomcat:
sudo systemctl daemon-reload sudo systemctl start tomcat sudo systemctl enable tomcat
After Apache Tomcat is started, you can run the
sudo systemctl status tomcat
command to check the status of Apache Tomcat. Ifactive (running)
is returned, Apache Tomcat is started.Check the environment configuration.
In the address bar of a web browser on your on-premises device, enter
http://<Public IP address of the ECS instance>:8080
. If the Apache Tomcat welcome page appears, the Java web environment is configured as expected.NoteIf the Apache Tomcat welcome page does not appear after you enter
http://<Public IP address of the ECS instance>:8080
, check whether a security group of the instance allows inbound traffic on port 8080.If you changed the Apache Tomcat port number, replace 8080 with the new Apache Tomcat port number that you specified and check whether a security group of the instance allows inbound traffic on the port.
If you want to upload an on-premises project to test the environment, upload a WAR package to the website root directory of Apache Tomcat. In this example, the
/usr/local/tomcat/webapps
directory is used. Then, enterhttp://<Public IP address of the ECS instance>:8080/<Package name>
in the address bar of a web browser to access the project. For more information, see Upload a file to or download a file from a Linux instance.
Windows
In this example, Apache Tomcat 9.0.97 is used.
Connect to your Windows instance. For more information, see Connect to an instance.
Download the Apache Tomcat installation package.
Visit the official Apache Tomcat website, select the version that you want to install, and then download the installation package.
Decompress the Apache Tomcat installation package.
Decompress the downloaded installation package to the directory in which you want to install Apache Tomcat. In this example, the
C:\Program Files
directory is used.Obtain the directory in which you want to install Apache Tomcat.
After you decompress the installation package to the directory, go to the directory and obtain the installation directory of Apache Tomcat from the address bar.
Configure environment variable settings.
Right-click This PC and select Properties.
On the About page, scroll down to the bottom and click Advanced system settings.
Click Environment Variables.
In the System variables section, click New to create a system variable. Then, click OK. The variable name is
CATALINA_HOME
and the variable value is the installation directory of Apache Tomcat. In this example, theC:\Program Files\apache-tomcat-9.0.97
directory is used.In the System variables section, select the
Path
variable and click Edit.In the Edit environment variable dialog box, click New to add the
%CATALINA_HOME%\bin
path.Click OK twice to save the environment variable.
Start Apache Tomcat.
Go to the bin folder in the installation directory of Apache Tomcat.
Enter
cmd
in the path bar and press the Enter key to open the command prompt.Run the
startup.bat
command to start Apache Tomcat. After the command is run, a new command prompt window is opened by default to display the startup logs of Apache Tomcat. Do not close the window.
Test Apache Tomcat.
In the address bar of a web browser on your on-premises device, enter
http://<Public IP address of the ECS instance>:8080
. If the Apache Tomcat welcome page appears, Apache Tomcat is deployed as expected.NoteIf the Apache Tomcat welcome page does not appear after you enter
http://<Public IP address of the ECS instance>:8080
, check the following items:Check whether security groups of the instance allow inbound traffic on port 8080.
Check whether the command prompt window that is opened by default after you run the
startup.bat
command to start Apache Tomcat is closed. If the window is closed, rerun thestartup.bat
command to open the window.
If you want to upload an on-premises project to test Apache Tomcat, upload a WAR package to the website root directory of Apache Tomcat. In this example, the
C:\Program Files\apache-tomcat-9.0.97\webapps
directory is used. Then, enterhttp://<Public IP address of the ECS instance>:8080/<Package name>
in the address bar of a web browser to access the project. For more information, see Upload a file to or download a file from a Linux instance.
What to do next
After you deploy a Java web environment on an ECS instance, you can configure a website on the instance and bind a domain name to the public IP address of the instance. For more information, see Build a website.
Select an appropriate development tool to develop web applications. If an application requires a database to store application data, add database configurations to the relevant configuration file of the corresponding web project, such as the Properties file, and connect to the database. After the development is complete, you can deploy the Java web applications to Apache Tomcat, such as to the webapps directory under the Apache Tomcat installation directory. After the Java web applications are deployed and started, you can access your applications over the web interface. For more information, see Apache Tomcat 9 Tomcat Web Application Deployment.
NoteIf you use a different version of Apache Tomcat, replace the version number in the URL with the corresponding version number.
You can upload on-premises files to the ECS instance. For more information, see Upload a file to or download a file from a Linux instance.
If you want to use a database to store and manage the data of a web application, you can use an ApsaraDB RDS for MySQL instance or deploy a MySQL database on the ECS instance. For more information, see Database overview.
You can use a Server Load Balancer (SLB) instance to improve the availability and performance of your website. For more information, see Getting Started.