zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Centos7 + Python3.6 + Django + virtualenv + gunicorn + supervisor 环境配置详解

centos7djangopython3.6配置 详解 环境 supervisor Gunicorn
2023-09-14 09:12:26 时间

跟着网上的教程走发现行不通阿!好多都是写个大概,而且每人的环境都是有些许差异的,比如说权限问题阿,等等都会造成安装的失败

说明:本教程在你已经拥有Centos7系统,已经安装好nginx服务器,已经安装了Python3.6 Django virtualenv gunicorn supervisor的前提下进行

 

接下来开始了!

 

1。 新建你的django项目,假设项目名为Hello

        django-admin.py startproject Hello

 

2.  想好你需要的端口号,假设端口号为8001(下面的端口号均以8001来举例,你可以换成你所需要的端口号),接下来启动服务器看看能不能运行,分两种情况

     1)如果你只是想在本地运行则

           python manage.py runserver127.0.0.1:8001

     2)如果你想要外网也可以访问则

           python manage.py runserver0.0.0.0:8001

 

3. 接下来在浏览器中输入 "服务器ip:8001" ,比如我服务器的公网IP为 192.163.189.166 则输入192.163.189.166:8001,可能会出现三种情况!

     1)成功运行

     2)出现 DisallowedHost at / Invalid HTTP_HOST header: '10.211.55.6:8001'. You may need to add u'10.211.55.6' to ALLOWED_HOSTS. 类似错误,解决方法:

         进入项目目录下的Hello目录(注意项目目录名是和该名称相同的,此Hello和manage.py同级打开setting.py将ALLOWED_HOSTS = []改为ALLOWED_HOSTS = ['*']

     3) 如果在确保地址输入正确,端口也正确的前提下浏览器出现了 Unable to connect 错误,那么很可能是你的Centos7没有开启8001端口号的原因,解决方法

       开启端口号

       firewall-cmd --zone=public --add-port=8001/tcp --permanent (--permanent意思是永久生效,重启后继续生效)

       重启防火墙

       firewall-cmd --reload

 

      此时再访问浏览器,如果还是访问不了,那可能是我没遇到的情况,还请自行搜索解决哦

4.配置virtualenv gunicorn

    1)在项目根目录下输入指令 virtualenv venv    (venv可以是其他名字了)

    2)虚拟环境生成后接着要在虚拟环境中安装django 和 gunicorn 了

           pip install django

           pip install gunicorn

    3) 在项目根目录下创建gunicorn.conf 用来配置gunicorn,我的配置为

         workers = 4
         bind = '0.0.0.0:8088'

5. 配置supervisor

    supervisor的配置文件一般在/etc/supervisord.conf
    1)vim /etc/supervisord.conf

    2)在末尾加入

     [program:hello]
     command=/项目路径/venv/bin/gunicorn -c /项目路径/gunicorn.conf Hello.wsgi:application
     directory=/项目路径
     autostart=true
     autorestart=true
     stdout_logfile=/项目路径/logs/gunicorn.log
     stderr_logfile=/项目路径/logs/gunicorn.err 

    3) 重启 supervisor

      unlink /tmp/supervisor.sock
      supervisord -c /etc/supervisord.conf

 

6. 配置nignx

   1)  打开nignx.conf

    2) 在合适地方加入

             location /  {
                     proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
                     proxy_set_header Host $http_host;
                     proxy_redirect off;
                     proxy_pass http://192.163.189.166:8001;   #http://外网ip:8001,如果是本机访问则 http://127.0.0.1:8001
              }

    3) 重启nginx

        systemctl restart nginx

 

7. 好啦,接下来在浏览器中输入 http://192.163.189.166:8001 应该能访问咯

 

附录:推荐一些可能对你有帮助的文章

1. django 的教程 http://www.runoob.com/django/django-first-app.html

2. nginx+virutalenv+gunicorn环境配置 http://blog.csdn.net/zhu_free/article/details/50522868

3. centos7端口  http://blog.csdn.net/u012486840/article/details/52472704

4. virtualenv的使用    http://blog.csdn.net/werewolf_st/article/details/47358767

5. django多项目搭建  http://python.jobbole.com/81229/
---------------------
作者:seanlee97
来源:CSDN
原文:https://blog.csdn.net/m0_37687051/article/details/75267679
版权声明:本文为博主原创文章,转载请附上博文链接!