Reproduced Deploying Python with Uwsgi and Nginx on Ubuntu 13.10

Source: Internet
Author: User
Tags directory create

Http://perlmaven.com/deploying-pyton-with-uwsgi-on-ubuntu-13-10

The following is a tutorial-to-set up Uwsgi with an Nginx front end-to-server simple Python scripts.

In this tutorial we'll only have the use of the packages that is supplied by Ubuntu.

It is tested on an Ubuntu 13.10 x64 droplet of Digital Ocean.

After your create a droplet with Ubuntu 13.10 x64-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 your would type this:

$ SSH [email protected]

On the Windows you ' d probably install putty and use that.

Once you is logged in your need to update the packages to the latest by typing the following:

# Aptitude update# Aptitude Safe-upgrade

Then reboot:

# reboot

This would disconnect you from the server. After a few seconds you can continue:

I ' d recommend copying your public SSH key-let-you ssh without password:

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

If The first command worked well and 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-vpython 2.7.5+# Uwsgi--version1.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 (' OK ', [(' Content-type ', ' text/html ')]    return " Hello World from Python "

Run the following script:

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 Yo u).

Without including the Python plugin, if I only run

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's probably better to create a configuration file called /home/dev/project/projec T-uwsgi.ini with the following content:

[Uwsgi]http-socket    =: 9090plugin    = Pythonwsgi-file =/home/dev/project/app.pyprocess   = 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 would start our servers 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 if Nginx starts this configuration file was 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 has the following in it.

DEF application (env, start_response):    start_response (' $ OK ', [(' Content-type ', ' text/html ')])    HTML = "< H1>hello World from python

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 cgidef Application (env, start_response):    start_response (' OK ', [(' Content-type ', ' text/html ')]    html = "

Of course you ' d probably don't build a real application this-the-to-the-good, but it's a, it's a, and the environment and see That everything works fine.

It can also is very useful to write small web interfaces.

[Reprint]deploying Python with Uwsgi and Nginx on Ubuntu 13.10

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.