Skip to main content

Configure a Back End for a Web Application

Hands-On Lab

 

Photo of

Training Architect

Length

01:00:00

Difficulty

Intermediate

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Configure a Back End for a Web Application

Introduction

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.

Connecting to the Lab

  1. Begin by logging in to the lab server using the credentials provided on the hands-on lab page.

    ssh cloud_user@PUBLIC_IP_ADDRESS

Create a New Directory for the Back End and Move Needed Files

  1. Create a new directory.

    mkdir /home/cloud_user/Backend
  2. Move the app.py file to the new directory.

    sudo mv /home/cloud_user/HardeningApache-master/app.py /home/cloud_user_Backend/

Install Dependencies and Configure the Firewall

  1. Install the necessary dependencies. Make sure to install these in the order provided.

    sudo yum install epel-release
    sudo yum install python36.x86_64
    sudo yum install python36-devel.x86_64
    sudo yum install groupinstall "Development Tools"
    sudo yum install python36-pip
  2. Create a symbolic link. This might already exist.

    ln -s /usr/local/bin/pip3 /usr/bin/pip3
  3. Install more dependencies.

    sudo pip3 install Flask
    sudo pip3 install flask-cors
    sudo pip3 install psutil
    sudo yum install tcpdump
  4. Open up port 65535 in the firewall.

    sudo firewall-cmd --permanent --add-port=65535/tcp
  5. Reload the firewall.

    sudo firewall-cmd --reload

Create a File for the Flask App

  1. Switch to root.

    sudo su
  2. Change directories.

    cd /etc/systemd/system/
  3. Create a service file.

    vim /etc/systemd/system/flaskapp.service
  4. Copy the following contents into the file.

    [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
  5. Write the changes and exit the editor.

  6. Reload the daemon.

    systemctl daemon-reload
  7. Start the Flask app.

    systemctl start flaskapp
  8. Verify Flask is running.

    systemctl status flaskapp
  9. Enable Flask.

    systemctl enable flaskapp

Configure the Front End and Test

  1. Open app.py.

    vim /home/cloud_user/Backend/app.py
  2. Verify the log directs to /log_messages.

  3. In a new console, test the connection. Replace PUBLIC_IP with the address provided in the instructions.

    curl http://PUBLIC_IP:65535/log_messages
  4. Back in the original console, close the editor.

  5. Open the index.html file.

    vim /var/www/html/index.html
  6. Find the line assigning a value to base_url. Replace the provided IP address with the public IP address of the lab found on the instructions page.

  7. Save the changes and exit the editor.

  8. Open a private browser tab and navigate to the public IP address provided in the lab instructions.

  9. Click LOGS.

  1. Click MESSAGES.

  2. Verify the existence of the log information. Click the other tabs and verify they populate as well.

Conclusion

Congratulations, you've completed this hands-on lab!