61.6-博客项目部署和MVC

最好的时光,是自己依然有勇气面对生命中任何成功与失败!

总结:

  1. ngnix:80端口开启,前面我们先发起一个http请求;直接访问的根http://192.168.0.101/api/post/2,相当于根本就没有代理;这个时候 ngnix 为项目提供静态内容,api被替换掉,服务往8000端口上uWSGI,相当于/api/post/2相当于访问/post/2;
  2. django 相当于一个程序,没有server;
  3. 讲 前后端 部署 流程 后30 ;

参考 :

  1. 小白学Django—— [Python进击者] 原创Django系列汇总篇

项目 部署

1. Django打包

打包 只打python 文件;
构建setup.py文件

from distutils.core import setup
import glob

setup(name='Distutils',
      version='1.0',
      description='blog project',
      author='wayne',
      author_email='[email protected]',
      url='https://magedu.com',
      packages=['blog', 'post', 'user','user.templatetags'],
      py_modules=['manage'],
      data_files=glob.glob('templates/*.html')+['requirements'] + glob.glob('static/*')
     )
#应用程序的根目录下打包(打包只针对py文件,对 html 文件无效, html要写路径)
(blog) C:\Users\dell\PycharmProjects\blog>pip freeze > requirements
-----------------------------------------------------------
asn1crypto==0.22.0
bcrypt==3.1.3
certifi==2016.2.28
cffi==1.10.0
cryptography==1.8.1
Django==1.11.16
idna==2.6
mysqlclient==1.3.13
packaging==16.8
pycparser==2.18
PyJWT==1.7.1
pyparsing==2.2.0
pytz==2020.1
simplejson==3.11.1
six==1.10.0
wincertstore==0.2

$python setup.pys dist--formats=gz tar#gz

(blog) C:\Users\dell\PycharmProjects\blog>python setup.py sdist
running sdist
running check
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)
warning: sdist: standard file not found: should have one of README, README.txt
writing manifest file 'MANIFEST'
creating Distutils-1.0
creating Distutils-1.0\blog
creating Distutils-1.0\post
creating Distutils-1.0\user
creating Distutils-1.0\user\templatetags
making hard links in Distutils-1.0...
hard linking setup.py -> Distutils-1.0
hard linking blog\__init__.py -> Distutils-1.0\blog
hard linking blog\settings.py -> Distutils-1.0\blog
hard linking blog\urls.py -> Distutils-1.0\blog
hard linking blog\wsgi.py -> Distutils-1.0\blog
hard linking post\__init__.py -> Distutils-1.0\post
hard linking post\admin.py -> Distutils-1.0\post
hard linking post\apps.py -> Distutils-1.0\post
hard linking post\models.py -> Distutils-1.0\post
hard linking post\tests.py -> Distutils-1.0\post
hard linking post\urls.py -> Distutils-1.0\post
hard linking post\views.py -> Distutils-1.0\post
hard linking user\__init__.py -> Distutils-1.0\user
hard linking user\admin.py -> Distutils-1.0\user
hard linking user\apps.py -> Distutils-1.0\user
hard linking user\models.py -> Distutils-1.0\user
hard linking user\tests.py -> Distutils-1.0\user
hard linking user\urls.py -> Distutils-1.0\user
hard linking user\views.py -> Distutils-1.0\user
hard linking user\templatetags\__init__.py -> Distutils-1.0\user\templatetags
hard linking user\templatetags\myfilter.py -> Distutils-1.0\user\templatetags
creating dist
Creating tar archive
removing 'Distutils-1.0' (and everything under it)

在Linux系统中创建一个python虚拟环境目录, 使用pyenv(conda )

# python/mysql编译文件包安装(root权限安装)
[root]$ yum install python-devel mysql-devel

[dell@Centos7 djangoweb]$ conda create -n blog366 python=3.6.6
Fetching package metadata .........
Solving package specifications: .

Package plan for installation in environment /home/dell/miniconda3/envs/blog366:

