Nginx+uWSGI+Django框架搭建

本文介绍如何使用Nginx、uWSGI和Django进行Web应用部署,包括各组件的安装、配置及测试步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nginx+uWsgi+Django
Nginx : 1.8.0
uWSGI : 2.0.6
Django : 1.8.4
系统环境:Ubuntu 14.04LTS

Nginx

官网:http://nginx.org/

安装

1. 系统安装

$sudo apt-get install nginx

2. 源码安装

官网下载源码包:建议下载稳定版
地址:http://nginx.org/en/download.html
安装依赖库,依赖库gcc,pcre,zlib,openssl

$sudo apt-get install gcc
$sudo apt-get install libpcre3 libpcre3-dev
$sudo apt-get install zlib1g zlib1g-dev
$sudo apt-get install openssl libssl-dev

解压,配置,编译,安装

$tar zxvf nginx-1.8.0.tar.gz
$cd nginx-1.8.0
$./configure --with-pcre --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_stub_status_module --with-debug
$make
$sudo make install

通过命令查看安装地址:

whereis nginx

默认安装路径为:/usr/local/nginx
在启动时nignx时遇到命令无法识别,执行以下指令

$sudo cp /usr/local/nginx/sbin/nginx /usr/bin/

然后启动nginx

sudo nginx

通过浏览器访问http://localhost:80
nginx默认的设置界面即表示启动成功
Welcome to nginx

3. nginx配置文件夹

conf : /usr/local/nginx/conf

相关命令

启动:

$sudo nginx 

关闭:

$sudo nginx -s stop

重启:

$sudo nginx -s reload

uWSGI

安装教程:http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html

1. 安装

$sudo apt-get install uwsgi-plugin-python
$sudo apt-get install uwsgi

源码安装

下载:http://projects.unbit.it/uwsgi/wiki/WikiStart
安装依赖项:

apt-get install build-essential python-dev

解压,配置,编译,安装

$tar zxvf uwsgi-2.0.6.tar.gz
$cd uwsgi-2.0.6
$sudo python setup.py install

如果提示

No module named setuptools

下载:https://pypi.python.org/pypi/setuptools/
解压,配置,编译,安装

$ tar zxvf setuptools-18.2.tar.gz
$ cd setuptools-18.2/
$ sudo python setup.py install

重新编译uWSGI
当安装完成后,会提示end of uWSGI configration

测试uWSGI

打印版本信息

$uwsgi --version

编写测试脚本,test.py

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

启动uWSGI,使用未占用端口

$uwsgi --http :9090 --wsgi-file test.py

在浏览器输入地址访问

http://localhost:9090/

浏览器输出 Hello World,表示安装成功

相关命令

启动:
执行相应脚本
关闭:

$killall  -9 uwsgi
$killall -s HUP /var/www/uwsgi 
$killall -s HUP /usr/local/bin/uwsgi

Django

安装依赖库

$sudo apt-get install python2.7

安装Django

下载源码:https://www.djangoproject.com/download/
解压,配置,编译,安装

$tar zxvf Django-1.8.4.tar.gz
$cd Django-1.8.4
$sudo python setup.py install

测试Django

打印Django版本信息

$python
>>import django
>>django.VERSION
(1,8,4, 'final', 0)

当出现如上显示时,说明Django安装成功

其他

MySQL

Mysql数据库服务

sudo apt-get install mysql-server

Django程序访问数据库,python接口

sudo apt-get install python-mysqldb

图形化管理工具workbench

sudo apt-get install mysql-workbench

Nginx->uWSGI

修改配置文件:默认路径 /usr/local/nginx/conf/nginx.conf
配置Nginx,将Nginx接收到的HTTP请求转发给uWSGI

// 将域名下的所有请求全部转发
location / {
// nginx配置文件同级目录下需包含uwsgi_params文件,默认是包含的
// 如果没有可以创建软连接
include uwsgi_params;
// 配置nginx与uWSGI通信的socket通信接口
uwsgi_pass 127.0.0.1:3031;
}

uWSGI->Django

uWSGI自启动脚本

编写自启动脚本,让uWSGI随系统自启动
在/etc/init/目录下新建uwsgi.conf文件

cd /etc/init/
sudo gedit uwsgi.conf

启动脚本如下:

#simple uWSGI script
description "uwsgi tiny instance"
start on runlevel [2345]
stop on runlevel [06]
respawn
env LOGTO=/var/log/uwsgi/emperor.log
exec uwsgi --master --emperor /etc/uwsgi/apps-enabled --die-on-term --uid 1000 --gid 1000 --logto $LOGTO --vacuum

创建软连接,将uWSGI配置文件连接到apps-enabled目录下

sudo ln -s ~/path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/apps-enabled/mysite_uwsgi.ini

mysite_uwsgi.ini(根据项目要求设置,或参考官方文档)

[uwsgi]
socket = 127.0.0.1:3031
chdir = /home/foobar/myproject/
pythonpath = ..
env = DJANGO_SETTINGS_MODULE=myproject.settings
module = django.core.handlers.wsgi:WSGIHandler()
processes = 4
threads = 2
stats = 127.0.0.1:9191

Django->MySQL

在Django项目的settings.py文件中配置MySQL

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'djangodb',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '127.0.0.1',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '3306',                      # Set to empty string for default.
    }
}

重启,查看端口是否已经启动

参考

http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html
http://segmentfault.com/q/1010000002523354
http://www.nowamagic.net/academy/detail/1330331
http://www.nowamagic.net/academy/detail/1330334
http://www.linuxidc.com/Linux/2014-09/106928.htm

查看端口命令

查看系统端口占用情况,netstat

sudo netstat -plnt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值