centos下使用nginx+uwsgi 部署django_centos8 uwsgi you are running uwsgi as root-程序员宅基地

CentOS 下使用uwsgi+nginx部署django

当前环境:

  • Centos7
  • Django2.0
  • Python3.7

安装uwsgi

pip install uwsgi

安装Nginx

cd  /usr/local
wget http://nginx.org/download/nginx-1.7.4.tar.gz  
tar -zxvf nginx-1.7.4.tar.gz  
cd  nginx-1.7.4  
./configure  $默认安装在/usr/local/nginx   
make  
make install     

Nginx常用命令

启动nginx:
/usr/local/nginx/sbin/nginx

其他参数:
[user@host dir]$ /usr/local/nginx/sbin/nginx -h
nginx version: nginx/1.8.0
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
-?,-h : this help  查看帮助信息
-v : show version and exit 显示 nginx 的版本
-V : show version and configure options then exit 显示 nginx 的版本,编译器版本和配置参数
-t : test configuration and exit 不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload  传递一个信号,stop快速关闭,quit从容关闭,reopen重新打开日志文件、用于切换日志文件,reload重载配置文件
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)  设置配置文件
-g directives : set global directives out of configuration file

django项目结构

├── db.sqlite3
├── manage.py
├── okr
│   ├── admin.py
│   ├── apps.py
│   ├── exc.py
│   ├── __init__.py
│   ├── migrations
│   ├── models.py
│   ├── okr_middleware.py
│   ├── serializers.py
│   ├── tests.py
│   ├── urls.py
│   ├── utils.py
│   └── views.py
├── okr_manage
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── README.md
├── requirements.txt
├── static
├── templates
│   ├── 404.html
│   ├── back.html
│   ├── Home.html
│   ├── index_back.html
│   ├── login.html
│   ├── personal_goals.html
│   ├── sector_strategy.html
│   ├── team_goals.html
│   └── test.html
└── uwsgi.ini

测试uwsgi

# 创建test.py文件
def application(env, start_response):
    start_response("200 OK", [("Content-Type","text/html")])
    return [b"Hello World"]
通过uwsgi运行该文件
uwsgi --http :8001 --wsgi-file test.py
# 访问127.0.0.1:8001 返回hello world代表uwsgi正常可用

配置Django与uwsgi连接

在我们通过Django创建okr项目时,在子目录okr_manage下已经帮我们生成的 wsgi.py文件。所以,我们只需要再创建uwsgi.ini配置文件即可,当然,uwsgi支持多种类型的配置文件,如xml,ini等。此处,使用ini类型的配置.

# uwsgi.ini配置
[uwsgi]
http = :9000
socket = /tmp/uwsgi.sock  # uwsgi启动时会创建这个文件,需chmod 777权限
chdir = /opt/okr
wsgi-file = okr_manage/wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9191
vacuum= true
其他配置项:
  • 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)。实际上最常用的,还是把运行记录输出到一个本地文件上。
  • pidfile : 指定pid文件的位置,记录主进程的pid号。
  • vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)

使用uwsgi启动django

uwsgi --ini myweb_uwsgi.ini 
# 成功信息:
# 注意查看uwsgi的启动信息,如果有错,就要检查配置文件的参数是否设置有误

[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.17.1 (64bit) on [Mon Aug 27 06:16:03 2018] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-28) on 27 August 2018 05:39:18
os: Linux-3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017
nodename: workstation-qa.yiguo.com
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /opt/okr
detected binary path: /usr/local/python3/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
chdir() to /opt/okr
your processes number limit is 63082
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :9000 fd 3
uwsgi socket 0 bound to TCP address 127.0.0.1:8000 fd 6
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
Python version: 3.7.0 (default, Aug  9 2018, 22:10:04)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
Python main interpreter initialized at 0x1f7b360
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 416880 bytes (407 KB) for 8 cores
*** Operational MODE: preforking+threaded ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1f7b360 pid: 4511 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 4511)
spawned uWSGI worker 1 (pid: 4512, cores: 2)
spawned uWSGI worker 2 (pid: 4513, cores: 2)
spawned uWSGI worker 3 (pid: 4515, cores: 2)
spawned uWSGI worker 4 (pid: 4517, cores: 2)
*** Stats server enabled on 127.0.0.1:9191 fd: 22 ***
spawned uWSGI http 1 (pid: 4520)

