Flask deploy into Apache on Ubuntu
Environment
- Ubuntu 16.04.3 LTS
- Apache 2.4.18
- the command of finding apache2 vesion is
apache2 -v
- the command of finding apache2 vesion is
- mod_wsgi 4.3.0
- Python 3.5.2
Project Structure
- log: sotre apache log such as error log and access log
- static: sotre static file such as image, css, js
- templates: store html file
- venv: the project's virtual environment
- web.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello world!"
- app.wsgi
import sys
sys.path.insert(0, '/your_project_path_in_server/MyApp')
from web import app as application
Deployment Process
- update apt-get
$ sudo apt-get update
- install apache2
$ sudo apt-get install apache2
- install mod_wsgi
# Python 2 using this command $ sudo apt-get install libapache2-mod-wsgi # Python 3 using this command $ sudo apt-get install libapache2-mod-wsgi-py3
add a new apache site config
Apache2 site config path is at/etc/apache2/sites-available
, and add a site config in this folder.
For example, create amyapp.conf
using vim command.$ sudo vim myapp.conf
And add the following content in the config.
# define a variable which is your project path Define PROJECT_PATH /your_project_path/MyApp <virtualhost *:80> # wsgi settings WSGIDaemonProcess myapp python-path=${PROJECT_PATH}:${PROJECT_PATH}/venv/lib/python3.5/site-packages # venv path WSGIProcessGroup myapp WSGIScriptAlias / ${PROJECT_PATH}/app.wsgi # wsgi script path # map server side static directory to {ip or domain_name}/static Alias /static ${PROJECT_PATH}/static # allow all requests to access this project's file <Directory ${PROJECT_PATH}/> Require all granted </Directory> # set log saved path ErrorLog ${PROJECT_PATH}/log/error.log CustomLog ${PROJECT_PATH}/log/access.log combined </virtualhost>
- enable this site config
$ sudo a2ensite myapp
- start apache server
$ sudo service apache2 start
- Now, you should be able to visit your application website.
If you encounter some problems, you can find the problems in your project error.log file.