top of page

"Install and configure Apache2 server with reverse proxy and tomcat application server in ubuntu"



ree


[1] Install and configure Apache2


step 1:

  • Launch AWS EC2 instance and connect through the SSH in your terminal !


step 2:

  • Update your Ec2 instance with this command

   sudo apt-get update	

step 3:

  • Install apache2 on ubuntu

   sudo apt-get install apache2

  • enable and start the apache2 service

   sudo systemctl enable apache2
   sudo systemctl start apache2

  • check the status of apache2 service

   sudo systemctl status apache2

  • Adjusting Firewall

sudo ufw app list
sudo ufw allow 'Apache'
sudo ufw status





[2] Install and configure Tomcat 10



Step 1: Installing Tomcat


  • Create a separate user and set appropriate permissions for it:

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

  • Update and upgrade the package list:

   sudo apt update
   sudo apt upgrade

  • Install the default JDK (Java Development Kit):

   sudo apt install default-jdk

  • Navigate to the temporary directory:

   cd /tmp

  • Download the Apache Tomcat archive using wget:

  • note: in some case this link is not work then download from this link:

  • Index of /tomcat (apache.org)

   wget https://dlcdn.apache.org/tomcat/tomcat- 10/v10.1.13/bin/apache-tomcat-10.1.13.tar.gz

  • Extract the downloaded archive to the /opt/tomcat directory:

   sudo tar xzvf apache-tomcat-10*tar.gz -C /opt/tomcat --strip-components=1

  • Set ownership and execute permissions for the Tomcat installation directory:

   sudo chown -R tomcat:tomcat /opt/tomcat/
   sudo chmod -R u+x /opt/tomcat/bin

Step 2: Configure Admin Users


  • Edit the tomcat-users.xml file to define Tomcat users:

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

  • Add the following lines within the <tomcat-users> section:

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

  • Remove the restriction for the Manager page by editing its configuration file:

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

  • Comment out the <Valve> definition within the <Context> tag, like this:

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

  • Repeat the same process for the Host Manager:

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

Step 3: Creating a systemd Service


  • Find the Java location:

   sudo update-java-alternatives -l

  • Create the tomcat.service file for systemd:

   sudo nano /etc/systemd/system/tomcat.service

  • Add the following content:

[Unit]
Description=Tomcat
After=network.target

[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
Environment="CATALINA_BASE=/opt/tomcat"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

  • Reload the systemd daemon to recognize the new service:

   sudo systemctl daemon-reload

  • Start the Tomcat service and check its status:

   sudo systemctl start tomcat
   sudo systemctl status tomcat
  • Enable Tomcat to start with the system:

   sudo systemctl enable tomcat

Step 4: Accessing the Web Interface


  • Allow traffic on port 8080 for Tomcat:

   sudo ufw allow 8080


[3] configure as a reverse proxy



  • Navigate to the directory /etc/apache2/sites-enabled:

   cd /etc/nginx/sites-enabled

  • Open 000-default.conf file using the nano text editor:

    nano 000-default.conf 

  • In the 000-default.conf file, insert the following configuration:


    # CustomLog ${APACHE_LOG_DIR}/access.log combined
    #put this two line in that file 
         ProxyPass / http://127.0.0.1:8080/
         ServerName loaclhost

  • To check the apache2 configuration for any syntax errors, run the following command:

   systemctl status apache2

  • Once you've created the 000-default.conf file and confirmed that the apache2 configuration is error-free, you can proceed to reload apache2 to apply the new configuration:

   sudo systemctl reload apache2

  • This will activate the apache2 configuration changes.


 
 
 

Comments


©2023 by CloudOpsSolution

bottom of page