Flask: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
Line 111: Line 111:


==References==
==References==
* [https://medium.com/@esteininger/python-3-5-flask-apache2-mod-wsgi3-on-ubuntu-16-04-67894abf9f70 Python3 Flask config by using Apache2 Mod_WSGI on Ubuntu]
{|
* [https://techtutorguro.com/how-to-install-flask-on-ubuntu-22-04-with-apache/ Flask on Ubuntu 22.04 with Apache and WSGI]
| valign="top" |
* [https://mozilla.github.io/nunjucks/templating.html JS Templating with Nunjucks inspired by Jinja]
* [https://medium.com/@esteininger/python-3-5-flask-apache2-mod-wsgi3-on-ubuntu-16-04-67894abf9f70 Flask » Python3 » Using Apache2_WSGI Module on Ubuntu]
* [https://stackoverflow.com/questions/1534210 Use different Python version with virtualenv]
* [https://mozilla.github.io/nunjucks/templating.html Flask » JS Templating with Nunjucks inspired by Jinja]
* [https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps Deploy a Flask Application on Ubuntu]
* [https://stackoverflow.com/questions/1534210 Flask » Use different Python version with virtualenv]
* [https://flask.palletsprojects.com/en/1.1.x/deploying/wsgi-standalone Standalone WSGI Containers]
* [https://techtutorguro.com/how-to-install-flask-on-ubuntu-22-04-with-apache/ Flask » Ubuntu 22.04 with Apache and WSGI]
* [https://flask.palletsprojects.com/en/1.1.x/tutorial/templates/ Jinja Templating Tutorial]
* [https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps Flask » Deploy Application on Ubuntu]
* [https://flask.palletsprojects.com/en/1.1.x/templating/ Templating with Jinja]
* [https://flask.palletsprojects.com/en/1.1.x/deploying/wsgi-standalone Flask » Standalone WSGI Containers]
* [https://flask.palletsprojects.com/en/1.1.x/tutorial/templates/ Flask » Jinja Templating Tutorial]
* [https://flask.palletsprojects.com/en/1.1.x/templating/ Flask » Templating with Jinja]
 
| valign="top" |
 
| valign="top" |
 
|-
| colspan="3" |
----
|-
| valign="top" |
* [[Convention for WSGI]]
* [[Convention for WSGI]]
* [[WinSW]]
| valign="top" |
| valign="top" |
|}

Revision as of 08:48, 9 February 2024

Python3

apt install python3.8
pip3 install virtualenv
cd /var/www/flask/RestApi/RestApi/

virtualenv venv
source venv/bin/activate
pip install Flask
deactivate

Flask

mkdir -p /var/www/flask/RestApi/RestApi/{static,templates}
cat > /var/www/flask/RestApi/RestApi/__init__.py << EOF
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello, Bangladesh!"
if __name__ == "__main__":
    app.run()
EOF

WSGI3

cd /var/www/flask/RestApi/
cat > /var/www/flask/RestApi/RestApi.wsgi << EOF
#!/usr/bin/python
activate_this = '/var/www/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/www/flask/RestApi/")

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

Apache2

WSGI3

apt install apache2
systemctl restart apache2
apt install libapache2-mod-wsgi python-dev
a2enmod wsgi
a2dismod wsgi
apt remove libapache2-mod-wsgi python-dev
apt install libapache2-mod-wsgi-py3 python3-dev
a2enmod wsgi
apache2ctl -t
systemctl enable apache2
systemctl restart apache2

SITE

vim /etc/apache2/sites-available/000-default.conf

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

Alias /rest/static "/var/www/flask/RestApi/RestApi/static/"
<Directory "/var/www/flask/RestApi/RestApi/static">
    Order Allow,Deny
    Allow from all
</Directory>
https://cdn.chorke.org/rest

Good To Know

a2enmod wsgi
a2dismod wsgi

apache2ctl -t
apache2ctl -M

systemctl status apache2
systemctl enable apache2
systemctl restart apache2

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

sudo chown -R www-data:www-data /opt/env/python/RestApi
sudo chown -R www-data:www-data /opt/www/flask/RestApi

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