HOWTO - Install Nginx + uWSGI + Django

本文档详细介绍了如何在CentOS 7上配置Nginx、uWSGI和Django。首先创建一个用于运行uwsgi和Django的用户,然后设置虚拟环境并安装Django。接着安装uWSGI,通过Unix套接字让uWSGI与Nginx通信。最后,配置Nginx以部署静态文件,并确保在系统启动时启动uWSGI。
摘要由CSDN通过智能技术生成

The basics are almost identical with the uWSGI official document. Here I would like to note a few important points when setting up Nginx + uWSGI + Django on CentOS 7.

Concept

A web server faces the outside world. It can serve files (HTML, images, CSS, etc) directly from the file system. However, it can’t talk directly to Django applications; it needs something that will run the application, feed it requests from web clients (such as browsers) and return responses.

A Web Server Gateway Interface - WSGI - does this job. WSGI is a Python standard.

uWSGI is a WSGI implementation. In this tutorial we will set up uWSGI so that it creates a Unix socket, and serves responses to the web server via the WSGI protocol. At the end, our complete stack of components will look like this:

the web client <-> the web server <-> the socket <-> uwsgi <-> Django

Before you start setting up uWSGI

Create a user web dedicated for running uwsgi and Django

As root:

useradd web

virtualenv

As user web, make sure you are in your home directory

cd ~

Make sure you are in a virtualenv for the software we need to install (we will describe how to install a system-wide uwsgi later):

virtualenv uwsgi-tutorial
cd uwsgi-tutorial

Django

As user web, install Django into your virtualenv, create a new project, and cd into the project:

pip install Django
django-admin.py startproject mysite
cd mysite

Basic uWSGI installation and configuration

Install uWSGI into your virtualenv

pip needs to compile some sources to install uwsgi, so you need development tools (gcc, make, …), libxml2 header and python headers.

As root:

yum groupinstall "Development Tools"
yum install python2.7-devel libxml2-devel

As user web, install uWSGI into your virtualenv

pip install uwsgi

Basic test

Create a file called test.py:

# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    # return [b"Hello World"] # python3
    return ["Hello World"] # python2

Run uWSGI:

uwsgi --http :8000 --wsgi-file test.py

This should serve a ‘hello world’ message directly to the browser on port 8000. Visit:

http://127.0.0.1:8000

to check. If so, it means the following stack of components works:

the web client <-> uWSGI <-> Python

Test your Django project

Now we want uWSGI to do the same thing, but to run a Django si

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值