nginx + gunicorn + django 2.0 踩坑


部署踩坑

部署踩坑提前准备服务器端准备安装nginx使用uwsgi部署使用gunicorn配置配置nginx配置django中的路径url

提前准备

在本地能够 python(3) manage.py runserver

服务器端准备

安装nginx

为了防止python的某些包没有安装

请先 sudo apt-get install python-dev

然后 sudo apt-get install nginx

使用uwsgi部署

求求你了 别用uwsgi 玩了4个小时 试了不下15种配置方法 无功而返

现在还没有solution 跳过

使用gunicorn配置

相比于uwsgi, guncorn不能再好

先进入虚拟环境source /path/to/env/bin/active

再安装gunicornsudo pip(3) install gunicorn

/path/to/env/bin/gunicorn--chdir/path/to/project--pythonpath/path/to/env/-w4-b0.0.0.0:8017project.wsgi:application

这边用的8017端口

配置nginx

新建一个配置文件sudo vim/etc/nginx/sites-available/your_conf.conf

your_conf.conf中写下如下内容:

server {
listen 80;
server_name your_domain_name.com;
charset utf-8;

client_max_body_size 75M;

location /media {
alias /path/to/project/media;
}

location /static {
alias /path/to/project/static;
}

location / {
uwsgi_pass unix:///home/tu/zqxt/zqxt.sock;
include /etc/nginx/uwsgi_params;
}
}

listen默认一般都是80端口, 如果没有media文件就不配置,这个static文件夹是运行python(3) manage.py collectstatic后生成的文件夹,因为没有用到uwsgi,所以uwsgi_pass看心情随缘,include按照这个来.

然后复制到/sites-enabled/中: sudo ln -s/etc/nginx/sites-available/your_conf.conf/etc/nginx/sites-enabled/your_conf.conf

ps: nginx配置一般都在/etc/nginx/中,/sites-available/里面保存你可能要用到的configure文件

/sites-enabled/保存目前生效的configure文件

配置django中的路径url

如果不配置这个会找不到static文件夹

from django.conf.urls.static import static
from your_project import settings

urlpatterns = [
# your path here
path('admin/', admin.site.urls, name='admin'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

这是Django 2.0版本之后才有的,详情请见官方文档 https://docs.djangoproject.com/en/2.0/ref/urls/

然后 service nginx restart重启nginx,就可以看到了网页了.

(注意只能在gunicorn中配置的端口访问 eg: your_domain_name.com:8017)

优质内容筛选与推荐>>
1、BitmapDrawable
2、在Mac上关于tomcat服务器的安装、配置、启动、部署web详细流程
3、strcmp函数和strcpy函数
4、iterator和ListIterator的区别和用法
5、第二次Sprint


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号