SpringBoot #2: Deploying WAR file to Google Cloud Server

Intro

Developing an application without deploy it in real server is like making a car without wheel… Useless.. haha

You don’t have to buy a physical server in order to deploy your apps. For learning purpose, there are a lot of free cloud server to use, such as AWS, Google Cloud Platform (GCP), Heroku, Digital Ocean, etc.

Personally, I am using GCP as my server. You can check how to configure GCP here.

Create WAR file

For java, there are two types of deployment packages: JAR and WAR. When you decide to use Springboot, at the beginning we can choose either JAR or WAR in the option. Easy explanation about JAR and WAR.

Since I use Eclipse as IDE and Gradle as library tool, I cannot find a good tutorial to create WAR file that match my requirement. Most of the tutorial show me how to create WAR with Maven, or create WAR using STS, etc. Some of them also use command prompt to generate WAR file. But I have no idea where to run the command 😐 Yeah, I am amateur. So, I tried to find the menu to generate WAR file from eclipse using Gradle.

  1. Open the Gradle Tasks View

  • Find the project name you want to generate the WAR file. Expand the project folder
  • Right click on the build option, then choose Run Gradle Task
  • You may check the WAR file in the libs folder –> [project folder]\build\libs

Deploy to GCP

This part is simpler than I thought. When I tried to find tutorial for this, a lot of articles showed me the luxurious method which is using their Kubernetes feature. Or deploying using SSH. I’ve tried both of them but failed. Then I tried to do it manually. I tried to upload the generated WAR file using tomcat manager gui. Well, it is more complex than I thought. Because you need to reconfigure the size file in the config file. If I’m not mistaken, the default allowed size was 50MB. While my WAR file has 60MB size. So I need to increase the upload size in webapps/manager/WEB-INF/web.xml .

Another way is using File Transfer, such as Filezilla to upload the WAR file into webapps folder. Once it is uploaded, the WAR file is automatically extracted inside the folder. If you have permission issue when uploading, just change the permission file itu 755 or 777 using chmod command.

Once uploaded, you may access it from browser [your website ip]/name_of_war_file

If you want to open it as home root, then rename the WAR file into ROOT.WAR

SpringBoot WAR filed cannot be deployed to Tomcat

Above method is the normal method, after I successfully removed all the error I found when deploying. Actually I spent like 1 month thinking how to solve my problem. I cannot deploy my WAR file to Tomcat. This error was reappearing again and again.

FAIL - Application at context path /myWebApp could not be started
FAIL - Encountered exception org.apache.catalina.LifecycleException:
Failed to start component
[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/myWebApp]]

Copy pasting the error to googling lead me to many wrong solution. However, I’ve all of them, and still not working. Then, I got this idea. In one of the solution said that maybe it is caused by the embedded tomcat inside the Springboot, so I have to configure it as providedRuntime, instead of compile. I’m sure I have configured it like that. But still not working. So I had another idea to check the tomcat version. Yeah!!! FINALLY I found it! The reason I cannot deploy my SpringBoot apps to Tomcat is because the version of both tomcat is different!

SpringBoot used the newest tomcat version, tomcat 9, while my remote server was using Tomcat 7! Why I didn’t realize it sooner!

Then the solution is upgrading the tomcat version at the remote server to 9. I will put extra content below about how I upgrade my tomcat to 9 manually. Yeah, manually, because yum doesn’t have 9 yet.

Honestly, I don’t remember how I upgraded the version in detail. But I will try to write what I remember.

Upgrading Tomcat Version

  • Access remote server using putty
  • Shutdown the current tomcat version service
  • Download the newest version installer from official tomcat website and install the package
  • cd /opt
    wget http://mirror.apache-kr.org/tomcat/tomcat-9/v9.0.10/bin/apache-tomcat-9.0.10.tar.gz
    tar -xzvf apache-tomcat-9.0.10.tar.gz
    rm -rf apache-tomcat-9.0.10.tar.gz
    mv apache-tomcat-9.0.10 tomcat9
    cd tomcat9
    export CATALINA_HOME=/opt/tomcat9/
    
  • Add profile file to configure the default tomcat configuration
  • vi /etc/profile
    
    
  • Fill the profile file with following content
  • CATALINA_BASE=/opt/tomcat9
    CATALINE_HOME=/opt/tomcat9
  • Set user group
  • useradd tomcat
    groupadd apache
    usermod -G apache tomcat
    cd /opt
    chown tomcat:apache /opt/tomcat9/ -R
    
  • Try to start the service
  • /opt/tomcat9/bin/catalina.sh start

     

I am afraid I didn’t write it in detail, but approximately I did the above steps. If the tomcat cannot be run, you may google it. There are a lot of solution about this.

After installed tomcat 9, you may remove the old version of tomcat.

%d bloggers like this: