How to install Apache Tomcat 9 with Nginx proxy on Debian 10

How to install Apache Tomcat 9 with Nginx proxy on Debian 10

Tomcat is a free, open source, lightweight application server for deploying Java-based applications. It can be used as a standalone server or combined with other servers (such as Apache and Nginx). It provides extended functions for interaction with Java Servlet, and implements some technical specifications of the Java platform. Currently, Tomcat is one of the most widely used application servers in Java.

In this tutorial, I will show you how to install Tomcat 9 with Nginx on Debian 10.

prerequisites

  • A server running Debian 10.
  • A valid domain name pointing to the server IP.
  • A root password is configured on your server.

getting Started

Before starting, it is best to update the APT cache to the latest version. You can update it by running the following command:

apt-get update -y

Once the system package cache is up to date, you can proceed to the next step.

Install Java

Tomcat is a Java-based application, so Java must be installed in the server. If it is not installed, you can install it with the following command:

apt-get install default-jdk -y

After installing Java, you can use the following command to verify the Java version:

java --version

You should get the following output:

openjdk 11.0.9.1 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-post-Debian-1deb10u2)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-post-Debian-1deb10u2, mixed mode, sharing)

Once completed, you can proceed to the next step.

Install Tomcat

First, you will need to create a dedicated user to run the Tomcat server. You can create it with the following command:

useradd -m -U -d /opt/tomcat -s /bin/false tomcat

Next, use the following command to download the latest version of Tomcat:

wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.45/bin/apache-tomcat-9.0.45.tar.gz

After the download is complete, use the following command to decompress the downloaded file:

tar -xvzf apache-tomcat-9.0.45.tar.gz

Next, use the following command to move the extracted directory to /opt:

mv apache-tomcat-9.0.45 /opt/tomcat/tomcat

Next, use the following commands to set the appropriate permissions and ownership:

chown -R tomcat:tomcat /opt/tomcat/tomcatchmod -R 755 /opt/tomcat/tomcat

Once completed, you can proceed to the next step.

Create a system service file for Tomcat

Next, you will need to create a systemd service file to manage the Tomcat service. You can create it with the following command:

nano /etc/systemd/system/tomcat.service

Add the following line:

[Unit]
Description=Tomcat 9.0 servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/default-java"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"

Environment="CATALINA_BASE=/opt/tomcat/tomcat"
Environment="CATALINA_HOME=/opt/tomcat/tomcat"
Environment="CATALINA_PID=/opt/tomcat/tomcat/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/tomcat/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

Save and close the file, then reload the systemd daemon using the following command:

systemctl daemon-reload

Next, start the Tomcat service and use the following command to enable it to start when the system reboots:

systemctl start tomcatsystemctl enable tomcat

You can also use the following command to verify the status of Tomcat:

systemctl status tomcat

You should get the following output:

? tomcat.service - Tomcat 9.0 servlet container
   Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled)
   Active: active (running) since Wed 2021-04-14 05:41:21 UTC; 42s ago
  Process: 5784 ExecStart=/opt/tomcat/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 5791 (java)
    Tasks: 30 (limit: 4701)
   Memory: 159.1M
   CGroup: /system.slice/tomcat.service
           ??5791 /usr/lib/jvm/default-java/bin/java -Djava.util.logging.config.file=/opt/tomcat/tomcat/conf/logging.properties -Djava.util.log

Apr 14 05:41:21 debian systemd[1]: Starting Tomcat 9.0 servlet container...
Apr 14 05:41:21 debian startup.sh[5784]: Tomcat started.
Apr 14 05:41:21 debian systemd[1]: Started Tomcat 9.0 servlet container.

Once completed, you can proceed to the next step.

Configure Tomcat web interface

Next, you will need to define a user to access the Tomcat web interface. You can use the tomcat-users.xml file to define:

nano /opt/tomcat/tomcat/conf/tomcat-users.xml

Find the section and add or modify the following lines:

  <role rolename="admin-gui"/>
  <role rolename="manager-gui"/>
  <user username="tomcatadmin" password="password" roles="admin-gui,manager-gui"/>

Save and close the file when you are done.

By default, the Tomcat web interface is configured to be accessed only from the local host. Therefore, you need to configure it for external access.

For the Manager application, edit the following line:

nano /opt/tomcat/tomcat/webapps/manager/META-INF/context.xml

For the host manager application, edit the following files:

nano /opt/tomcat/tomcat/webapps/host-manager/META-INF/context.xml

Delete the following lines:

  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127.d+.d+.d+|::1|0:0:0:0:0:0:0:1" />

Save and close the file, then restart the Tomcat service to apply the changes:

systemctl restart tomcat

Once completed, you can proceed to the next step.

Configure Nginx for Tomcat

Next, you will need to install Nginx and configure it as a reverse proxy for Tomcat. First, install the Nginx package using the following command:

apt-get install nginx -y

Once Nginx is installed, you will need to create a new Nginx virtual host configuration file for Tomcat. You can create it with the following command:

nano /etc/nginx/conf.d/tomcat.conf

Add the following line:

server {
  listen          80;
  server_name     tomcat.example.com;
  root            /opt/tomcat/tomcat/webapps/;


  location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass https://127.0.0.1:8080/;
  }
}

Save and close the file, and then verify whether there are syntax errors in Nginx:

nginx -t

You should see the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Next, restart the Nginx service to apply the changes:

systemctl restart nginx

Next, use the following command to verify the status of the Nginx service:

systemctl status nginx

You should see the following output:

? nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2021-04-14 05:57:03 UTC; 1min 38s ago
     Docs: man:nginx(8)
  Process: 6852 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 6853 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 6854 (nginx)
    Tasks: 3 (limit: 4701)
   Memory: 4.0M
   CGroup: /system.slice/nginx.service
           ??6854 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ??6855 nginx: worker process
           ??6856 nginx: worker process

Apr 14 05:57:03 debian systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 14 05:57:03 debian systemd[1]: Started A high performance web server and a reverse proxy server.

At this point, Nginx has been configured to access Tomcat. You can now proceed to the next step.

Visit Tomcat Web UI

Now, open your web browser and use the URL to access the Tomcat Web UI. https://tomcat.example.com. You should see the following page:

Click on manager application, You should see the following page:

Tomcat login

Provide your Manager App administrator username and password, and click Sign in Button. You should see the following page:

Tomcat dashboard

You can also click Landlord manager Access the host manager application as follows:

Virtual host manager

in conclusion

Congratulations! You have successfully installed Nginx’s Tomcat as a reverse proxy on Debian 10. You can now easily deploy Java applications using Tomcat. If you have any questions, please feel free to ask me.

Source

Related Posts