How to run NGINX on Ubuntu in the Cloud for free

Victor Martin
5 min readFeb 26, 2019

--

We are going to run the popular web server NGINX on Ubuntu Linux in Oracle cloud. Why? Let me quote the famous mountain climber George Mallory: “because it’s there”. So, let’s hack!

First of all, if you don’t have access to Oracle cloud, please go to Try it | Oracle Cloud where you can create an account with plenty of compute hours for free.

Prerequisites

We will need to create a Virtual Cloud Network (VCN). A networking abstraction where you place subnets, route tables, gateways, security policies and so on. We will see more of this in a minute.

In order to create a brand new instance, we will need an SSH key pair in your machine. You will use them to connect with SSH later.

$ ssh-keygen -t rsa -N “” -b 4096 -C “<key_name>”
# Replace <key_name> with your email or some unique identifier

Don’t forget to set file permissions 400 on our new private key otherwise, SSH will complain. For more details, please go to Managing key pairs.

$ chmod 400 <private_key_file>
#<private_key_path> is the file path to your private key

We can use the root Compartment because we are just testing. When you are creating resources for real workload you should create a specific compartment so you can fine tune user access and organize your resources.

Finally, you need the Virtual Cloud Network (VCN) we mentioned before. Let’s take a look at the layout of our future instance and the networking around.

Diagram of our instance in the network layout

As you can see, our Linux instance needs a Virtual Cloud Network (VCN) and a subnet to launch into. Our default route table will redirect traffic to an Internet Gateway to communicate with the Internet so we can talk to our instance with SSH. Availability domains are standalone data centres for high availability. Put two NGINX in different availability domains with a load balancer in front of them and you will have a high resilient web server service. Let’s do that on a separate post ;)

Now, it is time to create! Sign-in with your credentials and go to Oracle Cloud Dashboard for Infrastructure with these steps. Select the closest region EMEA or North America:

Access compute service for your closest region

Create a Virtual Cloud Network

Go to Menu > Networking > Virtual Cloud Networks and hit “Create Virtual Cloud Network” button as shown in the picture.

We will see the web form to fill the details for our VCN. Make sure you select the correct compartment, and name our new VCN as “Example_Network” and mark “Create Virtual Cloud Network Plus Related Resources” checkbox, it creates a Subnet, Internet Gateway and other associated resources we need.

Virtual Cloud Network creation web form

We can leave everything else with the default values. CIDR block range of 10.0.0.0/16, an Internet gateway, the default routing rule, a public subnet in each availability domain and the default security list. We will change later the security list to enable NGINX to listen on port 80.

Launching the Instance

Now we will launch an Ubuntu Linux instance image with a basic shape. The shape is how CPU and memory the virtual machine is going to have.

Create a compute instance

Time to pick a name for our server, “webserver” looks good to me. Everything else should be good to go. Although it is always a good practice to check compartment, VCN, subnet at the end of the web form to make sure the default configuration sounds. See the picture and click “Create” when happy.

In order to select the Ubuntu image, click on “Change Image Source” and select it from the list (following two images).

Select our Virtual Machine name, image and shape.
Select the Ubuntu image

Oracle cloud will create our compute instance in a few seconds and then we will see our instance in RUNNING state. In the Instance information screen, you can see “Public IP address” as follows:

Connect to our instance

First of all, we need to upload our public key to connect with SSH. In the details screen of our new instance check the menu on the bottom left corner and click “Console Connections” and then “Create Console Connection” button as follows:

Create Console Connection

We will upload the SSH public key (by default id_rsa.pub on your /home/<user>/.ssh folder) by hitting “Browse”. We keep our private key id_rsa nice and safe.

When the console Connection is active, we can go to the terminal an type:

$ ssh ubuntu@<public_ip>

The “ubuntu” part is the default user to connect on Ubuntu Linux. After you are logged in it is time for us to install the web server.

Install NGINX

In order to have a web server in production mode the approach would be different (find more information on the official website of NGINX)but just for testing purposes:

$ sudo apt install nginx

It will ask for confirmation, then start installing the package. After that we can confirm the installation with:

$ sudo nginx -v

You will get the version of the NGINX installed. Then we can start the service with:

$ sudo systemctl start nginx.service

And check the service is running with:

$ systemctl status nginx

You are done if you see “active (running)”! Test with cURL or a browser with the server public IP:

$ curl http://<public_ip>

Final step. The firewall is blocking the connection to port 80. All Oracle images have IPTABLES enabled with some rules already in place. We need to use iptables to allow TCP port 80 connections. These commands below “insert” a rule at the top of the INPUT and OUTPUT chain to ACCEPT connexions to port 80. Try again after adding these rules:

$ sudo iptables -I INPUT -p tcp --dport 80 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT$ sudo iptables -I OUTPUT -p tcp --sport 80 -m conntrack --ctstate ESTABLISHED -j ACCEPT

Welcome to NGINX in the Cloud! Well done!

--

--

Victor Martin

Principal Cloud Engineer. All opinions are my own. @OracleCloudInfrastructure