JasperReports is a powerful reporting engine and class library that enables developers to add reporting capabilities to their applications. It is a versatile tool that allows you to generate professional reports with charts, graphs, and images, and export them in various formats. In this article, we will guide you through the process of installing and configuring JasperReports on Ubuntu 22.04.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- A server running Ubuntu 22.04.
- A valid domain name pointing to your server’s IP address.
- A root password set on your server.
Installing Java JDK
JasperReports is built on Java, so the first step is to install the Java Development Kit (JDK) on your server. Open a terminal and run the following command to install the default JDK:
apt installdefault-jdk unzip wget -y
Once the installation is complete, verify the Java version by running the following command:
java --version
Installing and Configuring MariaDB Database
JasperReports requires a database to store its data. We will be using MariaDB for this purpose. Install MariaDB by running the following command:
apt install mariadb-server -y
After the installation, log into the MariaDB shell using the following command:
mysql
Once you are logged in, create a new user and set a password for it. Replace password
with your desired password:
MariaDB [(none)]> GRANT ALL ON *.* TO 'shapehost'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
Next, flush the privileges and exit the MariaDB shell:
MariaDB[(none)]> FLUSHPRIVILEGES; MariaDB[(none)]> EXIT;
Installing Tomcat Server
JasperReports runs on the Tomcat server, so we need to install it on our Ubuntu server. Follow these steps to install Tomcat:
- Create a dedicated user and group for Tomcat:
groupadd tomcat useradd -s /bin/bash -g tomcat -d /opt/tomcat tomcat
- Create a Tomcat directory:
mkdir /opt/tomcat
- Download the Tomcat 8 package from the official website:
wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.82/bin/apache-tomcat-8.5.82.tar.gz
- Extract the downloaded file to the Tomcat directory:
tar -xzvf apache-tomcat-8.5.82.tar.gz -C /opt/tomcat --strip-components=1
- Set proper permissions and ownership for the Tomcat directory:
chown -R tomcat: /opt/tomcat sh -c 'chmod +x /opt/tomcat/bin/*.sh'
Creating a Systemd Service File for Tomcat
To manage the Tomcat service easily, we will create a systemd service file. Open a text editor and create the following file:
nano /etc/systemd/system/tomcat.service
Add the following content to the file:
[Unit]
Description=Tomcat web 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
Start the Tomcat service:
systemctl start tomcat
Verify the status of the Tomcat service:
systemctl status tomcat
Installing and Configuring JasperReports
Now that we have Tomcat up and running, it’s time to install and configure JasperReports. Follow these steps:
- Switch to the Tomcat user:
su - tomcat
- Download the JasperReports file:
wget https://sourceforge.net/projects/jasperserver/files/JasperServer/JasperReports%20Server%20Community%20edition%208.0.0/TIB_js-jrs-cp_8.0.0_bin.zip
- Unzip the downloaded file:
unzip TIB_js-jrs-cp_8.0.0_bin.zip
- Copy the MySQL property file:
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
- Edit the MySQL property file:
nano jasperreports-server-cp-8.0.0-bin/buildomatic/default_master.properties
Set the following values in the file:
CATALINA_HOME = /opt/tomcat CATALINA_BASE = /opt/tomcat dbHost=localhost dbUsername=shapehost dbPassword=password encrypt = true
Save and close the file.
- Install JasperReports:
cd jasperreports-server-cp-8.0.0-bin/buildomatic/ ./js-install-ce.sh
Once the installation is complete, you should see a success message.
- Edit the Tomcat policy configuration file:
nano /opt/tomcat/conf/catalina.policy
Add the following lines to the file:
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.
- Edit the Application configuration file:
nano /opt/tomcat/webapps/jasperserver/WEB-INF/applicationContext.xml
Add the following lines to the file:
<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.
- Exit from the Tomcat user:
exit
- Restart the Tomcat service:
systemctl restart tomcat
Accessing JasperReports
JasperReports is now installed and configured on your Ubuntu server. You can access it by opening a web browser and entering the following URL:
http://YOUR_SERVER_IP_ADDRESS:8080/jasperserver/
You should see the JasperReports login page. Use the following credentials to log in:
- Username: jasperadmin
- Password: jasperadmin
After logging in, you will be redirected to the JasperReports dashboard, where you can create, view, and manage reports.
Configuring Nginx as a Reverse Proxy for JasperReports
To enhance security and improve performance, it’s recommended to set up Nginx as a reverse proxy for JasperReports. Follow these steps to configure Nginx:
- Install Nginx:
apt install nginx
- Create an Nginx virtual host configuration file:
nano /etc/nginx/conf.d/jasperreports.conf
Add the following configurations to the file:
upstream tomcat { server 127.0.0.1:8080 weight=100 max_fails=5 fail_timeout=5; } server { server_name yourdomain.com; location = / { return 301 http://yourdomain.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/; } }
Replace yourdomain.com
with your actual domain name.
- Verify the Nginx configuration:
nginx -t
- Restart the Nginx service:
systemctl restart nginx
You can now access JasperReports using your domain name instead of the server IP address.
Conclusion
Congratulations! You have successfully installed and configured JasperReports on your Ubuntu 22.04 server. You can now leverage its powerful reporting capabilities to generate professional reports in various formats. By setting up Nginx as a reverse proxy, you have enhanced the security and performance of your JasperReports deployment. Start exploring JasperReports’ features and enjoy seamless reporting for your applications.
For reliable and scalable cloud hosting solutions, including Linux SSD VPS, check out Shape.host.