It seems hard to find a complete step-by-step tutorial to install Jetty as a service (especially on CentOS), some of the tutorial online is somewhat outdated or does not work, so I will share what I’ve learnt. (painfully) Perhaps this tutorial will also be outdated at some point in the future, so I will state the current version of the software I use clearly.
This tutorial will cover achieving the following on CentOS 6+:
- Able to run jetty server as “jetty” user
- Able to start jetty by “sudo service start jetty”
- Able to start jetty when the system boot
- Able to specify a base directory for jetty to run instead of using JETTY_HOME
Getting started
1. make sure latest java is installed:
1 | $ sudo yum install java |
2. Download Jetty 9.3, go to this download page and download the Stable-9 version, I prefer the .tar.gz
1 2 | $ cd ~ $ wget http://download.eclipse.org/jetty/stable-9/dist/jetty-distribution-9.3.0.v20150612.tar.gz |
Uncompress and move it to desired installation path, some tutorial may suggest you install to /opt/, but I prefer /usr/local,
1 2 | $ tar -xvf jetty-distribution-9.3.0.v20150612.tar.gz $ mv jetty-distribution-9.3.0.v20150612 /usr/local/ |
Optional Symbolic link creation, so you can get a nice and clean “/usr/local/jetty/” path which points to the actual path
1 | $ ln -s /usr/local/jetty-distribution-9.3.0.v20150612 /usr/local/jetty |
Jetty user and base dir
3. create jetty user, assign home directory as /home/jetty (actually home dir is not very important):
1 | $ useradd --user-group --shell /bin/sh --home-dir /home/jetty jetty |
This is important: don’t use the shell /bin/false
(if you did, edit /etc/passwd to change it back)
4. We will use /home/jetty/ as the JETTY_BASE. To init the base dir with initial configs (required) :
1 | $ java -jar /usr/local/jetty/start.jar --add-to-start=deploy,http,logging,jsp |
add any modules you want in the “add-to-start” argument, you can still edit the start.ini in your base dir anytime
Edit Jetty Config
5. We will edit the /etc/defaults/jetty, this is where we config the main paths that Jetty use, this is my recommended config, if you follow my procedure, you can just use this without any changes
1 2 3 4 5 6 7 8 9 10 11 12 | JAVA_HOME=/usr/java/default JAVA=$JAVA_HOME/bin/java JAVA_OPTIONS=" -server -Xms256m -Xmx1024m -XX:+DisableExplicitGC " JETTY_HOME=/usr/local/jetty JETTY_BASE=/home/jetty JETTY_USER=jetty JETTY_RUN=/home/jetty/run JETTY_PORT=8080 JETTY_HOST=0.0.0.0 JETTY_LOGS=/home/jetty/logs TMPDIR=/home/jetty/temp |
6. Create the directories manually
1 2 3 4 | $ mkdir -p /home/jetty/tmp $ mkdir -p /home/jetty/run $ mkdir -p /home/jetty/logs $ sudo chown -R jetty:jetty /home/jetty |
Also make sure jetty home is accessible by jetty user
1 | $ sudo chown -R jetty:jetty /usr/local/jetty |
Install Jetty as Service
7. add jetty.sh to /etc/init.d, create a symbolic link to the /etc/init.d directory by:
1 | $ ln -s /usr/local/jetty/bin/jetty.sh /etc/init.d/jetty |
8. Verify the config:
1 | $ service jetty check |
9. Start jetty:
1 | $ sudo service jetty start |
10. Launch jetty at startup
1 | $ chkconfig jetty on |
Hope this tutorial helps!
Troubleshooting
Still getting FAILED or cannot start jetty? you may have to dig into the jetty.sh script, try to run the actual command that it use.
Usually it’s one of the following problem:
- Permission of the log path / pid file path not writable by “jetty” user
- jetty user is not created or something wrong, try to run
su - jetty -c "whoami" , it should output jetty. If it just login to root shell, you may have set the shell of “jetty” to /bin/false
1 reply on “Install Jetty as a service on CentOS”
By the way, path to temp in default/jetty should be TMPDIR=/home/jetty/tmp.. or mkdir -p /home/jetty/temp, either way ;).. Just got some weird errors when it was not pointing to the existing folder.