wiki:UoL LAMP HowTo Install Python Flask Applications

Version 10 (modified by Richard Bramley, 6 years ago) ( diff )

--

UoL LAMP HowTo Install Python Flask Applications

Tags: HowTo Install Pain in the proverbial UoL LAMP Server

Python 2

Difficulties

This installations is made more difficult by 3 things:

  1. Suse Linux does not have mod_wsgi in its repositories, so you're going to have to compile it.
  2. mod_wsgi seems picky about where it picks python apps and libraries from
  3. The LAMP servers don't put things where you'd expect them to be.
  4. Some other stuff that I don't quite understand

Requirements

Before installation can start, the following packages will have to be installed by IT services:

  • libmysqlclient-dev
  • python-dev

Start Services

Start the Apache and MySQL and make sure that they are restarted when the server is rebooted.

sudo /sbin/chkconfig uol.apache2 on
sudo /etc/init.d/uol.apache2 start
sudo /sbin/chkconfig uol.mysql on
sudo /etc/init.d/uol.mysql start

Proceduce

  1. Install mod_wsgi.so into the directory /local/apache2/etc/
    1. Compile mod_wsgi. See: HowTo Compile mod_wsgi for LAMP servers
    2. Copy mod_wsgi.so from the directory /local/apache2/etc/ of an existing UoL LAMP python Flask application.
  2. Clone the application from the git repository into /local/ directory.
  3. Install virtualenv:
    1. First switch to python 2.7 by running the command source /opt/python2/etc/python-env.sh
easy_install --install-dir=/local/python virtualenv

For some reason that I can't work out (point 4. above), it would only pick up some of the python libraries from a virtual env and not when they were installed in /local/python/. Maybe this was because of the order that I installed things, but I couldn't get it to work without the virtual environment.

  1. Create a virtual environment in the application directory.
cd /local/{application directory}
/local/python/virtualenv --no-site-packages BASELINE

BASELINE is the name of the virtual environment. It could be called anything, but it's called that! Also note that you need to use the full path to the virtualenv utility, since /local/python isn't in $PATH.

  1. Activate the environment, install the required packages, then deactivate. This example shows some common requirements:
source BASELINE/bin/activate
easy_install flask
easy_install flask-sqlalchemy
easy_install mysql-python
easy_install flask-wtf
easy_install WTForms-Components
deactivate
  1. Load the WSGI module into Apache, by editing the file /local/apache2/etc/loadmodule.conf by adding this line at the end.
LoadModule wsgi_module /local/apache2/etc/mod_wsgi.so
  1. Add the WSGI config to the Apache config file /local/apache2/etc/httpd.conf:
<Directory /local/{application directory}>
    WSGIProcessGroup {application name}
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

WSGIDaemonProcess {application name} user=wwwrun threads=5 python-path=/local/{application directory}/BASELINE/lib/python2.6/site-packages:/local/python:/usr/lib64/python2.6/site-packages:/usr/share/doc/packages/ home=/local/{application directory}/
WSGIScriptAlias / /local/{application directory}/{path to *.wsgi}

The two hard won things here are the values for the python-path and home arguments. The python-path must contain every directory where python libraries are installed, including a sub-directory of the virtualenv directory you created earlier. The home must point to your application directory, or you won't be able to pick up your own modules.

  1. Restart apache:
sudo /etc/init.d/uol.apache2 restart
  1. Create any databases that are required and given the appropriate user permissions.
  1. Update the application settings in /local/{application directory}
  1. Pray.

Other Possible Stuff (Not required last time used**)

  1. You may need to give wwwrun extra permissions to the /local/{application directory} directory.
  2. You might need to pray a bit more.
setfacl -m u:wwwrun:rwx /local/{application directory}
setfacl -m d:u:wwwrun:rwx /local/telomere/{application directory}

Gotchas

Python 3

Create Environment

  1. Add python 3 to your path by adding the following line to .profile file in your home directory:
PATH=$PATH:/opt/python3/bin/

You will need to log off and log back in to add the above to your current environment.

  1. Change directory into /local/ and create a virtual environment:
cd /local/ && virtualenv --no-site-packages venv
  1. Activate the virtual environment:
source /local/venv/bin/activate
  1. Download the application repository from GitHub, cd into the application directory and install the requirements by running the command:
pip install -r requirements.txt

Apache Setup

  1. Edit the Apache config:
vi /local/apache2/etc/httpd.conf
  1. Amend the values below the comment # List of resources to look for when the client requests a directory to be:
# List of resources to look for when the client requests a directory
DirectoryIndex index.html

### 'Main' server configuration #############################################
<Directory {{{path to application folder}}}>
    SetEnv FLASK_SECRET_KEY {{{Some Secret Key}}}
    SetEnv ENV1 env1_value
    SetEnv ENV2 env2_value
    SetEnv ENV3 env3_value
    WSGIProcessGroup application
    WSGIApplicationGroup %{GLOBAL}
    Order allow,deny
    Allow from all
</Directory>

WSGIDaemonProcess application user=wwwrun threads=5 python-path=/local/venv/lib/python3.5/site-packages home={{{path to application folder}}}
WSGIScriptAlias / {{{path to application}}}

Change the values of:

  • Some Secret Key to be somethin' secret
  • path to application folder to be the application folder
  • path to application to be the path to the actual application

And changing ENV1, 2 & 3 to be appropriate environment values.

  1. Edit the Apache modules conf:
vi /local/apache2/etc/loadmodule.conf
  1. Add the following line to the end of the file:
LoadModule      wsgi_module             /opt/python3/lib/mod_wsgi.so
  1. Restart Apache using the command:
sudo /etc/init.d/uol.apache2 restart

Error: Macro BackLinks(None) failed
'Environment' object has no attribute 'get_db_cnx'

Note: See TracWiki for help on using the wiki.