Deploying a Static HTML Website with Ansible on Ubuntu 20.04 with Nginx
NGINX(pronounced as “Engine -X”) is a free open source web server software, initially used to host and also used as a reverse proxy created. It was publicly released by Igor Sysoev.
Ansible is the easiest IT automation tool to implement, and most importantly, it is supported by most Linux systems and Mac OS. The best feature of Ansible is that it is agentless, which means we do not have to install agents like other services. Ansible uses an ssh push mechanism from a single source system to multiple remote machines.
We will use Ubuntu distro as it is simple, multiple-feature rich, and one of the most popular Linux distro. Our objective in this article is to deploy a static webpage in a remote ubuntu 20.04 server by automating with ansible with the help of Nginx.
Before we begin, let us understand how an Ansible structure works.
Ansible consists of playbooks which, in turn, consist of “plays.” Again, each play is divided into “tasks.” Each task performs certain operations known as the module. We will create a playbook to install the Nginx engine in the remote server, and from there, inside the Nginx will deploy our static website that we will create in another playbook.
Let us dive into the details of this process.
To host a website, we first need to install the Ansible in the source machine, with a clean installed Ubuntu 20.04 server having access to it.
After the setup, we have to add the IP address and name of the host servers in your local machine, i.e.,
Step 1: Create a folder
Create a folder of any name you wish. For instance, we will be using nginx_ansible to store the Ansible files.
cd mkdir nginx_ansible && cd nginx_ansible
Step 2: Creating the inventory.yml file.
YML is a human-readable file for data serialization used for reading and writing data independent of a specific programming language, easy to interpret and deploy. YML is not a markup language rather a Unicode-based system.
Create a file inventory.yml.
Now, create the template of the Nginx folder.
Step 3: Create the Nginx playbook
This playbook will install the Nginx in the remote Ubuntu server to host our static website.
Create a file inside the templates folder called nginx.conf.j2 and add the following:
Here, the domain is variable to be filled later when we create an Ansible playbook.
Save and close the file.
Step 4: Create a new Ansible playbook nginx_play.yml
Steps by step guide of the playbook:
- The playbook begins with the hosts’ definition set to websites and a become directive that commands Ansible to run all tasks as the root user by default (the same as manually running commands with sudo).
- We will update the apt-cache and then install the Nginx package on remote nodes.
- After that, we are coping with the website files, changing the permission to 0775, and deleting the default Nginx webpage.
- Now you have to apply the template that will configure the web server to host your static HTML file. We’ll create a symbolic link to the file inside /etc/nginx/sites-enabled and notify the Nginx service for a reboot.
- We will include the task which will enable port 80.
- At last, we will create a handler for the Nginx.
The code goes this way below:
After this is done, we approach the final step to deploy the website.
Step 5: Creating a folder to keep the static file
mkdir websites && cd websites
Create an index.html file and type or copy the code below.
Close the file and create another file.
Create a synchronize.yml file.
Step 6: To run the playbook do
ansible-playbook -i inventory.yml nginx_play.yml
ansible-playbook -i inventory.yml synchronize.yml
After running the lines, the Nginx will install in the remote server, and the hosted website will look like this given image:
E2E Cloud Facilities
We at E2E are amongst India’s fastest growing pureplay SSD Cloud players. E2E clouds provide various types of cloud services like Linux Cloud, Windows Cloud, Cloud GPUs, and some other cloud solutions. In the world of Machine Learning and Deep Learning, E2E cloud provides a leading advantage. It is easy to use, and our highly scalable system guarantees high-performance enhanced security thanks to our advanced AI algorithm and is of high reliability. Our systems help many companies to remain sustainable in the long run and are constantly evolving with the best technologies time has to offer today.
Now that you have deployed your static website in the remote Ubuntu server it can be concluded that Ubuntu is performance-rich, free, and easy to use. When it comes to performance, the Nginx Ansible combo is a powerful choice.