Convention for WSGI: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 10: Line 10:
mkdir -p /var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/{static,templates}
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
cat > /var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/__init__.py << EOF
from flask import Flask
from flask import Flask, json, jsonify
app = Flask(__name__)
app = Flask(__name__)
@app.route("/")
@app.route("/")
def hello():
def home():
     return "Hello, Academian!"
     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__":
if __name__ == "__main__":
     app.run()
     app.run()
Line 58: Line 74:
</source>
</source>


===Flask===
<source lang="bash">
<source lang="bash">
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 -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.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
source /var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/venv/bin/activate
pip install Flask
pip install Flask
Line 68: Line 86:
===Site===
===Site===
<code>nano /etc/chorke/apache2/conf.sites.all.d/01-api.chorke.org-le-ssl.conf</code>
<code>nano /etc/chorke/apache2/conf.sites.all.d/01-api.chorke.org-le-ssl.conf</code>
<source lang="apache" highlight="1,2,7,8" start="15" line>
<source lang="apache" highlight="1,2,7,8">
WSGIScriptAlias /wsgi/rest "/var/chorke/www/api.chorke.org/wsgi/flask/RestApi/wsgi.py"
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">
<Directory "/var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi">
Line 76: Line 94:


Alias /wsgi/rest/static "/var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/static/"
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">
<Directory "/var/chorke/www/api.chorke.org/wsgi/flask/RestApi/RestApi/static">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
     Order Allow,Deny
     Order Allow,Deny
     Allow from all
     Allow from all

Latest revision as of 06:13, 25 May 2020

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