top of page

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



ree


[1] Install and configure Nginx


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 Nginx on ubuntu

   sudo apt-get install nginx

  • enable and start the nginx service

   sudo systemctl enable nginx
   sudo systemctl start nginx

  • check the status of nginx service

   sudo systemctl status nginx
   nginx -t

  • put the public ip in browser

ree



[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/nginx/sites-enabled:

   cd /etc/nginx/sites-enabled

  • Create a tomcat.conf file using the nano text editor:

    nano tomcat.conf 

  • In the tomcat.conf file, insert the following configuration:

server {
    listen 80;
    #server_name server_ip;
    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/;
    }
}
  • note: Make sure to uncomment the server_name line and replace server_ip with the actual server's IP address if you intend to use a specific server name.


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

   nginx -t

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

   sudo systemctl reload nginx

  • This will activate the Nginx configuration changes.


Comments


©2023 by CloudOpsSolution

bottom of page