Skip to content

Instantly share code, notes, and snippets.

@isapir
Last active December 16, 2022 07:05
Show Gist options
  • Save isapir/58bf7a59b57888639a7bd63d2328aa57 to your computer and use it in GitHub Desktop.
Save isapir/58bf7a59b57888639a7bd63d2328aa57 to your computer and use it in GitHub Desktop.
Setup Tomcat to run as a non-Root user on Ubuntu
USER_ID=1000
TOMCAT_VERSION=9.0.70
TOMCAT_FILENAME=apache-tomcat-${TOMCAT_VERSION}
SETUP_DIR=/var/local/tomcat
mkdir -p ${SETUP_DIR} && cd ${SETUP_DIR}
wget https://dlcdn.apache.org/tomcat/tomcat-9/v${TOMCAT_VERSION}/bin/${TOMCAT_FILENAME}.tar.gz
tar -xvf ${TOMCAT_FILENAME}.tar.gz
cd ${TOMCAT_FILENAME}
## create uid=1000(tomcat) gid=1000(tomcat)
useradd --uid ${USER_ID} --user-group --shell /bin/bash tomcat
## set group and group permissions to directories
chgrp -R tomcat .
chmod -R g+rX .
chmod -R g+w logs temp work
export CATALINA_HOME=${SETUP_DIR}/${TOMCAT_FILENAME}
export CATALINA_BASE=/srv/www/catalina-base
mkdir -p ${CATALINA_BASE}
chgrp -R tomcat ${CATALINA_BASE}
chmod -R g+srwX ${CATALINA_BASE}
su tomcat
bin/makebase.sh ${CATALINA_BASE}
echo "Hello Tomcat" > ${CATALINA_BASE}/webapps/ROOT/index.html
bin/catalina.sh run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment