Django+Nginx+uWSGI部署

环境

  1. ubuntu 18.04
  2. python3.6.7
  3. Django 2.1
  4. uWSGI 2.0.17.1
  5. Nginx 1.14.2

准备

使用 virtualenvwrapper  创建一个环境

$ mkvirtualenv django2

安装和使用 virtualenvwrapper  参考 https://blog.csdn.net/weixin_38417098/article/details/86004118

安装 Django

1. 切换环境

adamstream@adamstream-lenovo-g505:~$ workon django2

2. 安装

(django2)adamstream@adamstream-lenovo-g505:~$ pip install  django==2.1

3. 测试

查看版本

(django2) adamstream@adamstream-lenovo-g505:~$ python -m django --version

新建简单项目

(django2) adamstream@adamstream-lenovo-g505:~$ django-admin startproject mysite
(django2) adamstream@adamstream-lenovo-g505:~$ cd mysite
(django2) adamstream@adamstream-lenovo-g505:~$ python manage.py runserver 0.0.0.0:8000
  1. 将已经完成开发的Django项目 mysite(mysite是Django项目名)拷贝到服务器,

  2. 这里拷贝到了 adamstream 用户( adamstream 是服务器可登录用户名)路径下,

  3. 最后相对路径是~/www/mysite,绝对路径是/home/adamstream/www/mysite

  4. 进入以上目录,使用Django的内置服务器测试看看 mysite 项目是否运行正常。

    adamstream@adamstream-lenovo-g505:~$ python manage.py runserver 127.0.0.1:8080
    

在浏览器内输入:http://127.0.0.1:8000,检查django是否运行正常。
在这里插入图片描述

安装 uWSGI

1. 下载安装

adamstream@adamstream-lenovo-g505:~$ pip3 install uwsgi

2. 测试

查看版本

adamstream@adamstream-lenovo-g505:~$ uwsgi --version

~/www/mysite目录中创建一个测试用的Python文件foobar.py

adamstream@adamstream-lenovo-g505:~$ touch foobar.py

foobar.py

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

在 HTTP 端口 9090 部署

adamstream@adamstream-lenovo-g505:~$ uwsgi --http :9090 --wsgi-file foobar.py 
# 参数说明
--http:        指定IP 端口
--wsgi-file:   指向具体Python文件

在浏览器内输入:http://127.0.0.1:9090,查看是否有"Hello World"输出

在这里插入图片描述

安装 Nginx

1. 安装依赖库

1. 安装 gcc 依赖库

Nginx是C语言开发,需要gcc依赖库

先检查本机是否有gcc环境

adamstream@adamstream-lenovo-g505:~$ gcc -v

如果没有gcc环境,则需要安装

adamstream@adamstream-lenovo-g505:~$ sudo apt install gcc

2. 安装 pcre 依赖库

  1. PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。

  2. nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库。

  3. pcre-devel 是使用 pcre 开发的一个二次开发库。

adamstream@adamstream-lenovo-g505:~$ sudo apt install libpcre3 libpcre3-dev

查看pcre版本

adamstream@adamstream-lenovo-g505:~$ pcre-config --version

3.安装 zlib 依赖库

  1. zlib 库提供了很多种压缩和解压缩的方式。
  2. nginx 使用 zlib 对 http 包的内容进行 gzip 。
adamstream@adamstream-lenovo-g505:~$ sudo apt install zlib1g-dev

4.安装 ssl 依赖库

  1. OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。

  2. nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要安装 OpenSSL 库。

adamstream@adamstream-lenovo-g505:~$ sudo apt install openssl

2. 安装Nginx

1. 下载Nginx最新版本

http://nginx.org/en/download.html

在这里插入图片描述

图中左边的Linux版本的tar.gz包,右边为Windows版本

2.下载到本地后解压

adamstream@adamstream-lenovo-g505:~/下载$ tar -zxvf nginx-1.14.2.tar.gz

3. 进入解压目录

adamstream@adamstream-lenovo-g505:~/下载$ cd nginx-1.14.2

4. 配置

不需要去配置相关东西,默认就可以了,默认会安装在 /usr/local/nginx

adamstream@adamstream-lenovo-g505:~/下载/nginx-1.14.2$ ./configure

5. 编译

adamstream@adamstream-lenovo-g505:~/下载/nginx-1.14.2$ make

6. 安装

adamstream@adamstream-lenovo-g505:~/下载/nginx-1.14.2$ make install

7. 启动服务

查找安装路径

adamstream@adamstream-lenovo-g505:~$ whereis nginx

启动服务

adamstream@adamstream-lenovo-g505:~$ cd /usr/local/nginx/sbin
adamstream@adamstream-lenovo-g505:~/usr/local/nginx/sbin$ sudo ./nginx

查看Nginx进程

adamstream@adamstream-lenovo-g505:~/usr/local/nginx/sbin$ ps -ef|grep nginx

在浏览器内输入:http://localhost:8080,检查 Nginx 是否运行正常

在这里插入图片描述

*8. 将nginx添加到环境变量中(还是不要配置好)

adamstream@adamstream-lenovo-g505:~$ sudo vim /etc/profile

profile最后添加语句

export PATH="$PATH:/usr/local/nginx/sbin"

读取配置

adamstream@adamstream-lenovo-g505:~$ source /etc/profile

测试启动

adamstream@adamstream-lenovo-g505:~$ nginx  
2019/01/26 23:50:01 [warn] 13601#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/nginx/conf/nginx.conf:2
2019/01/26 23:50:01 [emerg] 13601#0: open() "/usr/local/nginx/logs/nginx_error.log" failed (13: Permission denied)

无法启动,原因是nginx中大部分的文件只有root用户才能写入,所以配置是无法起效的

9. Nginx常见命令

adamstream@adamstream-lenovo-g505:~$ nginx            # 启动 Nginx
adamstream@adamstream-lenovo-g505:~$ nginx -s stop    # 停止 Nginx,待nginx进程处理任务完毕进行停止
adamstream@adamstream-lenovo-g505:~$ nginx -s quit    # 停止 Nginx,先查出nginx进程id再使用kill命令强制杀掉进程
adamstream@adamstream-lenovo-g505:~$ nginx -s reload  # 重新载入配置文件
adamstream@adamstream-lenovo-g505:~$ nginx -s reopen  # 重启 Nginx
# -s 都是采用向 Nginx 发送信号的方式

10. 配置

创建 Nginx 运行的用户

adamstream@adamstream-lenovo-g505:~$ /usr/sbin/groupadd www
adamstream@adamstream-lenovo-g505:~$ /usr/sbin/useradd -g www www

配置 nginx.conf (/usr/local/nginx/conf/nginx.conf)


user  www www;
# 设置值和CPU核心数量一致
worker_processes  4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
# 日志位置和日志级别
error_log  /usr/local/nginx/logs/nginx_error.log crit;

#pid        logs/nginx.pid;

# 指定此进程可以打开的最大文件描述符的值
worker_rlimit_nofile  65535;


events {
    # worker_connections  1024;
    use  epoll;
    worker_connections  65535;
}


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;
    
    client_header_buffer_size      32k;
    client_max_body_size           8m;
    large_client_header_buffers    4 32k;
    server_names_hash_bucket_size  128;

    sendfile  on;
    
    tcp_nopush   on;
    tcp_nodelay  on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    fastcgi_buffers            4 64k;
    fastcgi_buffer_size        64k;
    fastcgi_busy_buffers_size  128k;
    
    fastcgi_connect_timeout  300;
    fastcgi_read_timeout     300;
    fastcgi_send_timeout     300;
    

    gzip               on;
    gzip_buffers       4 16k;
    gzip_comp_level    2;
    gzip_http_version  1.0;
    gzip_min_length    1k;
    gzip_types         text/plain application/x-javascript text/css application/xml;
    gzip_vary          on;

    # server 虚拟主机的配置
    server {
        listen       80;         # 监听端口
        server_name  localhost;  # 域名
        
        index  index.html index.htm index.php;
        root   /usr/local/nginx/html;  # 站点目录

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        access_log off;

        location / {
            root   html;
            index  index.html index.htm;
            
            include  uwsgi_params;
            uwsgi_pass  192.168.31.93:8080;             //必须和uwsgi中的设置一致
            uwsgi_param UWSGI_SCRIPT mysite.wsgi; //入口文件,即wsgi.py相对于项目根目录的位置,“.”相当于一层目录
            uwsgi_param UWSGI_CHDIR ~/mysite;      //项目根目录
            index  index.html index.htm;
            client_max_body_size 35m;
        }

        #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;
        }
        
        location ~ .*\.(php|php5)?$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
         }
         
         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ {
             expires 30d;
         }
         
         location ~ .*\.(js|css)?$ {
             expires 15d;
         }

        # 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 + Django 的实现

1. uWSGI 命令启动 Django

1. 进入项目

adamstream@adamstream-lenovo-g505:~$ cd wwww/mysite
# db.sqlite3
# manage.py
# mysite
# polls
# static

2. 启动项目

  1. 这里依然使用HTTP协议,
  2. 指向Django项目的--file--module参数,
  3. 参数的值mysite.wsgi指向~/www/mysite/mysite/wsgi.py模块,
  4. 如果正常可以在浏览器http://192.168.31.93:8080端口打开了项目
adamstream@adamstream-lenovo-g505:~/wwww/mysite$ uwsgi --http 192.168.31.93:8080 --file mysite/wsgi.py --static-map=/static=static

adamstream@adamstream-lenovo-g505:~/wwww/mysite$ uwsgi --http 192.168.31.93:8080 --module mysite.wsgi --static-map=/static=static

在这里插入图片描述

# 参数说明
--http:    指定IP 端口

# mysite/wsgi.py文件里有一个反射,如果你在调用他的时候没有指定,Web Server就使用默认的
--file:    文件形式调用
--module:  模块形式调用

--static:  做一个映射,指定静态文件

2. uWSGI 配置文件启动 Django

  1. 对于uWSGI服务器的配置,uwsgi命令加上很多--http,--file,--static等参数非常麻烦
  2. 可以写成配置文件的方式

1. 创建配置文件

~/www/script(django项目同级目录创建script目录)中创建一个配置文件uwsgi.ini,用于存放配置脚本

adamstream@adamstream-lenovo-g505:~$ cd ~/www
adamstream@adamstream-lenovo-g505:~/www$ mkdir script
adamstream@adamstream-lenovo-g505:~/www/script$ cd script
adamstream@adamstream-lenovo-g505:~/www/script$ touch uwsgi.ini

uwsgi.ini

# uwsig使用配置文件启动
[uwsgi]
# 指定虚拟环境
virtualenv=/home/adamstream/.virtualenvs/django2
# 项目目录
chdir=/home/adamstream/www/mysite
# 指定项目的application
module=mysite.wsgi:application
# 指定sock的文件路径,sock是套接字文件
socket=/home/adamstream/www/script/uwsgi.sock
# 进程个数
workers=4
pidfile=/home/adamstream/www/script/uwsgi.pid
# 指定IP端口
http=192.168.31.93:8080
# 指定静态文件
static-map=/static=/home/adamstream/www/mysite/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/adamstream/www/script/uwsgi.log

2. 启动项目

adamstream@adamstream-lenovo-g505:~$ uwsgi --ini ~/www/script/uwsgi.ini

在这里插入图片描述

Django + Uwsgi + Nginx

1. 更改wsgi.ini

~/www/script/wsgi.ini

# uwsig使用配置文件启动
[uwsgi]
# 指定虚拟环境
#virtualenv=/home/adamstream/.virtualenvs/django2
# 项目目录
chdir=/home/adamstream/www/mysite
# 指定项目的application
module=mysite.wsgi:application
# 指定sock的文件路径
#socket=192.168.31.93:8080
socket=/home/adamstream/www/script/uwsgi.sock

###########################################################
# http参数用于以上测试,而与Nginx交互需要使用socket参数,
# 即使用TCP协议,WSGI和uwsgi协议都在TCP协议之上。
# socket参数也可以配置为网络地址,如socket=192.168.31.93:8080,
# 但如果Nginx和uWSGI同在一个服务器上,可以使用socket文件的形式。
###########################################################

# 动态配置socket文件的权限,因为socket文件会在每次uWSGI启动时被重新创建
chmod-socket=664
stats=192.168.31.93:9090
# 进程个数
workers=4
pidfile=/home/adamstream/www/script/uwsgi.pid
# 指定IP端口
#http=192.168.31.93:8080
# 指定静态文件
static-map=/static=/home/adamstream/www/mysite/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/adamstream/www/script/uwsgi.log

注意:/home/adamstream/www/script/uwsgi.sock文件新创建的用户www,无法写入,需要改变文件权限

# uwsgi.sock文件属性
# srw-rw-r-- 1 adamstream adamstream     0 1月  25 17:05 uwsgi.sock=
adamstream@adamstream-lenovo-g505:~/www/scrip$ sudo chmod o+w uwsgi.sock

但这个操作只是在当前终端上起效,关闭此终端或重新登陆后,就要再执行此次操作。

2. 更改nginx.conf

usr/local/nginx/conf/nginx.conf

...
http {
    ...

    # server 虚拟主机的配置
    server {
        listen       80;       # 监听端口
        server_name  "192.168.31.93";  # 域名
        
        index  index.html index.htm index.php;
        #root   /usr/local/nginx/html;  # 站点目录

        charset utf-8;

        gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; # 支持压缩的类型

        #access_log  logs/host.access.log  main;
        access_log off;

        # 指定项目路径 uWSGI
        # location 相当于 Django 的 url(r'^admin/', admin.site.urls)
        location / {
            include  uwsgi_params;  # 导入一个 Nginx 模块,用来和 uWSGI 进行通讯

            uwsgi_connect_timeout  30;  # 设置连接 uWSGI 的超时时间
            uwsgi_pass  unix:///home/adamstream/www/script/uwsgi.sock;  # 指定 uWSGI 的 sock 文件,所有动态请求就会直接丢给他
            #uwsgi_pass  192.168.31.93:8080;
            #uwsgi_param UWSGI_SCRIPT mysite.wsgi;
            #uwsgi_param UWSGI_CHDIR /home/adamstream/www/mysite;
            #index  index.html index.htm;
            #client_max_body_size 35m;
        }
        ...
    }
}

3. 开始测试

1. 首先启动uWSGI

adamstream@adamstream-lenovo-g505:~$ uwsgi --ini ~/www/script/uwsgi.ini
[uWSGI] getting INI configuration from /home/adamstream/www/script/uwsgi.ini
[uwsgi-static] added mapping for /static => /home/adamstream/www/mysite/static

2. 然后启动Nginx

adamstream@adamstream-lenovo-g505:~$ sudo /usr/local/nginx/sbin/nginx

3. 访问网站

在浏览器输入http://192.168.31:93

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值