Convention for WSGI

From Chorke Wiki
Revision as of 02:17, 25 May 2020 by Shahed (talk | contribs) (Created page with "==Python3== <source lang="bash"> apt update apt install python3 apt install python3.7 </source> ===Flask=== <source lang="bash" highlight="1,2" line> mkdir -p /var/chorke/www...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello, Academian!"
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
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
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">
    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