django+uwsgi+nginx部署项目

本文介绍如何在Ubuntu 16.04环境下使用Nginx和uWSGI部署Python Web应用,包括软件下载、安装配置、运行步骤等关键环节。

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

准备工具

nginx下载: wget http://nginx.org/download/nginx-1.16.0.tar.gz
pcre下载: wget https://netix.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
uwsgi下载: pip install uwsgi
系统环境: ubuntu 16.04

解压安装

pcre安装

# 解压pcre安装包
tar -zxvf pcre-8.40.tar.gz
#安装
cd pcre-8.40/
./configure
make
make install

nginx安装

解压
tar -zxvf nginx-1.16.0.tar.gz
安装
cd nginx-1.16.0/
./configure
make
make install

一般默认安装路径在/usr/local/下,找到nginx目录cd nginx/conf/

修改nginx配置文件

vim nginx.conf

nginx.conf修改后内容如下

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
   		# 修改监听端口
        listen       8081;
        # 修改IP
        server_name  192.168.0.100;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		# 这里是默认nginx根目录配置,把它注释掉
        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}
        # uwsgi配置,这里我使用了9001端口
        location / {
            uwsgi_pass 127.0.0.1:9001;
            include uwsgi_params;
        }
        # uwsgi不会加载静态文件,这里把静态文件交给nginx加载
        location /static {
            expires 30d;
            autoindex on;
            add_header Cache-Control private;
            # 静态文件路径
            alias /home/anwei/niu/python_project/Key_Managerment/static/;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

配置uwsgi.ini

项目根目录路径

Key_Managerment/
├── audit_management
├── key_management
├── Key_Managerment
├── manage.py
├── static
├── system_management
├── templates
├── utils
├── uwsgi.ini
├── uwsgi.log
└── uwsgi.pid
# 配置uwsgi
vim uwsgi.ini
[uwsgi]
# uwsgi监听端口
socket=127.0.0.1:9001
# 项目绝对路径
chdir=/home/anwei/niu/python_project/Key_Managerment
# wsgi相对路径
wsgi-file=Key_Managerment/wsgi.py
master=True
# 最大进程程数
processes=4
# 线程
threads=2
# log日志存放
daemonize=uwsgi.log
主进程id
pidfile=/home/anwei/niu/python_project/Key_Managerment/uwsgi.pid

修改项目配置

vim setting.py
# DEBUG = True
DEBUG = False
# ALLOWED_HOST = []
ALLOWED_HOST = ["*"]
# 添加STATiC_ROOT
STATIC_ROOT = os.path.join(BASE_DIR. "static")

进入到项目根目录下,运行python manage.py collectstatic

# 注释STATICFILES_DIRS
STATICFILES_DIRS = [os.path.join(BASR_DIR, "static")]

运行项目

运行uwsgi

uwsgi --ini uwsgi.ini

运行nginx

cd /usr/local/nginx/sbin/
sudo ./nginx

完成之后就可以上线访问了

后记

目前只是最基本的项目部署,将来可能还会需要负载均衡,增加ssl等,有时间会再次更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值