Saturday, May 2, 2020

Setup Apache as Reverse Proxy for Tomcat Server On Ubuntu 20.04 LTS / Linux Mint 19.3


Video Tutorial - https://youtu.be/Ho3AqTvX9fo

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

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 Tomcat Website - http://tomcat.apache.org/
Apache Reverse Proxy Guide - https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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
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

apt-get install apache2 -y
systemctl stop apache2.service ; systemctl start apache2.service ; systemctl enable apache2.service
a2enmod proxy proxy_http

nano /etc/apache2/sites-available/yourdomain.conf
<VirtualHost *:80>
        ServerName yourdomain.com
        ServerAlias www.yourdomain.com
        ServerAdmin webmaster@yourdomain.com
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        ProxyRequests Off
        <Proxy *>
          Order deny,allow
          Allow from all
        </Proxy>
      
        ProxyPass / http://127.0.0.1:8080/
        ProxyPassReverse / http://127.0.0.1:8080/

        <Location />
          Order allow,deny
          Allow from all
        </Location>

   </VirtualHost>

a2dissite 000-default.conf ; a2ensite yourdomain.conf
systemctl restart apache2.service
http://www.yourdomain.com           
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------apt-get install apache2 -y
systemctl stop apache2.service ; systemctl start apache2.service ; systemctl enable apache2.service
a2enmod proxy proxy_http
nano /etc/apache2/sites-available/example.conf
<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        ServerAdmin webmaster@example.com
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        ProxyRequests Off
        <Proxy *>
          Order deny,allow
          Allow from all
        </Proxy>
      
        ProxyPass / http://127.0.0.1:8080/
        ProxyPassReverse / http://127.0.0.1:8080/

        <Location />
          Order allow,deny
          Allow from all
        </Location>

   </VirtualHost>

a2dissite 000-default.conf ; a2ensite example.conf
systemctl restart apache2.service
http://www.example.com           
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1 comment: