最简单的部署方式:

    nohup python manage.py rserver 0.0.0.0:3000 &

复杂一点的
1.安装gunicorn

    1.进入你的虚拟环境:workon py3
    2.执行命令:pip install gunicorn

2.修改nginx配置文件

    1.回到主目录: cd ~
    2.编辑nginx配置文件:
  • 1.sudo subl /etc/nginx/nginx.conf
  • 2.添加配置文件内容:在http 大括号内添加以下代码,添加时注意缩进
  •         server {
                listen 80;
                server_name localhost;
                location / {
                    proxy_pass http://0.0.0.0:8000;
                    proxy_set_header Host $host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                }
                error_page 500 502 503 504 /50x.html;
                location = /50x.html {
                    root html;
                }
                location /media {
                    alias /root/bookstore/bookstore/static;#你的静态文件的目录
                }
                location /static {
                    alias /root/bookstore/bookstore/static; #你的静态文件的目录
                }
            }
    
    
  • 3.将以下代码注释掉
  •     # include /etc/nginx/sites-enabled/*;
    
  • 修改第一行代码后面的用户名
  • user xxxxxx;#你的用户名
    
  • 4.重启nginx:sudo nginx -s reload
  • 5.启动项目
    1. 1.前台启动:gunicorn -w 3 -b 0.0.0.0:8000 bookstore.wsgi:application
      2.后台启动:nohup gunicorn -w 3 -b 127.0.0.1:8080 bookstore.wsgi:application &

      然后回车,退出命令行ctrl+c,注意IP地址和端口号

    3.访问页面

      1.查看本机IP地址:ifconfig
      2.通过IP地址访问页面
    打赏

    1 对 “Django项目利用nginx+gunicorn实现简单部署”的想法;

    发表评论

    邮箱地址不会被公开。 必填项已用*标注