Created
June 24, 2020 09:06
-
-
Save CloudLinuxDeveloper/266f99968efce046c5a94996a15231e3 to your computer and use it in GitHub Desktop.
How to install Apache Tomcat on Ubuntu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Tomcat installation on Ubuntu: | |
# ============================== | |
# Install OpenJDK | |
sudo apt install default-jdk | |
# Create Tomcat User and Group (Do run as root). Create a new group and system user to run the Apache Tomcat service from the /opt/tomcat directory. | |
sudo groupadd tomcat | |
sudo useradd -s /bin/false –g tomcat –d /opt/tomcat tomcat | |
# Set directory to download the tomcat | |
cd /tmp | |
# Now download tomcat. | |
curl -O http://apache.mirrors.ionfish.org/tomcat/tomcat-8/v8.5.5/bin/apache-tomcat-8.5.5.tar.gz | |
# To extract the tar.gz Tomcat file, create a new /opt/tomcat/ directory with the command: | |
sudo mkdir /opt/tomcat | |
# Now extract tomcat tarbal using the command | |
sudo tar xzvf apache-tomcat-9.0.34.tar.gz -C /opt/tomcat --strip-components=1 | |
# Modify Tomcat User Permission | |
# ============================= | |
# Move to the directory where the Tomcat installation is located: | |
cd /opt/tomcat | |
# Grant group ownership over the installation directory to the tomcat group with the command: | |
sudo chgrp –R tomcat /opt/tomcat | |
# Give it read access to the conf directory and its contents by typing: | |
sudo chmod –R g+r conf | |
# Followed by changing directory permissions to grant execute access with: | |
sudo chmod g+x conf | |
# Finally, give the tomcat user ownership of the webapps, work, temp, and logs directories using the command: | |
sudo chown -R tomcat webapps/work temp/ logs | |
# Create System Unit File | |
# ====================== | |
# To configure the file, you first need to find the “JAVA_HOME” path. This is the exact location of the Java installation package. | |
sudo update-java-alternatives -l | |
# Create and open a new file in the /etc/system/system under the name tomcat.service: | |
sudo nano /etc/systemd/system/tomcat.service | |
# Once the file opens, copy and paste the content below, changing the JAVA_HOME value to the information you found in the previous step. | |
[Unit] | |
Description=Apache Tomcat Web Application Container | |
After=network.target | |
[Service] | |
Type=forking | |
Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64 | |
Environment=CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid | |
Environment=CATALINA_HOME=/opt/tomcat | |
Environment=CATALINA_BASE=/opt/tomcat | |
Environment=’CATALINA_OPTS=-Xms512M –Xmx1024M –server –XX:+UserParallelGC’ | |
Environment=’JAVA_OPTS=-Djava.awt.headless=true Djava.security.egd=file:/dev/./urandom’ | |
ExecStart=/opt/tomcat/bin/startup.sh | |
ExecStop=/opt/tomcat/bin/shutdown.sh | |
User=tomcat | |
Group=tomcat | |
UMast=0007 | |
RestartSec=10 | |
Restart=always | |
[Install] | |
WantedBy=multi-user.target | |
# Now save it and exit. | |
# For the changes to take place, reload the system daemon with the command: | |
sudo systemctl daemon-reload | |
# Now, you can finally start the Tomcat service: | |
sudo systemctl start tomcat | |
# Verify the Apache Tomcat service is running with the command: | |
sudo systemctl status tomcat | |
# Adjust Firewall | |
# =============== | |
# Open Port 8080 to allow traffic through it with the command: | |
sudo ufw allow 8080/tcp | |
# If the port is open, you should be able to see the Apache Tomcat splash page. Type the following in the browser window: | |
http://server_ip:8080 | |
or | |
http://localhost:8080 | |
sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml | |
sudo nano /opt/tomcat/latest/webapps/host-manager/META-INF/context.xml | |
# To allow access from a specific IP address, add the IP to the previous command, as follows: | |
----------- | |
<Context antiResourceLocking=”false” privileged=”true”> | |
<! -- | |
<Valve className=”org.apache.catalina.valves.RemoteAddrValve” | |
allow=”127\.\d+\.\d+\.\d+|::1|0000:1|THE.IP.ADDRESS.” /> | |
-- > | |
</Context> | |
-------------- | |
http://localhost:8080 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment