Until it works with virtualenv + flask + apache + wsgi

Until it works with virtualenv + flask + apache + wsgi

ʻApache` runs the flow to run the flask application.

With Werkzeug, it's pretty easy to start, but ʻApache+mod_wsgi` raises the hurdle as soon as it becomes, so I summarized it roughly.

Since all are for mac, please read the path etc. as appropriate.

Initial setting

Enter virtualenv referring to this area. http://qiita.com/k2tanaka/items/5f111612ec1b6d7584a6

mkvirtualenv -p ~/.pythonz/pythons/CPython-2.7.6/bin/python2.7  v2.7.6

workon and pip up to flask.

workon v2.7.6
pip install flask

Application part

The contents are very suitable. For the time being, with a minimum example.

Constitution

Make sure to match the file path with the conf of ʻapache` after this.

/Users/kuryu/workspace/test/flask
├── app.wsgi
├── app_templates
│   └── index.html
└── main.py

contents

app.wsgi


# -*- coding:utf-8 -*-

import sys, os

import logging
#Needed to log apache
logging.basicConfig(stream = sys.stderr)

sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))

from main import app as application

main.py


#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os
from flask import Flask, render_template
from jinja2 import FileSystemLoader

app = Flask(__name__)
#Try changing the template directory of jinja2
#It doesn't have to be separate.
#If omitted, the same hierarchy as this file"templates"become.
app.jinja_loader = FileSystemLoader(
        os.path.join(os.path.abspath(os.path.dirname(__file__)), 'app_templates')
    )

@app.route("/")
def index():
    from flask import render_template
    return render_template('index.html')

@app.route("/foo")
def foo():
    return "foo"

@app.route("/foo/bar")
def foo_bar():
    return "foobar"

if __name__ == "__main__":
    app.run()

app_templates/index.html


hogehoge

apache settings

How to insert mod_wsgi

Is omitted. Maybe linux comes in with ʻapt or yum, or it comes with ʻapache from the beginning.

on mac

brew tap homebrew/apache
brew install mod_wsgi

do it

brew info mod_wsgi

If you do, you will know the pass.

conf settings

Since vitualenv is used, specify the location of python with WSGIPythonHome.

flask.conf


## mod_wsgi
LoadModule wsgi_module /usr/local/Cellar/mod_wsgi/3.5/libexec/mod_wsgi.so

WSGIPythonHome /Users/kuryu/.virtualenvs/v2.7.6
WSGIDaemonProcess test user=kuryu group=staff threads=5
WSGIScriptAlias /test /Users/kuryu/workspace/test/flask/app.wsgi
WSGISocketPrefix /var/run/wsgi

<Directory /Users/kuryu/workspace/test/flask>
    Options +ExecCGI
    SetHandler wsgi-script
    AddHandler wsgi-script .wsgi
    Order deny,allow
    Allow from all
</Directory>

ʻApache` Reboot.

sudo apachectl restart

Try to hit

Since / test is specified by WSGIScriptAlias, the url is like this.

curl localhost/test/
curl localhost/test/foo
curl localhost/test/foo/bar

that's all.

90% of the causes of annoyance are ʻapache` ...

Recommended Posts

Until it works with virtualenv + flask + apache + wsgi
Flask + Gunicorn + Nginx + Supervisor Until it works
Run Apache2 + WSGI + Flask
Until you run python with apache
Environment construction of Flask / MySql / Apache / mod_wsgi / virtualenv with Redhat7 (Python2.7) November 2020
Until it turns light blue with AtCoder
Until you install Python with pythonbrew and run Flask on a WSGI server
POST the image with json and receive it with flask
Quickly build Apache + Pipenv + Flask + SQLAlchemy with Sakura VPS