Wednesday, October 12, 2011

How to setup web2py + Apache + wsgi

It's recommended setup to use web2py for production environment. This post is based on 11 Deployment Recipes on official book

Notice that current appache has a issue to load some modules such as pyodbc because it's compiled with older  visual studio complier. (See web2py-users post)

=> Brian M found the apache working with pyodbc ! (2012-05-25)
Yep, got burned by this when setting up a new server last month. The problem is covered in the pyodbc issue tracker here -  http://code.google.com/p/pyodbc/issues/detail?id=126 I ended up grabbing a build from  http://www.apachelounge.com/download/additional/ and then all was good.

Pre-Requirement:
Finished my previous post

Installation
1. Download and double click to install httpd-2.2.21-win32-x86-openssl-0.9.8r.msi

2. Input the required information if it's not already there. Typical setup is OK.



3. Go to http://localhost/. Apache is successfully running if you see "It works".

mod_wsgi
4. Download from mod_wsgi-win32-app22py27-3.3.so (This is for Python 2.7.x), renamed and place to C:\Program Files\Apache Software Foundation\Apache2.2\modules\mod_wsgi.so

Server certificate for https
5. If you already have, place to C:\Program Files\Apache Software Foundation\Apache2.2\conf\server.crt  and server.key

5.1 If not, go to http://smithii.com/node/117.  Copy "@echo off"  to  "popd" and create batch file called like ssl.bat and run.

5.2 Command-prompt will pop up, follow the wizard (Input pass phrase, Country name...)

5.3 server.crt, server.csr and server.key will be automatically added to C:\Program Files\Apache Software Foundation\Apache2.2\conf

Edit httpd.conf

6. Back up and open C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf

6.1. Add Listen 443 after Listen 80

Listen 80
Listen 443

6.2 Remove comment mark (the # character) from mod_ssl.so
#LoadModule ssl_module modules/mod_ssl.so
=>
LoadModule ssl_module modules/mod_ssl.so

6.3 Add mod_wsgi after all the other LoadModule lines

#LoadModule vhost_alias_module modules/mod_vhost_alias.so
LoadModule wsgi_module modules/mod_wsgi.so

6.4 Add the following at the last (Change ServerName to yours) and save


NameVirtualHost *:80
<VirtualHost *:80>
  DocumentRoot "C:/web2py/applications"
  ServerName ochba-183

  <Directory "C:/web2py">
    Order allow,deny
    Deny from all
  </Directory>

  <Location "/">
    Order deny,allow
    Allow from all
  </Location>

  <LocationMatch "^(/[\w_]*/static/.*)">
    Order Allow,Deny
    Allow from all
  </LocationMatch>

  WSGIScriptAlias / "C:/web2py/wsgihandler.py"

</VirtualHost>

NameVirtualHost *:443
<VirtualHost *:443>
  DocumentRoot "C:/web2py/applications"
  ServerName ochiba-183

  <Directory "C:/web2py">
    Order allow,deny
    Deny from all
  </Directory>

  <Location "/">
    Order deny,allow
    Allow from all
  </Location>

  <LocationMatch "^(/[\w_]*/static/.*)">
    Order Allow,Deny
    Allow from all
  </LocationMatch>

  WSGIScriptAlias / "C:/web2py/wsgihandler.py"

  SSLEngine On
  SSLCertificateFile conf/server.crt
  SSLCertificateKeyFile conf/server.key

</VirtualHost>

7. Restart Apache and Go to http://localhost/ again , you will see web2py welcome screen !!



13 comments:

  1. Thank you for this post. It helped me get a bit closer. I did get the results shown above however if I go to a different maching and type http://dfwmodeling01d.tqs.com/welcom/default/index then I get "Invalid Request" in big letters.

    ReplyDelete
    Replies
    1. I just noticed it's missing "e" for welcome. Can you try again ? Also can you connect the site from different machine if you replace dfwmodeling01d.tqs.com to IP address ?

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Hurray!!!!!! Thanks for noticing the missing e. Now I get out my mad scientist skills and really make an app!!! As long as there no e's I'm good.

      Delete
  2. Also, try replacing '*' for VirtualHost *:80 with you IP address of the server like VirtualHost 192.168.1.1:80

    ReplyDelete
  3. Thanks for your post.
    But in step 7 i got an error:
    Internal Server Error

    Please help me!

    ReplyDelete
    Replies
    1. It could be httpd.conf. if you send me a email (ochiba77@gmail.com) and I can send you my httpd.conf file.

      Delete
    2. After I execute several times again, now it's ok.
      I don't know exactly why.
      Maybe because I used "C:/web2py/handlers/wsgihandler.py" instead of "C:/web2py/wsgihandler.py"
      Thank you so much! :)

      Delete
  4. Hi,
    I have a question.
    In web2py, if I use the connection string db = DAL("sqlite://storage.db"), my application run okay.
    But if I use the connection string db = DAL("mssql://...."), the error ('', 'SQL_INVALID_HANDLE') frequently occurs when the controller executes a query string. Not always!
    After I press F5, the application runs again.
    I don't know why.
    Please help me!

    ReplyDelete
  5. Strange... I see you also posted the question on web2py forum so let's hope someone has answers.

    ReplyDelete