Publishing a website with JAVA backend using a Google virtual Ubuntu instance & Tomcat.
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:
- IntelliJ Ultimate. (JetBrains gives a free trial for 1 month)
- Apache Tomcat 8. (If you’re using this for server needs. You could use any later version)
- Google Cloud Platform account. (Google gives new users $300 credit)
- 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 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.
- Open project settings (⌘;) and click Artifacts.
- Click Add (+), point to Web Application: Archive, and then click For ‘YourWebApp:war exploded’. If necessary, create a manifest file.
- 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.
- Hit Create Instance.
- Change the name to a unique name.
- Choose the region for your instance, closer instances tend to be faster, but that’s not really important in small scales.
- Choose the machine type, I used micro (1 shared vCPU) because it’s enough for me and the example.
- Boot disk, I chose Ubuntu 19.4 for this example.
- Enable both Allow HTTP & HTTPS traffic.
- and finally, hit create.
- wait while it’s created, and hit the SSH button to enter your machine.
Step 3: Install all requirements (JAVA, Tomcat).
Command lines to apply on your VM Instance to get started:
- Update and upgrade Ubuntu.
- Install JAVA JRE.
- Install Tomcat9 & Tomcat9-admin.
- Restart Tomcat server.
Step 4: Configure server settings.
- 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.
- Add an admin user to tomcat-users.xml: Enter the tomcat-users.xml file and add a user inside <tomcat-users> tag.
- 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
// Edit tomcat-users.xml file in the same directory
$ sudo nano tomcat-users.xml// Restart tomcat server
sudo service tomcat9 restart
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.
Step 6: Uploading WAR file to tomcat.
- Click on the blue link “host-manager webapp”.
- Enter the username and password you added to tomcat-users.xml.
- Under WAR file to deploy, upload your WAR file which its name is your domain extension as in www.yourDomain.com/yourWarName.
- 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.
- Point your @ tag in your DNS Manager page to your instance external IP address.
- Go to /etc/hosts and add your domain.
- Restart your tomcat server.
// Edit hosts file
$ sudo nano /etc/hosts
Hit ctrl + X → Y → Enter/Return to save and continue.
Your hosts file should look something like this
// Restart tomcat server
$ sudo service tomcat9 restart
That’s it, have fun programming, and feel free to contact me if anything goes wrong :)