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


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


source (including examples):

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

tar xvzf tornado-0.2.tar.gz

cd tornado-0.2

python build

sudo python 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/

The chat application is now running on port 8888.

(3) bind apache to port 82 (use 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 mod_gnutls.c>

    Listen 443


(4) create a vhost “” 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>


     DocumentRoot /home/seb/workspace/example/htdocs

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

         # allow .htaccess:

        AllowOverride All

        allow from all



 (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




  listen 80;


  location / {

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


    include /etc/nginx/proxy.conf;






  listen 80;


  location / {

      # redirect everything to port 82 where apache is listening


    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:

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

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


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



echo ‘<a href=””> start tornado chat application …</a>’;

echo “<pre>”;


echo “</pre>”;


(10) testing

=========== -> start index.php in your browser -> starts the chat application in your browser

  1. sebschmidt posted this