CentOS   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了centos下配置django、uwsgi和nginx(亲测成功)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

版本为centos7.4,默认看本博客的人会基本的django开发,即知道如何使用pip安装django和venv虚拟环境并新建项目以及使用django自带的wsgi.py启动项目。 本教程使用的是root用户,并不推荐。 1、更新系统:  yum update  yum upgrade 2、安装nginx 如果能成功通过yum安装最好(也最简单),但是由于公司限制,我使用的是nginx源码进行安

版本为centos7.4,认看本博客的人会基本的django开发,即知道如何使用pip安装django和venv虚拟环境并新建项目以及使用django自带的wsgi.py启动项目。

本教程使用的是root用户,并不推荐。

1、更新系统:

yum update

yum upgrade

2、安装Nginx

如果能成功通过yum安装最好(也最简单),但是由于公司限制,我使用的是Nginx源码进行安装。

2.1、安装make和g++

yum -y install gcc automake autoconflibtool make

yum install gcc gcc-c++

2.2、安装PCRF库,自行百度下载tar.gz包,放在自己的路径下

tar -zxvf pcre-8.41.tar.gz

cd pcre-8.41

./configure

@H_723_17@make

@H_723_17@make install

2.3、安装zlib库,自行百度下载tar.gz包,放在自己的路径下

tar -zxvf zlib-1.2.11.tar.gz

cd zlib-1.2.11

./configure

@H_723_17@make

@H_723_17@make install

2.4、安装ssl,自行百度下载tar.gz包,放在自己的路径下

tar –zxvf openssl-1.1.0f.tar.gz

2.5、安装Nginx,自行百度下载tar.gz包,放在自己的路径下

此处我们把Nginx安装在/usr/local/Nginx 目录下

tar -zxvf Nginx-1.12.2.tar.gz

cd Nginx-1.12.2

./configure --sbin-path=/usr/local/Nginx/Nginx\

--conf-path=/usr/local/Nginx/Nginx.conf\

--pid-path=/usr/local/Nginx/Nginx.pid\

--with-http_ssl_module \

--with-pcre=/xxxxx/pcre-8.41 \

--with-zlib=/xxxxx/zlib-1.2.11 \

--with-openssl=/xxxxx/openssl-1.1.0f

@H_723_17@make

@H_723_17@make install

注意,此处的/xxxx/即为上面安装各库时选择的路径。

安装成功后/usr/local/Nginx 目录下如下


2.6、查看80端口有没有被占用:

netstat -ano|grep 80

2.7、启动

/usr/local/Nginx/Nginx

在浏览器中访问ip,出现下图即为成功。

3、安装uwsgi

进入虚拟目录

cd /home/xxxx/venv

开启虚拟环境

source bin/activate

安装uwsgi

yum install python-devel(跳过这步直接安装uwsgi报错,从stackoverflow上找到的解决方案)

pip install uwsgi

4、django和uwsgi结合

进虚拟环境,自己新建一个project Hello,用python manage.py runserver 0.0.0.0:8000测试是否能正常访问。

此处假设目录为/root/django/env/Hello

uwsgi --http :8008 --chdir /root/django/env/Hello --wsgi-file Hello/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:800

常用选项如下所示:

  • 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@H_887_404@ 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号。

  • @R_197_6449@ : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)

注意:@H_450_448@–wsgi-file后面跟的是相对目录

此时在浏览器中输入:http://localhost:8008/,出现如下界面,表示两者结合成功:

@H_450_448@

由于参数很多,我们需要把它们写入到一个配置文件中,在app目录下新建 Hello_uwsgi.ini,输入:

# Hello_uwsgi.ini file
[uwsgi]

# Django-related setTings
socket= :8008

# the base directory (full path)
chdir  = /root/django/env/Hello

# Django s wsgi file
module    = Hello.wsgi

# process-related setTings
# master
master     = true

# maximum number of worker processes
processes = 4

# ... with appropriate permissions - may be needed
# Django s wsgi file
module    = Hello.wsgi

# process-related setTings
# master
master     = true

# maximum number of worker processes
processes = 4

# ... with appropriate permissions - may be needed
# clear environment on exit
@R_197_6449@    = true

# chmod-socket = 664
# add uwsgi log
daemonize = /xxxxx/uwsgi.log

@H_359_489@保存后,运行:

uwsgi --iniHello_uwsgi.ini

在浏览器输入ip,同样看到it works的页面,说明配置成功。

5、django与uwsgi和Nginx结合

进入虚拟环境,在项目根目录(此处是/root/django/venv/project)新建static文件夹,运行

python manage.py collectstatic命令将所有@L_675_49@收集到static文件夹内。

修改/usr/local/Nginx/Nginx.conf

server {
        listen       8092;
        server_name  localhost;
        LOCATIOn /showdb/ {
                      include uwsgi_params;
                      uwsgi_pass你的ip:8008; # 端口跟ini中的端口一致
                      uwsgi_read_timeout 2;
                      root   html;
                      index  index.html index.htm;
         }
       # 配置@L_675_49@
       LOCATIOn /static/ {
                     root /root/django/venv/project/;
                    }
          }

运行命令(先确认uwsgi和Nginx的进程已经全部kill掉)

uwsgi --ini /root/django/env/Hello/Hello_uwsgi.ini & /usr/local/Nginx/Nginx

在浏览器访问ip:8092可以看到it works页面即成功。

大佬总结

以上是大佬教程为你收集整理的centos下配置django、uwsgi和nginx(亲测成功)全部内容,希望文章能够帮你解决centos下配置django、uwsgi和nginx(亲测成功)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。