记录一次uwsgi+nginx部署django项目的配置及遇到的坑

具体流程之前文章有记录这里就简单描述一下,不同的是这次采用ini方式配置。

1.首先在服务器创建我们存放项目及虚拟环境的目录将项目上传到服务器,并创建虚拟环境

2.进入虚拟环境安装项目依赖包(就是在虚拟环境下pip install),完成后使用python manage.py runserver 命令测试一下是否启动成功

3.安装uwsgi和nginx

4.在项目同级目录下创建script目录存放uwsgi配置文件

# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/home/pyweb/project/MyPro
# 指定项目的application
module=MyPro.wsgi:application
# 指定sock的文件路径
socket=/home/pyweb/project/script/uwsgi.sock
# 进程个数
workers=5
pidfile=/home/pyweb/project/script/uwsgi.pid
# 指定IP端口(单独使用uwsgi启动需要,使用nginx不需要)
#http=0.0.0.0:8003
# 指定静态文件(同上)
#static-map=/static=/home/pyweb/project/MyPro/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/home/pyweb/project/script/uwsgi.log

5.nginx配置

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  服务器域名或者ip也可;
        charset UTF-8;
        client_max_body_size 75M;

        location / {
            include uwsgi_params;
            uwsgi_pass unix:///home/pyweb/project/script/uwsgi.sock;
            uwsgi_read_timeout 15;
        }

        location /static {
            root /home/pyweb/project/MyPro/;
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

就先不做负载均衡配置了。

6.测试nginx配置文件不同版本linux好像有不一样的启动方式,根据实际情况定吧。没有提示信息或者提示成功就ok了。

7.uwsgi启动项目

切换到上面创建的script目录下   uwsgi --ini uwsgi.ini

8.重启nginx

nginx -s reload    (有的需要切换到/usr/loacl/nginx/sbin目录下执行  ./nginx -s reload  有的也不是,反正这个根据实际情况找吧。)

若提示未启动直接启动就好了。

注:若端口被占用可以使用  lsof -i :port  来查看是谁占用了,然后kill -9 pid  弄死它就完事了。

注意:下面就是大坑了

一切配置完成以后,应该可以访问的啊!但是由于我用的是华为云服务器,它外层安全策略竟然没有开启80端口,让我好搞了半天,防火墙关了启,启了关,设置策略,开放端口,好一顿搞。最终还是要登录华为云放开80端口,好像只有华为云服务器没有默认开放80端口。

 欢迎关注wx公众号:python web小栈,共同探讨学习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值