天天看点

Django+Nginx+uWSGI部署环境准备安装 Django安装 uWSGI安装 Nginx基于 uWSGI + Django 的实现Django + Uwsgi + Nginx

环境

  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. 切换环境

[email protected]:~$ workon django2
           

2. 安装

(django2)[email protected]:~$ pip install  django==2.1
           

3. 测试

查看版本

(django2) [email protected]:~$ python -m django --version
           

新建简单项目

(django2) [email protected]:~$ django-admin startproject mysite
(django2) [email protected]:~$ cd mysite
(django2) [email protected]:~$ 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 项目是否运行正常。
    [email protected]:~$ python manage.py runserver 127.0.0.1:8080
               

在浏览器内输入:http://127.0.0.1:8000,检查django是否运行正常。

Django+Nginx+uWSGI部署环境准备安装 Django安装 uWSGI安装 Nginx基于 uWSGI + Django 的实现Django + Uwsgi + Nginx

安装 uWSGI

1. 下载安装

[email protected]:~$ pip3 install uwsgi
           

2. 测试

查看版本

[email protected]:~$ uwsgi --version
           

~/www/mysite

目录中创建一个测试用的Python文件

foobar.py

[email protected]:~$ touch foobar.py
           

foobar.py

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

在 HTTP 端口 9090 部署

[email protected]:~$ uwsgi --http :9090 --wsgi-file foobar.py 
           
# 参数说明
--http:        指定IP 端口
--wsgi-file:   指向具体Python文件
           

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

Django+Nginx+uWSGI部署环境准备安装 Django安装 uWSGI安装 Nginx基于 uWSGI + Django 的实现Django + Uwsgi + Nginx

安装 Nginx

1. 安装依赖库

1. 安装 gcc 依赖库

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

先检查本机是否有gcc环境

[email protected]:~$ gcc -v
           

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

[email protected]:~$ sudo apt install gcc
           

2. 安装 pcre 依赖库

  1. PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。
  2. nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库。
  3. pcre-devel 是使用 pcre 开发的一个二次开发库。
[email protected]:~$ sudo apt install libpcre3 libpcre3-dev
           

查看pcre版本

[email protected]:~$ pcre-config --version
           

3.安装 zlib 依赖库

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

4.安装 ssl 依赖库

  1. OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
  2. nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要安装 OpenSSL 库。
[email protected]:~$ sudo apt install openssl
           

2. 安装Nginx

1. 下载Nginx最新版本

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

Django+Nginx+uWSGI部署环境准备安装 Django安装 uWSGI安装 Nginx基于 uWSGI + Django 的实现Django + Uwsgi + Nginx

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

2.下载到本地后解压

[email protected]:~/下载$ tar -zxvf nginx-1.14.2.tar.gz
           

3. 进入解压目录

[email protected]:~/下载$ cd nginx-1.14.2
           

4. 配置

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

[email protected]:~/下载/nginx-1.14.2$ ./configure
           

5. 编译

6. 安装

7. 启动服务

查找安装路径

[email protected]:~$ whereis nginx
           

启动服务

[email protected]:~$ cd /usr/local/nginx/sbin
[email protected]:~/usr/local/nginx/sbin$ sudo ./nginx
           

查看Nginx进程

[email protected]:~/usr/local/nginx/sbin$ ps -ef|grep nginx
           

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

Django+Nginx+uWSGI部署环境准备安装 Django安装 uWSGI安装 Nginx基于 uWSGI + Django 的实现Django + Uwsgi + Nginx

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

[email protected]:~$ sudo vim /etc/profile
           

profile

最后添加语句

读取配置

[email protected]:~$ source /etc/profile
           

测试启动

[email protected]:~$ 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常见命令

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

10. 配置

创建 Nginx 运行的用户

[email protected]:~$ /usr/sbin/groupadd www
[email protected]:~$ /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. 进入项目

[email protected]:~$ 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端口打开了项目
[email protected]:~/wwww/mysite$ uwsgi --http 192.168.31.93:8080 --file mysite/wsgi.py --static-map=/static=static
           

[email protected]:~/wwww/mysite$ uwsgi --http 192.168.31.93:8080 --module mysite.wsgi --static-map=/static=static
           
Django+Nginx+uWSGI部署环境准备安装 Django安装 uWSGI安装 Nginx基于 uWSGI + Django 的实现Django + Uwsgi + Nginx
# 参数说明
--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

,用于存放配置脚本

[email protected]:~$ cd ~/www
[email protected]:~/www$ mkdir script
[email protected]:~/www/script$ cd script
adam[email protected]:~/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. 启动项目

[email protected]:~$ uwsgi --ini ~/www/script/uwsgi.ini
           
Django+Nginx+uWSGI部署环境准备安装 Django安装 uWSGI安装 Nginx基于 uWSGI + Django 的实现Django + Uwsgi + Nginx

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=
[email protected]:~/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

[email protected]:~$ 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

[email protected]:~$ sudo /usr/local/nginx/sbin/nginx
           

3. 访问网站

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

Django+Nginx+uWSGI部署环境准备安装 Django安装 uWSGI安装 Nginx基于 uWSGI + Django 的实现Django + Uwsgi + Nginx

继续阅读