Working with Vue and Flask (Part 3 Flask Deployment with Apache 2)

Isaac Obella
4 min readSep 4, 2019

This is the third and final part in a series of Working with Vue and Flask. We shall be picking up from where we left. If you missed the previous two, you can quickly catchup.

In the previous parts, we learned how to set up and work with Flask and VueJS on the development environment. Now it’s time to deploy to production.

I’ll be taking a few assumptions:

  • Celery is our flask thread platform.
  • The deployment platform is Linux Ubuntu/Debian.
  • The Python version is 3.6 or above.
  • MySQL is the database environment

Setting up the environment

  • SSH into the server and clone your code repository.
  • Create a virtual environment for your project virtualenv --python=python3.6 venv
  • Activate your virtual environment with source PATH_TO_VIRTUAL_ENV/bin/activate
  • Install your project requirements pip install -r requirements/common.txt

Database Setup

  • Create your MySQL database.
  • With your virtual environment active, setup flask configurations.
export flask configuration variable to run deployment tasks
  • With that, we can now do the first-time deployment to autorun all actions required for the initial system state. python manage.py deploy
  • Build the Vue production environment with python manage.py build
manage.py deployment procedure.

If you look critically at our manage.py file, you should see a deploy function that runs migrations. You can customize this function to populate the database with initial data. For example, mine adds a default user to the database.

Having our database all set up now, we can then move on to preparing our celery thread queues.

Celery Queues

So Apache will not automatically start celery for you, this is why we employ the functionality of supervisor a process manager which makes managing several long-running programs a trivial task by providing a consistent interface through which they can be monitored and controlled.

We shall use supervisor to start and manage our celery threads.

  • First, we create a supervisor configuration file for our project nano /etc/supervisor/conf.d/celeryd.conf
  • Next, we add the different queues and sub-processes required to run celery successfully.
celeryd.conf file specifying celery beat and celery queue configurations
  • Now notify supervisor of a new configuration file.
Notify supervisor of a new configuration file.

Now that we have our celery tasks running, we can now complete the deployment by working on apache.

Apache2 Deployment

In previous python versions (2.7 and below), apache build mods to handle wsgi projects, however, in current python versions, a python package mod_wsgi was built to help with wsgi deployment.

For this to work, you’ll need to install the apache2-dev and python3.6-dev packages.

Installing mod_wsgi for python3.6

Now that we have mod_wsgi installed, we are going to create a custom apache2 mod that we will enable to run python wsgi projects.

Create an apache2 module wsgi_express.{conf,load} with the contents copied in output above in /etc/apache2/mods-available

Enable the module sudo a2enmod wsgi_express

Restart apache2 and go get a cup of coffee.

Apache2 will now have the ability to run our Python3.6 projects.

Next, update your application.wsgi file to match your production environment needs.

application.wsgi

Then create an apache 2 configuration file for the project, I like to split my Apache2 Configuration file into the main file and an include file.

The include file contains major project configurations which I’ll re-use in the main file. The include file will look as.

Apache 2 WSGI Include configuration file.

Replace the Server details, WSGI Process details, Application User details and SSL details with your appropriate values.

We can now simply create a main configuration file and include our configuration.

All that is now left is to enable the site, reload apache2 and test your site.

Congratulations, you have a flask app running on Apache 2. If you run into any errors, always check your apache2 log file to ascertain the cause of the error.

Thanks for reading.

--

--