How to Install JasperReports with Nginx Proxy on Ubuntu 22.04
JasperReports is a free and open-source java reporting engine and class library that helps developers to add reporting capabilities to their applications. It is a stand-alone and embeddable reporting server that provides reporting and analytics functionality. With JasperReports, you can generate professional reports, including images, charts, and graphs. This tool also helps you to write rich content onto the screen, printer, and several file formats. including HTML, PDF, XLS, RTF, CSV, XML, ODT and TXT.
In this post, we will show you how to install JasperReports Server on Ubuntu 22.04.
Prerequisites
- A server running Ubuntu 22.04.
- A valid domain name is pointed with your server IP.
- A root password is configured on the server.
Install Java JDK
JasperReports is based on Java, so you will need to install the Java JDK on your server. You can install it by running the following command:
apt install default-jdk unzip wget -y
Once the Java is installed, you can verify the Java version using the following command:
java --version
You will get the Java version in the following output:
openjdk 11.0.16 2022-07-19 OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu122.04) OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Ubuntu-0ubuntu122.04, mixed mode, sharing)
Install and Configure MariaDB Database
You will also need to install the MariaDB database server to your system. You can install it with the following command:
apt install mariadb-server -y
Once the MariaDB is installed, log into the MariaDB with the following command:
mysql
Once you are logged in, create a user and set a password with the following command:
MariaDB [(none)]> grant all on *.* to master@localhost identified by 'password';
Next, flush the privileges and exit from the MariaDB shell with the following command:
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit;
Install Tomcat Server
JasperReports runs on the Tomcat server. So you will need to install it to your server.
First, create a dedicated user and group for Tomcat using the following command:
groupadd tomcat
useradd -s /bin/bash -g tomcat -d /opt/tomcat tomcat
Next, create a Tomcat directory with the following command:
mkdir /opt/tomcat
Next, download the Tomcat 8 from their official website using the following command:
wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.82/bin/apache-tomcat-8.5.82.tar.gz
Next, extract the downloaded file to the /opt/tomcat directory:
tar -xzvf apache-tomcat-8.5.82.tar.gz -C /opt/tomcat --strip-components=1
Next, set proper permission and ownership to the Tomcat directory:
chown -R tomcat: /opt/tomcat
sh -c 'chmod +x /opt/tomcat/bin/*.sh'
Create a Systemd Service File for Tomcat
Next, you will need to create a systemd service file to manage the Tomcat service. You can create it with the following command:
nano /etc/systemd/system/tomcat.service
Add the following lines:
[Unit] Description=Tomcat webs servlet container After=network.target [Service] Type=forking User=tomcat Group=tomcat RestartSec=10 Restart=always Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64" Environment="JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom" Environment="CATALINA_BASE=/opt/tomcat" Environment="CATALINA_HOME=/opt/tomcat" Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid" Environment="CATALINA_OPTS=-Xms1024M -Xmx2048M -server -XX:+UseParallelGC" ExecStart=/opt/tomcat/bin/startup.sh ExecStop=/opt/tomcat/bin/shutdown.sh [Install] WantedBy=multi-user.target
Save and close the file, then reload the systemd daemon to apply the changes:
systemctl daemon-reload
Next, start the Tomcat service with the following command:
systemctl start tomcat
You can also check the status of the Tomcat service with the following command:
systemctl status tomcat
You will get the following output:
? tomcat.service - Tomcat webs servlet container Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled) Active: active (running) since Sun 2022-09-04 06:06:39 UTC; 6s ago Process: 6867 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS) Main PID: 6874 (java) Tasks: 29 (limit: 4579) Memory: 118.4M CPU: 4.427s CGroup: /system.slice/tomcat.service ??6874 /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djav> Sep 04 06:06:39 ubuntu2204 systemd[1]: Starting Tomcat webs servlet container... Sep 04 06:06:39 ubuntu2204 startup.sh[6867]: Tomcat started. Sep 04 06:06:39 ubuntu2204 systemd[1]: Started Tomcat webs servlet container.
Install and Configure JasperReports
First, switch the user to Tomcat and download the JasperReports file with the following command:
su - tomcat
wget https://sourceforge.net/projects/jasperserver/files/JasperServer/JasperReports%20Server%20Community%20edition%208.0.0/TIB_js-jrs-cp_8.0.0_bin.zip
Once the download is completed, unzip the downloaded file with the following command:
unzip TIB_js-jrs-cp_8.0.0_bin.zip
Next, copy the MySQL property file with the following command:
cp jasperreports-server-cp-8.0.0-bin/buildomatic/sample_conf/mysql_master.properties jasperreports-server-cp-8.0.0-bin/buildomatic/default_master.properties
Next, edit the MySQL property file using the following command:
nano jasperreports-server-cp-8.0.0-bin/buildomatic/default_master.properties
Define your Tomcat path, and database configuration details as shown below:
CATALINA_HOME = /opt/tomcat CATALINA_BASE = /opt/tomcat dbHost=localhost dbUsername=master dbPassword=password encrypt = true
Save and close the file then install the JasperReports with the following command:
cd jasperreports-server-cp-8.0.0-bin/buildomatic/
./js-install-ce.sh
Once the installation is complete, you will get the following output:
[echo] Found Groovy in import lib directory deploy-webapp-datasource-configs: [echo] --- (app-server.xml:deploy-webapp-datasource-configs) --- [echo] jsEdition = ce [echo] warFileDistSourceDir = /opt/tomcat/jasperreports-server-cp-8.0.0-bin/buildomatic/../jasperserver.war [echo] warTargetDir = /opt/tomcat/webapps/jasperserver [echo] webAppName = jasperserver [echo] webAppNameCE = jasperserver [echo] webAppNamePro = jasperserver-pro [echo] webAppNameSrc = jasperserver [echo] webAppNameDel = jasperserver, warTargetDirDel = /opt/tomcat/webapps/jasperserver [copy] Copying 9 files to /opt/tomcat/webapps/jasperserver scalableAdhoc-refinement: deploy-webapp-ce: install-normal-ce: [echo] Installation successfully completed! BUILD SUCCESSFUL Total time: 1 minute 26 seconds Checking Ant return code: OK ----------------------------------------------------------------------
Next, edit the Tomcat policy configuration file:
nano /opt/tomcat/conf/catalina.policy
Add the following lines:
grant codeBase "file:/groovy/script" { permission java.io.FilePermission "${catalina.home}${file.separator}webapps${file.separator} jasperserver-pro${file.separator}WEB-INF${file.separator}classes${file.separator}-", "read"; permission java.io.FilePermission "${catalina.home}${file.separator}webapps${file.separator} jasperserver-pro${file.separator}WEB-INF${file.separator}lib${file.separator}*", "read"; permission java.util.PropertyPermission "groovy.use.classvalue", "read"; };
Save and close the file, then edit the Application configuration file:
nano /opt/tomcat/webapps/jasperserver/WEB-INF/applicationContext.xml
Add the following lines:
<bean id="reportsProtectionDomainProvider" class="com.jaspersoft.jasperserver.api. engine.jasperreports.util.PermissionsListProtectionDomainProvider"> <property name="permissions"> <list> <bean class="java.io.FilePermission"> <constructor-arg value="${catalina.home}${file.separator}webapps ${file.separator}jasperserver-pro${file.separator} WEB-INF${file.separator}classes${file.separator}-"/> <constructor-arg value="read"/> </bean> <bean class="java.io.FilePermission"> <constructor-arg value="${catalina.home}${file.separator}webapps ${file.separator}jasperserver-pro${file.separator}WEB-INF ${file.separator}lib${file.separator}*"/> <constructor-arg value="read"/> </bean> </list> </property> </bean>
Save and close the file, then exit from the Tomcat user:
exit
Next, restart the Tomcat service to apply the changes.
systemctl restart tomcat
Access JasperReports Web UI
At this point, JasperReports is installed and configured. You can now access it using the URL http://YOUR_SERVER_IP_ADDRESS:8080/jasperserver/. You should see the JasperReports login page:
Provide the username: jasperadmin and password: jasperadmin then click on the Login button. You should see the JasperReports dashboard on the following page:
Configure Nginx as a Reverse Proxy for JasperReports
First, install the Nginx web server package with the following command:
apt install nginx
Next, create an Nginx virtual host configuration file with the following command:
nano /etc/nginx/conf.d/jasperreports.conf
Add the following configurations:
upstream tomcat { server 127.0.0.1:8080 weight=100 max_fails=5 fail_timeout=5; } server { server_name jasper.example.com; location = / { return 301 http://jasper.example.com/jasperserver/; } location / { proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://tomcat/; } }
Save and close the file then verify the Nginx configuration:
nginx -t
You will get the following output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Next, reload the Nginx service to apply the configuration changes:
systemctl restart nginx
You can verify the Nginx status using the following command:
systemctl status nginx
You should see the following output:
? nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2022-09-04 14:51:10 UTC; 7s ago Docs: man:nginx(8) Process: 7644 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 7645 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 7646 (nginx) Tasks: 3 (limit: 4579) Memory: 3.3M CPU: 45ms CGroup: /system.slice/nginx.service ??7646 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;" ??7647 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ??7648 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" Sep 04 14:51:10 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server... Sep 04 14:51:10 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.
You can now access your JasperReports using the URL http://jasper.example.com.
Conclusion
In this post, you learned how to install JasperReports on Ubuntu 22.04. You also learned how to configure Nginx as a reverse proxy for JasperReports. You can now use this tool to generate and embed reports into Java or non-Java Application.