Author Photo

Being a Linux Enthusiast and a Fan of Open Source Software, I created Ahmer’s SysAdmin Recipes blog to share my experiences with other System Administrators. The curiosity doesn’t let me stop; therefore, I am constantly trying to learn new software tools and techniques. Whereas I also have the passion and the patience to teach the others.

I am always ready to accept new challenges and wish to explore the areas, that I haven’t worked on. Please give me your feedback and help me improve this blog.

Thursday, 16 May 2019

Install Jetty 9 Web Server on CentOS 7

Install Jetty 9 Web Server on CentOS 7Jetty is a Java HTTP web server and Java servlet container. Jetty is a free and open source project developed and maintained by Eclipse Foundation. Jetty is used in many open source projects and products. Jetty can be easily embedded in devices, tools, frameworks, application servers and clusters.

In this article, we will install Jetty 9 web server on CentOS 7 and then deploy a Java web application on it.

 

System Specification:

We have provisioned a CentOS 7 virtual machine with following specification, for Jetty 9 installation.

Hostname: jetty-01.example.com
IP Address: 192.168.116.166/24
Operating System: CentOS 7.6
Jetty version: Jetty 9.4

 

Installing OpenJDK 8 on CentOS 7:

Connect with jetty-01.example.com using ssh as root user.

Jetty requires JVM to execute Java web applications. Therefore we are installing OpenJDK using yum command.

[root@jetty-01 ~]# yum install -y java-1.8.0-openjdk ... Installed: java-1.8.0-openjdk.x86_64 1:1.8.0.212.b04-0.el7_6 Dependency Installed: atk.x86_64 0:2.28.1-1.el7 avahi-libs.x86_64 0:0.6.31-19.el7 cairo.x86_64 0:1.15.12-3.el7 copy-jdk-configs.noarch 0:3.3-10.el7_5 cups-libs.x86_64 1:1.6.3-35.el7 dejavu-fonts-common.noarch 0:2.33-6.el7 dejavu-sans-fonts.noarch 0:2.33-6.el7 fontconfig.x86_64 0:2.13.0-4.3.el7 fontpackages-filesystem.noarch 0:1.44-8.el7 fribidi.x86_64 0:1.0.2-1.el7 gdk-pixbuf2.x86_64 0:2.36.12-3.el7 giflib.x86_64 0:4.1.6-9.el7 graphite2.x86_64 0:1.3.10-1.el7_3 gtk-update-icon-cache.x86_64 0:3.22.30-3.el7 gtk2.x86_64 0:2.24.31-1.el7 harfbuzz.x86_64 0:1.7.5-2.el7 hicolor-icon-theme.noarch 0:0.12-7.el7 jasper-libs.x86_64 0:1.900.1-33.el7 java-1.8.0-openjdk-headless.x86_64 1:1.8.0.212.b04-0.el7_6 javapackages-tools.noarch 0:3.4.1-11.el7 jbigkit-libs.x86_64 0:2.0-11.el7 libICE.x86_64 0:1.0.9-9.el7 libSM.x86_64 0:1.2.2-2.el7 libX11.x86_64 0:1.6.5-2.el7 libX11-common.noarch 0:1.6.5-2.el7 libXau.x86_64 0:1.0.8-2.1.el7 libXcomposite.x86_64 0:0.4.4-4.1.el7 libXcursor.x86_64 0:1.1.15-1.el7 libXdamage.x86_64 0:1.1.4-4.1.el7 libXext.x86_64 0:1.3.3-3.el7 libXfixes.x86_64 0:5.0.3-1.el7 libXft.x86_64 0:2.3.2-2.el7 libXi.x86_64 0:1.7.9-1.el7 libXinerama.x86_64 0:1.1.3-2.1.el7 libXrandr.x86_64 0:1.5.1-2.el7 libXrender.x86_64 0:0.9.10-1.el7 libXtst.x86_64 0:1.2.3-1.el7 libXxf86vm.x86_64 0:1.1.4-1.el7 libfontenc.x86_64 0:1.1.3-3.el7 libglvnd.x86_64 1:1.0.1-0.8.git5baa1e5.el7 libglvnd-egl.x86_64 1:1.0.1-0.8.git5baa1e5.el7 libglvnd-glx.x86_64 1:1.0.1-0.8.git5baa1e5.el7 libjpeg-turbo.x86_64 0:1.2.90-6.el7 libthai.x86_64 0:0.1.14-9.el7 libtiff.x86_64 0:4.0.3-27.el7_3 libwayland-client.x86_64 0:1.15.0-1.el7 libwayland-server.x86_64 0:1.15.0-1.el7 libxcb.x86_64 0:1.13-1.el7 libxshmfence.x86_64 0:1.2-1.el7 lksctp-tools.x86_64 0:1.0.17-2.el7 mesa-libEGL.x86_64 0:18.0.5-4.el7_6 mesa-libGL.x86_64 0:18.0.5-4.el7_6 mesa-libgbm.x86_64 0:18.0.5-4.el7_6 mesa-libglapi.x86_64 0:18.0.5-4.el7_6 pango.x86_64 0:1.42.4-2.el7_6 pcsc-lite-libs.x86_64 0:1.8.8-8.el7 pixman.x86_64 0:0.34.0-1.el7 python-javapackages.noarch 0:3.4.1-11.el7 python-lxml.x86_64 0:3.2.1-4.el7 ttmkfdir.x86_64 0:3.0.9-42.el7 tzdata-java.noarch 0:2019a-1.el7 xorg-x11-font-utils.x86_64 1:7.5-21.el7 xorg-x11-fonts-Type1.noarch 0:7.5-9.el7 Complete!

Set Java environment variables.

[root@jetty-01 ~]# echo "export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64" >> /etc/profile [root@jetty-01 ~]# . /etc/profile [root@jetty-01 ~]# env | grep JAVA_HOME JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64

Ensure Java installation by checking the version.

[root@jetty-01 ~]# java -version openjdk version "1.8.0_212" OpenJDK Runtime Environment (build 1.8.0_212-b04) OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode)

OpenJDK has been installed on CentOS 7.

 

Installing Jetty 9 Web Server on CentOS 7:

Create a user to own Jetty software.

[root@jetty-01 ~]# useradd jetty

Download latest version of Jetty from Eclipse website. Earlier releases can be download from Maven Central.

[root@jetty-01 ~]# cd /tmp [root@jetty-01 tmp]# curl -O https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-distribution/9.4.18.v20190429/jetty-distribution-9.4.18.v20190429.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 18.4M 100 18.4M 0 0 95411 0 0:03:22 0:03:22 --:--:-- 110k

Extract downloaded file to /opt directory.

[root@jetty-01 tmp]# tar zxvf jetty-distribution-9.4.18.v20190429.tar.gz -C /opt/ ... jetty-distribution-9.4.18.v20190429/modules/transactions.mod jetty-distribution-9.4.18.v20190429/modules/unixsocket-forwarded.mod jetty-distribution-9.4.18.v20190429/modules/unixsocket-http.mod jetty-distribution-9.4.18.v20190429/modules/unixsocket-http2c.mod jetty-distribution-9.4.18.v20190429/modules/unixsocket-proxy-protocol.mod jetty-distribution-9.4.18.v20190429/modules/unixsocket-secure.mod jetty-distribution-9.4.18.v20190429/modules/unixsocket.mod jetty-distribution-9.4.18.v20190429/modules/webapp.mod jetty-distribution-9.4.18.v20190429/modules/websocket.mod jetty-distribution-9.4.18.v20190429/notice.html jetty-distribution-9.4.18.v20190429/README.TXT jetty-distribution-9.4.18.v20190429/start.ini jetty-distribution-9.4.18.v20190429/start.jar jetty-distribution-9.4.18.v20190429/VERSION.txt jetty-distribution-9.4.18.v20190429/webapps/README.TXT

Create a soft link for /opt/jetty-distribution-9.4.18.v20190429/ directory.

[root@jetty-01 tmp]# ln -s /opt/jetty-distribution-9.4.18.v20190429/ /opt/jetty

Set Jetty software ownership to jetty user.

[root@jetty-01 tmp]# chown -R jetty:jetty /opt/jetty/

We need to set environment variables for Jetty server. You can also set JAVA_OPTIONS specific to Jetty web server here.

[root@jetty-01 tmp]# cat > /etc/default/jetty << EOF > JETTY_HOME=/opt/jetty > JETTY_BASE=/opt/jetty/webapps > JETTY_USER=jetty > JETTY_HOST=0.0.0.0 > JETTY_ARGS=jetty.port=8080 > EOF

Create and set privileges on Jetty’s run directory.

[root@jetty-01 tmp]# mkdir /var/run/jetty [root@jetty-01 tmp]# chown jetty:jetty /var/run/jetty

Allow Jetty service port in Linux Firewall.

[root@jetty-01 tmp]# firewall-cmd --permanent --add-port=8080/tcp success [root@jetty-01 tmp]# firewall-cmd --reload success

Create a systemd unit for Jetty service.

[root@jetty-01 tmp]# vi /usr/lib/systemd/system/jetty.service

and add following directives therein.

[Unit] Description = Jetty Web Server After = syslog.target network.target [Service] User = jetty ExecStart = /opt/jetty/bin/jetty.sh start ExecStop = /opt/jetty/bin/jetty.sh stop ExecReload = /opt/jetty/bin/jetty.sh restart Type = forking [Install] WantedBy = multi-user.target

