Creating a Flask App on a subdomain

Background

This website is hosted on Digital Ocean with WordPress. While WordPress is a great tool for building a website I wanted a place where I could build and host various tools.

Enter Flask.

Flask is a micro framework that allows you to write web applications using Python. This means that Python can be leveraged to write functions and utilities.

Resources

Initial Server Setup

link

Set up nginx

link

Getting the flask app set up

link

This didn’t work for me (probably because I thought I had set up an ubuntu 20 droplet when it is in fact ubuntu18)

python3.8 -m venv myprojectenv

So I installed all the packages I needed using pipenv

pipenv install wheel uwsgi flask

Because I used pipenv instead of virtualenv, this means my virtual environment location is diiferent.

On a linux machine pipenv creates the virtual environment in a ‘.local’ folder within the directory of the user.

So instead of the ‘/etc/systemd/system/myproject.service’ containing the following:

[Unit]
Description=uWSGI instance to serve myproject
After=network.target

[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myproject
Environment="PATH=/home/sammy/myproject/myprojectenv/bin"
ExecStart=/home/sammy/myproject/myprojectenv/bin/uwsgi --ini myproject.ini

[Install]
WantedBy=multi-user.target

it has:

[Unit]
Description=uWSGI instance to serve myproject
After=network.target

[Service]
User=marquin
Group=www-data
WorkingDirectory=/home/marquin/myproject/myproject
Environment="PATH=/home/marquin/.local/share/virtualenvs/myproject-C0MD5sDq/bin"
ExecStart=/home/marquin/.local/share/virtualenvs/myproject-C0MD5sDq/bin/uwsgi --ini myproject.ini

[Install]
WantedBy=multi-user.target

Server block configuration file. In this file, list the location of the subdomain on the server name file.

‘/etc/nginx/sites-available/myproject’

server {
    listen 80;
    server_name telephonies.xyz www.telephonies.xyz sub.marquinsmith.com www.sub.marquinsmith.com;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/marquin/myproject/myproject/myproject.sock;
    }
}

All other steps in the serving flask tutorial remain the same

Adding a subdomain

link

What we want to do here is add a new A record of the subdomain that points to the droplet we have just created.

With these configuration settings in place, navigating to the sub domain will be met with a response from the flask application.

Send a Comment

Your email address will not be published.