Data Enthusiast

Contact

How to install Flask on a Ubuntu Server

Installing Flask on a Ubuntu server can be quite a daunting task especially if its your first time doing this. I'm going to be using Python 3.6, MySQL V 8.0, uWSGI, nginx, and Ubuntu 18.04 LTS. Typically the LTS versions of Ubuntu are much more stable then the regular versions. Just recently Ubuntu 20.04 came out which has a whole host of some pretty cool features such as faster installation times and offering better performance overall. Flask is a popular micro web-framework that allows users to quickly build prototypes and run them live on a server. There are other popular frameworks such as Django that offers a more of a battery style approach which enables the users to have a more structured approach to their projects.

Getting Started

Before we go about setting your awesome Flask application, just make sure that you have access to a webserver. Digitial Ocean and Linode are great cloud providers and you can spin up your own server instance for as low as $5 where I'll be using Linode. Down below is a one of the plans from Linode that could get you up and running quickly.

To begin, SSH into your server instance and you will need to te ip address of your server. We'll first login into your root account and then create a separate account that we'll use to put to put your Flask application into production. It is not recommended to use your root account for this as it poses a security risk if someone gets a hold of your credentials.

ssh root@server_ip

Once your logged into your server instance, updates and other upgrades maybe needed to be installed on your server instance. This can be done using a single command coupled with the && operator which means and.

sudo apt update && apt upgrade

After the updates have been installed, its time to install the various pre-reqiuisties to setup this project. Python 3 should already be installed as it comes with the os bundle. First go ahead and install a basic firewall. There are various firewall packages out and the one I'm going to be using is ufw. The steps below will setup ufw along with the exceptions to allow you to ssh and run your flask application on port 5000 (default port). If ssh isn't allowed, this can prevent you from opening a remote session with your server.

1. sudo apt install ufw
2. sudo ufw default allow outgoing
3. sudo ufw default deny incoming
4. sudo ufw allow ssh
5. sudo ufw allow 5000 # Allow flask to run on port 5000
6. sudo ufw enable
7. sudo ufw status # Check the allowances

Next up since we don't want to use the root account, we'll go ahead and create a separate user profile. This user will also have sudo privileges so that way you don't need to keep logging into your root account. Once the user account has been setup log out of the root account and into this account.

1. adduser production # Use whatever name you want here
2. adduser production sudo

Transfer over your flask project by running the git clone command and the url is to your git repository.

git clone url

Setting up Python Virtual Environment

Next we're going to be setting up our running pyton virtual environment. Virtual enviroments are a great way to keep dependencies separate from other projects as other projects might run on a different set of requirements. It also makes it easier to run projects on various systems as you can use pip to create a requirements.txt file that contains all your python dependencies for your project. The steps below will get your python virtual environment all setup. In this setup I'll be calling my virtual environment venv, but your free to call it whatever you like. I have a requirements.txt file already but if you don't have one, go ahead

1. sudo apt install python3-pip
2. sudo apt install python3-venv
3. cd /path/to/project
4. python3 -m venv venv # Provide a name for your VE
5. source venv/bin/activate
6. pip install -r requirements.txt

Next setup your flask project and the first thing you will need is your environment variables. You will need variables such as Secret Key, Db URL, Email_pass, etc. Setup your variables in a config.jsob file which you will then place in the /etc folder or wherever suits your project needs. A typical config file will look like

1. {
2. “SECRET_KEY”: < Secret Key >,
3. “SQLALCHEMY_DATABASE_URI”: < URI >,
4. “SQLALCHEMY_TRACK_MODIFICATIONS”: “FALSE”,
5. “MAIL_SERVER”: “smtp.gmail.com”,
6. “MAIL_PORT”: 587,
7. “MAIL_USE_TLS”: 1,
8. “MAIL_USERNAME”: < email >,
9. “MAIL_PASSWORD”: < pass >
10. }

Also ensure that your flask project is able to read in the json file. If not you can add the following lines to your code to your app.py file or wherever you are creating the flask app object.

1. with open(“/etc/config.json”) as config_file:
2.  config=json.load(config_file)

Now that all the parameters are in place, you can test your flask application. We'll expose it to the public by adding 0.0.0.0 as one of the arguments below which will expose your flask application to the outside world. You'll run these commands in your terminal. If for whatever reason your application fails to run, try it in debug mode to see what errors it shows. After staring up your flask application, go ahead and open up your brower to server_ip:5000. You should be able to see your application.

1. export FLASK_APP = app.py
2. flask run -- host=0.0.0.0

Conclusion

Now that you are able to run your flask application on your server, you should be able to setup Gunicorn or uWSGI with your nginx instance which should allow you to run your website indefintely. You will need to configure nginx and one of the wsgi web servers above to get your flask application running.