Flask on Nethserver

I have finally gotten flask to work on nethserver, and I would like to make a howto, What do I do?

I think it will be surely appreciated :slight_smile:

Just Edit your first post with the instructions to make it work.
Cheers.

Finally I have gotten flask to work in the main html area, /var/www/html, lots of gotchas were encountered and google-fu-ed away.
Here are step by step instructions on how to wade into this swamp and get er done.
This is for nethserver7.

#Install Prerequisites:
yum -y install gcc openssl-devel python-devel httpd-devel mysql-devel python3 python3-devel nano bzip2-devel libffi-devel python-pip mlocate groupinstall ‘development tools’

#Make sure the system is stable:
#In nethserver this is a very good command that stabilizes changes
/etc/e-smith/events/actions/system-adjust

Install mod_wsgi, if you have upgraded pip3 already then there will be an extra module install of mod_wsgi in /etc/httpd/conf.modules.d/ and needs to be removed. You need the canonical location for the python site packages to be discovered.

PIP Install of Mod Wsgi:

this first install for some reason helps the next install to work!! Don’t know why just does.

pip3 install mod_wsgi

and again

pip3 install --target=/path/to/python3.x/site-packages mod_wsgi

#Check to see if it worked:
mod_wsgi-express module-config

Hmm only allowed two links, none were entered. Here is more

#Result you should get something like this, if not some fiddling is in order as you cannot go further until it #is fixed:
LoadModule wsgi_module “/usr/local/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so”
WSGIPythonHome “/usr”

now check to see if mod_wsgi is loaded, if it is you need to remove the snippit from the conf.modules.d #folder

sudo httpd -M | grep wsgi

not found , which at the moment is what we need. Now we change the httpd.conf so it will all work.

mkdir -p /etc/e-smith/templates-custom/etc/httpd/conf.d/nethserver.conf
cd /etc/e-smith/templates-custom/etc/httpd/conf.d/nethserver.conf

#Make the module snippit, I;m putting it here as it still works and it is old habit from 20 years with
#e-smith and I like to keep everything in one place.
nano 20LoadModuleWSGI_MOD

enter this line from the test above and then ctrl-o crtl-x

LoadModule wsgi_module “/usr/local/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so”

#magic
/etc/e-smith/events/actions/system-adjust

test

sudo httpd -M | grep wsgi

win! result, otherwise, more fiddling

wsgi_module (shared)

#Make the errorlog snippit
nano 20wsgierrorlog

ErrorLog: The location of the error log file. If this does not start

with /, ServerRoot is prepended to it.

ErrorLog /var/log/httpd/error_log

LogLevel: Control the number of messages logged to the error_log.

Possible values include: debug, info, notice, warn, error, crit,

alert, emerg.

LogLevel info

more essentiials

nano 90WSGIstuff

WSGIScriptAlias / /var/www/html/myapp.wsgi

#stabilize
/etc/e-smith/events/actions/system-adjust

#Now test the apache config
apachectl configtest

at this point you may want to use a virtual environment especially if you plan to host multiple

python websites on the server, that is more than I will describe here but it is possible to do and google # can lead you there.

make the wsgi file, there are two choices here, one for a simple app and one for a factory app

cd /var/www/html
nano myapp.wsgi

This one is for a simple python script

-- coding: utf-8 --

import sys
import os
project = “app”

Use instance folder, instead of env variables.

specify dev/production config

#os.environ[’%s_APP_CONFIG’ % project.upper()] = ‘’

http://code.google.com/p/modwsgi/wiki/ApplicationIssues#User_HOME_Environment_Variable

#os.environ[‘HOME’] = pwd.getpwuid(os.getuid()).pw_dir
BASE_DIR = os.path.join(os.path.dirname(file))
if BASE_DIR not in sys.path:
sys.path.append(BASE_DIR)

give wsgi the “application”

from app import app as application

This one is for a complex python factory script that starts with init

-- coding: utf-8 --

import sys
import os
project = “app”

Use instance folder, instead of env variables.

specify dev/production config

os.environ[’%s_APP_CONFIG’ % project.upper()] = ‘’

http://code.google.com/p/modwsgi/wiki/ApplicationIssues#User_HOME_Environment_Variable

os.environ[‘HOME’] = pwd.getpwuid(os.getuid()).pw_dir

BASE_DIR = os.path.join(os.path.dirname(file))
if BASE_DIR not in sys.path:
sys.path.append(BASE_DIR)

give wsgi the “application”

from app import app as application
application

This is the simple layout

html
myapp.wsgi
app.py

static
css
images
js
uploads
templates

This is the complex layout

Primary

html
myapp.wsgi
config.py

app
init.py

blueprints
static
css
images
js
uploads
templates

#now
pip3 install -r requirements.txt

#This a Test app:
nano app.py

from flask import Flask
app = Flask(name)
@app.route(’/’)
def hello_world():
return ‘Hello, World, God I hope this works…’
if name == ‘main’:
app.run(
host=“0.0.0.0”,
port=int(“80”),
debug=True
)

Turn on FTP access so you can transfer files to the site and chmod the /var/www/http folder to allow

write access

applications/webserver/ftp

go to system/services and edit the mysqld firewall access to get the ability to remote into the

database

so you can use mysqlworkbench to mess with the database.

#create the database

ENJOY!

Visit my site at gidout.com, It is a work in progress

Dang this editor really messes with layout, ugly

