kryo
kryo

Reputation: 726

Running Tornado in apache

My end goal is to implement a WebSocket server using python.

I'm accomplishing this by importing tornado in my python scripts. I've also installed mod_wsgi in apache, and their script outputs Hello World!, so WSGI seems to be working fine. Tornado is also working fine as far as I can tell.

The issue comes when I use tornado's wsgi "Hello, world" script:

import tornado.web
import tornado.wsgi
import wsgiref.simple_server

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

if __name__ == "__main__":
    application = tornado.wsgi.WSGIApplication([
        (r"/", MainHandler),
    ])
    server = wsgiref.simple_server.make_server('', 8888, application)
    server.serve_forever()

First, I get a 500 error and the log tells me WSGI can't find 'application'.

So I remove if __name__ == "__main__", and the page loads infinitely.

I assume this is because of server.serve_forever() so I removed it in an attempt to see Hello, world

But now I just get 404: Not Found. It's not my apache 404 page, and I know that the server can find my main .wsgi file...

Upvotes: 2

Views: 5726

Answers (4)

Ricky Levi
Ricky Levi

Reputation: 7997

If you still want to combine them both, you can use Apache as a proxy that will just be the 1st point in front of the user - but actually reroute the traffic to your local Tornado server ( In / Out )

In my case for example, my Apache listen in port 443 ( some default config ) Then I run my tornado in port 8080, and given a path - will redirect

#File: conf.d/myapp.conf

<VirtualHost *:80>
    ErrorLog  "logs/myapp_error_log"

    ProxyPreserveHost On
    ProxyRequests off
    ProxyPreserveHost On

    <Proxy *>
        Require all granted
    </Proxy>

    RewriteEngine on
    RewriteCond %{REQUEST_METHOD} ^TRACE
    RewriteRule .* - [F]

    ProxyPassMatch    "/myapp/(.*)" "http://localhost:8080/myapp/$1"
    ProxyPassReverse  "/myapp/"     "http://localhost:8080/myapp/"

</VirtualHost>

If you're using RedHat "family" OS also turn on the ability to forward network connections:

setsebool -P httpd_can_network_connect 1

Upvotes: 1

r2_d2
r2_d2

Reputation: 203

To use tornado in apache,add a mod-wsgi plugin to apache.

apt-get install libapache2-mod-wsgi

Write a tornado wsgi server with .wsgi NOTE:Dont use__name__

Configure the apache.conf to run your server.To configure use this mod-wsgi guide

Upvotes: 2

Ben Darnell
Ben Darnell

Reputation: 22134

You can't use websockets with Tornado's WSGIApplication. To use Tornado's websocket support you have to use Tornado's HTTPServer, not apache.

Upvotes: 2

kryo
kryo

Reputation: 726

The WSGIApplication handlers are relative to the webserver root. If your application url is /myapp, your 'application' must look like this:

application = tornado.wsgi.WSGIApplication([
    (r"/myapp", MainHandler),
    (r"/myapp/login/etc", LoginEtcHandler),
])

Oh, and it seems like the documentation is shit (as usual) and __name__ will look something like this when running under apache: _mod_wsgi_8a447ce1677c71c08069303864e1283e.


So! a correct "Hello World" python script will look like this:

/var/www/wsgi-scripts/myapp.wsgi:

import tornado.web
import tornado.wsgi
import wsgiref.simple_server

class MainHandler(tornado.web.RequestHandler):
    def get(self):
         self.write('Hello World')

application = tornado.wsgi.WSGIApplication([
    (r"/myapp", MainHandler),
])

And in the apache config (not .htaccess):

WSGIScriptAlias /myapp /var/www/wsgi-scripts/myapp.wsgi

Upvotes: 2

Related Questions