nginx.conf配置

# vim /usr/local/nginx/conf/nginx.conf
# 如果出现403问题,请设置项目目录的权限,并且在nginx配置中去掉注释# user=nobady  改成user=root即可

    server {
    listen         8099; # 指定的是nginx代理uwsgi对外的端口
    server_name    127.0.0.1 # 网上大多资料都是设置的一个网址(例,www.example.com),我这里如果设置成网址无法访问,所以,指定的到了本机默认ip
    charset UTF-8;
    access_log      /var/log/nginx/myweb_access.log;
    error_log       /var/log/nginx/myweb_error.log;

    client_max_body_size 75M;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/uwsgi.sock;
        uwsgi_read_timeout 600;
    }
    location /static {
        expires 30d;
        autoindex on;
        add_header Cache-Control private;
        alias /opt/okr/static/; # 这里的目录必须是当前项目的static目录
     }
 }

在进行配置的时候,我有个问题一直想不通。nginx到底是如何uwsgi产生关联。现在看来大概最主要的就是这两行配置 include uwsgi_params; uwsgi_pass 127.0.0.1:8000; include 必须指定为uwsgi_params;而uwsgi_pass指的本机IP的端口号与myweb_uwsgi.ini配置中的文件中的必须一致.

django设置

收集Django静态文件,把Django自带的静态文件收集到同一个static中,不然访问Django的admin页面会找不到静态文件。在django的setting文件中,添加下面一行内容.

settings.py

