Configure a Back End for a Web Application

1 hour
  • 4 Learning Objectives

About this Hands-on Lab

In this lab, we set up a back end for the existing front end of a web application. We do this by creating a proper folder that will hold the code of our back end. Then we proceed to create a systemd file which will be used to start, stop, and get the status of this process at any given time. In addition to this, we will daemonize the process in this manner.

In general, we might ask ourselves what does this have to do with Apache web server? If we have a front-end web application served to us via the Apache web server, there likely exists a back end that will fetch the content to be presented by the front end. In addition to this, Apache web server doesn’t necessarily need to be configured to serve websites. It can also serve as a reverse proxy for various API’s and serve pretty much any content imaginable. Therefore, it stands to reason we should know how to configure Apache web server to interact not only with the user, website, or front end, but the back end as well.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a New Directory for the Back End and Move `app.py` to the Newly-Created Directory
mkdir /home/cloud_user/Backend
sudo mv /home/cloud_user/HardeningApache-master/app.py /home/cloud_user/Backend/
sudo chown cloud_user:cloud_user /home/cloud_user/Backend/app.py
Install Dependencies: python36.x86_64, python36-devel.x86_64, “Development Tools”, python36-pip, Flask, flask-cors, psutil, and tcpdump, then Open Port 65535 with `firewalld`
sudo yum install epel-release
sudo yum update
sudo yum install python36
sudo yum install python36-devel
sudo yum groupinstall "Development Tools"
sudo yum install python36-pip
ln -s /usr/local/bin/pip3 /usr/bin/pip3
sudo pip3 install Flask
sudo pip3 install flask-cors
sudo pip3 install psutil
sudo yum install tcpdump
sudo firewall-cmd --permanent --add-port=65535/tcp
sudo firewall-cmd --reload
Create a systemd File for Our Back End Called “flaskapp.service” in `/etc/systemd/system/`
sudo vim /etc/systemd/system/flaskapp.service
[Unit]
Description=API_Backend
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=/home/cloud_user/Backend
Environment=FLASK_ENV=development
Environment=FLASK_APP=/home/cloud_user/Backend/app.py
ExecStart=/usr/local/bin/flask run -h 0.0.0.0 -p 65535
Restart=always

[Install]
WantedBy=multi-user.target

Save and Close

sudo systemctl daemon-reload
sudo systemctl start flaskapp
sudo systemctl enable flaskapp
Configure the Front End to Be Able to Access the Back End and Test If the Back End Works
vim /var/www/html/index.html

Insert the Server Public IP address here

var base_url = 'http://<Server_Public_IP>:65535';

Save and close

ESC
:wq
ENTER

Test if it works

curl http://<Server_Public_IP>:65535/log_messages

Additional Resources

As is the case with most web applications, there is more to them than the front end and what is visible. A devops engineer is responsible for deploying the back end which works as a service for the front end. With that said, we first need to create a new directory where we will store our back-end files. Then we need to install all the dependencies our back-end application requires.

With many of the services available from the repositories, we can integrate them with systemd out of the box for our own custom applications. However, in this case, we need to write our own service files to enable us to start, stop, restart, and get the status of our application with systemctl.

After we've made our back end fully functional, we need to go into the DocumentRoot, find the file called index.html, and edit it to set the address and port of our back end so the front end can access it.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?