Saturday, May 2, 2020

installing Tomcat with Nginx As a Reverse Proxy On Ubuntu 20.04 LTS / Linux Mint 19.3 Tricia

Video Tutorial - https://youtu.be/4LVA6emT9zI

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Apache Tomcat a free and open-source web server used for hosting the Java-based web application. It is one of the most widely used web server that enables you to run Java files easily.
Offcial Apache Tomcat Website - http://tomcat.apache.org/

Nginx is a high performance reverse proxy server and web server.
Proxying is typically used to distribute the load among several servers, seamlessly show content from different websites, or pass requests for processing to application servers over protocols other than HTTP.
Offcial Nginx Website - https://www.nginx.com/
NGINX Reverse Proxy - https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Os Details-
Ubuntu 20.04 LTS (focal fossa)|| hostname - www.yourdomain.com || java -version -openjdk version "11.0.7
Linux Mint 19.3 Tricia  || Hostname - www.example.com | java -version -openjdk version "11.0.7
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
lsb_release -cd 
apt install -y build-essential net-tools curl git software-properties-common nginx
groupadd tomcat ; useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Tomcat Download Page - https://tomcat.apache.org/download-90.cgi
cd /opt/ ; wget http://apachemirror.wuchna.com/tomcat/tomcat-9/v9.0.34/bin/apache-tomcat-9.0.34.tar.gz
tar -xzvf apache-tomcat-9.0.34.tar.gz ; mv apache-tomcat-9.0.34/ tomcat/
chown -R tomcat:tomcat /opt/tomcat ; chmod +x /opt/tomcat/bin/*

nano ~/.bashrc
export CATALINA_HOME=/opt/tomcat
source ~/.bashrc
echo $CATALINA_HOME ; $CATALINA_HOME/bin/startup.sh

$CATALINA_HOME/bin/shutdown.sh ; chown -hR tomcat:tomcat /opt/tomcat/
nano /etc/systemd/system/apache-tomcat.service
[Unit]
Description=Apache Tomcat 9 Servlet Container
After=syslog.target network.target

[Service]
User=tomcat
Group=tomcat
Type=forking
Environment=CATALINA_PID=/opt/tomcat/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target
systemctl daemon-reload ; systemctl start apache-tomcat ; systemctl enable apache-tomcat ; systemctl status apache-tomcat

nano /opt/tomcat/conf/tomcat-users.xml
<tomcat-users>
<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
<user username="admin" password="admin" roles="admin-gui,manager-gui"/>
</tomcat-users>

User - admin || Password - admin

nano /opt/tomcat/webapps/manager/META-INF/context.xml
<!-- -->
nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
<!-- -->
systemctl restart apache-tomcat

nano /etc/nginx/sites-available/tomcat.conf
server {
  listen 80;

  server_name   www.yourdomain.com;
  access_log /var/log/nginx/tomcat-access.log;
  error_log /var/log/nginx/tomcat-error.log;

  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 http://127.0.0.1:8080/;
  }
}
ln -s /etc/nginx/sites-available/tomcat.conf /etc/nginx/sites-enabled/
systemctl restart nginx

http://www.yourdomain.com            
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
nano /etc/nginx/sites-available/tomcat.conf
server {
  listen 80;

  server_name   www.example.com;
  access_log /var/log/nginx/tomcat-access.log;
  error_log /var/log/nginx/tomcat-error.log;

  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 http://127.0.0.1:8080/;
  }
}

ln -s /etc/nginx/sites-available/tomcat.conf /etc/nginx/sites-enabled/
systemctl restart nginx
http://www.example.com

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment