Installing Apache Jena Fuseki on Debian 9 server

In this post, we are going to install Apache Jena Fuseki as a SPARQL Endpoint web application on a remote Linux machine running Debian 9 so it can be accessed using our web browser. Let's dive in.

Keep in mind

This tutorial assumes we have a fresh installation of Debian 9 and Apache 2.4 HTTP Server configured. Then we are going to need a couple of things:
  • Apache Tomcat, since Fuseki is a Java application which needs to be served by a Servlet Container, and it requires Tomcat 7 (see here). This also means we have to install Java VM.
  • Apache Jena Fuseki binary distribution, which includes a WAR file (get it here). This packaging file contains the whole Fuseki application and we will give it to Tomcat for deployment.
Tomcat will be running at http://localhost:8080 and Fuseki will be running at http://localhost:8080/fuseki/ as a web application. In order for it to be accessible from the outside world, we are going to configure reverse proxy using Apache 2.4.

To get started, connect to your remote server and do the following steps with root privileges.

Step 1: Install Java

apt-get install openjdk-8-jdk
java -version
openjdk version "1.8.0_171"
Runtime Environment (build 1.8.0_171-8u171-b11-1~deb9u1-b11)
64-Bit Server VM (build 25.171-b11, mixed mode)

Step 2: Install Tomcat 7

The following commands will create a user for Tomcat, download and install Tomcat 7 and configure the system daemon to handle its process.
groupadd tomcat
mkdir /opt/tomcat
useradd -g tomcat -d /opt/tomcat -s /bin/nologin tomcat

mkdir ~/tmp
cd tmp
wget [link to the Tomcat 7.0.90 tar.gz file]
tar -zxvf apache-tomcat-7.0.90.tar.gz
mv apache-tomcat-7.0.90/* /opt/tomcat
chown -R tomcat:tomcat /opt/tomcat/
Create file /etc/systemd/system/tomcat.service with the following content.
nano /etc/systemd/system/tomcat.service
[Unit]
Description=Apache Tomcat 7
Wants=network.target
After=network.target

[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1G -Djava.net.preferIPv4Stack=true'
Environment='JAVA_OPTS=-Djava.awt.headless=true'
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
SuccessExitStatus=143
User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target
Reload the system daemon, start Tomcat and check its status.
systemctl daemon-reload
systemctl start tomcat
systemctl status tomcat
systemctl enable tomcat
netstat -antup | grep 8080
Now Tomcat should be running at http://localhost:8080. If we run
curl http://localhost:8080
we'll get a bunch of HTML containing the Tomcat homepage.

Step 3: Deploy Apache Jena Fuseki in Tomcat

This step is quite simple. We can just download the Fuseki binary distribution package and extract the fuseki.war file to /opt/tomcat/webapps/ and Tomcat will automatically deploy it as a web application in a couple of seconds. 
cd ~/tmp
wget [link to the Apache Jena Fuseki 3.8.0 tar.gz file]
tar -zxvf apache-jena-fuseki-3.8.0.tar.gz
mv ./apache-jena-fuseki-3.8.0/fuseki.war /opt/tomcat/webapps/fuseki.war 
Once that is done, the Fuseki application will be running at http://localhost:8080/fuseki/, so if we run
curl http://localhost:8080/fuseki/
we'll get a bunch of HTML containing the Fuseki homepage.

Step 4: Make Fuseki public using Reverse proxy

Reverse proxy is an amazing feature of Apache. It allows us to make local web applications public in no time. The main idea is to redirect all requests from the outside web to a local web server. For example, we can redirect https://our.domain.com/fuseki/ to http://localhost:8080/fuseki/ and that's exactly what we are going to do.
nano /etc/apache2/sites-available/our.domain.com.conf
At the end of the <VirtualHost _default_:443>, put in the following configurations:
...
    ProxyRequests Off
    ProxyPreserveHost On
    <Proxy *>
        Require all granted
    </Proxy>
    RewriteEngine on
    RewriteRule ^/fuseki$ /fuseki/ [R]
    ProxyPass /fuseki/  http://localhost:8080/fuseki/
    ProxyPassReverse /fuseki/  http://localhost:8080/fuseki/
</VirtualHost>

And voila! We can start using Fuseki in our web browser.

Gotchas!

Just a couple of troubles to shoot:
  • In the virtual host configuration, we used RewriteEngine. For this to work we need to enable the Apache2 Rewrite module with
    a2enmod rewrite
    systemctl restart apache2
    
  • If, for some reason, the Fuseki application couldn't be deployed, take a look at the log file to see what the issue is.
    cat /opt/tomcat/logs/catalina.out
    If it says
    ... FUSEKI_BASE is not writeable: /etc/fuseki
    then we can just run the following command to fix that
    chown -R tomcat:tomcat /etc/fuseki

References

  1. https://jena.apache.org/documentation/fuseki2/
  2. https://www.itzgeek.com/how-tos/linux/debian/how-to-install-tomcat-8-5-on-debian-9-ubuntu-16-04-linux-mint-18.html
  3. https://www.rosehosting.com/blog/how-to-install-tomcat-9-on-debian-9/
  4. https://stackoverflow.com/questions/869092/how-to-enable-mod-rewrite-for-apache-2-2
  5. https://serverfault.com/questions/134183/how-to-reverse-proxy-with-or-without-trailing-slash
  6. https://stackoverflow.com/questions/5109112/how-to-deploy-a-war-file-in-tomcat-7


Comments

  1. Hi, thank you so much. I followed your instructions and fuseki can be accessed successfully through Internet. However there is a problem still confusing me that I can only get results if I excute some simple queries which don't need a long processing time (less than 10mins in chrome exactly) from the backend. I will otherwise get a print of "unable to get response from endpoint" . I have incresed the timeout for fuseki as well reverse proxy, but this doesn't work for me. Do you know bit about this?

    ReplyDelete
    Replies
    1. Hi, it might be related to the timeout of the container. Try searching for "Tomcat connection timeout"

      Delete

Post a Comment

Popular Posts