Koozali.org formerly Contribs.org

How to deploy a Flask web service for production on SME 9.2,Apache and mod_wsgi?

Hi All,

I have deployed a Flask web service on SME 9.2, and I am able to run the service using the built-in Flask web server, and it works as expected.  Now for production, I need to deploy the web service on Apache and use mod_wsgi as the gateway.  I am confused as to how to do this.  I know that I will need to create a custom template for Apache to know about my Flask web service.  I'm not sure what that file would look like as far as the content.  Do I define <location> or <virtualhost>.  Has anyone deployed a Flask app or web service on Apache/mod_wsgi for production on SME 9.2?

Thank you for any assistance!

Sara
« Last Edit: August 31, 2019, 03:48:25 AM by swatson »

Offline ReetP

  • *
  • 2,167
Search & read the wiki.

This may help.

https://wiki.contribs.org/Webapps-common

To.do it manually check these.

https://wiki.contribs.org/SME_Server:Documentation:ProxyPass

Particularly read about 'TemplatePath'

I really must write a bit more on it.

If you set it you can create your own virtual host section with templates in:

/etc/e-smith/templates/etc/httpd/httpd.conf/myVirtualHostTemplatePath.

See how webapps does it.

The Rocket.Chat contrib does it so it wasn't reliant on webapps. Check the source.
...
1. Read the Manual
2. Read the Wiki
3. Don't ask for support on Unsupported versions
4. I have a job, wife, and kids and do this in my spare time. If you want something fixed, please help.

Bugs are easier than you think: http://wiki.contribs.org/Bugzilla_Help

If you love SME and don't want to lose it, join in: http://wiki.contribs.org/Koozali_Foundation

Thank you for your response.  I will take a look.

...for production, I need to deploy the web service on Apache and use mod_wsgi as the gateway.  I am confused as to how to do this.  I know that I will need to create a custom template for Apache to know about my Flask web service.  I'm not sure what that file would look like as far as the content.  Do I define <location> or <virtualhost>.  Has anyone deployed a Flask app or web service on Apache/mod_wsgi for production on SME 9.2

Thanks for chatting with me about this. To recap our conversation...
First, make certain the mod_wsgi.so is installed on the system. Yum can be used for this.
Second, create a custom template to load the mod_wsgi.so in Apache. This is a file that contains an Apache LoadModule directive. See the koozali docs as well as the Apache docs.
Finally, create a custom template to define an Apache Location. A location is part of a URL, such as a location “/status” that appears in a fully formed URL. E.g. www.mydomain/status?p=now.

The Flask docs should contain examples of the details for telling Apache how to communicate with the Flask app via wsgi, which can then be adapted to the template system.
- Mark

Hello, These are my notes of how I have my site https://gidout.com on a 9.2 server in the Primary bay. I was a pain as 9.2 is so very old. Flask needs python 2.7. 
Code: [Select]
Install sme 9.2

yum update

yum -y install gcc openssl-devel python-devel httpd-devel mysql-devel
signal-event post-upgrade; signal-event reboot

config setprop passwordstrength Users none

Python AltInstall

cd home
wget -N http://www.python.org/ftp/python/2.7.12/Python-2.7.12.tgz --no-check-certificate;
tar zxvf Python-2.7.12.tgz
./configure --enable-shared --prefix=/usr/local LDFLAGS="-Wl,--rpath=/usr/local/lib"
make; make altinstall;
ldconfig

cd ..
wget -N --quiet https://bootstrap.pypa.io/get-pip.py --no-check-certificate
python2.7 get-pip.py

MOD_WSGI
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.5.24.tar.gz
tar xvfz 4.5.24.tar.gz
cd mod_wsgi-4.5.24
./configure --with-python=/usr/local/bin/python2.7
make
make install

ln -s /usr/local/lib/libpython2.7.so.1.0 /usr/lib/libpython2.7.so.1.0
ldconfig
ldd /etc/httpd/modules/mod_wsgi.so

CONFIGURE HTTPD

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

nano 10ErrorLog
# 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

nano 20LoadModuleWSGI_MOD
LoadModule wsgi_module modules/mod_wsgi.so

nano 90WSGIstuff
WSGIScriptAlias / /home/e-smith/files/ibays/Primary/html/myapp.wsgi

expand-template /etc/httpd/conf/httpd.conf
/usr/sbin/apachectl configtest
sv t /service/httpd-e-smith

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

pip2.7 install flask
pip2.7 install googlemaps
pip2.7 install safe
pip2.7 install flask_security
pip2.7 install flask_principal
pip2.7 install blinker
pip2.7 install xtermcolor
pip2.7 install flask-uploads
pip2.7 install logging
pip2.7 install flask_migrate
pip2.7 install flask_mail
pip2.7 install flask_pagedown
pip2.7 install flask_moment
pip2.7 install flask_login
pip2.7 install flask_wtf
pip2.7 install flask_sqlalchemy
pip2.7 install pymysql
pip2.7 install paypalrestsdk
pip2.7 install mysql-python

cat /etc/httpd/conf/httpd.conf
cd /home/e-smith/files/ibays/Primary/html

Enjoy about 2 weeks of my work.

Also you will have to adjust the mysql settings and the wsgi settings to tune the system. The system will be attacked by the hordes of the internet and the tls stack will get overloaded after a couple of days. I am still working on this.
...

Offline ReetP

  • *
  • 2,167
Nice work!!

Any reason why you didn't use SCL for python? It is available (as well as 3.3/4/5) from SCL.
http://mirror.centos.org/centos/6/sclo/x86_64/rh/python27/
http://mirror.centos.org/centos/6/sclo/x86_64/rh/python33/
http://mirror.centos.org/centos/6/sclo/x86_64/rh/rh-python34/
http://mirror.centos.org/centos/6/sclo/x86_64/rh/rh-python35/
http://mirror.centos.org/centos/6/sclo/x86_64/rh/rh-python36/

Note that they need a bit of templating to make them work seemlessly but that isn't impossible.

Now I can see the httpd and flask files layout (the bits that none of us really got) you might save a lot of time using SCL). That makes sense/

Quote
Enjoy about 2 weeks of my work.

Know that feeling ;-)

Thank you nonetheless.

Quote
The system will be attacked by the hordes of the internet and the tls stack will get overloaded after a couple of days. I am still working on this.

If you want a login and come talk to us on Rocket.Chat just let me know. Message me here, get my mail off bugzilla etc.

We might be able to simplify the install and help you on some of your issues.
...
1. Read the Manual
2. Read the Wiki
3. Don't ask for support on Unsupported versions
4. I have a job, wife, and kids and do this in my spare time. If you want something fixed, please help.

Bugs are easier than you think: http://wiki.contribs.org/Bugzilla_Help

If you love SME and don't want to lose it, join in: http://wiki.contribs.org/Koozali_Foundation

gyrcom, thanks for sharing your information. That is cool you have it working!
- Mark