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 apache2into 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
- Punch
sudo apt-get install libapache2-mod-pythoninto the terminal - 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 allTo:
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
- Restart apache:
sudo /etc/init.d/apache2 restart - Test it by throwing something like this into a new file test.py in /var/www:
1 2
def index(req): return "Test successful"
- Navigate to localhost/test.py to see if the test is successful
Step 2: Get and Install pywebsocket
- Make sure you have subversion installed and run this in the terminal:
svn checkout http://pywebsocket.googlecode.com/svn/trunk/ pywebsocket-read-only - Now navigate into the pywebsocket-read-only folder where you downloaded it … aka
cd pywebsocket-read-only - Run
sudo python setup.py build - Then
sudo python setup.py install - Make a directory for the handlers … I made mine in my home directory as you can see below
- 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


Would anyone like me to expand on this post?
nope. it all seems pretty good xcept my n00bieness. where do you put the HTML5 files? what are websock handlers?
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
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.
@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.
ohhh this blog is removing the “IfModule python_module” statements. Read the docs for an example.
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.
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.
@pratik: Yeah, same thing.
I’m getting “AttributeError: ‘module’ object has no attribute ‘main_server’”
Anyone have any ideas?
@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.
Won’t all of the open websocket connections drain the pool of available Apache worker threads?
any way you can give us a mac terminal way of doing this? I’m trying to get this started on my mac.
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?