Taylor's Blog

Atypical ramblings

Phase 1 Complete!

I have been making some serious progress towards development of my app. Last night I was lying in bed configuring my Linux server on my phone with JuiceSSH and managed to get Tomcat set with Spring Boot. I was able to display a simple “Hello World” message when I visited which is a huge step forward. Doing so took a good amount of configuring however. Here are some of the challenges I faced just to get this far:

  • I was dead-set on using IntelliJ IDEA to setup Spring as I prefer it to Eclipse. However, in order to have Spring baked into IntelliJ by default, you need to have the IntelliJ Ultimate which is $12/month or $50/year. Luckily, because I am still a student at Oregon State University, I am able to use IntelliJ for free! Score!
  • I followed this video for instructions on how to set up a simple “Hello World” using Gradle and IntelliJ. It worked on localhost and I attempted to port it over to my server by just copying the whole directory over. I would later learn that this is not the proper way to do things.
  • Once I had ported over my project, I installed Tomcat, Gradle and Spring. However, I had issues with Gradle and Spring because their versions were too old to be compatible with my Tomcat installation, so I had to manually download the latest versions from the companies’ websites. I learned a valuable lesson in this – you can’t always trust the package manager for the latest version of software.
  • I was finally able to get the server up and running by typing gradle bootRun in the project directory! Victory… or so I thought!
  • A very helpful redditer informed me that my setup was erroneous. Here’s the advice he gave me:

You should be building a .jar (locally or somewhere else) and then uploading that jar to your server. You really shouldn’t be using gradle to run your application on the server. gradle bootRun is just an easy way to get your app running locally for development purposes.

You can build a .jar locally using ./gradlew build 

Then you will find a .jar file in build/libs/ that you can upload to the server.

You also need to run your app as a service. Simply executing the app on the command line like you are doing is great for local devlopment, of course, but it won’t work on a server. In fact, as soon as you cntrl+c or disconnect from the server, your app will shut down. This is why you need to run it as a service.

Thankfully, doing this with spring boot is actually pretty easy even if you aren’t very familiar with linux: https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html

That documentation gives you two methods, init.d and systemd (don’t worry too much if you don’t know what those mean right now). You probably want systemd unless your Debian is an older version that doesn’t support it (in which case use init.d).

Essentially what spring boot does it allow you to build a fat jar that is also an executable that can be run as a Linux service. You then create a configuration file (/etc/systemd/system/yourApp.service) where you point to that fat jar, and in that file you can also control stuff like where your output will go.

Once you do that, you can start your app like:

systemctl start yourApp.service

You’ll notice it will give you back the console and run the app in the background. You can see your output in various ways (look at the documentation, usually a file somewhere in in /var/log).

You can also “enable” the service

systemctl enable yourApp.service

This will make it start up automatically whenever the server starts up or restarts.

I followed his advice and the documentation he linked me and managed to get things working. This required me to add code to my build.gradle file and to run gradlew build to get a .jar file. I copied the .jar file over, created a .service file and started the service with sudo systemctl start myapp.service. I had to fix some errors with the service as it wouldn’t start right away which included making the .jar executable by typing chmod +x /var/myapp/myapp.jar . I also learned:

  • To see what services are running with the systemctl list-units command
  • To see details about a service using systemctl status -l myapp.service.
  • To see what ports are being listened on so I can make sure Tomcat is running using sudo lsof -i -P -n | grep LISTEN.
  • To restart a service, use sudo service myapp restart

I learned a lot setting this up, but now I get to move on to the fun part of linking the app to the back-end. I am looking forward to it!

Updated: June 6, 2017 — 11:31 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

Taylor's Blog © 2015