Prepare AWS EC2 Ubuntu instance for running WAR archives on Apache Tomcat server with MySQL database
Installing JAVA
sudo apt install openjdk-17-jre-headless
java -version
Setting JAVA_HOME environmental variable
- Check the list of all installed java versions and copy the desired path.
sudo update-alternatives --config java
Sample output:
/usr/lib/jvm/java-17-openjdk-amd64/bin/java
2. Open environment file
sudo nano /etc/environment
3. Add the Java path to end of the file
Remove the /bin/java from the path copied in step 1.
JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
4. Reload the changes
source /etc/environment
5. Verify the installation
echo $JAVA_HOME
java -version
Installing Tomcat
- Create an unprivileged
tomcat
user
sudo useradd -m -d /opt/tomcat -U -s /bin/false tomcat
2. Download tomcat archive in /tmp
directory
cd /tmp
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.69/bin/apache-tomcat-9.0.69.tar.gz
3. Extract the downloaded archive to opt/tomcat
directory
sudo tar xzvf apache-tomcat-9*tar.gz -C /opt/tomcat --strip-components=1
4. Grant ownership to tomcat user
sudo chown -R tomcat:tomcat /opt/tomcat/
sudo chmod -R u+x /opt/tomcat/bin
5. Create tomcat `admin` and `manager` users
sudo nano /opt/tomcat/conf/tomcat-users.xml
add below lines before the ending tag </ — ->
<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" />
6. Create systemd
service to easily manage tomcat service
create or open tomcat.service
file:
sudo nano /etc/systemd/system/tomcat.service
add below code, and modify the JAVA_HOME and CATALINA_HOME path if different
[Unit]
Description=Tomcat
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-1.17.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
save file content and exit.
7. Reload daemon
sudo systemctl daemon-reload
8. Start the tomcat service
sudo systemctl start tomcat
9. Enable tomcat service to auto-start on system start
sudo systemctl enable tomcat
Installing MySQL
- Update apt packages
sudo apt update
2. Installing mysql server
sudo apt install mysql-server
Once installing is complete, check server status
systemctl status mysql.service
3. Setting MySQL password
sudo mysql
run below sql script, (creates an user named root
with given password):
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'PASSWORD';
exit;