Start and enable Jetty service.

[root@jetty-01 tmp]# systemctl enable jetty.service Created symlink from /etc/systemd/system/multi-user.target.wants/jetty.service to /usr/lib/systemd/system/jetty.service. [root@jetty-01 tmp]# systemctl start jetty.service

Browse URL http://jetty-01.example.com:8080/ from a client's browser.

01-jetty-9-default-homepage

Jetty 9 web server has been installed on CentOS 7.

 

Deploy a Java Web Application on Jetty 9 Web Server:

Download a Java project source from GitHub.

We are downloading simple-crud-app, which is a simple Hello World application.

[root@jetty-01 tmp]# curl -O https://codeload.github.com/trdngy/toy-project-1/zip/master % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 15923 0 15923 0 0 2994 0 --:--:-- 0:00:05 --:--:-- 4201

Extract downloaded file.

[root@jetty-01 tmp]# unzip master ... inflating: toy-project-1-master/src/main/java/com/luv2code/crud/service/CustomerServiceImpl.java creating: toy-project-1-master/src/main/resources/ inflating: toy-project-1-master/src/main/resources/persistence-mysql.properties creating: toy-project-1-master/src/main/webapp/ creating: toy-project-1-master/src/main/webapp/WEB-INF/ inflating: toy-project-1-master/src/main/webapp/WEB-INF/web.xml inflating: toy-project-1-master/src/main/webapp/index.jsp

This project is powered by Apache Maven, therefore, we have to install Apache Maven to compile this project.

[root@jetty-01 tmp]# yum install -y maven ... Installed: maven.noarch 0:3.0.5-17.el7 Dependency Installed: aether-api.noarch 0:1.13.1-13.el7 aether-connector-wagon.noarch 0:1.13.1-13.el7 aether-impl.noarch 0:1.13.1-13.el7 aether-spi.noarch 0:1.13.1-13.el7 aether-util.noarch 0:1.13.1-13.el7 aopalliance.noarch 0:1.0-8.el7 apache-commons-cli.noarch 0:1.2-13.el7 apache-commons-codec.noarch 0:1.8-7.el7 apache-commons-io.noarch 1:2.4-12.el7 apache-commons-lang.noarch 0:2.6-15.el7 apache-commons-logging.noarch 0:1.1.2-7.el7 apache-commons-net.noarch 0:3.2-8.el7.centos atinject.noarch 0:1-13.20100611svn86.el7 avalon-framework.noarch 0:4.3-10.el7 avalon-logkit.noarch 0:2.1-14.el7 bcel.noarch 0:5.2-18.el7 cal10n.noarch 0:0.7.7-4.el7 cdi-api.noarch 0:1.0-11.SP4.el7 cglib.noarch 0:2.2-18.el7 easymock2.noarch 0:2.5.2-12.el7 felix-framework.noarch 0:4.2.1-5.el7 geronimo-annotation.noarch 0:1.0-15.el7 geronimo-jms.noarch 0:1.1.1-19.el7 google-guice.noarch 0:3.1.3-9.el7 guava.noarch 0:13.0-6.el7 hamcrest.noarch 0:1.3-6.el7 httpcomponents-client.noarch 0:4.2.5-5.el7_0 httpcomponents-core.noarch 0:4.2.4-6.el7 jakarta-commons-httpclient.noarch 1:3.1-16.el7_0 java-1.8.0-openjdk-devel.x86_64 1:1.8.0.212.b04-0.el7_6 javamail.noarch 0:1.4.6-8.el7 javassist.noarch 0:3.16.1-10.el7 jboss-ejb-3.1-api.noarch 0:1.0.2-10.el7 jboss-el-2.2-api.noarch 0:1.0.1-0.7.20120212git2fabd8.el7 jboss-interceptors-1.1-api.noarch 0:1.0.2-0.9.20120319git49a904.el7 jboss-jaxrpc-1.1-api.noarch 0:1.0.1-7.el7 jboss-servlet-3.0-api.noarch 0:1.0.1-9.el7 jboss-transaction-1.1-api.noarch 0:1.0.1-8.el7 jline.noarch 0:1.0-8.el7 jsch.noarch 0:0.1.50-5.el7 jsoup.noarch 0:1.6.1-10.el7 junit.noarch 0:4.11-8.el7 jzlib.noarch 0:1.1.1-6.el7 log4j.noarch 0:1.2.17-16.el7_4 maven-wagon.noarch 0:2.4-3.el7 nekohtml.noarch 0:1.9.14-13.el7 objectweb-asm.noarch 0:3.3.1-9.el7 plexus-cipher.noarch 0:1.7-5.el7 plexus-classworlds.noarch 0:2.4.2-8.el7 plexus-component-api.noarch 0:1.0-0.16.alpha15.el7 plexus-containers-component-annotations.noarch 0:1.5.5-14.el7 plexus-containers-container-default.noarch 0:1.5.5-14.el7 plexus-interactivity.noarch 0:1.0-0.14.alpha6.el7 plexus-interpolation.noarch 0:1.15-8.el7 plexus-sec-dispatcher.noarch 0:1.4-13.el7 plexus-utils.noarch 0:3.0.9-9.el7 qdox.noarch 0:1.12.1-10.el7 regexp.noarch 0:1.5-13.el7 sisu-inject-bean.noarch 0:2.3.0-11.el7 sisu-inject-plexus.noarch 0:2.3.0-11.el7 slf4j.noarch 0:1.7.4-4.el7_4 tomcat-servlet-3.0-api.noarch 0:7.0.76-9.el7_6 xalan-j2.noarch 0:2.7.1-23.el7 xbean.noarch 0:3.13-6.el7 xerces-j2.noarch 0:2.11.0-17.el7_0 xml-commons-apis.noarch 0:1.4.01-16.el7 xml-commons-resolver.noarch 0:1.2-15.el7 Complete!

