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 ...
随机推荐
- MathType给公式底部加箭头的教程
箭头符号在数学中很常用的一个符号了,不管是在推导过程用以表示逻辑关系,还是表示向量,箭头符号都起着它就的作用.我们经常习惯给公式或者字母的上部加上箭头,那如何给公式的底部加上箭头呢?下面来介绍word ...
- UITextField 全属性
//初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...
- ionic creator(ionic生成器)
用来生成前端 html 还是挺方便的(接口数据另算),弄好就可以直接下载 https://creator.ionic.io/app/dashboard/projects
- /etc/logrotate.conf
/etc/logrotate.conf 是 Logrotate 工具的一个配置文件,这个工具用来自动切割系统日志,Logrotate 是基于 cron 来运行的,如下: [root@localhost ...
- 如何利用h5将视频设置为背景
我们常常有着将视频作为网页背景的需要,但是在设置时也经常差强人意,今天设置了一下,可以基本达到要求了,可能有些小细节做的不是太好,希望指出来,一起进步 第一步:准备工作 工欲善其事必先利其器,我们首先 ...
- iOS应用国际化教程(2014版)
本文转载至 http://www.cocoachina.com/industry/20140526/8554.html 这篇教程将通过一款名为iLikeIt的应用带你了解最基础的国际化概念,并为你的应 ...
- Android 使用线性布局LinearLayout和Button实现一个点红块游戏
这个游戏的功能类似打地鼠. 项目地址:https://github.com/moonlightpoet/RedBlock 程序下载试玩地址:https://github.com/moonlightpo ...
- js获取一个字符串中指定字符第n次出现的位置
function nthIndexOf(str,c,n){ var x=str.indexOf(c); for(var i=0;i<num;i++){ x=str.indexOf(c,x+1); ...
- N小时改变一次url时间戳的方法
//为url添加时间戳//time 为多长时间改变一次时间戳,以小时为单位function setTimeStamp(url, time){ var time = time || 4, ...
- object.prototype.call
object.prototype.call /* * object.prototype.call * @ 当一个object没有某个方法,但是其他的有,我们可以借助call或apply用其它对象的方法 ...