Flask: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
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>


==Apache2==
==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
  https://cdn.chorke.org/rest
Line 87: Line 87:
{|
{|
| valign="top" |
| valign="top" |
<source lang="bash">
<syntaxhighlight lang="bash">
python3 -m venv ~/.venv/flask --prompt="Flask"
python3 -m venv ~/.venv/flask --prompt="Flask"
# source ~/.venv/flask/bin/activate
# source ~/.venv/flask/bin/activate
# (e)flask $
# (e)flask $
</source>
</syntaxhighlight>


| valign="top" |
| valign="top" |
<source lang="bash">
<syntaxhighlight lang="bash">
python3 -m venv ~/.venv/flask --prompt="Flask"
python3 -m venv ~/.venv/flask --prompt="Flask"
# source ~/.venv/flask/bin/activate
# source ~/.venv/flask/bin/activate
# (e)flask $
# (e)flask $
</source>
</syntaxhighlight>


| valign="top" |
| valign="top" |
<source lang="PowerShell">
<syntaxhighlight lang="PowerShell">
python -m venv ~\.venv\flask --prompt="Flask"
python -m venv ~\.venv\flask --prompt="Flask"
# ~\.venv\flask\Scripts\activate
# ~\.venv\flask\Scripts\activate
# (e)flask PS>
# (e)flask PS>
</source>
</syntaxhighlight>


|-
|-
Line 112: Line 112:
|-
|-
| valign="top" |
| valign="top" |
<source lang="bash">
<syntaxhighlight lang="bash">
a2enmod  wsgi
a2enmod  wsgi
a2dismod wsgi
a2dismod wsgi
</source>
</syntaxhighlight>


| valign="top" |
| valign="top" |
<source lang="bash">
<syntaxhighlight lang="bash">
apache2ctl -t
apache2ctl -t
apache2ctl -M
apache2ctl -M
</source>
</syntaxhighlight>


| valign="top" |
| valign="top" |
<source lang="bash">
<syntaxhighlight lang="bash">
systemctl enable --now apache2
systemctl enable --now apache2
systemctl status apache2
systemctl status apache2
</source>
</syntaxhighlight>


|-
|-
Line 134: Line 134:
|-
|-
| valign="top" |
| valign="top" |
<source lang="bash">
<syntaxhighlight lang="bash">
  sudo chown -R www-data:www-data /opt/env/python/RestApi
  sudo chown -R www-data:www-data /opt/env/python/RestApi
  sudo chown -R www-data:www-data /opt/www/flask/RestApi
  sudo chown -R www-data:www-data /opt/www/flask/RestApi
  timeout 60 telnet localhost 1983 ;printf '\n'
  timeout 60 telnet localhost 1983 ;printf '\n'
</source>
</syntaxhighlight>


| valign="top" |
| valign="top" |
<source lang="bash">
<syntaxhighlight lang="bash">
  nohup python RestApi/RestApi/__init__.py>RestApi.log &
  nohup python RestApi/RestApi/__init__.py>RestApi.log &
  ssh -L 1983:localhost:1983 [email protected]
  ssh -L 1983:localhost:1983 [email protected]
  nc -w 60 localhost 1983 ;printf '\n'
  nc -w 60 localhost 1983 ;printf '\n'
</source>
</syntaxhighlight>


| valign="top" |
| valign="top" |
<source lang="bash">
<syntaxhighlight lang="bash">
sudo apt install net-tools
sudo apt install net-tools
ss -lptn 'sport = :1983'
ss -lptn 'sport = :1983'
netstat -nlp | grep :1983
netstat -nlp | grep :1983
</source>
</syntaxhighlight>


|}
|}

Revision as of 03:13, 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