| | 1 | = UoL LAMP HowTo Install Python Flask Applications |
| | 2 | |
| | 3 | Tags: [[HowTo]] [[Install]] [[Pain in the proverbial]] [[UoL LAMP Server]] |
| | 4 | |
| | 5 | == Python 2 |
| | 6 | |
| | 7 | === Difficulties |
| | 8 | |
| | 9 | This installations is made more difficult by 3 things: |
| | 10 | |
| | 11 | 1. Suse Linux does not have mod_wsgi in its repositories, so you're going to have to compile it. |
| | 12 | 2. mod_wsgi seems picky about where it picks python apps and libraries from |
| | 13 | 3. The LAMP servers don't put things where you'd expect them to be. |
| | 14 | 4. Some other stuff that I don't quite understand |
| | 15 | |
| | 16 | === Requirements |
| | 17 | |
| | 18 | Before installation can start, the following packages will have to be installed by IT services: |
| | 19 | |
| | 20 | - libmysqlclient-dev |
| | 21 | - python-dev |
| | 22 | |
| | 23 | === Start Services |
| | 24 | |
| | 25 | Start the Apache and MySQL and make sure that they are restarted when the server is rebooted. |
| | 26 | |
| | 27 | {{{ |
| | 28 | sudo /sbin/chkconfig uol.apache2 on |
| | 29 | sudo /etc/init.d/uol.apache2 start |
| | 30 | sudo /sbin/chkconfig uol.mysql on |
| | 31 | sudo /etc/init.d/uol.mysql start |
| | 32 | }}} |
| | 33 | |
| | 34 | === Proceduce |
| | 35 | |
| | 36 | 1. Install {{{mod_wsgi.so}}} into the directory {{{/local/apache2/etc/}}} |
| | 37 | 1. Compile `mod_wsgi`. See: [[HowTo Compile mod_wsgi for LAMP servers]] |
| | 38 | 2. Copy `mod_wsgi.so` from the directory {{{/local/apache2/etc/}}} of an existing UoL LAMP python Flask application. |
| | 39 | 2. Clone the application from the `git` repository into `/local/` directory. |
| | 40 | 3. Install virtualenv: |
| | 41 | a. First switch to python 2.7 by running the command `source /opt/python2/etc/python-env.sh` |
| | 42 | |
| | 43 | {{{ |
| | 44 | easy_install --install-dir=/local/python virtualenv |
| | 45 | }}} |
| | 46 | |
| | 47 | ''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.'' |
| | 48 | 4. Create a virtual environment in the application directory. |
| | 49 | |
| | 50 | {{{ |
| | 51 | cd /local/{application directory} |
| | 52 | /local/python/virtualenv --no-site-packages BASELINE |
| | 53 | }}} |
| | 54 | |
| | 55 | ''`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`.'' |
| | 56 | |
| | 57 | 5. Activate the environment, install the required packages, then deactivate. This example shows some common requirements: |
| | 58 | |
| | 59 | {{{ |
| | 60 | source BASELINE/bin/activate |
| | 61 | easy_install flask |
| | 62 | easy_install flask-sqlalchemy |
| | 63 | easy_install mysql-python |
| | 64 | easy_install flask-wtf |
| | 65 | easy_install WTForms-Components |
| | 66 | deactivate |
| | 67 | }}} |
| | 68 | |
| | 69 | 6. Load the WSGI module into Apache, by editing the file `/local/apache2/etc/loadmodule.conf` by adding this line at the end. |
| | 70 | |
| | 71 | {{{ |
| | 72 | LoadModule wsgi_module /local/apache2/etc/mod_wsgi.so |
| | 73 | }}} |
| | 74 | |
| | 75 | 7. Add the WSGI config to the Apache config file `/local/apache2/etc/httpd.conf`: |
| | 76 | |
| | 77 | {{{ |
| | 78 | <Directory /local/{application directory}> |
| | 79 | WSGIProcessGroup {application name} |
| | 80 | WSGIApplicationGroup %{GLOBAL} |
| | 81 | Order deny,allow |
| | 82 | Allow from all |
| | 83 | </Directory> |
| | 84 | |
| | 85 | 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}/ |
| | 86 | WSGIScriptAlias / /local/{application directory}/{path to *.wsgi} |
| | 87 | }}} |
| | 88 | |
| | 89 | ''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.'' |
| | 90 | |
| | 91 | 8. Restart apache: |
| | 92 | |
| | 93 | {{{ |
| | 94 | sudo /etc/init.d/uol.apache2 restart |
| | 95 | }}} |
| | 96 | |
| | 97 | 9. Create any databases that are required and given the appropriate user permissions. |
| | 98 | |
| | 99 | 10. Update the application settings in {{{/local/{application directory} }}} |
| | 100 | |
| | 101 | 10. Pray. |
| | 102 | |
| | 103 | == Other Possible Stuff (**Not required last time used!**) |
| | 104 | |
| | 105 | 1. You may need to give `wwwrun` extra permissions to the `/local/{application directory}` directory. |
| | 106 | 2. You might need to pray a bit more. |
| | 107 | |
| | 108 | {{{ |
| | 109 | setfacl -m u:wwwrun:rwx /local/{application directory} |
| | 110 | setfacl -m d:u:wwwrun:rwx /local/telomere/{application directory} |
| | 111 | }}} |
| | 112 | |
| | 113 | === Gotchas |
| | 114 | |
| | 115 | - [[Python Flask Gotchas Import Database Module Fails]] |
| | 116 | |
| | 117 | [[BackLinks]] |