NGINX + uWSGI +Django +ubantu下发布

本文介绍如何使用uWSGI和NGINX部署Django项目,包括安装配置uWSGI及NGINX、设置静态文件映射及环境变量等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先安装uWSGI :

pip install uwsgi测试测试是否安装成功:$ uwsgi --version
2.0.15测试文件:
# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
    #return ["Hello World"] # python2
运行uwsgi:
uwsgi --http :8000 --wsgi-file test.py

如果想要运行项目来测试

# uwsgi --http :8000 --chdir 项目路径 -w 项目.wsg --static-map=/static=static

uWSGI常用命令:

uwsgi --chdir=/path/to/your/project \
    --module=mysite.wsgi:application \
    --env DJANGO_SETTINGS_MODULE=mysite.settings \
    --master --pidfile=/tmp/project-master.pid \
    --socket=127.0.0.1:49152 \      # 可以ip地址,也可以是文件 
    --processes=5 \                 # 进程数量
    --uid=1000 --gid=2000 \         # 如果是root用户,uwsgi可以有删除权限
    --harakiri=20 \                 # 一个请求超时时间
    --max-requests=5000 \           # 一个工作进程最大请求数
    --vacuum \                      # 退出时清楚环境
    --home=/path/to/virtual/env \   # virtualenv的路径
    -- static                       # 做一个映射,指定静态文件
    --http                          # 这个就和runserver一样指定IP 端口
    --daemonize=/var/log/uwsgi/yourproject.log      # 日志 

配置项目启动文件uwsgi.ini

在项目根目录创建uwsgi.ini文件[uwsgi]# 指定IP端口
http=127.0.0.1:8000
#socket=/home/www/work/project/pro/nginx_uwsgi.socket#socket套接字,与uwsgi通信
socket = 127.0.0.1:8001#项目目录
chdir=/home/EdmureBlog/
chmod-socket=664# 启用主进程
master=true进程数
processes=4线程数
threads=2# 指定项目的application
module=EdmureBlog.wsgi
#wsgi-file=uwsgi_test.py
#stats=127.0.0.1:9000# 自动移除unix Socket和pid文件当服务停止的时候
#vacuum = true启动配置$ uwsgi --ini uwsgi.ini   # 启动uwsgi配置
[uwsgi-static] added mapping for /static => /home/EdmureBlog//static    # 启动成功
$ uwsgi --stop uwsgi.pid  # 关闭uwsgi
signal_pidfile()/kill(): Operation not permitted [core/uwsgi.c line 1659]
$ uwsgi --reload uwsgi.pid  #重新加载配置

2.安装NGINX

(1)安装NGINX

$ sudo apt-get install nginx  #安装
是否安装成功
$ ps -ef|grep -i nginx
root       6961      1  0 03:56 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data   6962   6961  0 03:56 ?        00:00:00 nginx: worker process
pala       6985   2090  0 03:57 pts/0    00:00:00 grep --color=auto -i nginx

NGINX常用命令:

$ /etc/init.d/nginx start  #启动
$ /etc/init.d/nginx stop  #关闭
$ /etc/init.d/nginx restart  #重启
$  killall nginx    杀死所有nginx

# 如果是生产环境的话Nginx正在运行,就不要直接stop start 或者 restart  直接reload就行了
# 对线上影响最低
$ /etc/init.d/nginx reload

配置/etc/nginx/nginx.conf

     server {
      listen       80;          server_name  www.example.com;
         charset UTF-8;
         access_log  /var/log/nginx/EdmureBlog_access.log;#nginx访问日志          error_log   /var/log/nginx/EdmureBlog_error.log;#nginx错误日志
  proxy_request_buffering off;
  proxy_buffering off;
         client_max_body_size 75M;          location / {                      include /etc/nginx/uwsgi_params;                  #uwsgi_pass unix:/home/EdmureBlog/myweb.sock;    uwsgi_pass   127.0.0.1:8001;#                  uwsgi_read_timeout 150;    #myweb.wsgi的路径    uwsgi_param  UWSGI_SCRIPT  myweb.wsgi;    uwsgi_param  UWSGI_CHDIR   /home/EdmureBlog;               root  /home/EdmureBlog/templates;    #alias  /home/wb/myweb/templates/;    index  index.html index.htm;          } #静态文件日志
         location /static/ {               #autoindex on;               alias /home/EdmureBlog/static/;          } #上传文件日志
         location /media/ {               #autoindex on;               alias /home/EdmureBlog/media/;          }         }

检查nginx的语法$ nginx -t  # 检查nginx语法问题nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful重启nginx$ /etc/init.d/nginx restart  #重启uwsgi默认不会加载静态文件,需要手动加载      
Django项目下的setting.py设置
DEBUG = False #生产下的配置,调试关了STATIC_ROOT = os.path.join(BASE_DIR, 'statics')#将静态文件都收集到此指定目录执行collectstatic命令:
python manage.py collectstatic

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值