Sebastian Schmidt
nginx: how to setup tornado and apache on a virtual machine

The challenge: We want python services and php services running on the same vm.

The solution: nginx as a proxy server

The scenario: I have a ubuntu vm running on top of my windows.

(1)  Install nginx

==================

http://wiki.nginx.org/Install

sudo -s

nginx=stable # use nginx=development for latest development version

add-apt-repository ppa:nginx/$nginx

apt-get update

apt-get install nginx

usefull commands:

————————-

- start nginx: sudo nginx

- stop nginx: sudo nginx -s stop

You may get noticed that nginx can’t bind to port 80, since it is used by apache.

We’ll check that lateron.

(2) Install tornado and run a example application

=================================================

http://techgurulive.com/2010/05/18/what-is-tornado-web-server-how-to-install-tornado-web-server/

source (including examples): https://github.com/facebook/tornado

sudo apt-get install python-dev python-pycurl python-simplejson

tar xvzf tornado-0.2.tar.gz

cd tornado-0.2

python setup.py build

sudo python setup.py install

You may want to run a tornado application (distributed with the source from github)…

In my case, I was lazy and created the tornado folder on my desktop.

launch the app: python /home/seb/Desktop/tornado/demos/chat/chatdemo.py

The chat application is now running on port 8888.

(3) bind apache to port 82 (use example.com as domain for the vhost)

====================================================================

# file: /etc/apache2/sites-available/example

# since nginx uses port 80 - we bind apache to port 82

NameVirtualHost *:82

Listen 82

<IfModule mod_ssl.c>

    # If you add NameVirtualHost *:443 here, you will also have to change

    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl

    # to <VirtualHost *:443>

    # Server Name Indication for SSL named virtual hosts is currently not

    # supported by MSIE on Windows XP.

    Listen 443

</IfModule>

<IfModule mod_gnutls.c>

    Listen 443

</IfModule>

(4) create a vhost “example.com” on apache

===========================================

# file: /etc/apache2/sites-available/example

# Notice: sudo a2ensite example (to enable that site)

# For port 82, server name the same as nginx

  <VirtualHost *:82>

    ServerName     example.com

     DocumentRoot /home/seb/workspace/example/htdocs

     <Directory /home/seb/workspace/example/htdocs>

         # allow .htaccess:

        AllowOverride All

        allow from all

    </Directory>

  </VirtualHost>

 (5) restart apache

 ===================

 Since configuration changed, restart apache…

 sudo / etc / init.d / apache2 restart

 (6) configure nginx as a proxy

 ==========================================

 # file: /etc/nginx/proxy.conf

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 128m;

client_body_buffer_size 256k;

proxy_connect_timeout 60;

proxy_send_timeout 60;

proxy_read_timeout 60;

proxy_buffer_size 4k;

proxy_buffers 32 256k;

proxy_busy_buffers_size 512k;

proxy_temp_file_write_size 512k;

# file: /etc/nginx/sites-enabled/vhost

# tornado.example.com

server

{

  listen 80;

  server_name tornado.example.com;

  location / {

    # redirect everything to port 8888 where tornado chat application is listening.

    proxy_pass  http://127.0.0.1:8888;

    include /etc/nginx/proxy.conf;

    }

}

# example.com

server

{

  listen 80;

  server_name example.com www.example.com;

  location / {

      # redirect everything to port 82 where apache is listening

      proxy_pass  http://127.0.0.1:82;

    include /etc/nginx/proxy.conf;

    }

}

(7) (re-)start nginx

=====================

sudo nginx -s stop

sudo nginx

(8) edit hosts (windows)

========================

the lan ip of my ubuntu vm is: 192.168.222.118

#file: C:\Windows\System32\drivers\etc\hosts

192.168.222.118 tornado.example.com

192.168.222.118 example.com

(9) create an example php script: index.php

===========================================

#file: /home/seb/workspace/example/htdocs/index.php

<?php

var_dump(__FILE__);

echo ‘<a href=”http://tornado.example.com”> start tornado chat application …</a>’;

echo “<pre>”;

var_dump($_SERVER);

echo “</pre>”;

var_dump($_SERVER[“HTTP_HOST”]);

(10) testing

===========

http://example.com/index.php -> start index.php in your browser

http://tornado.example.com -> starts the chat application in your browser

  1. sebschmidt posted this