Formatting is done with Markdown; here are some of the basics:
https://commonmark.org/help/

I’ve also updated your trust level so you shouldn’t have the limits on links any more.

In addition some tips on formatting:

BTW, the first post is wikified so it can be easily edited.

Having a plain text choice would work for me, If anyone nees more just DM me here

Cleaned it up

Finally I have gotten Flask to work in the main html area, /var/www/html, lots of gotchas were encountered and google-fu-ed away.
Here are step by step instructions on how to wade into this swamp and get er done.
This is for nethserver7.

Install Prerequisites:

yum -y install gcc openssl-devel python-devel httpd-devel mysql-devel python3 python3-devel nano bzip2-devel libffi-devel python-pip mlocate groupinstall 'development tools'

Make sure the system is stable: In nethserver this is a very good command that stabilizes changes

/etc/e-smith/events/actions/system-adjust  

Install mod_wsgi, if you have upgraded pip3 already then there will be an extra module install of mod_wsgi in /etc/httpd/conf.modules.d/ and needs to be removed. This is because the canonical location is needed for the python site packages to be discovered.

PIP Install of Mod Wsgi: this first install for some reason helps the next install to work!! Don’t know why just does.

pip3 install mod_wsgi 

and again

pip3 install --target=/path/to/python3.x/site-packages mod_wsgi

Check to see if it worked:

mod_wsgi-express module-config

Result, You should get something like this, if not some fiddling is in order as you cannot go further until it is fixed:

LoadModule wsgi_module “/usr/local/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so”
WSGIPythonHome “/usr”

now check to see if mod_wsgi is loaded, if it is you need to remove the snippit from the conf.modules.d folder

sudo httpd -M | grep wsgi

If not found, which at the moment is what we need. Now we change the httpd.conf so it will all work.

mkdir -p /etc/e-smith/templates-custom/etc/httpd/conf.d/nethserver.conf
cd /etc/e-smith/templates-custom/etc/httpd/conf.d/nethserver.conf

Make the module snippit, I’m putting it here in nethserver.conf as it still works and it is old habit from 20 years with e-smith and I like to keep everything in one place.

nano 20LoadModuleWSGI_MOD

enter this line from the test above and then ctrl-o crtl-x

LoadModule wsgi_module "/usr/local/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"

magic

/etc/e-smith/events/actions/system-adjust

test

sudo httpd -M | grep wsgi

win! If not good result, more fiddling

wsgi_module (shared)

Make the errorlog snippit

nano 20wsgierrorlog
#ErrorLog: The location of the error log file. If this does not start with /, 
#ServerRoot is prepended to it.
ErrorLog /var/log/httpd/error_log
#LogLevel: Control the number of messages logged to the error_log.
#Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel info

more essentiials

nano 90WSGIstuff
WSGIScriptAlias / /var/www/html/myapp.wsgi

stabilize

/etc/e-smith/events/actions/system-adjust

Now test the apache config

apachectl configtest

at this point you may want to use a virtual environment especially if you plan to host multiple

python websites on the server, that is more than I will describe here but it is possible to do and google can lead you there.

make the wsgi file, there are two choices here, one for a simple app and one for a factory app

cd /var/www/html
nano myapp.wsgi

This one is for a simple python script

# -*- coding: utf-8 -*-
import sys
import os
project = "app"
# Use instance folder, instead of env variables.
# specify dev/production config
#os.environ['%s_APP_CONFIG' % project.upper()] = ''
# http://code.google.com/p/modwsgi/wiki/ApplicationIssues#User_HOME_Environment_Variable
#os.environ['HOME'] = pwd.getpwuid(os.getuid()).pw_dir
BASE_DIR = os.path.join(os.path.dirname(__file__))
if BASE_DIR not in sys.path:
    sys.path.append(BASE_DIR)
# give wsgi the "application"
from app import app as application

This one is for a complex python factory script that starts with init

# -*- coding: utf-8 -*-
import sys
import os
project = "app"
# Use instance folder, instead of env variables.
# specify dev/production config
# os.environ['%s_APP_CONFIG' % project.upper()] = ''
# http://code.google.com/p/modwsgi/wiki/ApplicationIssues#User_HOME_Environment_Variable
# os.environ['HOME'] = pwd.getpwuid(os.getuid()).pw_dir
BASE_DIR = os.path.join(os.path.dirname(__file__))
if BASE_DIR not in sys.path:
    sys.path.append(BASE_DIR)
# give wsgi the "application"
from app import app as application
application

This is the simple layout

>>html
    myapp.wsgi
    app.py
    >>static
      >>css
      >>images
      >>js
      >>uploads
    >>templates

This is the complex layout

Primary
>>html
  myapp.wsgi
  config.py
  >>app
    __init__.py
    >>blueprints
    >>static
      >>css
      >>images
      >>js
      >>uploads
    >>templates

now install all the modules you need

pip3 install -r requirements.txt

This a Test app:

nano app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello, World, God I hope this works...'
if __name__ == '__main__':
    app.run(
        host="0.0.0.0",
        port=int("80"),
        debug=True
        )

Turn on FTP access so you can transfer files to the site and chmod the /var/www/http folder to allow write access applications/webserver/ftp

go to system/services and edit the mysqld firewall access to the local network to get the ability to remote into the database so you can use mysqlworkbench to mess with the database.

create the database

ENJOY!

Visit my site at gidout.com, It is a work in progress