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

智能推荐

while循环&CPU占用率高问题深入分析与解决方案_main函数使用while(1)循环cpu占用99-程序员宅基地

文章浏览阅读3.8k次,点赞9次,收藏28次。直接上一个工作中碰到的问题,另外一个系统开启多线程调用我这边的接口,然后我这边会开启多线程批量查询第三方接口并且返回给调用方。使用的是两三年前别人遗留下来的方法,放到线上后发现确实是可以正常取到结果,但是一旦调用,CPU占用就直接100%(部署环境是win server服务器)。因此查看了下相关的老代码并使用JProfiler查看发现是在某个while循环的时候有问题。具体项目代码就不贴了,类似于下面这段代码。​​​​​​while(flag) {//your code;}这里的flag._main函数使用while(1)循环cpu占用99

【无标题】jetbrains idea shift f6不生效_idea shift +f6快捷键不生效-程序员宅基地

文章浏览阅读347次。idea shift f6 快捷键无效_idea shift +f6快捷键不生效

node.js学习笔记之Node中的核心模块_node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是-程序员宅基地

文章浏览阅读135次。Ecmacript 中没有DOM 和 BOM核心模块Node为JavaScript提供了很多服务器级别,这些API绝大多数都被包装到了一个具名和核心模块中了,例如文件操作的 fs 核心模块 ,http服务构建的http 模块 path 路径操作模块 os 操作系统信息模块// 用来获取机器信息的var os = require('os')// 用来操作路径的var path = require('path')// 获取当前机器的 CPU 信息console.log(os.cpus._node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是

数学建模【SPSS 下载-安装、方差分析与回归分析的SPSS实现(软件概述、方差分析、回归分析)】_化工数学模型数据回归软件-程序员宅基地

文章浏览阅读10w+次,点赞435次,收藏3.4k次。SPSS 22 下载安装过程7.6 方差分析与回归分析的SPSS实现7.6.1 SPSS软件概述1 SPSS版本与安装2 SPSS界面3 SPSS特点4 SPSS数据7.6.2 SPSS与方差分析1 单因素方差分析2 双因素方差分析7.6.3 SPSS与回归分析SPSS回归分析过程牙膏价格问题的回归分析_化工数学模型数据回归软件

利用hutool实现邮件发送功能_hutool发送邮件-程序员宅基地

文章浏览阅读7.5k次。如何利用hutool工具包实现邮件发送功能呢?1、首先引入hutool依赖<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.19</version></dependency>2、编写邮件发送工具类package com.pc.c..._hutool发送邮件

docker安装elasticsearch,elasticsearch-head,kibana,ik分词器_docker安装kibana连接elasticsearch并且elasticsearch有密码-程序员宅基地

文章浏览阅读867次,点赞2次,收藏2次。docker安装elasticsearch,elasticsearch-head,kibana,ik分词器安装方式基本有两种,一种是pull的方式,一种是Dockerfile的方式,由于pull的方式pull下来后还需配置许多东西且不便于复用,个人比较喜欢使用Dockerfile的方式所有docker支持的镜像基本都在https://hub.docker.com/docker的官网上能找到合..._docker安装kibana连接elasticsearch并且elasticsearch有密码

随便推点

Python 攻克移动开发失败!_beeware-程序员宅基地

文章浏览阅读1.3w次,点赞57次,收藏92次。整理 | 郑丽媛出品 | CSDN(ID:CSDNnews)近年来,随着机器学习的兴起,有一门编程语言逐渐变得火热——Python。得益于其针对机器学习提供了大量开源框架和第三方模块,内置..._beeware

Swift4.0_Timer 的基本使用_swift timer 暂停-程序员宅基地

文章浏览阅读7.9k次。//// ViewController.swift// Day_10_Timer//// Created by dongqiangfei on 2018/10/15.// Copyright 2018年 飞飞. All rights reserved.//import UIKitclass ViewController: UIViewController { ..._swift timer 暂停

元素三大等待-程序员宅基地

文章浏览阅读986次,点赞2次,收藏2次。1.硬性等待让当前线程暂停执行,应用场景:代码执行速度太快了,但是UI元素没有立马加载出来,造成两者不同步,这时候就可以让代码等待一下,再去执行找元素的动作线程休眠,强制等待 Thread.sleep(long mills)package com.example.demo;import org.junit.jupiter.api.Test;import org.openqa.selenium.By;import org.openqa.selenium.firefox.Firefox.._元素三大等待

Java软件工程师职位分析_java岗位分析-程序员宅基地

文章浏览阅读3k次,点赞4次,收藏14次。Java软件工程师职位分析_java岗位分析

Java:Unreachable code的解决方法_java unreachable code-程序员宅基地

文章浏览阅读2k次。Java:Unreachable code的解决方法_java unreachable code

标签data-*自定义属性值和根据data属性值查找对应标签_如何根据data-*属性获取对应的标签对象-程序员宅基地

文章浏览阅读1w次。1、html中设置标签data-*的值 标题 11111 222222、点击获取当前标签的data-url的值$('dd').on('click', function() { var urlVal = $(this).data('ur_如何根据data-*属性获取对应的标签对象

推荐文章

热门文章

相关标签