[转载]Deploying Python with uWSGI and Nginx on Ubuntu 13.10

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 [email protected]

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:

$ scp ~/.ssh/id_rsa.pub [email protected]:.ssh/authorized_keys
$ ssh [email protected]

If the first command worked well, then the second won‘t ask for a password.

Install uWSGI

# aptitude install uwsgi
# aptitude install uwsgi-plugin-python

Just to make sure, check the versions we have. I got the following:

# python -V
Python 2.7.5+
# uwsgi --version
1.9.13-debian

Then create a user called dev so we won‘t do everything as root.

# adduser --gecos ‘‘ --disabled-password  dev

Switch to the new user and create a directory for the project.

# su - dev
$ mkdir project
$ cd project/

In the project/ directory create a file called app.py with the following content:

def application(env, start_response):
    start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html‘)])
    return "Hello World From Python"

Run the following script:

$ uwsgi --http-socket :9090 --plugin python --wsgi-file app.py

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

$ uwsgi --http-socket :9090 --wsgi-file app.py

I‘d get the following error:

uwsgi: unrecognized option ‘--wsgi-file‘
getopt_long() error

Further uWSGI configuration (3 processes handle the requests) can be provided on the command line:

$ uwsgi --http-socket :9090 --plugin python --wsgi-file app.py --process 3

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:

[uwsgi]
http-socket    = :9090
plugin    = python
wsgi-file = /home/dev/project/app.py
process   = 3

Now we can launch the server using the following command:

uwsgi --ini project-uwsgi.ini

We can shut it down by pressing Ctrl-C.

Then we switch back to user root typing

$ exit

We can then create a symbolic link so uWSGI will start our server automatically when the server boots up:

# ln -s /home/dev/project/project-uwsgi.ini /etc/uwsgi/apps-enabled/

now we can launch the service as root with the following command:

# service uwsgi start

Add Nginx to the mix

First thing, replace http-socket by socket in project-uwsgi.ini file.

Install Nginx and remove the default configuration file:

# aptitude install nginx
# service nginx start
# rm /etc/nginx/sites-enabled/default

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.

时间: 2024-10-05 21:04:55

[转载]Deploying Python with uWSGI and Nginx on Ubuntu 13.10的相关文章

[django] Deploy Django Applications Using uWSGI and Nginx on Ubuntu 14.04

关键点1:chmod-socket=666 (mysite_uwsgi.ini) 关键点2 : 工程目录和虚拟环境目录搞清楚 几个参考: http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-

python + flask + uwsgi + gevent + nginx 环境搭建(非阻塞)

Flask是Python中一个微型的Web开发框架.在debug 模式 或 单纯的 uwsgi模式下,flask是阻塞模式的,也就是说一次只能效应一个请求,或者在uwsgi 开启多进程,响应已知的请求个数:我们这里使用  uwsgi 和 gevent 配合nginx 解决flask的阻塞模式. 1.环境 CentOS Linux release 7.0.1406 (Core) Python 2.7.5 2.安装类库 yum install python-devel zlib-devel bzip

Installing teamviewer 9 on 64-bit Ubuntu 13.10 (转载)

While trying to install Teamviewer 9 on 64-bit Ubuntu 13.10, you’ll get a dependencies error such as this: Unpacking teamviewer (from teamviewer_linux_x64.deb) ... dpkg: dependency problems prevent configuration of teamviewer:  teamviewer depends on

一个简单的python web应用部署 nginx+uwsgi

1.环境配置 1)所需环境 操作系统 Mac os python3.6,web.py 0.40, uWSGI 2.0.15, uwsgitop 0.10, nginx 1.10.3 2)步骤 安装python3.6 -略 安装web.py easy_install web.py(推荐) pip install web.py 如果报错 可能还需安装utils db pip install utils pip install db 安装uWSGI pip install uwsgi 安装uwsgit

python django+uwsgi+nginx

概念: WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface, 是为 Python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接口. 自从 WSGI 被开发出来以后,许多其它语言中也出现了类似接口.可以理解为一个标准和规范. 很多框架都自带了 WSGI server ,比如 Flask,webpy,Django.CherryPy等等.当然性能都不好,自带的 web s

python django+uwsgi+nginx 搭建步骤(血一般的教训)

安装Django 没有pip的童鞋可以看http://blog.csdn.net/imphp/article/details/38228619安装python包管理工具pip 先在 https://pypi.python.org/ 上 搜索Django,排在第一的那个就是我们要安装的主要框架Django啦- 点进去可以查找一些相关文档,虽然全部是鸟语花香,不过有翻译.百度.炕忙,就算是菜鸟也成英国鸟了. Django 的官网文档 https://docs.djangoproject.com/en

python服务器环境搭建Flask,uwsgi和nginx

python服务器环境搭建Flask,uwsgi和nginx 环境配置 服务器配置 [部署系统环境Ubuntu] 使用python的Flask框架搭建好网页后台后,便要开始将网站部署到服务器平台了.为了部署python服务器,我选择使用uwsgi和nginx. 使用Nginx和uWSGI来运行Python应用 Nginx Nginx是高效的Web服务器和反向代理服务器,同时并发高,部署简单,内存消耗小,最重要的是,支持uWSGI的uwsgi协议,可以直接使用,一个简单的uwsgi_pass就可以

Setting up Django and your web server with uWSGI and nginx

https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html Setting up Django and your web server with uWSGI and nginx This tutorial is aimed at the Django user who wants to set up a production web server. It takes you through the steps re

[r]Setting up Django and your web server with uWSGI and nginx

Setting up Django and your web server with uWSGI and nginx This tutorial is aimed at the Django user who wants to set up a production web server. It takes you through the steps required to set up Django so that it works nicely with uWSGI and nginx. I