2. Automatically Starting Your Web Server

Jeff Davies
Aug 2 · 3 min read

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:

cd /etc/systemd/system
sudo vi starthelloserver.service

Here is the contents of the starthelloserver.service script:

[Unit]
Description=Start the Node helloserver web server on startup
[Service]
Type=oneshot
ExecStart=/home/opc/node-v12.7.0-linux-x64/bin/node /home/opc/helloserver/index.js
StandardOutput=syslog
StandardError=syslog
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

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.

Errata

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.

Jeff Davies

Written by

Long time software engineer, software architect, technical evangelist and motorcycle enthusiast.

Oracle Developers

Aggregation of articles from Oracle & partners engineers, Groundbreaker ambassadors & the developer community on all things Oracle Cloud and its technologies. The views expressed are those of authors solely and do not necessarily reflect Oracle's. Contact @jimgris or @brhubart

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade