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
An ECS instance that runs Alibaba Cloud Linux 3 is created. For more information, see Creation method overview.
Add inbound rules to the security group of the instance to allow traffic on required ports. 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. For more information, see Add a security group rule.
Background information
In this topic, the following instance type and software versions are used. Operations may vary based on your software versions.
Instance type: ecs.c6.large
Operating system: Alibaba Cloud Linux 3.2104 LTS 64-bit
Java Development Kit (JDK): 1.8.0_352
Apache Tomcat: 8.5.88
Step 1: Make preparations
Connect to the instance.
For more information, see Connect to a Linux instance by using a password.
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.
To temporarily disable the firewall, run the following command:
systemctl stop firewalld
NoteAfter you run the preceding command, the firewall is 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:
systemctl disable firewalld
NoteYou can re-enable the firewall after it is disabled. For more information, visit the official firewalld website.
Disable Security-Enhanced Linux (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. Skip this step if SELinux is already disabled.
To temporarily disable SELinux, run the
setenforce 0
command.NoteAfter you run the preceding command, SELinux is 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 switch to the edit mode. Change the line toSELINUX=disabled
and press the Esc key. Enter :wq and press the Enter key to save the changes and close the SELinux configuration file. Restart the operating system to apply the settings.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.
yum -y install java-1.8.0-openjdk-devel.x86_64
Run the following command to check the version of JDK:
java -version
JDK version information in the example command output:
[root@iZbp14h7n3cwipjln62**** ~]# java -version openjdk version "1.8.0_352" OpenJDK Runtime Environment (build 1.8.0_352-b08) OpenJDK 64-Bit Server VM (build 25.352-b08, 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*'
A command output similar to the following one is displayed:
[root@iZbp14h7n3cwipjln62kxvZ ~]# find /usr/lib/jvm -name 'java-1.8.0-openjdk-1.8.0*' /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.352.b08-2.al8.x86_64
Configure Java environment variables.
Run the following command to open the configuration file:
vim /etc/profile
At the end of the configuration file, press the I key to enter the edit mode.
Add the following information:
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.352.b08-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 information about how to view the path, see the View the JDK installation path step in this topic.Press the Esc key to exit the edit mode. Enter
:wq
and press the Enter key to save and close the configuration 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.
Download and decompress the Apache Tomcat installation package.
Run the following command to download the Apache Tomcat installation package.
In this example, Apache Tomcat 8.5.88 is downloaded.
wget --no-check-certificate https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.88/bin/apache-tomcat-8.5.88.tar.gz
NoteThe 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.
Run the following command to decompress the Apache Tomcat installation package:
tar -zxvf apache-tomcat-8.5.88.tar.gz
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/
For security purposes, create a standard user to run Apache Tomcat.
In this example, a standard user named www is created.
useradd www
Run the following command to set the owner of the file to www:
chown -R www.www /usr/local/tomcat/
The /usr/local/tomcat/ directory contains the following subdirectories:
bin: stores Apache Tomcat script files, such as scripts used to enable and disable Apache Tomcat.
conf: stores various global configuration files of the Apache Tomcat server, among which server.xml and web.xml are the most important files.
webapps: serves as the main web publishing directory of Apache Tomcat. Web application files are stored in this directory by default.
logs: stores Apache Tomcat operation log files.
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 server.xml file.
Run the following command to create and open the server.xml file:
vim server.xml
Press the I key to enter the edit mode and add the following content:
<?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 the edit mode. Enter
:wq
and press the Enter key to save and close the file.
Configure 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 the edit mode and add the following content.
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 the edit mode. Enter
:wq
and press the Enter key to save and close the file.
Configure a script to enable Apache Tomcat to run 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 a browser to obtain the content of the script.wget https://raw.githubusercontent.com/oneinstack/oneinstack/master/init.d/Tomcat-init
Run the following command to move and rename Tomcat-init:
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, Tomcat cannot start. For information about how to view the path, see the View the JDK installation path step in this topic.
sed -i 's@^export JAVA_HOME=.*@export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.352.b08-2.al8.x86_64@' /etc/init.d/tomcat
Run the following commands in sequence to enable Apache Tomcat to auto-start on system startup:
chkconfig --add tomcat chkconfig tomcat on
Run the following command to start Apache Tomcat:
service tomcat start
A command output similar to the following one indicates that Apache Tomcat is started:
[root@iZbp14h7n3cwipjln62**** conf]# service tomcat start 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.352.b08-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: 6849
Step 4: Deploy and verify the test project
Upload the WAR package of Java web project files to the website root directory and change the owner of files in the root directory to www. 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 set the owner of the website root directory to www:
chown -R www.www /data/wwwroot
Run the following command to create a test file:
echo Tomcat test > /data/wwwroot/default/index.jsp
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 access on Apache Tomcat port 8080. For more information, see Add a security group rule.
Check whether the configured JDK installation path is correct. You can run the find /usr/lib/jvm -name 'java-1.8.0-openjdk-1.8.0*' command to check the JDK installation path. The Configure Java environment variables and Set JAVA_HOME in the script steps in this topic involve the JDK installation path.