读django文档——nginx + uwsgi 部署django项目

目录

一、配置uwsgi

二、配置nginx


一、配置uwsgi

本例是在anaconda虚拟环境webenv下面建立的django项目,名字叫webdev。

激活虚拟环境,在该环境里面安装uwsgi。

conda activate webenv
conda install uwsgi

 在django项目webdev目录里创建 uwsgi.ini文件,编辑内容如下。如果遇到目录不存在,比如/var/log/uwsgi,那么自行创建一下。

特别注意一下,如果uwsgi配置文件中配置了 daemonize=/path/to/uwsgi.log (uwsgi服务以守护进程运行),会导致后面sytemctl启动时多次重启而导致启动失败,需改为 logto=/path/to/uwsgi.log

[uwsgi]
# /path/to/your/project (full path)
chdir = /data/webdev

# Django's wsgi file (locate in project.wsgi.py)
module = webdev.wsgi:application

# the virtualenv (full path)
home = /anaconda3/envs/webenv

# set an environment variable
env = DJANGO_SETTINGS_MODULE=webdev.settings

# process-related settings
# the socket (IP:port or  /path/to/your/project/mysite.sock)
socket = 127.0.0.1:8001
# master
master=True
# max number of worker processes
processes = 10
# vacuum means clear environment on exit
vacuum=True
# respawn processes taking more than 20 seconds
harakiri = 20
# respawn processes after serving 5000 requests
max-requests = 5000
# background the process & log
#daemonize = /var/log/uwsgi/webdev.log
logto = /var/log/uwsgi/webdev.log
# /path/to/pid.file (full path)
pidfile = /run/webdev.pid

然后启动。这个是手动的,只是验证可以使用。

uwsgi --ini uwsgi.ini

配置uwsgi开机启动,添加一个systemd服务 /etc/systemd/system/uwsgi-webdev.service 内容。

[Unit]
Description=UWSGI For WEBDEV 
After=syslog.target
 
[Service]
KillSignal=SIGQUIT
ExecStart=/anaconda3/envs/webenv/bin/uwsgi --ini /data/webdev/uwsgi.ini
Restart=always
Type=notify
NotifyAccess=all
StandardError=syslog
 
[Install]
WantedBy=multi-user.target

然后设置开机启动

systemctl daemon-reload 
systemctl start uwsgi-webdev.service 

二、配置nginx

编辑nginx配置文件 /etc/nginx/conf.d/webdev.conf,内容如下。注意里面的 static 、 media 等配置,涉及到django的 STATIC_ROOT目录。

# the upstream component nginx needs to connect to
upstream webdev {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name 192.168.27.7 # substitute your machine's IP address or FQDN
    charset     utf-8;
    access_log  /var/log/nginx/access.log;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /data/webdev/media;  # your Django project's media files - amend as required
    }

    # Django static
    location /static {
        alias /data/webdev/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        include     uwsgi_params; # the uwsgi_params file you installed
        uwsgi_pass  webdev;
        uwsgi_connect_timeout 20;
    }
}

然后重启nginx,访问 IP:8000 验证结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Linux系统上部署Django + nginx + uWSGI的步骤如下: 1. 安装必要的软件 在Linux系统上安装必要的软件包,包括Python、pip、nginxuWSGI等。 2. 创建Django项目 使用Django创建一个新项目或使用现有的Django项目。 3. 配置uWSGIDjango项目的根目录下创建一个uwsgi.ini文件,用于配置uWSGI。示例配置如下: ``` [uwsgi] # 指定运行模式为WSGI http = :8000 # 指定Django应用的wsgi模块 wsgi-file = myproject.wsgi # 指定进程数 processes = 4 # 指定线程数 threads = 2 # 指定静态文件路径 static-map = /static=/path/to/static # 指定日志路径 logto = /path/to/logfile ``` 其中,http参数指定了监听的端口号,wsgi-file参数指定了Django应用的wsgi模块,processes参数指定了进程数,threads参数指定了线程数,static-map参数指定了静态文件的路径,logto参数指定了日志文件的路径。 4. 配置nginxnginx的配置文件中添加以下内容: ``` server { listen 80; server_name example.com; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { uwsgi_pass 127.0.0.1:8000; include uwsgi_params; } location /static { alias /path/to/static; } } ``` 其中,server_name指定了域名,access_log和error_log指定了日志文件的路径,uwsgi_pass指定了uWSGI的地址和端口号,include指定了uWSGI的参数。 5. 启动uWSGI服务 使用以下命令启动uWSGI服务: ``` uwsgi --ini uwsgi.ini ``` 6. 启动nginx服务 使用以下命令启动nginx服务: ``` sudo service nginx start ``` 这样就完成了Django + nginx + uWSGI部署。可以通过访问该网站的域名来验证是否部署成功。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苦行僧(csdn)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值