Convention for WSGI

From Chorke Wiki
Jump to navigation Jump to search

Python3

apt update
apt install python3
apt install python3.7

Flask

mkdir -p /var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/{static,templates}
cat > /var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/__init__.py << EOF
from flask import Flask, json, jsonify
app = Flask(__name__)

@app.route("/")
def home():
    return jsonify(
        success = True,
        message = "Welcome, Academian",
        data    = [],
        total   = 0
    )

@app.route('/users')
def users():
    return jsonify(
        success = True,
        message = "success",
        data    = [],
        total   = 0
    )

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

Wsgi3

cat > /var/chorke/www/api.chorke.org/wsgi/flask/RestApi/wsgi.py << EOF
#!/usr/bin/python
activate_this = '/var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/venv/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/chorke/www/api.chorke.org/wsgi/flask/RestApi/")

from RestApi import app as application
application.secret_key = 'Add your secret key'
EOF

Apache2

Wsgi3

apt list --upgradable
apt install python3-pip
apt install python-virtualenv
apt install python3-virtualenv
apt install python3-pip --fix-missing
apt install libapache2-mod-wsgi-py3 python3-dev
a2enmod wsgi
apache2ctl -t
apache2ctl -M
systemctl status apache2
systemctl enable apache2
systemctl restart apache2

Flask

virtualenv /var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/venv -p `which python3.7`
virtualenv /var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/venv --python=python3.7
virtualenv /var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/venv --python=python3
source /var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/venv/bin/activate
pip install Flask
deactivate

Site

nano /etc/chorke/apache2/conf.sites.all.d/01-api.chorke.org-le-ssl.conf

WSGIScriptAlias /wsgi/rest "/var/chorke/www/api.chorke.org/wsgi/flask/RestApi/wsgi.py"
<Directory "/var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi">
    Order Allow,Deny
    Allow from all
</Directory>

Alias /wsgi/rest/static "/var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/static/"
<Directory "/var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/static">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    Order Allow,Deny
    Allow from all
</Directory>
apache2ctl -t
systemctl status apache2
systemctl restart apache2
cat /var/chorke/www/api.chorke.org/error.log
echo `curl -s -L https://api.chorke.org/wsgi/rest`

Good To Know

virtualenv venv --python=python3.6
source venv/bin/activate

nohup python RestApi/RestApi/__init__.py>RestApi.log &
ssh -L 5000:localhost:5000 [email protected]

apt install net-tools
ss -lptn 'sport = :5000'
netstat -nlp | grep :5000

References