Flask: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
 
(29 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Python3==
==Python3==
<source lang="bash">
<syntaxhighlight lang="bash">
apt install python3.8
apt install python3.8
pip3 install virtualenv
pip3 install virtualenv
Line 9: Line 9:
pip install Flask
pip install Flask
deactivate
deactivate
</source>
</syntaxhighlight>


===Flask===
===Flask===
<source lang="bash" highlight="1,2" line>
<syntaxhighlight lang="bash" highlight="1,2" line>
mkdir -p /var/www/flask/RestApi/RestApi/{static,templates}
mkdir -p /var/www/flask/RestApi/RestApi/{static,templates}
cat > /var/www/flask/RestApi/RestApi/__init__.py << EOF
cat > /var/www/flask/RestApi/RestApi/__init__.py << EOF
Line 23: Line 23:
     app.run()
     app.run()
EOF
EOF
</source>
</syntaxhighlight>


===WSGI3===
===WSGI3===
<source lang="bash" highlight="1,2,4-6,11" line>
<syntaxhighlight lang="bash" highlight="1,2,4-6,11" line>
cd /var/www/flask/RestApi/
cd /var/www/flask/RestApi/
cat > /var/www/flask/RestApi/RestApi.wsgi << EOF
cat > /var/www/flask/RestApi/RestApi.wsgi << EOF
Line 42: Line 42:
application.secret_key = 'Add your secret key'
application.secret_key = 'Add your secret key'
EOF
EOF
</source>
</syntaxhighlight>


==Apache3==
==Apache2==
===WSGI3===
===WSGI3===
<source lang="bash">
<syntaxhighlight lang="bash">
apt install apache2
apt install apache2
systemctl restart apache2
systemctl restart apache2
apt install libapache2-mod-wsgi python-dev
apt install libapache2-mod-wsgi python-dev
a2enmod wsgi
a2enmod wsgi
</source>
</syntaxhighlight>


<source lang="bash">
<syntaxhighlight lang="bash">
a2dismod wsgi
a2dismod wsgi
apt remove libapache2-mod-wsgi python-dev
apt remove libapache2-mod-wsgi python-dev
apt install libapache2-mod-wsgi-py3 python3-dev
apt install libapache2-mod-wsgi-py3 python3-dev
</source>
</syntaxhighlight>


<source lang="bash">
<syntaxhighlight lang="bash">
a2enmod wsgi
a2enmod wsgi
apache2ctl -t
apache2ctl -t
systemctl enable apache2
systemctl enable apache2
systemctl restart apache2
systemctl restart apache2
</source>
</syntaxhighlight>


===SITE===
===SITE===
<code>vim /etc/apache2/sites-available/000-default.conf</code>
<code>vim /etc/apache2/sites-available/000-default.conf</code>
<source lang="apache" highlight="1,2,7,8" start="15" line>
<syntaxhighlight lang="apache" highlight="1,2,7,8" start="15" line>
WSGIScriptAlias /rest "/var/www/flask/RestApi/RestApi.wsgi"
WSGIScriptAlias /rest "/var/www/flask/RestApi/RestApi.wsgi"
<Directory "/var/www/flask/RestApi/RestApi">
<Directory "/var/www/flask/RestApi/RestApi">
Line 80: Line 80:
     Allow from all
     Allow from all
</Directory>
</Directory>
</source>
</syntaxhighlight>
 
https://cdn.chorke.org/rest
 
==Knowledge==
{|
| valign="top" |
<syntaxhighlight lang="bash">
python3 -m venv ~/.venv/flask --prompt="Flask"
# source ~/.venv/flask/bin/activate
# (e)flask $
</syntaxhighlight>
 
| valign="top" |
<syntaxhighlight lang="bash">
python3 -m venv ~/.venv/flask --prompt="Flask"
# source ~/.venv/flask/bin/activate
# (e)flask $
</syntaxhighlight>
 
| valign="top" |
<syntaxhighlight lang="PowerShell">
python -m venv ~\.venv\flask --prompt="Flask"
# ~\.venv\flask\Scripts\activate
# (e)flask PS>
</syntaxhighlight>
 
|-
| colspan="3" |
----
|-
| valign="top" |
<syntaxhighlight lang="bash">
a2enmod  wsgi
a2dismod wsgi
</syntaxhighlight>
 
| valign="top" |
<syntaxhighlight lang="bash">
apache2ctl -t
apache2ctl -M
</syntaxhighlight>
 
| valign="top" |
<syntaxhighlight lang="bash">
systemctl enable --now apache2
systemctl status apache2
</syntaxhighlight>
 
|-
| colspan="3" |
----
|-
| valign="top" |
<syntaxhighlight lang="bash">
sudo chown -R www-data:www-data /opt/env/python/RestApi
sudo chown -R www-data:www-data /opt/www/flask/RestApi
timeout 60 telnet localhost 1983 ;printf '\n'
</syntaxhighlight>
 
| valign="top" |
<syntaxhighlight lang="bash">
nohup python RestApi/RestApi/__init__.py>RestApi.log &
ssh -L 1983:localhost:1983 [email protected]
nc -w 60 localhost 1983 ;printf '\n'
</syntaxhighlight>
 
| valign="top" |
<syntaxhighlight lang="bash">
sudo apt install net-tools
ss -lptn 'sport = :1983'
netstat -nlp | grep :1983
</syntaxhighlight>
 
|}


==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://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps Deploy a Flask Application on Ubuntu]
| valign="top" |
* [https://mozilla.github.io/nunjucks/templating.html Flask » JS Templating with Nunjucks inspired by Jinja]
* [https://stackoverflow.com/questions/1534210 Flask » Use different Python version with virtualenv]
* [https://techtutorguro.com/how-to-install-flask-on-ubuntu-22-04-with-apache/ Flask » Ubuntu 22.04 with Apache and WSGI]
* [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/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]
* [https://flask.palletsprojects.com/en/3.0.x/quickstart/ Flask » 3.0  » Quickstart]
* [https://flask.palletsprojects.com/en/3.0.x/api/ Flask » 3.0  » API]
* [https://flask.palletsprojects.com/en/3.0.x/ Flask » 3.0]
 
| valign="top" |
* [https://medium.com/@esteininger/python-3-5-flask-apache2-mod-wsgi3-on-ubuntu-16-04-67894abf9f70 Flask » Python3 » Apache2 Mod_WSGI on Ubuntu]
* [https://blog.stoplight.io/python-rest-api Flask » Quick Mocking REST API]
* [https://stackoverflow.com/questions/60537557/ Flask » REST server and client]
* [https://stackoverflow.com/questions/10434599/ Flask » Fetch Request Data]
* [https://stackoverflow.com/questions/21133976/ Flask » Load Local JSON]
* [https://stackoverflow.com/questions/20212894/ Flask » Run on port 80]
* [https://stackoverflow.com/questions/54566480/ Flask » Read a File]
 
| valign="top" |
 
|-
| colspan="3" |
----
|-
| valign="top" |
* [https://www.linode.com/docs/guides/python-variables/ Python » Getting Started with Variables]
* [https://realpython.com/python-sleep/ Python » Time Delays]
* [[Convention for WSGI]]
* [[TensorFlow]]
* [[WinSW]]
* [https://falconframework.org/ Falcon]
* [[Locale]]
 
| valign="top" |
 
| valign="top" |
 
|}

Latest revision as of 19:25, 20 April 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

Knowledge

python3 -m venv ~/.venv/flask --prompt="Flask"
# source ~/.venv/flask/bin/activate
# (e)flask $
python3 -m venv ~/.venv/flask --prompt="Flask"
# source ~/.venv/flask/bin/activate
# (e)flask $
python -m venv ~\.venv\flask --prompt="Flask"
# ~\.venv\flask\Scripts\activate
# (e)flask PS>

a2enmod  wsgi
a2dismod wsgi
apache2ctl -t
apache2ctl -M
systemctl enable --now apache2
systemctl status apache2

 sudo chown -R www-data:www-data /opt/env/python/RestApi
 sudo chown -R www-data:www-data /opt/www/flask/RestApi
 timeout 60 telnet localhost 1983 ;printf '\n'
 nohup python RestApi/RestApi/__init__.py>RestApi.log &
 ssh -L 1983:localhost:1983 [email protected]
 nc -w 60 localhost 1983 ;printf '\n'
sudo apt install net-tools
ss -lptn 'sport = :1983'
netstat -nlp | grep :1983

References