Today I wanted to try out websockets because I heard that Google Chrome just integrated a preliminary version of it. I discovered that their isn’t a simple straightforward guide for doing this with apache2. Here is a simple guide to getting it working:

Starting with:

  • Ubuntu 9.10 (I think it will work with most recent versions of ubuntu and other distributions will work if you change the package manager commands)
  • Apache2 installed and working (punch sudo apt-get install apache2 into the terminal, type 127.0.0.1 into the browser to check)
  • Python 2.6.x installed and working
  • Subversion

Step 1: Install mod_python and get it working with apache

  1. Punch sudo apt-get install libapache2-mod-python into the terminal
  2. In /etc/apache2/sites-available/default change the part that looks like:
    1
    2
    3
    4
    
      Options Indexes FollowSymLinks MultiViews
      AllowOverride None
      Order allow,deny
      allow from all

    To:

    1
    2
    3
    4
    5
    6
    7
    
      Options Indexes FollowSymLinks MultiViews
      AllowOverride None
      Order allow,deny
      allow from all
      AddHandler mod_python .py
      PythonHandler mod_python.publisher
      PythonDebug On
  3. Restart apache: sudo /etc/init.d/apache2 restart
  4. Test it by throwing something like this into a new file test.py in /var/www:
    1
    2
    
    def index(req):
      return "Test successful"
  5. Navigate to localhost/test.py to see if the test is successful

Step 2: Get and Install pywebsocket

  1. Make sure you have subversion installed and run this in the terminal: svn checkout http://pywebsocket.googlecode.com/svn/trunk/ pywebsocket-read-only
  2. Now navigate into the pywebsocket-read-only folder where you downloaded it … aka cd pywebsocket-read-only
  3. Run sudo python setup.py build
  4. Then sudo python setup.py install
  5. Make a directory for the handlers … I made mine in my home directory as you can see below
  6. Now edit /etc/apache2/httpd.conf to include the following:
    1
    2
    3
    4
    5
    
      PythonPath "sys.path+['/usr/local/lib/python2.6/dist-packages/mod_pywebsocket']"
     
      PythonOption mod_pywebsocket.handler_root /home/travis/Desktop/websock_handlers
     
      PythonHeaderParserHandler mod_pywebsocket.headerparserhandler

    The first path /usr/local/lib/python2.6/dist-packages/mod_pywebsocket is where you installed mod_pywebsocket and should be in a similar location for you.

    The second path /home/travis/Desktop/websock_handlers is where I put my handlers directory.

    Remember to change the paths to fit your implementation

Step 3: Make some handlers where you said they would be above and you should be all set

Be Sociable, Share!
 

13 Responses to How to set up apache to serve html5 websocket applications with pywebsocket

  1. Travis says:

    Would anyone like me to expand on this post?

  2. billius says:

    nope. it all seems pretty good xcept my n00bieness. where do you put the HTML5 files? what are websock handlers?

  3. fastshare says:

    Yes. I have similar configuration in my apache,but it did not work.
    in httpd.conf

    AddHandler python-program .py

    PythonPath “sys.path+['/home/fastshare/mod_pywebsocket']”
    PythonOption mod_pywebsocket.handler_root /home/fastshare/mod_pywebsocket
    PythonHeaderParserHandler mod_pywebsocket.headerparserhandler

    I have echo_wsh.py in /home/fastshare/mod_pywebsocket.
    When I give /echo request, i am gettting redirection response from apache
    So I changed echo_wsh.py as foo_wsh.py and gave request as /foo , but apache said file does not exist

    Could you help me please.

    But for standalone.py server it is working fine.

    Apache Version
    [notice] Apache/1.3.41 (Unix) mod_fastcgi/2.2.10 PHP/5.2.8 mod_python/2.7.11 Python/2.3.4 configured — resuming normal operations

    Thanks,
    fastshare

  4. billius Says:
    February 23rd, 2010 at 2:21 am

    “nope. it all seems pretty good xcept my n00bieness. where do you put the HTML5 files? what are websock handlers?”

    Ditto that.

  5. asslb.er says:

    @fastshare: your httpd.conf is wrong. Read the docs, type: pydoc mod_pywebsocket

    Try this in httpd.conf:

    PythonPath “sys.path+['/usr/local/lib/python2.6/dist-packages/mod_pywebsocket']”
    PythonOption mod_pywebsocket.handler_root /home/www/websock_handlers
    PythonHeaderParserHandler mod_pywebsocket.headerparserhandler

    PythonOption mod_pywebsocket.allow_draft75 On

    Then just put your handlers in /home/www/websock_handlers.

  6. asslb.er says:

    ohhh this blog is removing the “IfModule python_module” statements. Read the docs for an example.

  7. sam says:

    Hello
    Can you tell me how do I test my configuration to see if the pywebsocket working?
    I follow your setup but I am not clear about to test it to see if the websocket working or not.

  8. pratik says:

    I can’t seem to get this to work.

    It works with the standalone server, but not with apache.

    I have also tried following the instructions above exactly replacing travis with the name I have set up.

    Anyone else with similar problem?

    I’m getting a error code 500 when I try it.

  9. emDot says:

    @pratik: Yeah, same thing.
    I’m getting “AttributeError: ‘module’ object has no attribute ‘main_server'”

    Anyone have any ideas?

  10. emDot says:

    @pratik: Cancel that! I was running on CentOS, not Ubuntu.

    I do have a constructive note, though: Don’t forget to restart (or just reload?) apache after step 2.6.

  11. Aardvark says:

    Won’t all of the open websocket connections drain the pool of available Apache worker threads?

  12. Nick says:

    any way you can give us a mac terminal way of doing this? I’m trying to get this started on my mac.

  13. Nick says:

    If I may be a little more specific, in what way do you edit httpd.conf to include those lines (modified for my machine of course)? just plop them down any-old-where?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>