In this article, I will demonstrate how to spin up an EC2 instance on the AWS cloud and install the Tomcat server. I hope you already have an AWS cloud account.
Launch an EC2 instance
Login to your AWS account and search for EC2 under the AWS services search bar.
Click EC2 to proceed. On the next window click on “Launch Instance” to provision a new instance.
On the next page, we have to select an Amazon Machine Image(AMI). AMI is a template which used to launch a VM. The template may contain only the Operating system or we can create a custom template. Here I have chosen “Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type” to install the Tomcat server.
On the next page, we have to select the Instance type ( How much CPU and memory required). I have selected “t2.micro” because it is a free tier eligible. You can select a higher configuration based on your requirements. t2.micro only has 1vCPU and 1G memory.
The next page is for “Configure Instance”. Here we can add the number of instances, VPC, and IP address. I have chosen all are default values (Default VPC and IP address).
Click “Next:add storage” button to set a volume size (Disk size) for the VM. I have used 10G, you can increase the value based on your requirement.
Click the “Add tag” button to add a tag for the instance. I created a name tag and set name as Tomcat-Server.
Next is to configure a security group or firewall for the instance. Add a name and description for your security group. Refer to the below screenshot for more details. By default port, 22 rule will be present for ssh service and additionally, I have added port 8080 for tomcat service.
Click the “Review and Launch” button to review all the details that you have given. If you want to modify any values you can come back and do the changes. If no changes required, click on the “Launch” button to provision the instance. On a pop-up window, it will ask you to set up a private key to log in to the server. Select create a new key pair and set a name for it. Click the download button to save it in your local machine.
Click the Launch button to start provision the instance. On the bottom right corner, you can see a button to view the instance, click on it to view the instance.
It may take a few minutes to complete the instance initialization. In the same dashboard, you can see all the information about your servers like public and private IP addresses, and DNS names, Root device names, etc.
Now we have provisioned a VM to install the Tomcat server.
Tomcat server installation
Log in to the server with the help of the key pair generated at the time of launching. Execute below command from the terminal to log in.
$ chmod 400 aws-tomcat-key.pem $ ssh -i aws-tomcat-key.pem email@example.com
The first command is to set read-only permission for private key and second command is to log in. Replace the key name and public IP address accordingly.
Now, download the tomcat server package from the below link
Here, we are installing Tomcat 8.5.49 version. Right-click on the tar.gz link and copy the link address.
[root@ip-172-31-81-46 ~]# hostname tomcat-server [root@ip-172-31-81-46 ~]# logout [ec2-user@ip-172-31-81-46 ~]$ sudo su - Last login: Sat Nov 30 07:19:24 UTC 2019 on pts/0 [root@tomcat-server ~]# cd /opt/ # wget https://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.49/bin/apache-tomcat-8.5.49.tar.gz
The hostname command is to set a hostname for the server. I have downloaded the package on “/opt” directory.
untar the downloaded package and rename it.
# tar -xvzf apache-tomcat-8.5.49.tar.gz # mv apache-tomcat-8.5.49 tomcat
To start the tomcat service enter the bin folder and execute the below commands.
# cd bin/ [root@tomcat-server bin]# ./startup.sh Using CATALINA_BASE: /opt/tomcat Using CATALINA_HOME: /opt/tomcat Using CATALINA_TMPDIR: /opt/tomcat/temp Using JRE_HOME: /usr/lib/jvm/jre Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar Tomcat started.
From the above output, we can confirm the Tomcat has started successfully. To access the service open a browser and enter http://<ip address>:8080 [ use your server IP address ]
You are not able to access the “Manager app” because by default Tomcat allows accessing only from the localhost. If you try to access you may get below error.
Tomcat server configuration
As mentioned in the error to resolve this issue we have to make changes on context.xml file. Go back to the Tomcat installation directory and search for context.xml file.
[root@tomcat-server tomcat]# find ./ -name context.xml ./webapps/manager/META-INF/context.xml ./webapps/host-manager/META-INF/context.xml ./conf/context.xml [root@tomcat-server tomcat]#
We have to make changes only on context.xml file which is present in the webapps directory.
Open the following files and comment out the following lines.
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
Here you can see the configuration allows only localhost (127.0.0.1) to access the “Manager app” and we have commented it. If we make any change on configuration files we must restart the service to pick new values. To restart the service execute below command from “bin” directory.
[root@tomcat-server bin]# ./shutdown.sh Using CATALINA_BASE: /opt/tomcat Using CATALINA_HOME: /opt/tomcat Using CATALINA_TMPDIR: /opt/tomcat/temp Using JRE_HOME: /usr/lib/jvm/jre Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar [root@tomcat-server bin]# ./startup.sh Using CATALINA_BASE: /opt/tomcat Using CATALINA_HOME: /opt/tomcat Using CATALINA_TMPDIR: /opt/tomcat/temp Using JRE_HOME: /usr/lib/jvm/jre Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar Tomcat started.
Go the browser again and try to open the “Manager app”
This time it will not show the Access denied error but a popup window will open to enter user credentials.
We haven’t configured any user so go to the terminal and create users. Open the tomcat-users.xml file and add the below codes to create the users.
<role rolename="manager-gui"/> <role rolename="manager-script"/> <role rolename="manager-jmx"/> <role rolename="manager-status"/> <user username="admin" password="admin" roles="manager-gui, manager-script,manager-jmx, manager-status"/> <user username="dev" password="DevXYZ" roles="manager-script"/> <user username="tomcat" password="PassCode!" roles="manager-gui"/>
The code should be added just above the “</tomcat-users>”.
We added 3 users one is admin which access to all roles like manager-gui (Manager app login). and manager-script, etc. The second user is “dev” which has a role “manager-script” this role helps to copy code from other from server or source. The Third is a “tomcat” user which used to login to the “Manager app” from the GUI.
Restart the service to pick up new configuration changes. Execute below command from the bin directory.
[root@tomcat-server bin]# ./shutdown.sh [root@tomcat-server bin]# ./startup.sh
Go to the browser and log in with the user credentials that we created. I used “tomcat” user and you can see a screen like the below screenshot.
yes, we have completed the installation and configuration Tomcat server.