Setup GeoServer

Let’s install Tomcat and edit the default startup script.

sudo apt install tomcat9 -y
sudo vim /etc/systemd/system/tomcat9.service

Edit the startup script with the following content:

[Unit]
Description=Apache Tomcat Server
After=syslog.target network.target
RequiresMountsFor=/var/log/geoserver /opt/data/geoserver_data /opt/data/geoserver_cache

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/default-java"
Environment="CATALINA_BASE=/var/lib/tomcat9"
Environment="CATALINA_HOME=/usr/share/tomcat9"
Environment="CATALINA_PID=/tmp/tomcat.pid"

# Security
ReadWritePaths=/opt/data/geoserver_data
ReadWritePaths=/var/log/geoserver
ReadWritePaths=/opt/data/geoserver_cache

ExecStart=/usr/share/tomcat9/bin/startup.sh
ExecStop=/usr/share/tomcat9/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

Reload the service

sudo rm -Rf /etc/systemd/system/tomcat9.service.d
sudo systemctl daemon-reload

Create the setenv script as follows

sudo vim /usr/share/tomcat9/bin/setenv.sh

Edit the setenv script with the following content:

GEOSERVER_DATA_DIR="/opt/data/geoserver_data/data"
GEOSERVER_LOG_LOCATION="/var/log/geoserver/geoserver.log"
GEOWEBCACHE_CACHE_DIR="/opt/data/geoserver_cache"
GEOFENCE_DIR="${GEOSERVER_DATA_DIR}/geofence"
TIMEZONE="UTC"
JAVA_OPTS="-server -Xms1024m -Xmx1024m -Djava.awt.headless=true -Dgwc.context.suffix=gwc -Dfile.encoding=UTF8 -Duser.timezone=${TIMEZONE} -Djavax.servlet.request.encoding=UTF-8 -Djavax.servlet.response.encoding=UTF-8 -DGEOSERVER_CSRF_DISABLED=true -DPRINT_BASE_URL=http://localhost:8080/geoserver/pdf -DGEOSERVER_DATA_DIR=${GEOSERVER_DATA_DIR} -DGEOSERVER_LOG_LOCATION=${GEOSERVER_LOG_LOCATION} -Dgeofence.dir=${GEOFENCE_DIR} -DGEOWEBCACHE_CACHE_DIR=${GEOWEBCACHE_CACHE_DIR}"

Make the setenv script executable:

sudo chmod +x /usr/share/tomcat9/bin/setenv.sh

Create the Directory Structure and Install the needed packages

Stop running Tomcat for GeoServer provisioning

sudo systemctl stop tomcat9

Create the GeoServer directories

sudo mkdir -p /opt/data/geoserver_data
sudo mkdir -p /var/log/geoserver
sudo mkdir -p /opt/data/geoserver_cache

Set the directory permissions

sudo chmod 0755 /opt/data/geoserver_data
sudo chmod 2750 /var/log/geoserver
sudo chmod 0750 /opt/data/geoserver_cache

Install the data directory template

cd /opt/data/geoserver_data
sudo wget --no-check-certificate "https://artifacts.geonode.org/geoserver/2.23.0/geonode-geoserver-ext-web-app-data.zip" -O data-2.23.0.zip
sudo unzip data-2.23.0.zip

Set the GeoServer directory ownership

sudo chown -R tomcat: /var/lib/tomcat9
sudo chown -R tomcat: /usr/share/tomcat9
sudo chown -R tomcat:tomcat /opt/data/geoserver_data
sudo chown -R tomcat:adm /var/log/geoserver
sudo chown -R tomcat:tomcat /opt/data/geoserver_cache

Temporary placeholder for downloading the war file

cd /opt && sudo mkdir -p geoserver && cd geoserver

Install the GeoServer war

sudo wget --no-check-certificate "https://artifacts.geonode.org/geoserver/2.23.0/geoserver.war" -O geoserver.war
sudo mv geoserver.war /var/lib/tomcat9/webapps

Update the tomcat shutdown port

sudo vim /var/lib/tomcat9/conf/server.xml
<Server port="8005" shutdown="SHUTDOWN">

Start Tomcat9

sudo systemctl start tomcat9

Check if GeoServer has started by going to http://localhost:8080/geoserver in the browser.

Troubleshooting

If Tomcat is not starting GeoServer due to an error containing this snippet of stacktrace

Caused by: java.lang.IllegalStateException: Unable to complete the scan for annotations for web application [/geoserver] due to a StackOverflowError. 
Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies. 
The class hierarchy being processed was [org.bouncycastle.asn1.ASN1EncodableVector>org.bouncycastle.asn1.DEREncodableVector->org.bouncycastle.asn1.ASN1EncodableVector]

you may need to edit the catalina.properties file:

sudo sed -i -e 's/xom-\*\.jar/xom-\*\.jar,bcprov\*\.jar/g' /var/lib/tomcat9/conf/catalina.properties

and restart Tomcat.