Nginx+Python+uwsgi+Django的web开发环境安装及配置
Nginx+Python+uwsgi+Django的web开发环境安装及配置
nginx安装
nginx的安装这里就略过了...
python安装
通常系统已经自带了,这里也略过
uwsgi安装
安装步骤如下:
yum -y install python-devel
wget -c https://projects.unbit.it/downloads/uwsgi-2.0.14.tar.gz
tar zxf uwsgi-2.0.14.tar.gz
cd uwsgi-2.0.14
make
# python uwsgiconfig.py --build
报错处理
plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
主要是没有安装python-devel
yum install python-devel
让Nginx支持uwsgi配置
相关网址
http://uwsgi-docs.readthedocs.io/en/latest/Nginx.html
#uwsgi_pass unix:///tmp/uwsgi.sock;
uwsgi_pass 127.0.0.1:6000;
include uwsgi_params;
uwsgi_params文件内容
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_ADDR $server_addr;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
集群配置
upstream uwsgicluster {
server unix:///tmp/uwsgi.sock;
server 192.168.1.235:3031;
server 10.0.0.17:3017;
}
uwsgi_pass uwsgicluster;
配置uwsgi
配置说明
http://uwsgi-docs.readthedocs.io/en/latest/Configuration.html
相关配置选项
http://uwsgi-docs.readthedocs.io/en/latest/Options.html
进入刚安装uwsgi的目录
执行./uwsgi
可以查看已经安装成功
有多种方式可以进行uwsgi配置,可以使用命令行进行配置,也可以使用配置文件
配置文件有ini xml json yaml等4种方式的配置方式,任选一种
命令行示例:
uwsgi --http-socket :9090 --psgi myapp.pl
uwsgi 的参数:
-M 开启Master进程
-p 4 开启4个进程
-s 使用的端口或者socket地址
-d 使用daemon的方式运行, 注意, 使用-d后, 需要加上log文件地址, 比如-d /var/log/uwsgi.log
-R 10000 开启10000个进程后, 自动respawn下
-t 30 设置30s的超时时间, 超时后, 自动放弃该链接
-limit-as 32 将进程的总内存量控制在32M
-x 使用配置文件模式
等价于配置文件
[uwsgi]
http-socket = :9090
psgi = myapp.pl
也可配置好后,通过命令行载入配置文件
uwsgi --ini http://uwsgi.it/configs/myapp.ini # HTTP
uwsgi --xml - # standard input
uwsgi --yaml fd://0 # file descriptor
uwsgi --json 'exec://nc 192.168.11.2:33000' # arbitrary executable
相关配置
[uwsgi]
socket = 127.0.0.1:3031
chdir = /home/foobar/myproject/
wsgi-file = myproject/wsgi.py
processes = 4
生产环境推荐开启
master = true
threads = 2
stats = 127.0.0.1:9191
python自动重载,当修改py文件后,自动重新载入,仅开发时用
py-autoreload = 2
[uwsgi]
workdir = /var
ipaddress = 0.0.0.0
; start an http router on port 8080
http = %(ipaddress):8080
; enable the stats server on port 9191
stats = 127.0.0.1:9191
; spawn 2 threads in 4 processes (concurrency level: 8)
processes = 4
threads = 2
; drop privileges
uid = nobody
gid = nogroup
; serve static files in /var/www
static-index = index.html
static-index = index.htm
check-static = %(workdir)/www
; skip serving static files ending with .lua
static-skip-ext = .lua
; route requests to the CGI plugin
http-modifier1 = 9
; map /cgi-bin requests to /var/cgi
cgi = /cgi-bin=%(workdir)/cgi
; only .lua script can be executed
cgi-allowed-ext = .lua
; .lua files are executed with the 'lua' command (it avoids the need of giving execute permission to files)
cgi-helper = .lua=lua
; search for index.lua if a directory is requested
cgi-index = index.lua
https支持
openssl genrsa -out foobar.key 2048
openssl req -new -key foobar.key -out foobar.csr
openssl x509 -req -days 365 -in foobar.csr -signkey foobar.key -out foobar.crt
[uwsgi]
https = :9090,foobar.crt,foobar.key
chdir = path_to_web2py
module = wsgihandler
master = true
processes = 8
启动uwsgi
uwsgi --ini /configs/myapp.ini
uwsgi --http-socket 127.0.0.1:3031 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
uwsgi --socket 127.0.0.1:3031 --chdir /home/foobar/myproject/ --wsgi-file myproject/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
重新启动uwsgi
# using kill to send the signal
kill -HUP `cat /tmp/project-master.pid`
# or the convenience option --reload
uwsgi --reload /tmp/project-master.pid
# or if uwsgi was started with touch-reload=/tmp/somefile
touch /tmp/somefile
停止uwsgi
kill -INT `cat /tmp/project-master.pid`
# or for convenience...
uwsgi --stop /tmp/project-master.pid
开机启动uwsgi
/etc/rc.local中添加启动脚本,如
/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log
xinted方式
相关文档
http://uwsgi-docs.readthedocs.io/en/latest/Inetd.html
service uwsgi
{
disable = no
id = uwsgi-000
type = UNLISTED
socket_type = stream
server = /root/uwsgi/uwsgi
server_args = --chdir /root/uwsgi/ --module welcome --logto /tmp/uwsgi.log
port = 3031
bind = 127.0.0.1
user = root
wait = yes
}
关于socket,http,http-socket,uwsgi-socket的区别
socket绑定到特定的unix/tcp socket使用默认协议,如将py请求转发至后端socket接口,如nginx转发请求,可以开启该接口
可以是一个端口:如 socket = :6000,对应的nginx配置为uwsgi_pass 127.0.0.1:6000
也可以是一个sock文件 socket = /tmp/uwsgi.sock,对应的nginx配置为uwsgi_pass unix:///path/to/your/mysite/mysite.sock;
http,绑定到特定的unix/tcp socket使用http协议,端口会暴露在外
http-socket绑定到特定的unix/tcp socket使用http协议,本地http协议,端口不会暴露在外
uwsgi-socket 绑定到特定的unix/tcp socket使用uwsgi的协议,类似socket,nginx也可转发到该选项配置的端口
可以配置开机启动,一个简单的配置/etc/init/uwsgi.conf
配置文件添加以下代码
# simple uWSGI script
description "uwsgi tiny instance"
start on runlevel [2345]
stop on runlevel [06]
respawn
exec uwsgi --master --processes 4 --die-on-term --socket :3031 --wsgi-file /var/www/myapp.wsgi
或者使用Emperor,可以配置多个app,在/etc/uwsgi目录下添加多个配置
# Emperor uWSGI script
description "uWSGI Emperor"
start on runlevel [2345]
stop on runlevel [06]
respawn
exec uwsgi --master --die-on-term --emperor /etc/uwsgi
nginx+uwsgi的配置实例
http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
实战nginx配置,测试用
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:6000; # for a web port socket (we'll use this first)
}
server {
listen 8080;
server_name uwsgi.com;
location /hi {
alias /home/www/hi/;
}
location ~* .*\.py($|/) {
# uwsgi_pass 127.0.0.1:6000;
uwsgi_pass django;
include uwsgi_params;
# uwsgi_param UWSGI_SCRIPT index;
# uwsgi_param UWSGI_PYHOME $document_root;
# uwsgi_param UWSGI_CHDIR $document_root;
}
access_log /home/httplogs/test.com-access.log main;
error_log /home/httplogs/test.com-error.log;
}
实战uwsgi配置,测试用
[uwsgi]
# socket = :6000
# http = :6000
uwsgi-socket = :6000
# http-socket = :6000
master = true
chdir = /home/www/
wsgi-file = /home/www/test.py
processes = 4
stats = 127.0.0.1:9090
daemonize = /home/log/log.log
pidfile = /tmp/uwsgi.pid
vacuum = true
# disable-logging = true
py-autoreload = 2
多应用部署
http://uwsgi-docs.readthedocs.io/en/latest/Emperor.html
http://uwsgi-docs.readthedocs.io/en/latest/Nginx.html?highlight=mount
[uwsgi]
socket = 127.0.0.1:3031
; mount apps
mount = /app1=app1.py
mount = /app2=app2.py
; rewrite SCRIPT_NAME and PATH_INFO accordingly
manage-script-name = true
使用virtualenv方式
Nginx+Python+uwsgi+Django的web开发环境安装及配置的更多相关文章
- Python & PyCharm & Django 搭建web开发环境
一.安装软件 1.安装 Python 2.7.PyCharm.pip(Python包管理工具).Django ( pip install Django) 二.部署 1.PyCharm 新建Django ...
- Python使用django搭建web开发环境
安装 Python 去 Python 官方网站找到 Python 3 的下载地址,根据你的系统选择 32 位或者 64 位的安装包,下载好后双击安装即可. 检测是否安装完成 C:\WINDOWS\sy ...
- Python & PyCharm & Django 搭建web开发环境(续)
由于Django自带轻量级的server,因此在前篇博文中,默认使用该server,但实际生产中是不允许这么干的,生产环境中通常使用Apache Httpd Server结合mod_wsgi.so来做 ...
- pycharm+python+Django之web开发环境的搭建(windows)
转载:https://blog.csdn.net/yjx2323999451/article/details/53200243/ pycharm+python+Django之web开发环境的搭建(wi ...
- [Python] 利用Django进行Web开发系列(二)
1 编写第一个静态页面——Hello world页面 在上一篇博客<[Python] 利用Django进行Web开发系列(一)>中,我们创建了自己的目录mysite. Step1:创建视图 ...
- PyCharm社区版+Django搭建web开发环境-2
接上一篇:PyCharm社区版+Django搭建web开发环境-1 1. 创建好django项目并建立app应用:web 2. setting.py:配置app应用 INSTALLED_APPS = ...
- 第1章 开发环境安装和配置(二)安装JDK、SDK、NDK
原文 第1章 开发环境安装和配置(二)安装JDK.SDK.NDK 无论是用C#和VS2015开发Androd App还是用Java和Eclipse开发Androd App,都需要先安装JDK和Andr ...
- React Native 开发环境安装和配置使用报错: -bash: react-native: command not found
[React Native 开发环境安装和配置:-bash: react-native: command not found 报错: 前提是安装homebrew,node.js ,npm ,watc ...
- [Python] 利用Django进行Web开发系列(一)
1 写在前面 在没有接触互联网这个行业的时候,我就一直很好奇网站是怎么构建的.现在虽然从事互联网相关的工作,但是也一直没有接触过Web开发之类的东西,但是兴趣终归还是要有的,而且是需要自己动手去实践的 ...
随机推荐
- bootstrap tab标签页
<ul id="myTab" class="nav nav-tabs"> <li class="active"> & ...
- 数据库 Mysql事务详解
Mysql事务 mysql的事务默认是自动提交的,也就是你提交一个query,他就直接执行!我们可以通过 禁止自动提交 开启自动提交 //mysql事务 #include <stdio.h> ...
- 使用React Native来撰写跨平台的App
React Native 是一个 JavaScript 的框架,用来撰写实时的.可原生呈现 iOS 和 Android 的应用.其是基于 React的,而 React 是 Facebook 的用于构建 ...
- DNS分析
一.DNS服务器 区域传送: 区域(zone)和 域(domain) 区域:物理概念 域:逻辑概念 区域数据库文件: 资源记录:RR 有类型:A AAAA PTR SOA NS CNAME MX SO ...
- 转自他人——————TLE之前,没有一个节点叫失败!!!
我想我十八年来记忆最深刻的夜晚,大概便是在吉林大学燥热的宿舍里,穿着橘黄色的短袖,努力咽下大口大口的雪碧.那天晚上我仰躺在吉大热得几乎不能入眠的床板上,一动不动,看夏夜里打不尽的蚊子在惨白的天花板下飞 ...
- FarPoint Spread ChildView子视图
有一种需求场景在很多地方都会用到,就是父子关系(头表和子表的关系),比如订单和订单明细. 做过winform的朋友第spread控件应该比较熟悉,或者了解.他的展示方式就通过一个关联关系就可以了,下面 ...
- iOS 之 UITextView
_lableAssess = [[UITextView alloc] init]; [_lableAssess setFrame:CGRectMake(left2, top2, width2, siz ...
- iOS 之 后台下载,前台显示模式,双 block
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //耗时的操作 NSURL *url ...
- QT第二天学习
回顾: qmake: qmake -project //生成*.pro文件 qmake //makefile make 注:if(qmake -v >=5) then QT += widget ...
- Java jsp基本结构
<!DOCTYPE html> <!-- [ published at 2015-11-13 12:30:50 ] --> <html> <head> ...