I bought a Raspberry Pi for my IOT course this quarter. This little guy is so powerful. So why not have a try with something interesting.
1. Install Java
Usually Raspberry Pi should have Java Environment installed already, you can call
1 | $ java --version |
to check. And if not, here are some simple steps for your reference.
First, update the package index
1 | $ sudo apt-get update |
Next, install Java. You can choose either JRE or JDK.
More about JRE(Java Runtime Environment) or JDK(Java Development Kit)
The JDK is usually needed if you are going to compile Java programs. And besides, the JDK does contain the JRE, so if you do not care about the larger file size, there are no disadvantages if you install the JDK instead of the JRE.
You can install the JRE or JDK with the following commands:
1 | $ sudo apt-get install default-jre |
Then, set the JAVA_HOME Environment Variable
Use the following command to find out where Java is installed.
1 | $ sudo update-alternatives --config java |
Copy the path and then open /etc/environment using any text editor like vim
1 | $ sudo vim /etc/environment |
At the end of this file, add the following line, and replace everything in the quotation marks.
1 | JAVA_HOME="<your java path, end before go into the bin dir>" |
Save and exit the file, and reload it
1 | $ source /etc/environment |
You can now test whether the environment variable has been set by executing the following command:
1 | $ echo $JAVA_HOME |
Finished!
2. Install MySQL
First, install MySQL
Again, simply update the package index and install the default package with apt-get
1 | $ sudo apt-get update |
Next, Configuring MySQL
With the MySQL server software installed to the Raspberry Pi, we will now need to secure it by setting a password for the “root” user.
By default, MySQL is installed without any password set up. Run the following command to begin the MySQL securing process
1 | $ sudo mysql_secure_installation |
Just follow the prompts to set a password for the root user and then secure your MySQL installation.
Now if you want to access your Raspberry Pi’s MySQL server and start making changes to your databases, you can enter the following command.
1 | $ sudo mysql -u root -p |
We can only use sudo to connect to MySQL as root user at this time. So we’d better create new users and grant privileges, such as following commands:
1 | CREATE USER 'mytestuser'@'localhost' IDENTIFIED BY 'mypassword'; |
Finished!
3. Install Tomcat on Raspberry Pi
First, Download Tomcat
You can find the latest version of tomcat 8.5.X from http://tomcat.apache.org/download-80.cgi. You can copy the download URL and use the wget command on Raspberry Pi to download it.
1 | $ wget latest_tomcat_download_url (it should look like this: http://archive.apache.org/dist/tomcat/tomcat-8/v8.5.53/bin/apache-tomcat-8.5.53.tar.gz) |
After you download the tar.gz, decompress it to a desired destination (for example, I use /home/pi/tomcat)
1 | $ mkdir /home/pi/tomcat |
Second, Configure Tomcat Memory
By default, the maximum heap available for Tomcat may not be enough for large projects. Thus, we need to change the memory configuration by the following steps.
Go to /home/pi/tomcat/bin/, and create a text file name setenv.sh, which contains the following lines:
1 | #!/bin/sh |
Xmx means the maximum heap memory available for Tomcat, and Xms means the initial memory available after each grabage collection. You may very them based on your machine capacity.
Third, Change Access Setting of host-manager and manager
host-manager and manager are two useful default applications provided by Tomcat to manage the Tomcat server. However, by default they can be accessed by only the machine hosting tomcat, i.e., localhost.
To change this, you need to change /home/pi/tomcat/webapps/host-manager/META-INF/context.xml and /home/pi/tomcat/webapps/manager/META-INF/context.xml by deleting the following lines:
1 | <Valve className="org.apache.catalina.valves.RemoteAddrValve" |
Fourth, Create a Tomcat User
Now, create a tomcat user so that you can manage the tomcat server remotely.
Edit /home/pi/tomcat/conf/tomcat-users.xml, and add the following line:
1 | <user username="tomcat" password="123456" roles="manager-gui,admin-gui"/> |
By doing so, you’ve created a user named “tomcat”, and its password is “123456”. You’ve also granted two roles “manager-gui” and “admin-gui” to it.
Finally, Start Tomcat
Now you are ready to start tomcat by executing the following command:
1 | $ /home/pi/tomcat/bin/startup.sh |
By default, Tomcat logging is enabled. Log files are located in logs folder in your Tomcat installation directory. Monitor log files by running the command (replace path with your installation directory):
1 | $ sudo tail -f /home/pi/tomcat/logs/* |
Finished!
configure Raspberry Pi
Now that we already set up the local server environment, we still can not visit our pi from public network.
We have many options depending on how stable/reliable you want. The main idea is to get our public IP address, then go to your router and make a port forwarding. Then it should be able to accesse from public network. However, since the public IP address is usually dynamic, which means it might change someday. So if you do now want to change your configuration often, you can make a dynamic IP binding. But for convenience, I’m not gonna do it at this time.
First, Get Public IP Address
There are many simple ways, if you can access your browser, you can just type in the following url:
1 | ifconfig.me/ip |
Or you can use the following command as well:
1 | curl http://members.3322.org/dyndns/getip |
Now that you have got your public IP address. However, actually this IP address may also change someday. Since our router has the dynamically allocated public address, we can access it temporally. But if you want to access it permanently, you might need to ask for help from ISP(Internet Service Provider). They may or may not provide you the static public IP, because it is kind of a sensitive process.
So for this time, we are just going to use our dynamic public IP address. If it changed someday, we just configure it again. Or, I don’t know if it’s possible, but maybe you can try to write a shell to finish all these automatically.
Second, Port Forwarding
This is simple, you can just use this command
1 | $ ifconfig |
You can focus on either eth0(ethernet) or wlan0(WiFi) depending on which way your pi is connected. Remember your inet address, which is your local IP address. And if you want to make your local IP address static, you might also need to remember your broadcast and netmask address.
Then, go to your router setting page(also your gateway address, mostly it should be something like 192.168.0.1 or 192.168.1.1)
For my TP-LINK router, I use 192.168.0.1 to access the login page, and then on my left, I have a “Forwarding” button. This might be different between each router, you might need to take some time to figure out how to deal with your router.
In the forwarding, add new, there are going to be some form to fill. In my case, the Service Port is the port we want to access from public(more about port, it should be about 65000 ports you can use, but the port less than 1024 are usually assigned for special use, so you’d better choose some other numbers). The Internal Port is the port we want the router forward to. Since I’m using tomcat server, I would set this port to 8080. The IP Address is our Raspberry Pi’s local IP address(which is something like 192.168.0.1XX). For the Protocol I would like to choose All(both TCP and UDP). The status should be Enabled.
Also, if you want to access your Raspberry Pi using ssh via public network instead of local area network, you can also make another port(for example 12345) forwarding to your Raspberry Pi’s 22 port. Then next time if you want to access your Pi from other places, you can access it using the following command(for example the service port is 12345, my Raspberry Pi’s user name is pi, and public IP address is 123.123.123.123):
1 | $ ssh -p 12345 pi@123.123.123.123 |
Finished!
Now, we can access our Raspberry Pi from public network. Thus our Pi can be a simple server now. There’s still more you can do with it. Anyway thanks for reading.
References:
https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-get-on-ubuntu-16-04
https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-16-04
https://pimylifeup.com/raspberry-pi-mysql/
https://geektechstuff.com/2019/05/26/installing-apache-tomcat-jenkins-raspberry-pi/