The following NEW packages will be INSTALLED:
    _libgcc_mutex:   0.1-conda_forge      https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    _openmp_mutex:   4.5-1_gnu            https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    ca-certificates: 2020.6.20-hecda079_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    certifi:         2016.2.28-py36_0     https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    libffi:          3.2.1-1              https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    libgcc-ng:       9.3.0-h24d8f2e_16    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    libgomp:         9.3.0-h24d8f2e_16    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    libstdcxx-ng:    9.3.0-hdf63c60_16    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    ncurses:         6.2-he1b5a44_1       https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    openssl:         1.0.2u-h516909a_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    pip:             9.0.1-py36_1         https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    python:          3.6.6-hd21baee_1003  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    readline:        7.0-hf8c457e_1001    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    setuptools:      36.4.0-py36_1        https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    sqlite:          3.28.0-h8b20d00_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    tk:              8.6.10-hed695b0_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    wheel:           0.29.0-py36_0        https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    xz:              5.2.5-h516909a_1     https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
    zlib:            1.2.11-0             https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
Proceed ([y]/n)? y
---------------------------------------------------------------------------------------------------------------------

-rw-rw-r--. 1 dell dell  96211 Aug 23  2020 blog-1.0.tar.gz
drwxrwxr-x. 7 dell dell    142 Aug 23  2020 Distutils-1.0
-rw-rw-r--. 1 dell dell 843357 Jan 14  2019 tengine-1.2.3.tar.gz
(blog366) [dell@Centos7 djangoweb]$ rm blog-1.0.tar.gz
(blog366) [dell@Centos7 djangoweb]$ ll
total 824
drwxrwxr-x. 7 dell dell    142 Aug 23  2020 Distutils-1.0
-rw-rw-r--. 1 dell dell 843357 Jan 14  2019 tengine-1.2.3.tar.gz
(blog366) [dell@Centos7 djangoweb]$ mv Distutils-1.0/* ./
(blog366) [dell@Centos7 djangoweb]$ ll
total 840
drwxrwxr-x. 2 dell dell     74 Aug 23  2020 blog
drwxrwxr-x. 2 dell dell      6 Aug 11 16:33 Distutils-1.0
-rw-rw-r--. 1 dell dell    824 Aug 10 08:59 manage.py
-rw-rw-r--. 1 dell dell    214 Aug 23  2020 PKG-INFO
drwxrwxr-x. 2 dell dell    120 Aug 23  2020 post
-rw-rw-r--. 1 dell dell    271 Aug 23  2020 requirements
-rw-rw-r--. 1 dell dell    416 Aug 23  2020 setup.py
drwxrwxr-x. 2 dell dell     57 Aug 23  2020 static
drwxrwxr-x. 2 dell dell     24 Aug 23  2020 templates
-rw-rw-r--. 1 dell dell 843357 Jan 14  2019 tengine-1.2.3.tar.gz
drwxrwxr-x. 3 dell dell    140 Aug 23  2020 user

# 安装依赖包 ,启动后端blog服务;
(blog366) [dell@Centos7 djangoweb]$ pip install -r requirements
Collecting asn1crypto==0.22.0 (from -r requirements (line 1))
  Using cached https://files.pythonhosted.org/packages/97/ba/7e8117d8efcee589f4d96dd2b2eb1d997f96d27d214cf2b7134ad8acf6ab/asn1crypto-0.22.0-py2.py3-none-any.whl
Requirement already satisfied: bcrypt==3.1.3 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 2))
Collecting certifi==2016.2.28 (from -r requirements (line 3))
  Downloading https://files.pythonhosted.org/packages/db/60/1ed0106bde7b14b363b15b17cc308aad93ba57d3582570f3ad7180ae0fae/certifi-2016.2.28-py2.py3-none-any.whl (366kB)
    100% |████████████████████████████████| 368kB 11kB/s
Requirement already satisfied: cffi==1.10.0 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 4))
Requirement already satisfied: cryptography==1.8.1 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 5))
Requirement already satisfied: Django==1.11.16 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 6))
Requirement already satisfied: idna==2.6 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 7))
Requirement already satisfied: mysqlclient==1.3.13 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 8))
Requirement already satisfied: packaging==16.8 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 9))
Requirement already satisfied: pycparser==2.18 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 10))
Requirement already satisfied: PyJWT==1.7.1 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 11))
Requirement already satisfied: pyparsing==2.2.0 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 12))
Requirement already satisfied: pytz==2020.1 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 13))
Requirement already satisfied: simplejson==3.11.1 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 14))
Requirement already satisfied: six==1.10.0 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 15))
Requirement already satisfied: wincertstore==0.2 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from -r requirements (line 16))
Requirement already satisfied: setuptools>=11.3 in /home/dell/miniconda3/envs/blog366/lib/python3.6/site-packages (from cryptography==1.8.1->-r requirements (line 5))
Installing collected packages: asn1crypto, certifi
Successfully installed asn1crypto-0.22.0 certifi-2016.2.28
You are using pip version 9.0.1, however version 20.2.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(blog366) [dell@Centos7 djangoweb]$ python manage.py runserver
Performing system checks...

(0.001) SELECT @@SQL_AUTO_IS_NULL; args=None
(0.000) SELECT VERSION(); args=None
System check identified no issues (0 silenced).
(0.006) SHOW FULL TABLES; args=None
(0.005) SELECT `django_migrations`.`app`, `django_migrations`.`name` FROM `django_migrations`; args=()
August 11, 2020 - 17:02:24
Django version 1.11.16, using settings 'blog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.             

test 部署数据库; python部署blog后端;无法访问;


修改setting里面的配置,

DEBUG = False           # 上线false,测试true

ALLOWED_HOSTS = ['*']        #  正常应该指定几个固定 IP访问  ;

# 重启runserver
(blog366) [dell@Centos7 djangoweb]$ python manage.py runserver 0.0.0.0:8000
[dell@Centos7 djangoweb]$ ss -tanl       # 8000端口已经开启
State      Recv-Q Send-Q                                                                       Local Address:Port                                                                                      Peer Address:Port
LISTEN     0      5                                                                            192.168.122.1:53                                                                                                   *:*
LISTEN     0      128                                                                                      *:22                                                                                                   *:*
LISTEN     0      128                                                                              127.0.0.1:631                                                                                                  *:*
LISTEN     0      100                                                                              127.0.0.1:25                                                                                                   *:*
LISTEN     0      10                                                                                       *:8000                                                                                                 *:*
LISTEN     0      50                                                                                       *:3306                                                                                                 *:*
LISTEN     0      128                                                                                      *:111                                                                                                  *:*
LISTEN     0      128                                                                                   [::]:22                                                                                                [::]:*
LISTEN     0      128                                                                                  [::1]:631                                                                                               [::]:*
LISTEN     0      100                                                                                  [::1]:25                                                                                                [::]:*
LISTEN     0      128

访问成功

使用 http://192.168.0.101:8000/post?page=2&size=2 成功返回数据,说明Django应用成功;
至此,Django应用部署完成。Django带了个开发用Web Server,生成环境不用,需要借助其它Server。
注意:ALLOWED_HOSTS = ["*"] 这是所有都可以访问,生产环境应指定具体可以访问的IP,而不是所有。

2. WSGI

Web Server Gateway Interface,是Python中定义的Web Server与应用程序的接口定义。
应用程序有WSGI 的 Django框架负责,WSGI Server谁来做?

1. uWSGI ( uWSGI server服务开启, Django自带的server没开启,blog就是一个程序;

  1. http

3. uWSGI 项目

uW SGI是一个C语言的项目, 提供一个WEB服务器, 它支持WSGI协议, 可以和Python的WSGI应用程序通信。
的高薪职
官方文档https://uwsgi-docs.readthedocs.io/en/latest/
uW SGI可以直接启动HTTP服务, 接收HTTP请求, 并调用Django应用。

安装
(blog366) [dell@Centos7 djangoweb]$ conda install uwsgi

4. uWSGI + Django部署

在Django项目根目录下,运行 $ uwsgi --http :8000 --wsgi-file blog/wsgi.py --stats :8001 --stats-http ,使用下面的链接测试
http://192.168.0.101:8000/post/2

(blog366) [dell@Centos7 djangoweb]$ uwsgi --http:8000 --wsgi-file blog/wsgi.py

uwsgi: unrecognized option '--http:8000'
getopt_long() error
(blog366) [dell@Centos7 djangoweb]$ uwsgi --http :8000 --wsgi-file blog/wsgi.py
*** Starting uWSGI 2.0.18 (64bit) on [Tue Aug 11 17:39:42 2020] ***
compiled with version: 7.3.0 on 12 May 2020 20:20:39
os: Linux-3.10.0-1127.18.2.el7.x86_64 #1 SMP Sun Jul 26 15:27:06 UTC 2020
nodename: Centos7
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /home/dell/projects/djangoweb
detected binary path: /home/dell/miniconda3/envs/blog366/bin/uwsgi
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 4096
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8000 fd 4
spawned uWSGI http 1 (pid: 97963)
uwsgi socket 0 bound to TCP address 127.0.0.1:35377 (port auto-assigned) fd 3
Python version: 3.6.10 | packaged by conda-forge | (default, Apr 24 2020, 16:44:11)  [GCC 7.3.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x55f1311b8970
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x55f1311b8970 pid: 97962 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 97962, cores: 1)
[pid: 97962|app: 0|req: 1/1] 192.168.0.103 () {36 vars in 468 bytes} [Tue Aug 11 17:40:39 2020] GET /post => generated 0 bytes in 35 msecs (HTTP/1.1 301) 3 headers in 111 bytes (1 switches on core 0)
2 
[pid: 97962|app: 0|req: 2/2] 192.168.0.103 () {36 vars in 472 bytes} [Tue Aug 11 17:40:47 2020] GET /post/2 => generated 137 bytes in 30 msecs (HTTP/1.1 200) 3 headers in 101 bytes (1 switches on core 0)

stats能够显示服务器状态值。--stats-http选项可以使用http访问这个值。
安装uwsgitop获取这个stat值。注意使用这个命令不要使用--stats-http选项。

5. Nginx uwsgi 部署

Nginx (发音同"engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx的特点是占有内存少,并发能力强。中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

Nginx的优势
1.支持高并发连接
由于apache采用的是select网络I/O模型,Ngnix在处理大量连续高并发访问时效率要比Apache高很多。可通过top命令可以查看系统负载和CPU使用率

2.内存消耗少
在服务器3W并发连接下,开启10个Nginx进程消耗150MB内存(15MB10),开启64个php-cgi进程消耗128MB内存(20MB64),共消耗不到2GB内存。可以使用压力测试工具webbench对Nginx进行压力测试。
3.成本低廉
购买F5 BIG-IP、NetScaler等负载均衡交换机需要几十万RMB,而开源Nginx替代这些商业设备。
4.支持热部署
能够在不间断服务的情况下、对软件版本进行升级
5.配置简单
6.其他优势
支持rewrite重写规则,能够根据域名、URL的不同、将HTTP请求分发到不同的后端服务器群组;内置的健康检查功能;节省带宽,支持GZIP压缩,可以添加浏览器本地缓存的Header头。

5.1 Ngnix 编译安装

Ngnix应该在低权限用户下跑的;

# 依赖包安装;
[root@Centos7 tengine-1.2.3]# yum install gcc pcre-devel openssl-devel

[root@Centos7 ~]# tar xf tengine-1.2.3.tar.gz
[root@Centos7 ~]# cd tengine-1.2.3/
[root@Centos7 tengine-1.2.3]# ll
total 516
-rw-r--r--. 1 51062 users    487 Feb 10  2012 AUTHORS.te
drwxr-xr-x. 6 51062 users   4096 Feb 27  2012 auto
-rw-r--r--. 1 51062 users 196581 Feb 17  2012 CHANGES
-rw-r--r--. 1 51062 users 299535 Feb 17  2012 CHANGES.ru
-rw-r--r--. 1 51062 users   5754 Feb 27  2012 CHANGES.te
drwxr-xr-x. 2 51062 users    168 Feb 27  2012 conf
-rwxr-xr-x. 1 51062 users   2411 Feb 17  2012 configure
drwxr-xr-x. 3 51062 users     84 Feb 27  2012 contrib
drwxr-xr-x. 2 51062 users     40 Feb 27  2012 html
-rw-r--r--. 1 51062 users   1422 Feb 17  2012 LICENSE
drwxr-xr-x. 2 51062 users     21 Feb 27  2012 man
-rw-r--r--. 1 51062 users     78 Dec  1  2011 README
drwxr-xr-x. 8 51062 users     77 Feb 27  2012 src
drwxr-xr-x. 4 51062 users     43 Feb 27  2012 tests
[root@Centos7 tengine-1.2.3]# ./configure --help | grep uwsgi
  --without-http_uwsgi_module        disable ngx_http_uwsgi_module
  --http-uwsgi-temp-path=PATH        set path to store
                                     http uwsgi temporary files
[root@Centos7 tengine-1.2.3]# ./configure
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + md5: using system crypto library
  + sha1: using system crypto library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@Centos7 tengine-1.2.3]# make && make install               #  第二部 && 第三步
make[1]: Leaving directory `/root/tengine-1.2.3'

[root@Centos7 tengine-1.2.3]# cd /usr/local/nginx/conf       # 进入默认安装位置;
-rw-r--r--. 1 root root  979 Aug 11 18:07 fastcgi.conf
-rw-r--r--. 1 root root  979 Aug 11 18:07 fastcgi.conf.default
-rw-r--r--. 1 root root  909 Aug 11 18:07 fastcgi_params
-rw-r--r--. 1 root root  909 Aug 11 18:07 fastcgi_params.default
-rw-r--r--. 1 root root 2837 Aug 11 18:07 koi-utf
-rw-r--r--. 1 root root 2223 Aug 11 18:07 koi-win
-rw-r--r--. 1 root root 3559 Aug 11 18:07 mime.types
-rw-r--r--. 1 root root 3559 Aug 11 18:07 mime.types.default
-rw-r--r--. 1 root root 2685 Aug 11 18:07 nginx.conf
-rw-r--r--. 1 root root 2685 Aug 11 18:07 nginx.conf.default
-rw-r--r--. 1 root root  544 Aug 11 18:07 scgi_params
-rw-r--r--. 1 root root  544 Aug 11 18:07 scgi_params.default
-rw-r--r--. 1 root root  570 Aug 11 18:07 uwsgi_params          # 安装成功
-rw-r--r--. 1 root root  570 Aug 11 18:07 uwsgi_params.default
-rw-r--r--. 1 root root 3610 Aug 11 18:07 win-utf

5.2 Ngnix.conf 配置修改

nginx作为web服务以及nginx.conf详解
https://www.cnblogs.com/f-ck-need-u/p/7683027.html

[root@Centos7 conf]# nano nginx.conf
---------------------------------------------------------------------
        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 ^~ /api/ {
            rewrite ^/api(/.*) $1 break;
            proxy_pass   http://127.0.0.1:8000;
        }
5.3 Ngnix启动
[root@Centos7 nginx]# sbin/ngnix      #  启动
[root@Centos7 conf]# ss -tanl           # 80端口被占用;
State      Recv-Q Send-Q                                                                       Local Address:Port                                                                                      Peer Address:Port
LISTEN     0      5                                                                            192.168.122.1:53                                                                                                   *:*
LISTEN     0      128                                                                                      *:80                                                                                                   *:*
LISTEN     0      100                                                                              127.0.0.1:35377                                                                                                *:*

[root@Centos7 nginx]# tail -f logs/access.log          # 查看网站访问信息;
192.168.0.103 - - [11/Aug/2020:19:01:21 +0800] "GET / HTTP/1.1" 200 151 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"
192.168.0.103 - - [11/Aug/2020:19:01:21 +0800] "GET /favicon.ico HTTP/1.1" 404 588 "http://192.168.0.101/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"
192.168.0.103 - - [11/Aug/2020:19:04:36 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"
192.168.0.103 - - [11/Aug/2020:19:05:26 +0800] "GET /api/post/2 HTTP/1.1" 200 137 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"

访问 http://192.168.0.101/ ,可以看到nginx的首页了。

5.4 基于socket 的uWSGI+ngnix高性能模式

这是 基于http协议(访问量不高没什么问题);webserver访问压力大 uWSGI可以修改为

(blog366) [dell@Centos7 djangoweb]$ uwsgi --socket :9000 --wsgi-file blog/wsgi.py          #二进制传输方式;

*** Starting uWSGI 2.0.18 (64bit) on [Tue Aug 11 19:21:11 2020] ***
compiled with version: 7.3.0 on 12 May 2020 20:20:39
os: Linux-3.10.0-1127.18.2.el7.x86_64 #1 SMP Sun Jul 26 15:27:06 UTC 2020
nodename: Centos7
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /home/dell/projects/djangoweb
detected binary path: /home/dell/miniconda3/envs/blog366/bin/uwsgi
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 4096
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :9000 fd 3
Python version: 3.6.10 | packaged by conda-forge | (default, Apr 24 2020, 16:44:11)  [GCC 7.3.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x55a05b72a340
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x55a05b72a340 pid: 102139 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 102139, cores: 1)


(blog366) [dell@Centos7 djangoweb]$ uwsgi --socket :9000 --wsgi-file blog/wsgi.py  
# 或者 将 写进blog.ini 配置文件中去;
[dell@Centos7 djangoweb]$ nano blog.ini

blog.ini配置如下
-----------------------------------------------
[uwsgi]
socket=:9000
wsgi-file=blog/wsgi.py
chdir=/home/dell/projects/djangoweb

(blog366) [dell@Centos7 djangoweb]$ uwsgi blog.ini

ngnix.conf配置修改

ngnix.conf配置修改

[root@Centos7 conf]# ss -tanl         # 9000 端口已开启
State      Recv-Q Send-Q                                                                                 Local Address:Port                                                                                                Peer Address:Port

LISTEN     0      100                                                                                                *:9000                                                                                                           *:*
LISTEN     0      50                                                                                                 *:3306                                                                                                           *:*
LISTEN     0      128                                                                                                *:111                                                                                                            *:*
LISTEN     0      128                                                                                                *:80                                                                                                             *:*

[root@Centos7 conf]# cd ../
[root@Centos7 nginx]# ll
total 4
drwx------. 2 nobody root    6 Aug 11 19:00 client_body_temp
drwxr-xr-x. 2 root   root 4096 Aug 11 18:58 conf
drwx------. 2 nobody root    6 Aug 11 19:00 fastcgi_temp
drwxr-xr-x. 2 root   root   40 Aug 11 18:07 html
drwxr-xr-x. 2 root   root   58 Aug 11 19:00 logs
drwx------. 2 nobody root    6 Aug 11 19:00 proxy_temp
drwxr-xr-x. 2 root   root   19 Aug 11 18:07 sbin
drwx------. 2 nobody root    6 Aug 11 19:00 scgi_temp
drwx------. 2 nobody root    6 Aug 11 19:00 uwsgi_temp
[root@Centos7 nginx]# sbin/nginx -s reload       # 重载 ngnix
[http://192.168.0.101/api/post/1]
5.5 部署博客前端系统
1. React 项目打包

blog 脚手架

# npm安装依赖
$ npm install rimraf --save-dev 或者 $ yarn add rimraf --dev
rimraf 递归删除文件,rm -rf 


$ yarn run build     # 打包
yarn run v1.22.4      
$ rimraf dist && webpack -p --config webpack.config.prod.js   # 加载 package.json 文件中的 built

Hash: 47e0053c99d5dfb52b73
Version: webpack 2.7.0
Time: 115131ms
              Asset       Size  Chunks                    Chunk Names
    app-47e0053c.js    2.96 MB       0  [emitted]  [big]  app
app-47e0053c.js.map    16.9 MB       0  [emitted]         app
         index.html  217 bytes          [emitted]
   [5] ./~/babel-runtime/helpers/classCallCheck.js 208 bytes {0} [built]
   [6] ./~/babel-runtime/helpers/inherits.js 1.11 kB {0} [built]
   [7] ./~/babel-runtime/helpers/possibleConstructorReturn.js 542 bytes {0} [built]
  [10] ./~/react-dom/index.js 59 bytes {0} [built]
  [13] ./~/babel-runtime/helpers/createClass.js 904 bytes {0} [built]
 [643] ./src/index.js 7.41 kB {0} [built]
 [790] ./~/antd/lib/icon/style/index.js 75 bytes {0} [built]
 [793] ./~/antd/lib/locale-provider/zh_CN.js 339 bytes {0} [built]
 [795] ./~/antd/lib/menu/style/index.js 108 bytes {0} [built]
[1371] ./src/component/detail.js 4.35 kB {0} [built]
[1372] ./src/component/list.js 5.3 kB {0} [built]
[1373] ./src/component/login.js 5.59 kB {0} [built]
[1374] ./src/component/pub.js 5.25 kB {0} [built]
[1375] ./src/component/reg.js 5.45 kB {0} [built]
[1456] multi ./src/index 28 bytes {0} [built]
    + 1442 hidden modules
Child html-webpack-plugin for "index.html":
       [0] ./~/lodash/lodash.js 543 kB {0} [built]
       [1] ./~/html-webpack-plugin/lib/loader.js!./src/index.html 528 bytes {0} [built]
       [2] (webpack)/buildin/global.js 509 bytes {0} [built]
       [3] (webpack)/buildin/module.js 517 bytes {0} [built]
Done in 116.48s.
2. 部署博客前端系统

新建assets文件夹,将app-*.js 移动到assets中


将编译好的前端项目文件index.htm与assets文件夹复制到 /usr/local/nginx/html ;
刷新首页,一切从此开始。

192.168.0.101

重新装载nginx配置文件,成功运行。
至此,前后端分离的开发、动静分离的部署的博客项目大功告成。
参看 https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html
uwsgi协议 https://uwsgi-docs.readthedocs.io/en/latest/Protocol.html

部署图
3.MVC设计模式

Controller控制器:处理输入: 负责接收用户请求, 调用Model完成数据, 调用view完成对用户的响应
Model模型:负责业务数据的处理
View视图:负责用户的交互界面

6. Django框架简介

在Django框架中,也是遵循MVC框架的。只不过名称有点不一样,在Django中,这种框架名叫 MVT,即模型M,视图V和模版T
同样可以通过注册案例来理解,其思想是和MVC相同的:

Model层
表示应用程序核心(比如 ORM 建立对象关系映射,提供数据库操作)

Template层
负责数据的可视化,使用HTML、CSS等构成模板,将数据应用到模板中,并返回给浏览器。

View层
显示数据 :Django完成URL映射后,把请求交给view层的视图函数处理,调用Model层完成数据,如有必要调用Template层
响应客户端,如果不需要,直接返回数据。

你可能感兴趣的:(61.6-博客项目部署和MVC)