Verify Apache Maven installation by checking its version.

[root@jetty-01 tmp]# mvn -v Apache Maven 3.0.5 (Red Hat 3.0.5-17) Maven home: /usr/share/maven Java version: 1.8.0_212, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"

Compile the source to generate WAR file. We will deploy this WAR on our Jetty 9 web server.

[root@jetty-01 tmp]# cd toy-project-1-master/ [root@jetty-01 toy-project-1-master]# mvn compile [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building simple-crud-app Maven Webapp 0.0.1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ simple-crud-app --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ simple-crud-app --- [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 8 source files to /tmp/toy-project-1-master/target/classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.954s [INFO] Finished at: Wed May 15 01:18:17 PKT 2019 [INFO] Final Memory: 13M/32M [INFO] ------------------------------------------------------------------------

Build WAR file to deploy on our Jetty 9 web server.

[root@jetty-01 toy-project-1-master]# mvn package [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building simple-crud-app Maven Webapp 0.0.1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ simple-crud-app --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ simple-crud-app --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ simple-crud-app --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /tmp/toy-project-1-master/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ simple-crud-app --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ simple-crud-app --- [INFO] No tests to run. [INFO] Surefire report directory: /tmp/toy-project-1-master/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-war-plugin:3.2.0:war (default-war) @ simple-crud-app --- [INFO] Packaging webapp [INFO] Assembling webapp [simple-crud-app] in [/tmp/toy-project-1-master/target/simple-crud-app] [INFO] Processing war project [INFO] Copying webapp resources [/tmp/toy-project-1-master/src/main/webapp] [INFO] Webapp assembled in [378 msecs] [INFO] Building war: /tmp/toy-project-1-master/target/simple-crud-app.war [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.324s [INFO] Finished at: Wed May 15 01:19:15 PKT 2019 [INFO] Final Memory: 9M/92M [INFO] ------------------------------------------------------------------------

Copy the generated WAR file in Jetty 9 webapps directory.

[root@jetty-01 toy-project-1-master]# mv /tmp/toy-project-1-master/target/simple-crud-app.war /opt/jetty/webapps/

If you access the URL http://jetty-01.example.com:8080/ , you may notice that our application is automatically added in Jetty 9 web server.

02-jetty-9-default-homepage

Browser URL http://jetty-01.example.com:8080/simple-crud-app/ to access our Java Web Application via Jetty 9 web server.

03-jetty-9-simple-crud-app

We have successfully deployed a Java web application in our Jetty 9 web server on CentOS 7. If you want to know more about Jetty, then you should read Java Web Services: Up and Running: A Quick, Practical, and Thorough Introduction Second Edition by Martin Kalin.

Install Jetty 9 Web Server on CentOS 7


YOU MIGHT ALSO LIKE:

2 comments:

  1. Thank you for this. Just a small note:

    It seems to me that you have omitted the step of creating a base directory using the start.jar. As a result, when you try to start the service you get an error such as "Cannot find a start.ini file or a start.d directory in your JETTY_BASE directory" in journactl -xe.

    I corrected this by running "java -jar ../start.jar --add-to-startd=http,deploy" in my JETTY_BASE directory.

    ReplyDelete
    Replies
    1. Hi, Thanks for your feedback.

      Well, I am not receiving any such error. May be you are using some other version of Jetty or Linux. Thus, the step you have mentioned above is not mandatory.

      Delete