In the first installment in this series, I showed you how to register for a free trial version of Oracle Cloud Infrastructure (OCI) and how to create and deploy a simple web server to the cloud. In this article, I will show you how to automatically start that web server whenever the instance reboots. This is important because at scale you don’t want to manually have to SSH into each server instance and start the web server.
Please note that this article assumes you have a trial account for OCI and you have already performed all of the steps outlined in the first article. This article builds directly on the work you did in the first one.
Configure The Web Server to Auto-Start
We need the web server to start automatically when the server starts or reboots. To do this we need to create a system service. SSH into the webserver1 instance and execute the following commands:
sudo vi starthelloserver.service
Here is the contents of the starthelloserver.service script:
Description=Start the Node helloserver web server on startup[Service]
Please note that the ExecStart line may appear on 2 lines in this article but it is in face a single line that shows the full path to the node program and the full path to the index.js program. If you copy and paste from the source above it should come out fine. Save the file. Now you need to activate the service with the following command:
sudo systemctl enable starthelloserver.service
Now let’s make sure that script is working correctly. Log into your trial instance at https://cloud.oracle.com. The navigate to the Compute instances using the menu in the upper left corner of the browser screen and selecting Compute -> Instances
Click on your webserver1 instance to see the details. Near the top of the screen is a Reboot button. Click on that button now.
The compute instance status will change to STOPPING and then quickly return to RUNNING. Once the compute instance is in the RUNNING state again, open a browser to the URL http://<your public IP>:3000 and you should get the Healthy message.
That’s All There Is!
As simple as that was, it took some digging on my part to figure it all out. Linux’s approach to automatically running a script or service has changed over the years and there is a plethora of outdated information on the web that simply doesn’t work anymore. If you are a Linux expert and know of a better way to do this, please let me know in the comments.
When I first published this article today it contained a bug in the starthelloserver.service file. I removed the Timeout instruction so that it will continue to run after 300 seconds! If you somehow grabbed the initial version of this story, rest assured it’s all working correctly now.