Build Web Application Server (Java) on VPS

I built a Web Server to study Server and tried to execute Java Application.

table of contents

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

1. Operating environment

2. What to do

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

3. Construction procedure

Java execution environment construction

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)

Preparation of Servlet container

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 /)

スクリーンショット 2020-07-25 18.18.47.png

Building a Web Server

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)

Java application placement

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) スクリーンショット 2020-07-25 22.32.50.png

Recommended Posts

Build Web Application Server (Java) on VPS
Build a Java runtime environment on Sakura VPS
Volume of trying to create a Java Web application on Windows Server 2016
Java (WebSphere Application Server) memo [1]
Rails web server and application server
Build a Minecraft server on AWS
Build a web application with Javalin
Try using Java framework Nablarch [Web application]
Build a Java development environment on Mac
Deploy a Java web app on Heroku
Memory measurement of Java application on Windows
Build OpenCV with Java Wrapper on Ubuntu 18.04
Role of JSP in Web application [Java]
[Java] Deploy a web application created with Eclipse + Maven + Ontology on Heroku
Try to build Java8 environment on Amazon Linux2
vagrant java build
Place Java project using database on the server created in VPS so far
Java 9+ application status
[Java] Build Java development environment on Ubuntu & check execution
I built a Java EE environment on AWS and tried running a web application
Build Java x Spring x VSCode x Gradle on Docker (1)
[Java / PostgreSQL] Connect the WEB application to the database
Try communication using gRPC on Android + Java server
[For beginners] Until building a Web application development environment using Java on Mac OS
Build Java development environment with VS Code on Mac
Build a docker container for a python simple web server
[Tutorial] Download Eclipse → Run Web application with Java (Pleiades)
I tried using Log4j2 on a Java EE server
Create a JAVA WEB application and try OMC APM
Web Bluetooth on Ubuntu20.04
Install Java on Mac
Run PostgreSQL on Java
java build a triangle
First gradle build (Java)
Web application test automation
JPS (Java Server pages)
Build Java with Wercker
Web application development environment construction in Java (for inexperienced people)
Use Java Web Start in an OpenJDK environment on Windows
Application file deployment location on the local host Tomcat server
1. Quickly run Java web modules on Google Kubernetes Engine (GKE)
Show detailed error in Logger when running Java on server