[Java] Build Web Application Server (Java) on VPS

4 minute read

To study Server, I built WebServer and tried Java Application.

table of contents

  1. Operating environment
  2. What to do
  3. Construction procedure
  4. Reference site

1. Operating environment

  • Cent OS7

2. What to do

As a result of researching, it seems that you can move it for the time being if you do the following.

  • Java execution environment construction
  • Servlet container preparation
  • Build Web Server
  • Java application deployment

3. Construction procedure

Build Java execution environment

Install a JDK to run Java execution on the server. The following sites were easy to understand. ■Reference: I 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 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)

Preparing the servlet container

To run Java Servlet (Java program running on the server), install tomcat, which is a servlet container. I often hear about tomcat, but I used it without understanding… First, add a user to run Tomcat (username: tomcat).

# useradd -s /sbin/nologin tomcat
  • /Sbin/nologin seems to mean a user who cannot log in. Is the user “tomcat” created here a script for starting the service? Since it’s only used by (systemd), it doesn’t seem necessary to be able to log in.

■Reference: Method to execute commands as a user who cannot log in + bonus

Then download the file to the server to install Tomcat.

# Curl -O https://ftp.jaist.ac.jp/pub/apache/tomcat/tomcat-9/v9.0.37/bin/apache-tomcat-9.0.37.tar.gz

*The part “https://ftp.jaist.ac.jp/pub/apache/tomcat/tomcat-9/v9.0.37/bin/apache-tomcat-9.0.37.tar.gz” may change. Check below. tomcat download site

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

Change the user owner: group owner to the user “tomcat” created earlier with the following command

# chown -R tomcat:tomcat /usr/local/tomcat

In order to make Tomcat recognize as a service that can be executed by the server, create a systemd unit file with the following command.

  • Since it is systemd from CentOS 7, it seems that it manages the service.

Reference: Getting Started with Systemd

# vi /etc/systemd/system/tomcat.service

Enter the following contents in the created file and save. To be honest, I don’t really understand the contents. I understand that I have declared that I will make 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

Furthermore, set read/write/execute permissions for the created file.

# chmod 755 /etc/systemd/system/tomcat.service
  • “Owner: Read/Write/Executable” “Owner Group: Read/Executable” “Other (Other): Read/Executable” is set

■Reference: [[chmod] command–change file/directory permissions (permission attributes)) (https://www.atmarkit.co.jp/ait/articles/1605/23/news020.html)

Set automatic 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 it is OK when the tomcat default screen appears.

http://server IP address:8080/

Screenshot 2020-07-25 18.18.47.png

Build Web Server

Originally it seems good to install Apatch HTTP server, Since Tomcat seems to have the function of Web Server, I will use it (so I will not install it). Then, I thought that I didn’t need Apache HttpServer in the first place, but tomcat’s Web server seems to be a bonus, and considering performance, it should be there.

■Reference: [In-house study session] Apache and Tomcat (2017/03/09)

Java application deployment

It seems that the java application should be placed in the webapps (/usr/local/tomcat/webapps/) directly under the installed Tomcat. I don’t really understand because I don’t usually think about this area either. The following sites were very easy to understand.

■Reference: Hello World with Tomcat

For the time being, create a project folder with the following command

# mkdir /usr/local/tomcat/webapps/javaproject

Then create a class file and web.xml. Create the 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

HelloWorld in the same directory. OK if you have a class. After that, create web.xml and associate the URL with the 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>

At last we are nearing the end. It was long because I was addicted to it… Restart tomcat with the following command.

# systemctl restart tomcat

It is OK when you access the following URL and HelloWorld is displayed!

http://server IP address:8080/javaproject/servlet/hello Screenshot 2020-07-25 22.32.50.png