STATIC_ROOT = os.path.join(BASE_DIR, "/static/)
DEBUG = False
ALLOWED_HOSTS = ["*"]
运行manage.py命令
python manage.py collectstatic

启动服务

uwsgi --ini uwsgi.ini
/usr/local/nginx/sbin/nginx

测试访问

http://127.0.0.1:8099
223916_bL9y_2663968.jpg
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_36646275/article/details/83965626

智能推荐

使用nginx解决浏览器跨域问题_nginx不停的xhr-程序员宅基地

文章浏览阅读1k次。通过使用ajax方法跨域请求是浏览器所不允许的,浏览器出于安全考虑是禁止的。警告信息如下:不过jQuery对跨域问题也有解决方案,使用jsonp的方式解决,方法如下:$.ajax({ async:false, url: 'http://www.mysite.com/demo.do', // 跨域URL ty..._nginx不停的xhr

在 Oracle 中配置 extproc 以访问 ST_Geometry-程序员宅基地

文章浏览阅读2k次。关于在 Oracle 中配置 extproc 以访问 ST_Geometry,也就是我们所说的 使用空间SQL 的方法,官方文档链接如下。http://desktop.arcgis.com/zh-cn/arcmap/latest/manage-data/gdbs-in-oracle/configure-oracle-extproc.htm其实简单总结一下,主要就分为以下几个步骤。..._extproc

Linux C++ gbk转为utf-8_linux c++ gbk->utf8-程序员宅基地

文章浏览阅读1.5w次。linux下没有上面的两个函数,需要使用函数 mbstowcs和wcstombsmbstowcs将多字节编码转换为宽字节编码wcstombs将宽字节编码转换为多字节编码这两个函数,转换过程中受到系统编码类型的影响,需要通过设置来设定转换前和转换后的编码类型。通过函数setlocale进行系统编码的设置。linux下输入命名locale -a查看系统支持的编码_linux c++ gbk->utf8

IMP-00009: 导出文件异常结束-程序员宅基地

文章浏览阅读750次。今天准备从生产库向测试库进行数据导入,结果在imp导入的时候遇到“ IMP-00009:导出文件异常结束” 错误,google一下,发现可能有如下原因导致imp的数据太大,没有写buffer和commit两个数据库字符集不同从低版本exp的dmp文件,向高版本imp导出的dmp文件出错传输dmp文件时,文件损坏解决办法:imp时指定..._imp-00009导出文件异常结束

python程序员需要深入掌握的技能_Python用数据说明程序员需要掌握的技能-程序员宅基地

文章浏览阅读143次。当下是一个大数据的时代,各个行业都离不开数据的支持。因此,网络爬虫就应运而生。网络爬虫当下最为火热的是Python,Python开发爬虫相对简单,而且功能库相当完善,力压众多开发语言。本次教程我们爬取前程无忧的招聘信息来分析Python程序员需要掌握那些编程技术。首先在谷歌浏览器打开前程无忧的首页,按F12打开浏览器的开发者工具。浏览器开发者工具是用于捕捉网站的请求信息,通过分析请求信息可以了解请..._初级python程序员能力要求

Spring @Service生成bean名称的规则(当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致)_@service beanname-程序员宅基地

文章浏览阅读7.6k次,点赞2次,收藏6次。@Service标注的bean,类名:ABDemoService查看源码后发现,原来是经过一个特殊处理:当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致public class AnnotationBeanNameGenerator implements BeanNameGenerator { private static final String C..._@service beanname

随便推点

二叉树的各种创建方法_二叉树的建立-程序员宅基地

文章浏览阅读6.9w次,点赞73次,收藏463次。1.前序创建#include<stdio.h>#include<string.h>#include<stdlib.h>#include<malloc.h>#include<iostream>#include<stack>#include<queue>using namespace std;typed_二叉树的建立

解决asp.net导出excel时中文文件名乱码_asp.net utf8 导出中文字符乱码-程序员宅基地

文章浏览阅读7.1k次。在Asp.net上使用Excel导出功能,如果文件名出现中文,便会以乱码视之。 解决方法: fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);_asp.net utf8 导出中文字符乱码

笔记-编译原理-实验一-词法分析器设计_对pl/0作以下修改扩充。增加单词-程序员宅基地

文章浏览阅读2.1k次,点赞4次,收藏23次。第一次实验 词法分析实验报告设计思想词法分析的主要任务是根据文法的词汇表以及对应约定的编码进行一定的识别,找出文件中所有的合法的单词,并给出一定的信息作为最后的结果,用于后续语法分析程序的使用;本实验针对 PL/0 语言 的文法、词汇表编写一个词法分析程序,对于每个单词根据词汇表输出: (单词种类, 单词的值) 二元对。词汇表:种别编码单词符号助记符0beginb..._对pl/0作以下修改扩充。增加单词

android adb shell 权限,android adb shell权限被拒绝-程序员宅基地

文章浏览阅读773次。我在使用adb.exe时遇到了麻烦.我想使用与bash相同的adb.exe shell提示符,所以我决定更改默认的bash二进制文件(当然二进制文件是交叉编译的,一切都很完美)更改bash二进制文件遵循以下顺序> adb remount> adb push bash / system / bin /> adb shell> cd / system / bin> chm..._adb shell mv 权限

投影仪-相机标定_相机-投影仪标定-程序员宅基地

文章浏览阅读6.8k次,点赞12次,收藏125次。1. 单目相机标定引言相机标定已经研究多年,标定的算法可以分为基于摄影测量的标定和自标定。其中,应用最为广泛的还是张正友标定法。这是一种简单灵活、高鲁棒性、低成本的相机标定算法。仅需要一台相机和一块平面标定板构建相机标定系统,在标定过程中,相机拍摄多个角度下(至少两个角度,推荐10~20个角度)的标定板图像(相机和标定板都可以移动),即可对相机的内外参数进行标定。下面介绍张氏标定法(以下也这么称呼)的原理。原理相机模型和单应矩阵相机标定,就是对相机的内外参数进行计算的过程,从而得到物体到图像的投影_相机-投影仪标定

Wayland架构、渲染、硬件支持-程序员宅基地

文章浏览阅读2.2k次。文章目录Wayland 架构Wayland 渲染Wayland的 硬件支持简 述: 翻译一篇关于和 wayland 有关的技术文章, 其英文标题为Wayland Architecture .Wayland 架构若是想要更好的理解 Wayland 架构及其与 X (X11 or X Window System) 结构;一种很好的方法是将事件从输入设备就开始跟踪, 查看期间所有的屏幕上出现的变化。这就是我们现在对 X 的理解。 内核是从一个输入设备中获取一个事件,并通过 evdev 输入_wayland

推荐文章

热门文章

相关标签