gunicorn模块(django与nginx整合)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gunicorn模块(django与nginx整合)相关的知识,希望对你有一定的参考价值。

gunicorn模块


1、安装gunicorn

[[email protected] ~]# yum search nginx
[[email protected] ~]# yum install -y nginx.x86_64
[[email protected] ~]# /etc/init.d/nginx restart
nginx: [warn] conflicting server name "112.65.140.132" on 0.0.0.0:80, ignored
停止 nginx:                                               [失败]
正在启动 nginx:nginx: [warn] conflicting server name "112.65.140.132" on 0.0.0.0:80, ignored
                                                          [确定]

2、测试nginx

技术分享


3、安装gunicorn

[[email protected] ~]# pip install gunicorn


4、修改nginx的配置文件

[[email protected] simplecmdb]# vim /etc/nginx/conf.d/virtual.conf
# A virtual host using mix of IP-, name-, and port-based configuration
server {
   listen       112.65.140.133:8000;
   server_name  www.daixuan.com;

   location /static/admin {
       root   /opt/amos/lib/python2.7/lib/python2.7/site-packages/django/contrib/admin;
       index  index.html index.htm;
   }

   location / {
       proxy_pass http://localhost:8000;
   }
}

[[email protected] simplecmdb]# service nginx restart
nginx: [warn] conflicting server name "112.65.140.132" on 0.0.0.0:80, ignored
停止 nginx:                                               [确定]
正在启动 nginx:nginx: [warn] conflicting server name "112.65.140.132" on 0.0.0.0:80, ignored
                                                          [确定]

访问:http://112.65.140.133:8000/   返回502 


5、启用gunicorn

[[email protected] simplecmdb]# cd /opt/python/django/simplecmdb-gunicorn/
[[email protected] simplecmdb-gunicorn]# ls
112.65.140.132  exit  hostinfo  manage.py  nohup.out  simplecmdb  sysinformation-pickle.py

[[email protected] simplecmdb-gunicorn]# gunicorn simplecmdb.wsgi:application -D
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
ImportError: No module named site
根据提示:安装site模块
[[email protected] simplecmdb-gunicorn]# pip install site
Collecting site
/opt/amos/python2.7/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/opt/amos/python2.7/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading site-0.0.1.tar.gz
Installing collected packages: site
  Running setup.py install for site ... done
Successfully installed site-0.0.1
[[email protected] simplecmdb-gunicorn]# vim /etc/profile
export PYTHONHOME=/opt/amos/python2.7
[[email protected] simplecmdb-gunicorn]# source /etc/profile
[[email protected] simplecmdb-gunicorn]# echo $PYTHONHOME
/opt/amos/python2.7
[[email protected] simplecmdb-gunicorn]# pwd
/opt/python/django/simplecmdb-gunicorn
[[email protected] simplecmdb-gunicorn]# gunicorn simplecmdb.wsgi:application -D
[[email protected] simplecmdb-gunicorn]# netstat -anput | grep :80
tcp        0      0 127.0.0.1:8000              0.0.0.0:*                   LISTEN      8106/python2.7
tcp        0      0 112.65.140.133:8000         0.0.0.0:*                   LISTEN      7817/nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      7817/nginx
tcp        0    531 112.65.140.133:63251        182.50.136.239:80           FIN_WAIT1   -
tcp        0      0 :::80                       :::*                        LISTEN      7817/nginx
可以看到-D 指定的127.0.0.1上开启的端口


5、访问测试

技术分享 

技术分享


技术分享技术分享


这里css等文件被403了,所以肯定是用户权限问题。user从nginx修改为root,然后重启nginx即可。

[[email protected] simplecmdb-gunicorn]# vim /etc/nginx/nginx.conf
user root;



本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1912250

以上是关于gunicorn模块(django与nginx整合)的主要内容,如果未能解决你的问题,请参考以下文章

部署了解nginx+gunicorn+django架构

部署了解nginx+gunicorn+django架构

Gunicorn 和 Django 与 Upstart 和 Nginx

Django应用程序中的空REMOTE_ADDR值,当使用nginx作为反向代理与gunicorn

使用 Traefik 时如何使用 Nginx 和 Django Gunicorn 提供静态内容

如何运行与 gunicorn 绑定的 django 应用程序?