Publishing a website with JAVA backend using a Google virtual Ubuntu instance & Tomcat.

Pierre Janineh
Coding with PierreJanineh

--

Pixabay pictures.

There are plenty of reasons why we’d create a virtual instance for publishing a site, the first and most compelling, is that your site could be online the whole time, even if anything happened to your equipment, the second good reason is that you could publish your webpages to a domain you own.

Step 7 describes how to link your domain to your virtual instance server.

Things you’ll need to get this done:

  1. IntelliJ Ultimate. (JetBrains gives a free trial for 1 month)
  2. Apache Tomcat 8. (If you’re using this for server needs. You could use any later version)
  3. Google Cloud Platform account. (Google gives new users $300 credit)
  4. Internet connection. (of course)

Getting started

After downloading IntelliJ Ultimate, go ahead and start a new project, choose Web Application, pick a name and hit “finish”.

IntelliJ ScreenShot: New Web Application creation.

IntelliJ is going to create a template to get you started with a Web App, go on and customise it for your needs as you wish. You can as well use it as a server for making API requests (GET/POST).

Once you have finished, we can get started with deploying the app to a WAR file, then we’d start building our server.

Step 1: Deploying a WAR file.

  1. Open project settings (⌘;) and click Artifacts.
  2. Click Add (+), point to Web Application: Archive, and then click For ‘YourWebApp:war exploded’. If necessary, create a manifest file.
  3. Open the Build menu and click Build Artifacts. Select to build the YourWebApp:war artifact. You should see the artifact out/artifacts/YourWebApp_war/YourWebApp_war.war

Step 2: Create a virtual instance on Google Cloud Console.

  1. Hit Create Instance.
  2. Change the name to a unique name.
  3. Choose the region for your instance, closer instances tend to be faster, but that’s not really important in small scales.
  4. Choose the machine type, I used micro (1 shared vCPU) because it’s enough for me and the example.
  5. Boot disk, I chose Ubuntu 19.4 for this example.
  6. Enable both Allow HTTP & HTTPS traffic.
  7. and finally, hit create.
  8. wait while it’s created, and hit the SSH button to enter your machine.
VM Instance creation GIF. Google Cloud console

Step 3: Install all requirements (JAVA, Tomcat).

Command lines to apply on your VM Instance to get started:

  1. Update and upgrade Ubuntu.
  2. Install JAVA JRE.
  3. Install Tomcat9 & Tomcat9-admin.
  4. Restart Tomcat server.
Command lines for starting up your VM Instance and Tomcat server.
Updating and upgrading system.
Install JAVA.
Install Tomcat9 and Tomcat9-admin

Step 4: Configure server settings.

  1. Exchange port number with 80 and redirectPort with 443: Go to /etc/tomcat9 and change the numbers in server.xml. To save and exit press ctrl + X → Y → Enter/Return.
  2. Add an admin user to tomcat-users.xml: Enter the tomcat-users.xml file and add a user inside <tomcat-users> tag.
  3. Restart your tomcat server by typing: sudo service tomcat9 restart
// Go to /etc/tomcat9
$ cd /etc/tomcat9
// Edit server.xml file
$ sudo nano server.xml
Changing port and redirectPort numbers.
// Edit tomcat-users.xml file in the same directory
$ sudo nano tomcat-users.xml
// Restart tomcat server
sudo service tomcat9 restart
Adding an admin user to tomcat-users.xml file. sorry for the typo in “roles”.

Step 5: Check if it works.

Enter your instance’s external IP address in your browser and a webpage would appear saying “It Works”.

Make sure you enter the IP address without the “S” in HTTPS.

Google Cloud Console ScreenShot: Your External IP
It Works! ScreenShot

Step 6: Uploading WAR file to tomcat.

  1. Click on the blue link “host-manager webapp”.
  2. Enter the username and password you added to tomcat-users.xml.
  3. Under WAR file to deploy, upload your WAR file which its name is your domain extension as in www.yourDomain.com/yourWarName.
  4. Instead of #3, you could upload your file to your server immediately to avoid adding an extension to your home page. To do that you’ll need to go to the directory /var/lib/tomcat9/webapps/ROOT, delete index.html file and upload your file there using the SSH console window options.

Step 7: Linking your own domain to your virtual instance.

  1. Point your @ tag in your DNS Manager page to your instance external IP address.
  2. Go to /etc/hosts and add your domain.
  3. Restart your tomcat server.
// Edit hosts file
$ sudo nano /etc/hosts

Hit ctrl + XYEnter/Return to save and continue.

Your hosts file should look something like this

hosts file in /etc/hosts
// Restart tomcat server
$ sudo service tomcat9 restart

That’s it, have fun programming, and feel free to contact me if anything goes wrong :)

--

--