7、架构--location、LNMP架构、uwsgi部署、BBS项目部署
阅读原文时间:2023年07月09日阅读:1

笔记

1、晨考

1、Nginx中常用的模块
autoindex
stub_status
allow 和 deny
basic
limit_conn
limit_req

2、配置步骤
    1、创建连接池
    2、调用

2、昨日问题

1、权限问题
2、端口占用问题
3、开机IP没了

3、今日内容

1、location
2、LNMP架构

4、location

使用Nginx Location可以控制访问网站的路径, 但一个server可以有多个location配置, 多个location的优先级该如何区分。

4.1、location匹配符号

匹配符

匹配规则

优先级

=

精确匹配

1

^~

以某个字符串开头

2

~

区分大小写的正则匹配

3

~*

不区分大小写的正则匹配

3

/

通用匹配,任何请求都会匹配到

4

server {
    listen 80;
    server_name _;

    location ~* /python {
        default_type text/html;
        return 200 "Location ~*";
    }

    location ~ /Python {
        default_type text/html;
        return 200 "Location ~";
    }

    location ^~ /python {
        default_type text/html;
        return 200 "Location ^~";
    }

    location = /python {
        default_type text/html;
        return 200 "Location =";
    }
}

5、LNMP架构

LNMP是一套技术的组合,L=Linux、N=Nginx、M~=MySQL、P~=Python

首先Nginx服务是不能处理动态请求,那么当用户发起动态请求时, Nginx又是如何进行处理的。
    1.静态请求:请求的内容是静态文件就是静态请求
        1)静态文件:文件上传到服务器,永远不会改变的文件就是静态文件
        2)html就是一个标准的静态文件
    2.动态请求:请求的内容是动态的就是动态请求
        1)不是真实存在服务器上的内容,是通过数据库或者其他服务拼凑成的数据

当用户发起http请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回,如果是动态请求Nginx则通过uwsgi协议转交给后端的Python程序处理

5.1、uwsgi

因为nginx不支持wsgi协议,无法直接调用py开发的webApp。
在nginx+uWsgi+Django的框架里,nginx代理+webServer,uWsgi是wsgiServer,Django是webApp。
nginx接收用户请求,并判定哪些转发到uWsgi,uWsgi再去调用pyWebApp。

5.2、uwsgi服务部署

1、创建用户
[root@web01 opt]# groupadd django -g 888
[root@web01 opt]# useradd django -u 888 -g 888 -r -M -s /bin/sh

2、安装依赖软件
[root@web01 opt]# yum install python3 libxml* python-devel gcc* pcre-devel openssl-devel python3-devel -y

3、安装Django和uwsgi
[root@web01 opt]# pip3 install django
[root@web01 opt]# pip3 install uwsgi

4、创建项目
[root@web01 opt]# cd /opt
[root@web01 opt]# django-admin startproject linux
[root@web01 opt]# cd linux
[root@web01 opt]# django-admin startapp app01
[root@web01 linux]# vim linux/settings.py
ALLOWED_HOSTS = ['*']
DATABASES = {}
# 启动测试
[root@web01 linux]# python3 manage.py runserver 0.0.0.0:8000

5、编辑项目配置文件
[root@localhost ~]# cat /opt/linux/myweb_uwsgi.ini
[uwsgi]
# 端口号
socket            = :8000
# 指定项目的目录
chdir           = /opt/linux
# wsgi文件路径
wsgi-file       = linux/wsgi.py
# 模块wsgi路径
module          = linux.wsgi
# 是否开启master进程
master          = true
# 工作进程的最大数目
processes       = 4
# 结束后是否清理文件
vacuum          = true

6、启动uwsgi
[root@web01 linux]# uwsgi -d --ini myweb_uwsgi.ini --uid 666

-d       : 以守护进程方式运行
--ini : 指定配置文件路径
--uid : 指定uid

TCP 服务

7、编辑Nginx配置文件
[root@localhost ~]# cat /etc/nginx/conf.d/python.conf
server {
    listen 80;
    server_name py.test.com;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8000;
        uwsgi_read_timeout 2;
        uwsgi_param UWSGI_SCRIPT linux.wsgi;
        uwsgi_param UWSGI_CHDIR /opt/linux;
        index  index.html index.htm;
        client_max_body_size 35m;
    }
}

8、重启Nginx配置
systemctl restart nginx

6、部署BBS项目

1、部署数据库
[root@db01 ~]# yum install mariadb* -y

2、启动数据库
[root@db01 ~]# systemctl start mariadb 

3、远程连接MySQL数据
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> CREATE DATABASE `bbs` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Query OK, 1 row affected (0.00 sec)

4、部署BBS

4.1、上传代码
[root@db01 ~]# unzip bbs.zip
[root@db01 ~]# mv bbs /opt/

4.2、数据库迁移
[root@web01 migrations]# pwd
/opt/bbs/app01/migrations
[root@web01 migrations]# rm -rf 00*
[root@web01 migrations]# rm -rf __pycache__/

[root@web01 migrations]# cd /opt/bbs/
[root@web01 bbs]# pwd
/opt/bbs

# 修改Django版本
[root@web01 bbs]# pip3 uninstall django
[root@web01 bbs]# pip3 install django==1.11

# 安装MySQL数据库插件
[root@web01 bbs]# pip3 install pymysql

# 修改数据连接
[root@web01 bbs]# vim bbs/settings.py
ALLOWED_HOSTS = ['*']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bbs',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '172.16.1.61',
        'PORT': 3306,
        'CHARSET': 'utf8'
    }
}

# 创建数据库迁移文件
[root@web01 bbs]# python3 manage.py makemigrations

# 数据库迁移
[root@web01 bbs]# python3 manage.py migrate

4.3、配置UWSGI
[root@localhost ~]# cat /opt/bbs/myweb_uwsgi.ini
[uwsgi]
# 端口号
socket            = :8002
# 指定项目的目录
chdir           = /opt/bbs
# wsgi文件路径
wsgi-file       = bbs/wsgi.py
# 模块wsgi路径
module          = bbs.wsgi
# 是否开启master进程
master          = true
# 工作进程的最大数目
processes       = 4
# 结束后是否清理文件
vacuum          = true

[root@web01 bbs]# uwsgi -d --ini myweb_uwsgi.ini --uid 666

4.4、配置Nginx
[root@localhost ~]# cat /etc/nginx/conf.d/python.conf
server {
    listen 80;
    server_name bbs.test.com;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8002;
        uwsgi_read_timeout 2;
        uwsgi_param UWSGI_SCRIPT bbs.wsgi;
        uwsgi_param UWSGI_CHDIR /opt/bbs;
        index  index.html index.htm;
        client_max_body_size 35m;
    }
}

[root@web01 bbs]# systemctl restart nginx 

4.5、测试访问BBS

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章