MEWBIES@:  Facebook  Twitter  G+  YouTube  DeviantArt  Forum  Wall
 SHARE:
    ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
   ██                                                                       ██
  █▌      -   COMPILE & INSTALL PYTHON MODS MOD_WSGI AND MOD_PYTHON   -      █▌
 █▌                           FOR WEB SERVER - LINUX                          █▌
 █                                                                            ▐▌
 █ The Python modules mod_wsgi and mod_python provides an interface between   ▐▌
 █ web server and Python web applications. mod_wsgi - Web Server Gateway      ▐▌
 █ Interface, only executes Python applications where as mod_python  will     ▐▌
 █ also execute any pure Python script that ends .py.                         ▐▌
 █ For mods mod_wsgi and mod_python to co-exist without conflicts you need to ▐▌
 █ compile both manually and some extra steps which is not covered in this    ▐▌
 █ tutorial but it is covered HERE. If you do not need both then only install ▐▌
 █ the one that suits your requirements. By the way on June 17 2010           ▐▌
 █ mod_python was officially dead then Jan 8 2013 it came back to life here   ▐▌
 █ and on GitHub all spruced up with support for Python 3.                    ▐▌
 █                                                                            ▐▌
  Module mod_wsgi and mod_python Loaded
 █                                                                            ▐▌
 █                          ___________..___________                          ▐▌
 █                                                                            ▐▌
 █ MISMATCH ERRORS                                                            ▐▌
 █ COMPILE AND INSTALL PYTHON MODULE MOD_WSGI                                 ▐▌
 █ COMPILE AND INSTALL PYTHON MODULE MOD_PYTHON                               ▐▌
 █ TEST MOD_PYTHON                                                            ▐▌
 █ HOW TO VIEW IF YOUR PYTHON WAS COMPILED WITH THREADING & SHARED LIBRARY    ▐▌
 █                                                                            ▐▌
 █                          ___________..___________                          ▐▌
 █                                                                            ▐▌
 █ MISMATCH ERRORS:                                                           ▐▌
 █ ````````````````                                                           ▐▌
 █ I originally installed mod_wsgi and mod_python using (DON'T DO):           ▐▌
 █ aptitude install libapache2-mod-wsgi libapache2-mod-python                 ▐▌
 █ I can't recall what it was for or even if I still needed them. I only      ▐▌
 █ noticed these modules after upgrading my Debian from Squeeze to Wheezy     ▐▌
 █ when viewing my Apache logs:                                               ▐▌
 █ tail -50 /var/log/apache2/error.log                                        ▐▌
 █ [warn] mod_wsgi: Compiled for Python/2.7.2+.                               ▐▌
 █ [warn] mod_wsgi: Runtime using Python/2.7.3.                               ▐▌
 █ [notice] Apache/2.2.22 (Debian) mod_ssl/2.2.22 OpenSSL/1.0.1e mod_wsgi/3.3 Python/2.7.3 configured -- resuming normal operations
 █ and                                                                        ▐▌
 █ [error] python_init: Python version mismatch, expected '2.7.2+', found '2.7.3'.
 █ [error] python_init: Python executable found '/usr/bin/python'.            ▐▌
 █ [error] python_init: Python path being used '/usr/lib/python2.7/:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/l.
 █ [notice] mod_python: Creating 8 session mutexes based on 50 max processes and 0 max threads.
 █ [notice] mod_python: using mutex_directory /tmp                            ▐▌
 █                                                                            ▐▌
 █ The only way to fix those conflicts is to compile and install it manually. ▐▌
 █ There are instructions how to install mod_wsgi here and here that helped   ▐▌
 █ guide me.                                                                  ▐▌
 █                                                                            ▐▌
 █                          ___________..___________                          ▐▌
 █                                                                            ▐▌
 █ COMPILE AND INSTALL PYTHON MODULE MOD_WSGI:                                ▐▌
 █ ```````````````````````````````````````````                                ▐▌
 █ 1. First we install some dependencies:                                     ▐▌
 █ su                                                                         ▐▌
 █ aptitude update                                                            ▐▌
 █ If you already have installed mod_wsgi:                                    ▐▌
 █ aptitude remove libapache2-mod-wsgi                                        ▐▌
 █ You need Python header files to compile mod_wsgi. Adjust the version       ▐▌
 █ number '2.7' below to match your version of Python:                        ▐▌
 █ python -V                                                                  ▐▌
 █ aptitude install libaprutil1-dev apache2-threaded-dev libpython2.7 python2.7-dev
 █ OR this might do it: aptitude build-dep libapache2-mod-wsgi                ▐▌
 █ service apache2 restart                                                    ▐▌
 █ exit                                                                       ▐▌
 █                                                                            ▐▌
 █ 2. Now to compile mod_wsgi:                                                ▐▌
 █ Go to their website here for the correct URL for the version you require.  ▐▌
 █ wget http://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz               ▐▌
 █ tar xvfz mod_wsgi-3.4.tar.gz && cd mod_wsgi-3.4                            ▐▌
 █ ./configure                                                                ▐▌
 █ make                                                                       ▐▌
 █ If it doesn't compile properly; quits with an error, read here to fix that ▐▌
 █ noticing what the error is i.e 'Python.h: No such file or directory' which ▐▌
 █ won't happen because we installed python2.7-dev.                           ▐▌
 █ Once you run 'make' without errors then continue:                          ▐▌
 █ su                                                                         ▐▌
 █ make install                                                               ▐▌
 █ Output:                                                                    ▐▌
 █ [snip]                                                                     ▐▌
 █ libtool: install: cp .libs/mod_wsgi.so                                     ▐▌
 █ /usr/lib/apache2/modules/mod_wsgi.so                                       ▐▌
 █ libtool: install: cp .libs/mod_wsgi.lai                                    ▐▌
 █ /usr/lib/apache2/modules/mod_wsgi.la                                       ▐▌
 █ [snip]                                                                     ▐▌
 █ chmod 644 /usr/lib/apache2/modules/mod_wsgi.so                             ▐▌
 █                                                                            ▐▌
 █ After Apache will restart. If it didn't then:                              ▐▌
 █ service apache2 restart                                                    ▐▌
 █                                                                            ▐▌
 █ 3. Now to enable the module; change the paths to the correct paths  for    ▐▌
 █ your setup. The install created a file 'wsgi.load' here:                   ▐▌
 █ ls -al /etc/apache2/mods-enabled/wsgi.load                                 ▐▌
 █ This file contains the information telling Apache where 'mod_wsgi.so' is   ▐▌
 █ located. View it:                                                          ▐▌
 █ cat /etc/apache2/mods-enabled/wsgi.load                                    ▐▌
 █ Output: LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so        ▐▌
 █                                                                            ▐▌
 █ If you don't have wsgi.load then create it and paste in the output above   ▐▌
 █ with the correct path to your mod_wsgi.so:                                 ▐▌
 █ pico /etc/apache2/mods-enabled/wsgi.load                                   ▐▌
 █                                                                            ▐▌
 █ Then enable the module (this will create symbolic links for it):           ▐▌
 █ a2enmod wsgi                                                               ▐▌
 █ Output:                                                                    ▐▌
 █ Enabling module wsgi.                                                      ▐▌
 █ To activate the new configuration, you need to run:                        ▐▌
 █   service apache2 restart                                                  ▐▌
 █                                                                            ▐▌
 █ So lets restart Apache:                                                    ▐▌
 █ service apache2 restart                                                    ▐▌
 █ Done :)                                                                    ▐▌
 █                                                                            ▐▌
 █ View your phpinfo() for mod_wsgi loaded as shown in the image above.       ▐▌
 █ View your logs if the errors are gone:                                     ▐▌
 █ tail -50 /var/log/apache2/error.log                                        ▐▌
 █ exit                                                                       ▐▌
 █                                                                            ▐▌
 █ If you want to test mod_wsgi I suggest reading here or here as it is       ▐▌
 █ covered well, just change the paths to match yours. We will though test    ▐▌
 █ mod_python below.                                                          ▐▌
 █                                                                            ▐▌
 █                          ___________..___________                          ▐▌
 █                                                                            ▐▌
 █ COMPILE AND INSTALL PYTHON MODULE MOD_PYTHON:                              ▐▌
 █ `````````````````````````````````````````````                              ▐▌
 █ su                                                                         ▐▌
 █ 1. If you already have installed mod_python:                               ▐▌
 █ aptitude remove libapache2-mod-python                                      ▐▌
 █ Install the dependencies:                                                  ▐▌
 █ aptitude build-dep libapache2-mod-python                                   ▐▌
 █ aptitude install flex                                                      ▐▌
 █ service apache2 restart                                                    ▐▌
 █ exit                                                                       ▐▌
 █                                                                            ▐▌
 █ 2. Go to their website here for the correct URL for the version you        ▐▌
 █ require.                                                                   ▐▌
 █ For Debian Squeeze I will use the package from here.                       ▐▌
 █ wget http://ftp.de.debian.org/debian/pool/main/liba/libapache2-mod-python/libapache2-mod-python_3.3.1.orig.tar.gztar xvfz libapache2-mod-python_3.3.1.orig.tar.gz && cd mod_python-3.3.1    ▐▌
 █ ./configure                                                                ▐▌
 █ make                                                                       ▐▌
 █ If 'make' succeeded without ending in an error skip down to 'su' and 'make ▐▌
 █ install' steps.                                                            ▐▌
 █ If make failed with an error similar to:                                   ▐▌
 █ connobject.c:142:19: error: request for member 'next' in something not a   ▐▌
 █ structure or union                                                         ▐▌
 █ apxs:Error: Command failed with rc=65536                                   ▐▌
 █ .                                                                          ▐▌
 █ make[1]: *** [mod_python.so] Error 1                                       ▐▌
 █ make[1]: Leaving directory `/home/eatme/mod_python-3.3.1/src'              ▐▌
 █ make: *** [do_dso] Error 2                                                 ▐▌
 █                                                                            ▐▌
 █ Then do this (found here):                                                 ▐▌
 █ cd .. && rm mod_python-3.3.1 -rf                                           ▐▌
 █ tar xvfz libapache2-mod-python_3.3.1.orig.tar.gz && cd mod_python-3.3.1    ▐▌
 █ pico src/connobject.c                                                      ▐▌
 █ Find and change as below then save changes:                                ▐▌
 █ Has this:                                                                  ▐▌
 █ !(b == APR_BRIGADE_SENTINEL(b) ||                                          ▐▌
 █ Change to:                                                                 ▐▌
 █ !(b == APR_BRIGADE_SENTINEL(bb) ||                                         ▐▌
 █                                                                            ▐▌
 █ Run configure again:                                                       ▐▌
 █ ./configure                                                                ▐▌
 █ make                                                                       ▐▌
 █ su                                                                         ▐▌
 █ make install                                                               ▐▌
 █                                                                            ▐▌
 █ 3. Instruct Apache to load the module:                                     ▐▌
 █ Find the location of 'python.load'. Mine is here:                          ▐▌
 █ cat /etc/apache2/mods-available/python.load                                ▐▌
 █ LoadModule python_module /usr/lib/apache2/modules/mod_python.so            ▐▌
 █                                                                            ▐▌
 █ Create a symbolic link to python.load (change paths to match yours):       ▐▌
 █ cd /etc/apache2/mods-enabled                                               ▐▌
 █ ln -s ../mods-available/python.load                                        ▐▌
 █ service apache2 restart                                                    ▐▌
 █ You should see notices that mod_python is loaded:                          ▐▌
 █ tail -f /var/log/apache2/error.log                                         ▐▌
 █ [notice] mod_python: Creating 8 session mutexes based on 50 max processes  ▐▌
 █ and 0 max threads.                                                         ▐▌
 █ [notice] mod_python: using mutex_directory /tmp                            ▐▌
 █ [notice] Apache/2.2.22 (Debian) mod_python/3.3.1 Python/2.7.3              ▐▌
 █ mod_ssl/2.2.22 OpenSSL/1.0.1e mod_wsgi/3.4 configured -- resuming norms    ▐▌
 █                                                                            ▐▌
 █ If there were errors; view what and where the error is happening and work  ▐▌
 █ from there. Could be a simple typo or path to the files are wrong.         ▐▌
 █                                                                            ▐▌
 █ 4. Instruct Apache how to handle Python:                                   ▐▌
 █ I am going to use the 'Publisher Handler' (handles pure Python code with   ▐▌
 █ ext .py). There is also PSP Handler (handles embed Python code into HTML). ▐▌
 █ Both are explained here and how to use the PSP Handler instead (if that    ▐▌
 █ link stops working use this link).                                         ▐▌
 █ Add the settings to your web server's default conf file. Your location     ▐▌
 █ might be different, mine is here:                                          ▐▌
 █ pico /etc/apache2/sites-available/000-default.conf                         ▐▌
 █ Of if your page or entire site is HTTPS then add the below to your web     ▐▌
 █ server's ssl file:                                                         ▐▌
 █ pico /etc/apache2/sites-available/ssl                                      ▐▌
 █                                                                            ▐▌
 █ Add the three bold python lines as shown and save changes:                 ▐▌
                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
 █ Mine has this, yours will be your own alterations:                         ▐▌
 █         <Directory /var/www/>                                              ▐▌
 █                 Options -Indexes FollowSymLinks MultiViews                 ▐▌
 █                 AllowOverride AuthConfig                                   ▐▌
 █                 Order allow,deny                                           ▐▌
 █                 allow from all                                             ▐▌
 █                 AuthUserFile /etc/apache2/.htpasswd                        ▐▌
 █                 AuthGroupFile /dev/null                                    ▐▌
 █                 AuthName  "Authorization Required"                         ▐▌
 █                 AuthType Basic                                             ▐▌
 █                 require user MewbiesMewbie                                 ▐▌
 █         </Directory>                                                       ▐▌
 █                                                                            ▐▌
 █ Change to:                                                                 ▐▌
 █         <Directory /var/www/>                                              ▐▌
 █                 Options -Indexes FollowSymLinks MultiViews                 ▐▌
 █                 AllowOverride AuthConfig                                   ▐▌
 █                 Order allow,deny                                           ▐▌
 █                 allow from all                                             ▐▌
 █                 AuthUserFile /etc/apache2/.htpasswd                        ▐▌
 █                 AuthGroupFile /dev/null                                    ▐▌
 █                 AuthName  "Authorization Required"                         ▐▌
 █                 AuthType Basic                                             ▐▌
 █                 require user MewbiesMewbie                                 ▐▌
 █                 AddHandler mod_python .py                                  ▐▌
 █                 PythonHandler mod_python.publisher                         ▐▌
 █                 PythonDebug On                                             ▐▌
 █         </Directory>                                                       ▐▌
 █                                                                            ▐▌
 █ Restart your server:                                                       ▐▌
 █ service apache2 restart                                                    ▐▌
 █ If your server didn't restart; view what and where the error is happening  ▐▌
 █ and work from there.                                                       ▐▌
 █ View your phpinfo() that mod_python is loaded as shown in the image above. ▐▌
 █ View your logs for any related errors.                                     ▐▌
 █ tail -50 /var/log/apache2/error.log                                        ▐▌
 █ exit                                                                       ▐▌
 █ Done :)                                                                    ▐▌
 █                                                                            ▐▌
 █ TEST MOD_PYTHON:                                                           ▐▌
 █ ````````````````                                                           ▐▌
 █ To test if mod_python is truly working we will create a directory for a    ▐▌
 █ test page in your web server's HTML directory (for security when testing   ▐▌
 █ always use a separate directory and not user's home):                      ▐▌
 █ su                                                                         ▐▌
 █ My web server is here, change path to match yours:                         ▐▌
 █ mkdir /var/www/py                                                          ▐▌
 █ pico /var/www/py/test.py                                                   ▐▌
 █ Copy and paste in then save changes:                                       ▐▌
def index(req):
  return "Mewibies mod_python Test Successful, Yay!";
 █                                                                            ▐▌
 █ Now open the page in your browser: http://yourwebsite.com/py/test.py       ▐▌
 █ If all worked the page will display:                                       ▐▌
 █ Mewibies mod_python Test Successful, Yay!                                  ▐▌
 █                                                                            ▐▌
 █ Clean up:                                                                  ▐▌
 █ cd /var/www/                                                               ▐▌
 █ rm py -rf                                                                  ▐▌
 █                                                                            ▐▌
 █                          ___________..___________                          ▐▌
 █                                                                            ▐▌
 █ HOW TO VIEW IF YOUR PYTHON WAS COMPILED WITH THREADING & SHARED LIBRARY:   ▐▌
 █ ````````````````````````````````````````````````````````````````````````   ▐▌
 █ It is better to use modules with a Python that was configured and complied ▐▌
 █ with "--with-threads --enable-shared" so that the library is shared thus   ▐▌
 █ resulting in less space and memory being used. To view if your Python has  ▐▌
 █ threading and shared library (probably other ways to view it but this is   ▐▌
 █ how I found out mine was):                                                 ▐▌
 █ Threading:                                                                 ▐▌
 █ python$version -m threading                                                ▐▌
 █ which python                                                               ▐▌
 █ ldd /usr/bin/python2.7                                                     ▐▌
 █ Threading will have similar to this:                                       ▐▌
 █ libpthread.so.0 => /lib/i386-linux-gnu/i686/cmov/libpthread.so.0 (0xb779.. ▐▌
 █                                                                            ▐▌
 █ To view if Python was compiled to share library select a Python module .so ▐▌
 █ file from the list:                                                        ▐▌
 █ ls -al /usr/lib/apache2/modules/                                           ▐▌
 █ I have mod_wsgi.so and we'll use 'ldd' to print the shared library         ▐▌
 █ information for it:                                                        ▐▌
 █ ldd /usr/lib/apache2/modules/mod_wsgi.so                                   ▐▌
 █ Output:                                                                    ▐▌
 █ [snip]                                                                     ▐▌
 █ libpython2.7.so.1.0 => /usr/lib/libpython2.7.so.1.0 (0xb7437000)           ▐▌
 █ [snip]                                                                     ▐▌
 █ It shows it is dependent on the .so file for Python, thus it is a shared   ▐▌
 █ library. If it isn't listed than it is using a static Python library.      ▐▌
 █ Another indicator if you have Python shared libraries is mod_wsgi.so,      ▐▌
 █ according to here, will be less than 250 Kb:                               ▐▌
 █ ls -alh /usr/lib/apache2/modules/mod_wsgi.so                               ▐▌
 █ -rw-r--r-- 1 root root 186K  /usr/lib/apache2/modules/mod_wsgi.so          ▐▌
 █ If the library isn't shared the size will be over 1MB.                     ▐▌
 █                                                                            ▐▌
 █ //----------------------------------------------------------------------   ▐▌
 █                                                                            ▐▌
 █ If you find mistakes, have suggestions, and or questions please post at    ▐▌
 █ mewbies forum HERE - thank you.                                            ▐▌
 █                                                                            ▐▌
 █ Last update on 20 Nov '13                                                  ▐▌
 █                                                                            ▐▌
 █▌                                                                           █▌
  █▌                          -   mewbies.com   -                            █▌
   █▌                                                                       █▌
    ██▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄██