This topic describes how to manually deploy a Java web environment on an Elastic Compute Service (ECS) instance that runs Alibaba Cloud Linux 3. This topic is applicable to individual users who are new to website construction on ECS instances.
Prerequisites
Before you deploy a Java web environment on an ECS instance, make sure that the instance meets the following requirements:
The instance has a public IP address or an elastic IP address (EIP). For more information, see Associate or disassociate an EIP.
The instance runs Alibaba Cloud Linux 3.
An inbound security group rule is added to a security group of the instance to allow traffic on ports 22, 80, 443, and 8080. For more information, see Add a security group rule.
Step 1: Disable the firewall and SELinux
Connect to the Linux instance.
For more information, see Connect to a Linux instance by using a password or key.
Disable the firewall.
Run the following command to check the state of the firewall:
systemctl status firewalld
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. Perform the following steps:
To temporarily disable the firewall, run the following command:
sudo systemctl stop firewalld
NoteAfter you run the preceding command, the firewall is temporarily disabled. The next time you restart the Linux operating system, the firewall is re-enabled and enters the active state.
To permanently disable the firewall, run the following command:
sudo systemctl disable firewalld
NoteYou can re-enable the firewall after it is disabled. For more information, visit the official firewalld website.
Disable SELinux.
Run the following command to check the state of SELinux:
getenforce
If SELinux is in the Disabled state, SELinux is disabled.
If SELinux is in the Enforcing state, SELinux is enabled.
Disable SELinux. If SELinux is disabled, skip this step.
To temporarily disable SELinux, run the
setenforce 0
command.NoteAfter you run the preceding command, SELinux is temporarily disabled. The next time you restart the Linux operating system, SELinux is re-enabled and enters the Enforcing state.
To permanently disable SELinux, run the vi /etc/selinux/config command to edit the SELinux configuration file. Press the Enter key. Move the pointer to the
SELINUX=enforcing
line and press the I key to enter Insert mode. Replace SELINUX=enforcing withSELINUX=disabled
. Press the Esc key, enter :wq, and then press the Enter key to save and close the SELinux configuration file. Restart the operating system to make the settings take effect.NoteYou can re-enable SELinux after it is disabled. For more information, see the official SELinux documentation.
Step 2: Install JDK
In this example, JDK 1.8.0_351 is used.
Run the following command to query JDK packages:
yum -y list java*
Run the following command to install a JDK package.
In this example, the JDK 1.8.0 package is installed.
sudo yum -y install java-1.8.0-openjdk-devel.x86_64
Run the following command to check the version of JDK:
java -version
Sample command output:
openjdk version "1.8.0_372" OpenJDK Runtime Environment (build 1.8.0_372-b07) OpenJDK 64-Bit Server VM (build 25.372-b07, mixed mode)
Run the following command to view the JDK installation path:
find /usr/lib/jvm -name 'java-1.8.0-openjdk-1.8.0*'
Sample command output:
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-4.0.2.al8.x86_64
Configure Java environment variables.
Run the following command to open the configuration file:
sudo vim /etc/profile
Press the
I
key to enter Insert mode.Add the following content to the end of the script:
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-4.0.2.al8.x86_64 PATH=$PATH:$JAVA_HOME/bin CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME CLASSPATH PATH
In the preceding information, set
JAVA_HOME
to the JDK installation path. For more information about the path, see Step 2.4 of this topic.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 apply the environment variables:
source /etc/profile
Step 3: Install Apache Tomcat
Apache Tomcat is a web application server. In this example, Apache Tomcat 8.5.83 is used.
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/
Configure the server.xml file.
Run the following command to go to the /usr/local/tomcat/conf/ directory:
cd /usr/local/tomcat/conf/
Run the following command to rename the server.xml file:
mv server.xml server.xml_bk
Create a file named server.xml.
Run the following command to create and open the server.xml file:
vim server.xml
Press the I key to enter Insert mode and then add the following content to the file:
<?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>
Press the Esc key to exit Insert mode. Enter
:wq
and press the Enter key to save and close the file.
Configure the Java Virtual Machine (JVM) memory parameters.
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 then add the following content to the file.
Specify the
JAVA_OPTS
parameter to set JVM memory information and the encoding format. In this example, the encoding format is set to UTF-8.JAVA_OPTS='-Djava.security.egd=file:/dev/./urandom -server -Xms256m -Xmx496m -Dfile.encoding=UTF-8'
Press the
Esc
key to exit Insert mode. Enter:wq
and press theEnter
key to save and close the file.
Configure a script to enable Apache Tomcat to automatically start on system startup.
Run the following command to download the script.
NoteThis script is maintained by the community and is provided for reference only. Alibaba Cloud does not make any guarantee, express or implied, with respect to the performance and reliability of the script or the potential impacts of the script 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 Tomcat-init:
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
Run the following command to set JAVA_HOME in the script.
ImportantThe JDK path specified in the script must be the JDK installation path. Otherwise, Apache Tomcat cannot start. For more information about the path, see Step 2.4 of this topic.
sudo sed -i 's@^export JAVA_HOME=.*@export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-4.0.2.al8.x86_64@' /etc/init.d/tomcat
Run the following commands in sequence to enable Apache Tomcat to automatically start on system startup:
sudo chkconfig --add tomcat sudo chkconfig tomcat on
Run the following command to start Apache Tomcat:
sudo service tomcat start
Apache Tomcat is started. Sample output:
Starting tomcat Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-4.0.2.al8.x86_64 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Using CATALINA_OPTS: Tomcat started. Tomcat is running with pid: 4517
Step 4: Deploy a test project to check whether the Java web environment is deployed
Upload the WAR package of Java web project files to the website root directory. You can use a remote connection tool that provides a file transfer feature or build an FTP site to upload project files.
In this example, the website root directory is /data/wwwroot/default. Perform the following operations to create an Apache Tomcat test page in the website root directory and access the page:
Run the following command to create a website root directory:
mkdir -p /data/wwwroot/default
Run the following command to create a test file:
echo sh -c Tomcat test > /data/wwwroot/default/index.jsp 'echo "${vSGX_ip_addr} grpc.tf-serving.service.com" >> /etc/hosts'
Open your browser and enter the following address in the address bar to check whether a Java web environment is deployed:
http://<Public IP address of the instance>:8080
.The page shown in the following figure indicates that the Java web environment is deployed.
FAQ
If the Java web environment is inaccessible, perform the following operations to troubleshoot the issue:
Check whether rules are added to the security groups of the instance to allow traffic on Apache Tomcat port 8080. For more information, see Add a security group rule.
Check whether the JDK installation path is properly configured. You can run the find /usr/lib/jvm -name 'java-1.8.0-openjdk-1.8.0*' command to check the JDK installation path. Two settings involve the JDK installation path. For more information, see Step 2.5 and Step 3.5 of this topic.