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.
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:
virtualenv --python=python3.6 venv
source PATH_TO_VIRTUAL_ENV/bin/activate
pip install -r requirements/common.txt
export FLASK_CONFIG=production
export DATABASE_URL="mysql+pymysql://MYSQL_USER:MYSQL_PASSWORD@MYSQL_HOST:MYSQL_PORT/MYSQL_DATABASE"
python manage.py deploy
python manage.py build
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 log types to the database.
@manager.command
def deploy():
"""Run deployment tasks."""
from flask.ext.migrate import upgrade
# migrate database to latest revision
upgrade()
LogType.insert_log_types()
print('>>> Deployment Successful')
Having our database all set up now, we can then move on to preparing our celery thread 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.
vi /etc/supervisor/conf.d/celeryd.conf
; ==================================
; Dev API supervisor
; ==================================
[group:<app>-dev]
program=<app>-dev-api
[program:<app>-dev-api]
command=make start_server
directory=//absolute-directory
user=user
numprocs=1
stdout_logfile=/var/log/<app>/worker/out.log
stdout_logfile_maxbytes=10MB
stderr_logfile=/var/log/<app>/worker/err.log
stderr_logfile_maxbytes=10MB
autostart=true
autorestart=true
startsecs=5
stopsignal=INT
stopwaitsecs = 60
stopasgroup=true
priority=997
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start <app>-dev
sudo supervisorctl status
Now that we have our celery tasks running, we can now complete the deployment by working on apache.
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.
sudo apt-get install apache2-dev python3.6-dev
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.
.venv/bin/mod_wsgi-express install-module
LoadModule wsgi_module "/usr/lib/apache2/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "PATH_TO_VIRTUAL_ENV"
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.
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.