I built a Web Server to study Server and tried to execute Java Application.
After investigating, it seems that you can move it for the time being by doing the following.
--Java execution environment construction --Preparation of Servlet container --Building a Web Server --Java application placement
Install the JDK to run Java execution on the server. The following site was easy to understand. ■ Reference: I tried to put Tomcat 9 in CentOS 7
Install by running the following command on the server (JDK8).
# yum install java-1.8.0-openjdk-devel.x86_64
(If you want to search for other JDK, execute the following command.)
# yum search openjdk
Confirm that it is installed with the following command.
# java -version
It is OK if the following output is output.
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
Install tomcat, a servlet container, to run Java Servlets (Java programs that run on the server). I often hear tomcat, but I used it without knowing it ... First, add a user to run Tomcat (username: tomcat).
# useradd -s /sbin/nologin tomcat
■ Reference: How to execute commands as a user who cannot log in + bonus
Then download the file to the server for Tomcat installation.
# curl -O https://ftp.jaist.ac.jp/pub/apache/tomcat/tomcat-9/v9.0.37/bin/apache-tomcat-9.0.37.tar.gz
Unzip the downloaded file with the following command.
# tar -xzvf apache-tomcat-9.0.37.tar.gz
Move to / usr / local / tomcat with the following command.
# mv apache-tomcat-9.0.37 /usr/local/tomcat
User owner: Change the group owner to the user "tomcat" created earlier with the following command
# chown -R tomcat:tomcat /usr/local/tomcat
Create a systemd unit file with the following command to make Tomcat recognized as a service that can be executed by the server.
■ Reference: Introduction to Systemd
# vi /etc/systemd/system/tomcat.service
Write the following contents in the created file and save it. To be honest, I don't really understand the content. Understanding that we are declaring that we will create such a service.
[Unit]
Description=Apache Tomcat 9
After=syslog.target network.target
[Service]
User=tomcat
Group=tomcat
Type=oneshot
PIDFile=/usr/local/tomcat/tomcat.pid
RemainAfterExit=yes
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
ExecReStart=/usr/local/tomcat/bin/shutdown.sh;/usr/local/tomcat/bin/startup.sh
[Install]
WantedBy=multi-user.target
In addition, set read permission, write permission, and execute permission for the created file.
# chmod 755 /etc/systemd/system/tomcat.service
■ Reference: [chmod] command-change file / directory permissions (permission attributes)
Set auto-start and enable service
# systemctl enable tomcat
Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /etc/systemd/system/tomcat.service.
# systemctl start tomcat
Start Tomcat.
Access the following URL and OK when the tomcat default screen appears.
[http: // server IP address: 8080 /](http: // [server IP address]: 8080 /)
It seems that it is better to install Apache HTTP server, but It seems that Tomcat also has a Web Server function, so use it (so do not install it in particular). Then, I thought that Apache HttpServer wasn't necessary in the first place, but tomcat's Web server seems to be a bonus, and considering performance, it seems better to have it.
■ Reference: [In-house study session] Apache and Tomcat (2017/03/09)
It seems that the java application should be placed in the webapps (/ usr / local / tomcat / webapps /) directly under the installed Tomcat. I wasn't aware of this area either, so I didn't really understand it. The following site was very easy to understand.
■ Reference: Hello World on Tomcat
For the time being, create a project folder with the following command
# mkdir /usr/local/tomcat/webapps/javaproject
After that, create a class file and web.xml. Create a classes directory with the following command.
# mkdir /usr/local/tomcat/webapps/javaproject/WEB-INF/classes
Create a java source file with the following command.
# vi /usr/local/tomcat/webapps/javaproject/WEB-INF/classes/HelloWorld.java
Edit and save as follows.
HelloWorld.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("HelloWorld");
out.println("</html>");
out.close();
}
}
Compile with the following command.
# cd /usr/local/tomcat/webapps/javaproject/WEB-INF/classes/
# javac -classpath /usr/local/tomcat/lib/servlet-api.jar HelloWorld.java
Hello World. In the same directory. OK if you have a class. After that, create web.xml and link the URL and servlet. Create web.xml with the following command.
# vi /usr/local/tomcat/webapps/javaproject/WEB-INF/web.xml
Edit and save as follows
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
hello
</servlet-name>
<url-pattern>
/servlet/hello
</url-pattern>
</servlet-mapping>
</web-app>
It's finally nearing the end. It was long because I was addicted to it ... Restart tomcat with the following command.
# systemctl restart tomcat
Access the following URL and when Hello World is displayed, it's OK!
[http: // server IP address: 8080 / javaproject / servlet / hello](http: // [server IP address]: 8080 / javaproject / servlet / hello)
Recommended Posts