Hooking Solr with Systemd on Ubuntu 18.04

We have begun the migration of Apache Solr from Ubuntu 14.04 to 18.04. Although we will continue to use Solr on 18.04 in an almost identical configuration as we do on 14.04, one critical change we must perform is to move from the old-style “init” service management to the new “systemd” service management suite of tools (although systemd has been around since 2014, I still consider it a new pattern for system service management; a good reference for systemd is Redhat’s online documentation). This process has taken a bit of Googling and a bit of experimenting, but I beleive the following systemd service block, reworked from Hossam Hammady, works pretty well:

[Unit]
Description=Apache SOLR
After=syslog.target network.target
Before=multi-user.target
Conflicts=shutdown.target

[Service]
User=<USER>
Group=<USER>
PIDFile=/<ROOT_PATH>/solr/bin/solr-8983.pid
ExecStart=/<ROOT_PATH>/solr/bin/solr start -noprompt -s /<ROOT_PATH>/solr/example/my_solr
ExecStop=/<ROOT_PATH>/solr/bin/solr stop -all
LimitNOFILE=65000
LimitNPROC=65000

[Install]
WantedBy=multi-user.target

After creating the “systemd” service block, save it into the directory /etc/systemd/system using a file name like solr.service and then enable it for automated startup using systemctl enable solr.service. Unlike the old “init” service management, “systemd” begins the service based on the parameters After and Before, which simply translates to “do not start until after the services syslog.target and network.target are started, but before multi-user.target begins”.

Endnote…

I thought for sure that it best to use a standard DEB distribution of Apache Solr to get the latest and greatest stable release, so I went about installing the necessary packages. The first set included solr-jetty, which never seemed to bring Solr up. On the second attempt, I used solr-tomcat, which did work. Finally bringing up the Solr admin page, I almost fell out of my seat – the Solr version was 3.6.2. At 4.3, the version of Solr we are now running is more modern than what is available on Ubuntu 18.04 - go figure. Note, we have since upgraded to Solr 8.3 in Spring 2020.