http://perlmaven.com/deploying-pyton-with-uwsgi-on-ubuntu-13-10
The following is a tutorial on how to set up uWSGI with an Nginx front end to server simple Python scripts.
In this tutorial we will only use the packages that are supplied by Ubuntu.
It was tested on an Ubuntu 13.10 x64 droplet of Digital Ocean.
After you create a droplet with Ubuntu 13.10 x64 you'll get an e-mail with your IP address and the password of root. In this example I'll use 1.2.3.4 as the IP address. You'll have to replace the commands with the IP address of your server.
First just ssh to the server. On Linux/Unix/OSX you would type this:
$ ssh root@1.2.3.4
On Windows you'd probably install putty and use that.
Once you are logged in you need to update the packages to the latest by typing the following:
# aptitude update
# aptitude safe-upgrade
Then reboot:
# reboot
This will disconnect you from the server. After a few seconds you can continue:
I'd recommend copying your public ssh key to let you ssh without password:
Now you can already visit the we site by following th URL: http://1.2.3.4:9090 (remember to replace the IP with the one you have).
Without including the python plugin, if I only run
But, instead of the command line, it is probably better to create a configuration file called /home/dev/project/project-uwsgi.ini with the following content:
Instead of that create a new configuration file in /home/dev/project/nginx-uwsgi.conf with the following content:
server {
location /hello/ {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
}
}
Create a symbolic link in the directory of Nginx so when Nginx starts this configuration file is taken in account.
# ln -s /home/dev/project/nginx-uwsgi.conf /etc/nginx/sites-enabled/
# service nginx restart
Now you can visit http://1.2.3.4 and see the output of the same script as you saw earlier.
Show the environment
Edit the /home/dev/project/app.py file to have the following in it.
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
html = "<h1>Hello World From Python</h1>\n"
html += "<table>\n"
for k in env:
html += "<tr><td>{}</td><td>{}</td></tr>\n".format(k, env[k])
html += "</table>\n"
return html
and visit your home page again. You'll see all the environment it receives.
Add echo form
Update the script again to include a form and to echo back whatever the user typed in:
import cgi
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
html = "<h1>Hello World From Python</h1>\n"
html += "<table>\n"
for k in env:
html += "<tr><td>{}</td><td>{}</td></tr>\n".format(k, env[k])
html += "</table>\n"
html += "<form>\n"
html += '<input name="txt" />\n'
html += '<input type="submit" value="Echo" />\n'
html += "</form>\n"
form = cgi.FieldStorage(environ=env)
if 'txt' in form:
html += "<hr>You said: <b>{}</b>\n".format(form['txt'].value)
return html
Of course you'd probably not build a real application this way, but it is a good way to play with the environment and see that everything works fine.
It can also be very useful to write small web interfaces.