python+Nginx+uWSGI使用说明
安装环境
Remote: CentOS 7.4 x64 (django.example.com)
Python: Python3.6.5
Django: Django 2.0.4
nWSGI: uwsgi-2.0.15
Nginx: nginx- 1.10.2-1.el6
一. 系统环境配置
1.关闭iptables和selinux
# su - root
# service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux
修改
SELINUX=disabled
2.添加本地host DNS
# vi /etc/hosts
127.0.0.1 django.example.com
二. Python配置
1.安装python3.6.5源及依赖包
# yum install epel-release -y
# yum groupinstall "Development tools" -y
# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel zx-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel -y
2.编译安装python3.6.5以及pip package manager
# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz --no-check-certificate
# tar xf Python-3.6.5.tar.xz
# cd Python-3.6.5
# ./configure --prefix=/usr/local --with-ensurepip=install --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
# make && make altinstall
3.安装virtualenv
# pip3.6 install --upgrade pip
# pip3.6 install virtualenv
三. Nginx配置
1. 安装nginx package
# yum install nginx -y
2.配置nginx with nWSGI
# vi /etc/nginx/conf.d/django.conf
server {
listen 80;
server_name django.example.com; charset utf-8; access_log /var/log/nginx/django_access.log;
error_log /var/log/nginx/django_error.log; location = /favicon.ico { access_log off; log_not_found off; } location /static/ {
root /usr/share/nginx/html/django.example.com;
} client_max_body_size 20M; location / {
include uwsgi_params;
uwsgi_pass unix:/etc/uwsgi/uwsgi-django.sock;
uwsgi_read_timeout 30s;
uwsgi_send_timeout 30s;
} }
四. Django+uWSGI配置
1. uWSGI配置
# mkdir -p /etc/uwsgi
# vi /etc/uwsgi/uwsgi-django.ini
[uwsgi]
project = django.example.com
base = /data/www chdir = %(base)/%(project)
home = %(base)/%(project)/.py3env
module = myproject.wsgi:application pidfile = /tmp/uwsgi-master-django.pid master = true
processes = 2
enable-threads = true # use unix socket because it is more secure and faster than TCP socket
socket = /etc/uwsgi/uwsgi-django.sock
chmod-socket = 660
uid = nginx
gid = nginx vacuum = true
die-on-term = true logto = /var/log/nginx/uwsgi-django.log
socket : 地址和端口号,例如:socket = 127.0.0.1:50000
processes : 开启的进程数量
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number of workers / 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)。实际上最常用的,还是把运行记录输出到一个本地文件上。
log-maxsize :以固定的文件大小(单位KB),切割日志文件。 例如:log-maxsize = 50000000 就是50M一个日志文件。
pidfile : 指定pid文件的位置,记录主进程的pid号。
vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
disable-logging : 不记录请求信息的日志。只记录错误以及uWSGI内部消息到日志中。如果不开启这项,那么你的日志中会大量出现这种记录:
[pid: 347|app: 0|req: 106/367] 117.116.122.172 () {52 vars in 961 bytes} [Thu Jul 7 19:20:56 2016] POST /post => generated 65 bytes in 6 msecs (HTTP/1.1 200) 2 headers in 88 bytes (1 switches on core 0)
log-maxsize: 日志大小,当大于这个大小会进行切分 (Byte)
log-truncate: 当启动时切分日志
2. 配置Django base folder
# cd /usr/share/nginx/html
# mkdir django.example.com
# cd django.example.com
# virtualenv -p /usr/local/bin/python3 .py3env
3. 开启virtualenv python3环境
# source .py3env/bin/activate
4. 在此环境安装Django相关模块
# pip install django uwsgi PyMySQL
5. 创建uWSGI启动脚本
# mkdir -p /etc/uwsgi/bin
# vi /etc/systemd/system/uwsgi-django.service
[Unit]
Description=uWSGI instance to serve myproject [Service]
BASE=/data/www/django.example.com
ENV=$BASE/.py3env
ExecStartPre=-/usr/bin/bash -c 'chown -R nginx:nginx /etc/uwsgi'
ExecStart=/usr/bin/bash -c 'source /usr/share/nginx/html/django.example.com/.py3env/bin/activate; uwsgi --ini /etc/uwsgi/uwsgi-django.ini' [Install]
WantedBy=multi-user.target
五. Django项目配置
1. 保证virtualenv python3环境开启
# cd /usr/share/nginx/html/django.example.com/
# source .py3env/bin/activate
2.创建一个Django项目
# django-admin startproject myproject .
3.添加static目录
# vi myproject/settings.py
末行添加:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
4.创建本地SQLlite文件
Tip:这里使用SQLlite代替其他数据库作为我们项目的DB
# ./manage.py makemigrations
# ./manage.py migrate
5.创建项目管理员账户
# ./manage.py createsuperuser
6.生成项目静态文件目录
# ./manage.py collectstatic
7.修改wsgi入口文件
# vi myproject/wsgi.py
import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
sys.path.append('/usr/share/nginx/html/django.example.com') from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
8.添加ALLOWED_HOSTS
# vi myproject/settings.py
ALLOWED_HOSTS = ['django.example.com']
9. 修改权限(可执行并保持与nginx启动user一致)
# chmod -R 755 /etc/uwsgi
# chown -R nginx:nginx /etc/uwsgi
# chmod -R 755 /usr/share/nginx/html/django.example.com
# chown -R nginx:nginx /usr/share/nginx/html/django.example.com
10.启动nginx+uwsgi
# systemctl restart nginx
# systemctl restart uwsgi-django
ps.欠flask,多实例,优化,动静分离
python+Nginx+uWSGI使用说明的更多相关文章
- centOS 6.5采用python+nginx+uwsgi实现爬金十财经日历
上一篇中有关于安装nginx.python.uwsgi的过程,这里不再重述.下面是有关在具体布署中的一些过程和问题处理 一.因为用到了bs4(BeautifulSoup)\paste\lxml所以这些 ...
- python nginx+uwsgi+WSGI 处理请求详解
https://blog.csdn.net/a519640026/article/details/76157976 请求从 Nginx 到 uwsgi 到 django 交互概览 作为python w ...
- centos python nginx uwsgi
先更新系统,并安装编译环境等等. yum update yum install python python-devel libxml2 libxml2-devel python-setuptools ...
- 【金】nginx+uwsgi+django+python 应用架构部署
网上有很多这种配置,但就是没一个靠普的,费了好大的力气才完成架构部署.顺便记录一下. 一.部署前的说明 先安装好 python,django,uwsgi,nginx软件后.后配置运行的软件是分先后的. ...
- Nginx+uWSGI+Django+Python+ MySQL 搭建可靠的Python Web服务器
一.安装所需工具 yum -y install gcc gcc-c++ rpm-build mysql* libtool-ltdl* libtool automake autoconf libtool ...
- Nginx+uWSGI+Django+Python在Linux上的部署
搞了一整天,终于以发现自己访问网络的端口是错误的结束了. 首先要安装Nginx,uWSGI,Django,Python,这些都可以再网上查到. 安装好后可以用 whereis 命令查看是否安装好了各种 ...
- Centos+nginx+uwsgi+Python多站点环境搭建
前言 新公司的第一个项目,服务器端打算用python作为restful api.所以需要在Centos上搭建nginx+fastcgi+python的开发环境,但后面网上很多言论都说uwsgi比fas ...
- Centos下搭建 nginx+uwsgi+python
python做web应用最麻烦的还是配置服务器了,此话不假,光中间件就有好几种选择,fastcgi.wsgi.uwsgi,难 免让人眼花缭乱. 而听说uwsgi的效率是fastcgi和wsgi的10倍 ...
- Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器
Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器 闲着无聊的时候部署了一个Django项目玩,用vm虚拟机部署的. 准备工作 我使用的系统是Ubuntu16 ...
随机推荐
- Unity3D工程源码目录
2-0 暗黑破坏神3 链接:http://pan.baidu.com/s/1dEAUZoX 密码:cly4 2-1 炉石传说 客户端加服务器端 链接:http://pan.baidu.co ...
- AOPR密码过滤器
在Advanced Office Password Recovery任一攻击的破解选项设置窗口都有一个“编辑密码过滤器”按钮,设置好AOPR密码破解工具的密码过滤器可以大大提高破解速度,下文将具体介绍 ...
- 手机web不同屏幕字体大小高度自适应
body{font-size:0.6rem:} <script> //使用rem策略,不断更新html的fontsize (function(){ function sizeHtm ...
- VS-Qt-OSG-CMake基本项目框架
在VS-Qt-CMake基础上,打开mainwindow.ui,添加一个QWidget,然后在widget上右键-提升,选择SceneViewWidget CMakeLists.txt中添加了OSG相 ...
- C#中的抽象类与重写
今天的我们学习了好多,最初上午学习了文件流的方法,老师告诉我们是选修,可能以后不怎么用吧,但是还是想学下,似乎用个小程序读写文件很快地节奏,所以有点小兴趣学习,明天我再看看啦!今天之后学习了多态,继承 ...
- 《C++ Primer Plus》14.3 多重继承 学习笔记
多重继承(MI)描述的是有多个直接基类的类.与单继承一样,共有MI表示的也是is-a关系.例如,可以从Awiter类和Singer类派生出SingingWaiter类:class SingingWai ...
- Linux下Redis集群环境的搭建
一.安装redis(使用redis3.0版本) 1.需要gcc环境,如果没有执行命令安装gcc yum install gcc-c++ 2.下载redis3.0的源码包并上传至服务器 3.解压源码包 ...
- poj_1236 强连通分支
题目大意 有N个学校,这些学校之间用一些单向边连接,若学校A连接到学校B(B不一定连接到A),那么给学校A发一套软件,则学校B也可以获得.现给出学校之间的连接关系,求出至少给几个学校分发软件,才能使得 ...
- BNU4207:台风
东方非想天则(TH12.3)是一款优秀的格斗游戏,其以华丽的弹幕,连贯的技能衔接及优美的音乐吸引了众多玩家(宅男更多-_-),而且各平台上也为其提供了联机的机会. 好了,言归正传,天气系统是本游戏的一 ...
- activemq 实战 一
This chapter covers Introduction to the use case for each of the book examples Use of Maven for ...