Tomcat 8.0.9 and systemd on Fedora#
In fedora you can yum install tomcat (even the "native"
version I believe).
However, I find the fedora/linux way of installing and
linking things all over the place very confusing.
Since tomcat is a fairly small, nice and autonomous
piece of software, I'd rather install it myself,
in one place, thank you very much.
("Installing" is simply a matter of downloading the binaries,
extract that downloaded .zip/.gz somewhere,
do <somewhere>/bin/startup.sh and you should be good to go)
However, after manually installing tomcat 8; how do you add
it to the usual automagic startup, and how do you
start/stop it using (what seems to be) the
standard systemd scheme?
It shouldn't be very hard, should it? Well..
As usual, most examples and howtos are broken/old/useless.
systemd/systemctl documentation is, to say the
least; cryptic, unfocused, downright unhelpful and... well,
pretty much useless, at least to me!
Setup#
jsvc#
First, you need the Apache Commons daemon: "jsvc".I dunno what it is, but I think it makes your life easier.
It can be yum'med and installed easily in fedora
[geirru@urd ~]$ sudo yum -y install jsvc(Supposedly, it is also possible to download and build this daemon thing yourself, the Tomcat people even claim it to be part of the Tomcat binary download, but I have no idea where/how... no, I didn't even look)
The program would normally be installed/linked as /usr/bin/jsvc
Tomcat#
I created a user, tomcat, and installed Tomcat 8.0.9
in the users home folder.
Of course, you can install tomcat where-ever you like,
and run it as root if you like, I don't care!
tomcat user#
I prefer to create a "tomcat" user to run the tomcat webserver[geirru@urd ~]$ sudo useradd tomcat [geirru@urd ~]$ sudo usermod -G apache tomcat(The second line add the tomcat user to the "apache" group... just because I like living on the edge.)
Tomcat 8.0.x#
Switch to the tomcat user, and download the tomcat binary from apache
,
or whereever you can get your hands on the binaries[geirru@urd ~]$ sudo su - tomcat [tomcat@urd ~]$ wget http://apache.mirrors.spacedump.net/tomcat/tomcat-8/v8.0.9/bin/apache-tomcat-8.0.9.tar.gz
--2014-08-23 17:46:06-- http://apache.mirrors.spacedump.net/tomcat/tomcat-8/v8.0.9/bin/apache-tomcat-8.0.9.tar.gz Resolving apache.mirrors.spacedump.net (apache.mirrors.spacedump.net)... 212.63.222.76 Connecting to apache.mirrors.spacedump.net (apache.mirrors.spacedump.net)|212.63.222.76|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 9144234 (8.7M) [application/x-gzip] Saving to: "apache-tomcat-8.0.9.tar.gz" 100%[=================================================================================================================================>] 9,144,234 2.27MB/s in 3.9s 2014-08-23 17:46:10 (2.25 MB/s) - "apache-tomcat-8.0.9.tar.gz" saved [9144234/9144234]
Extract the download bundle
[tomcat@urd ~]$ tar -xvzf apache-tomcat-8.0.9.tar.gz
apache-tomcat-8.0.9/bin/catalina.sh apache-tomcat-8.0.9/bin/configtest.sh apache-tomcat-8.0.9/bin/daemon.sh apache-tomcat-8.0.9/bin/digest.sh apache-tomcat-8.0.9/bin/setclasspath.sh apache-tomcat-8.0.9/bin/shutdown.sh apache-tomcat-8.0.9/bin/startup.sh apache-tomcat-8.0.9/bin/tool-wrapper.sh apache-tomcat-8.0.9/bin/version.sh apache-tomcat-8.0.9/conf/ apache-tomcat-8.0.9/conf/catalina.policy <snip> ... lots of files..
Since I'm lazy, I created a symbolic link, naming it "tomcat" to spare me some typing (and to be able to switch the tomcat installation without too much hazzle)
[tomcat@urd ~]$ ln -s apache-tomcat-8.0.9 tomcat [tomcat@urd ~]$ ls -la
[tomcat@urd ~]$ ls -la total 64696 drwx------ 5 tomcat tomcat 4096 Aug 23 17:50 . drwxr-xr-x 10 root root 4096 Aug 23 16:01 .. drwxrwxr-x 9 tomcat tomcat 4096 Aug 23 17:50 apache-tomcat-8.0.9 -rw-rw-r-- 1 tomcat tomcat 9144234 Jun 19 15:24 apache-tomcat-8.0.9.tar.gz lrwxrwxrwx 1 tomcat tomcat 19 Aug 23 17:50 tomcat -> apache-tomcat-8.0.9
I also like changing the default 8080 port number that tomcat uses (as it may clash with other stuff).
To do so, just alter the port number on the line in tomcat/conf/server.xml that reads
<Connector port="8080" protocol="HTTP/1.1" ....to some other port number you want, for example 8090:
<Connector port="8090" protocol="HTTP/1.1" ....
That concludes the tomcat user and installation.
That wasn't so hard, was it?
systemd#
Next, the real point of all this; setting up tomcat as a systemd service.
First we need to create a "service" file for tomcat that
systemd can use to do its magic (that is; starting and
stopping the thing)
This service file should be stuffed in /usr/lib/systemd/system/
for reasons unknown, and since I have no imagination, I named
it tomcat.service.
It should look something like the following
[Unit] Description=Tomcat webserver After=syslog.target network.target [Service] Type=forking User=tomcat EnvironmentFile=/etc/sysconfig/tomcat ExecStart=/home/tomcat/tomcat/bin/daemon.sh start ExecStop=/home/tomcat/tomcat/bin/daemon.sh stop [Install] WantedBy=multi-user.target
(The daemon.sh script is part of the tomcat installation, take a look at it if you are curious)
The properties file referenced (EnvironmentFile) should be created in /etc/sysconfig/ (or possibly where-ever), and I unimaginatively named it tomcat
This is what I finally ended up with, change as appropriate for your setup
CATALINA_HOME=/home/tomcat/tomcat JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.65-2.5.1.2.fc19.x86_64 JSVC=/usr/bin/jsvc(The jsvc daemon program is called from the tomcat/bin/daemon.sh script, the JSVC=.. reference seems to be required, so that systemd can find the bastard, and the regular path is mysteriously gone when doing systemd stuff)
(I also suppose the hardcoded path to /home/tomcat/tomcat/ in the tomcat.service file could be replaced by this CATALINA_HOME property, but after a short browse of the systemd docs, I gave up without trying anything to avoid the typical linux-docs-and-frothing-at-the-mouth syndrome)
Enable the tomcat service
[geirru@urd ~]$ sudo systemctl enable tomcat.service
The systemctl command above should have created a symbolic link
in /etc/systemd/system/multi-user.target.wants/,
tothe tomcat.service in /usr/lib/systemd/system/
However, I was unable to make this work. Of course.
To manually create this soft link, I did
[geirru@urd ~]$ sudo ln -s '/usr/lib/systemd/system/tomcat.service' '/etc/systemd/system/multi-user.target.wants/tomcat.service'
After the tomcat.service has been enabled, we probably want to start it.
To start the tomcat service, do
[geirru@urd ~]$ sudo systemctl start tomcat.service
And to verify that it worked
[geirru@urd ~]$ systemctl status tomcat.service
tomcat.service - Tomcat webserver, on port 8090
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled)
Active: active (running) since Sat 2014-08-23 17:12:33 CEST; 7min ago
Process: 14808 ExecStart=/home/tomcat/tomcat/bin/daemon.sh start (code=exited, status=0/SUCCESS)
Main PID: 14816 (jsvc)
CGroup: name=systemd:/system/tomcat.service
14816 jsvc.exec -java-home /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.65-2.5.1.2.fc19.x86_64 -user tomcat -pidfile /home/tomcat/tomcat/logs/catalina-daemon.pid ...
14817 jsvc.exec -java-home /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.65-2.5.1.2.fc19.x86_64 -user tomcat -pidfile /home/tomcat/tomcat/logs/catalina-daemon.pid ...
Aug 23 17:12:18 urd systemd[1]: Starting Tomcat webserver, on port 8090...
Aug 23 17:12:33 urd systemd[1]: Started Tomcat webserver, on port 8090.
An even better verification is, of course, to see that something gets displayed in a web browser (using the correct address and port number and so forth).
If the web page does not come up, check any firewall settings that may be stopping traffic (tomcat uses 8080 by default).
Also check /var/log/messages and <your-tomcat-installation>/logs/daemon.out for any obvious wrongdoings.
And if you have the selinux abomination running... blame yourself!
Good luck, and good night!
