安装Django、Nginx和uWSGI
安装Django、Nginx和uWSGI
1.确定已经安装了2.7版本的Python;
2.安装python-devel
yum install python-devel
3.安装uwsgi
pip install uwsgi
测试uwsgi是否能正常工作
1.新建一个index.py;
# index.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"
- 1
- 2
- 3
- 4
2.uwsgi –http :8000 –wsgi-file index.py
浏览器访问8000端口看是否有hello world输出
注意:确保8000端口能被外网访问
测试Django能否正常工作
$ cd /var/www/ $ django-admin startproject mysite $ cd mysite $ python manage.py runserver 0.0.0.0:8000
浏览器访问8000端口看是否有hello world输出
测试uwsgi是否能和django集成
uwsgi --http :8000 --chdir=/var/www/mysite --module mysite.wsgi
or uwsgi --http :8008 --chdir /var/www/mysite --wsgi-file weixin/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9192
在浏览器中访问8000端口,看能否正常访问django网站。
参数说明:
# http : 协议类型和端口号
# processes : 开启的进程数量
# workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
# chdir : 指定运行目录(chdir to specified directory before apps loading)
# wsgi-file : 载入wsgi-file(load .wsgi file)
# stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
# threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
# master : 允许主进程存在(enable master process)
# daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常
用的,还是把运行记录输出到一个本地文件上。
# daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常
用的,还是把运行记录输出到一个本地文件上。
# vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
配置Nginx,使Nginx能为Django提供服务
在/etc/nginx/conf.d/下创建一个针对mysite项目的配置文件,详细如下:
# /etc/nginx/conf.d/mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server 127.0.0.1:8000; # for a web port socket
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django 的static和 media目录
# 如果没有static或media目录,你需要先创建
location /media {
alias /var/www/mysite/media;
}
location /static {
alias /var/www/mysite/static;
}
# 将所有非静态文件的请求转给django server处理,这里的django server用的是uwsgi。
location / {
uwsgi_pass django;
include /var/www/mysite/uwsgi_params;
}
}
#你可以从/etc/nginx/uwsgi_params复制一个拷贝到/var/www/mysite/uwsgi_params。
$ cp /etc/nginx/uwsgi_params /var/www/mysite/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
需要补充说明的是,在/etc/nginx/nginx.conf文件中,在最后一行的配置是include /etc/nginx/conf.d/*.conf,也就是说,/etc/nginx/conf.d/mysite_nginx.conf是会被包含在/etc/nginx/nginx.conf中的。
重启nginx服务器,验证访问结果
/etc/init.d/nginx restart
通过浏览器访问80端口,你发现了什么?502 Bad Gateway?是不是?想一想,这是为什么呢?原因是你访问80端口时,请求的资源不是static,也不是media,这个时候Nginx就把请求转给upstream django,upstream的网关配置的127.0.0.1:8000,而127.0.0.1:8000是要靠uwsgi启动的,所以报了一个502 Bad Gateway。你,明白了吗?
注:CentOS 7启动服务的命令是systemctl restart nginx.service
启动uwsgi,再次验证结果
执行下面一个命令,启动uwsgi。 uwsgi --socket :8000 --chdir=/var/www/mysite --module mysite.wsgi
重启Nginx服务/etc/init.d/nginx restart,再次通过浏览器访问80端口试试看。是不是成功了?
注:CentOS 7启动服务的命令是systemctl restart nginx.service
如何使uwsgi以配置文件运行?Configuring uWSGI to run with a .ini file
创建一个mysite_uwsgi.ini文件,内容如下:
[uwsgi]
socket=:8000
chdir = /var/www/mysite
#wsgi-file = mysite/wsgi.py
module=mysite.wsgi:application
processes = 10
threads = 2
#django<1.4,必须指定env和module
env = DJANGO_SETTINGS_MODULE=mysite.settings
# clear environment on exit
vacuum = true
safe-pidfile = /tmp/project-master.pid # create a pidfile
harakiri = 20 # respawn processes taking more than 20 seconds
limit-as = 128 # limit the project to 128 MB
max-requests = 5000 # respawn processes after serving 5000 requests
daemonize = /var/log/uwsgi/mysite.log # background the process & log
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
执行命令uwsgi --ini mysite_uwsgi.ini即可运行
如何以Emperor模式运行?
什么是Emperor模式?,官网说的很清楚,如下:
uWSGI can run in ‘emperor’ mode. In this mode it keeps an eye on a directory of uWSGI config files, and will spawn instances (‘vassals’) for each one it finds.
Whenever a config file is amended, the emperor will automatically restart the vassal.
按下面的步骤操作,即可以Emperor模式运行uwsgi:
1. create a directory for the vassals sudo mkdir /etc/uwsgi sudo mkdir /etc/uwsgi/vassals
2. symlink from the default config directory to your config file sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
3. run the emperor uwsgi --emperor /etc/uwsgi/vassals --uid nginx --gid nginx
如何创建uwsgi服务?
在Linux中,一个服务其实就是一个shell脚本。在CenOS6中,服务脚本一般都在/etc/init.d/目录下。
首先我们在/etc/initd/目录下创建一个uwsgi文件,文件内容如下:
#!/bin/sh
#
### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $syslog $remote_fs
# Should-Start: $time ypbind smtp
# Required-Stop: $syslog $remote_fs
# Should-Stop: ypbind smtp
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
# Check for missing binaries (stale symlinks should not happen)
UWSGI_BIN="/usr/local/bin/uwsgi"
UWSGI_EMPEROR_MODE=true
UWSGI_VASSALS="/etc/uwsgi/vassals/"
UWSGI_OPTIONS="--uid nginx --gid nginx --logto /var/log/uwsgi/uwsgi.log"
lockfile=/var/lock/subsys/uwsgi
UWSGI_OPTIONS="$UWSGI_OPTIONS --autoload"
if [ "$UWSGI_EMPEROR_MODE" = "true" ] ; then
UWSGI_OPTIONS="$UWSGI_OPTIONS --emperor $UWSGI_VASSALS"
fi
case "$1" in
start)
echo "Starting uWSGI ... "
daemon $UWSGI_BIN $UWSGI_OPTIONS &
;;
stop)
echo "Shutting down uWSGI ... "
killproc $UWSGI_BIN
;;
restart)
$0 stop
$0 start
;;
status)
echo -n "Checking for service uWSGI "
status $UWSGI_BIN
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac
exit 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
然后,我们可以使用此脚本来管理uwsgi,如下:
/etc/init.d/uwsgi start
/etc/init.d/uwsgi stop
/etc/init.d/uwsgi restart
/etc/init.d/uwsgi status
需要注意的是,日志文件夹的所属权应该归配置文件中指定的用户nginx $ chown nginx.nginx /var/log/uwsgi -R
如何设置开机起动uwsgi?
把启动uwsgi的命令添加到“/etc/rc.local”文件中即可。
多站点部署问题
#Simple HTTP server
server {
listen 80;
root /usr/share/nginx/www;
server_name host1.example.com;
}
#Django server
server {
listen 80;
server_name host2.example.com;
#...upstream config...
}
安装Django、Nginx和uWSGI的更多相关文章
- ubuntu 16 安装django nginx uWSGI
参考 https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and- ...
- linux下安装python27 nginx 和uwsgi
注意: python27 默认没有安装 pip 和setuptools所以要提前安装.(务必先提前安装python27 哈 ) wget --no-check-certificate https:// ...
- 使用Nginx和uwsgi部署Flask项目
前言 之前用Flask框架开发了一个Python的Web项目,使用Nginx和uWSGI部署起来感觉挺麻烦,过程中还因为对Flask框架的不熟悉,花了好长时间才把应用完全部署起来.下面分享部署成功 ...
- Django Nginx+uwsgi 安装配置
使用 python manage.py runserver 来运行服务器.这只适用测试环境中使用. 正式发布的服务,我们需要一个可以稳定而持续的服务器,比如apache, Nginx, lighttp ...
- Nginx+Python+uwsgi+Django的web开发环境安装及配置
Nginx+Python+uwsgi+Django的web开发环境安装及配置 nginx安装 nginx的安装这里就略过了... python安装 通常系统已经自带了,这里也略过 uwsgi安装 官网 ...
- 源码安装python +NGINX 的坎坷路 +uwsgi安装 部署django 的CRM项目
一.Nginx安装(基于ubuntu17.10 版本) 首先我们是基于源码安装,主要有如下步骤 1.安装依赖包 1.安装gcc g++的依赖库 sudo apt-get install build-e ...
- centos6.5安装nginx+python+uwsgi+django
nginx+uwsgi+django环境部署及测试 默认系统自带的python2.6.6 第一步(安装setuptools) wget https://pypi.python.org/packages ...
- 第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置
第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置 软件版本 uwsgi- ...
- python Django Nginx+ uWSGI 安装配置
环境: CentOS7.python-3.5.3.Nignx 1.10.3 .Django 1.10.6.uWSGI 2.0.14 django项目目录:/var/webRoot/p1 项目结构: 基 ...
随机推荐
- python:正则0
Python3 正则表达式特殊符号及用法(详细列表) 正则表达式的强大之处在于特殊符号的应用,特殊符号定义了字符集合.子组匹配.模式重复次数.正是这些特殊符号使得一个正则表达式可以匹配字符串集合而不只 ...
- Android的系统框架的深入认识
Android采用层次化系统架构,官方公布的标准架构如下图所示.Android由底层往上分为4个主要功能层,分别是linux内核层(Linux Kernel),系统运行时库层(Libraries和An ...
- PowerDesigner从安装到同步数据库
前言 最近项目在如火如荼的进行着4.0版本的数据库设计工作,我们几个后端小伙伴也马不停蹄的进行着数据库的设计.使用的设计软件是PowerDesigner,这里记录一些常见的问题以备日后查看 安装 链接 ...
- 2019-9-20:渗透测试,基础学习,phpstudy搭建Wordpress,Burpsuite抓取WorePress cms的post包
一.搭建WordPress的cms网站管理系统 1,下载Wordpress cms源码,下载地址:https://wordpress.org/download/ 2,将源码解压到phpstudy目录下 ...
- 【2018寒假集训 Day2】【动态规划】垃圾陷阱(挖坑等填,未完成)
垃圾陷阱 (well) 卡门--农夫约翰极其珍视的一条Holsteins奶牛--已经落了到"垃圾井"中."垃圾井"是农夫们扔垃圾的地方,它的深度为D (2 &l ...
- (四十五)golang--反射
反射基本介绍: (1)反射可以在运行时动态获取变量的各种信息,比如变量的类型.类别: (2)如果是结构体变量,还可以获取结构体本身的信息(包括结构体字段.方法): (3)通过反射,可以修改变量的值,可 ...
- linux进程管理常用命令
初始化进程在centos5,6,7中的发展: 在centos5中使用sysv init 是一个shell脚本,依靠依次执行脚本中的命令启动系统,只能串行执行. 在centos6中使用upstart,也 ...
- Django4模型(操作数据库)
模型入门同步数据库的两个指令创建模型注意事项1.外键ForeignKey 模型入门 同步数据库的两个指令 python manage.py makemigrations python manage.p ...
- 申请SSL证书
1.为什么需要申请SSL证书呢? 因为之前公司网站是通过http访问的,现在要通过https方式访问,前面多了一个s,那就需要SSL证书,用https方式访问的,会加密用户上传和下载的数据,使访问更加 ...
- jQuery九宫格抽奖
<div id="box"> <div class="content content-1">1</div> <div ...