Location配置项匹配符号的意义以及LNMP架构是什么?如何使用LNMP架构,uwsgi服务部署,这里都有!!!👆
使用Nginx Location可以控制访问网站的路径, 但一个server可以有多个location配置, 多个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 =";
}
}
LNMP是一套技术的组合,L=Linux
、N=Nginx
、M~=MySQL
、P~=Python
首先Nginx服务是不能处理动态请求,那么当用户发起动态请求时, Nginx又是如何进行处理的。
当用户发起http请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回,如果是动态请求Nginx则通过uwsgi协议转交给后端的Python程序处理
因为nginx不支持wsgi协议,无法直接调用py开发的webApp
1、创建用户
groupadd django -g 888
useradd django -u 888 -g 888 -r -M -s /bin/sh
2、安装依赖软件
yum install python3 libxml* python-devel gcc* pcre-devel openssl-devel python3-devel -y
3、安装Django和uwsgi
pip3 install uwsgi django
4、创建项目
cd /opt
django-admin startproject linux
cd linux
django-admin startapp app01
4.1、修改配置文件
vim /opt/linux/linux/settings.py
ALLOWED_HOSTS = ['*'] # 所有网站都可以访问
DATABASES = {} # 清空
4.2、测试 (测试上面的步骤)
[root@web01 linux]# python3 manage.py runserver 0.0.0.0:8000
5、编辑项目配置文件
[root@web01 ~]# vim /opt/linux/myweb_uwsgi.ini
[uwsgi]
# 端口号
socket = :8000
# 指定项目的目录
chdir = /opt/linux
# wsgi文件路径
wsgi-file = linux/wsgi.py
# 模块wsgi路径
module = linux.wsgi
# 是否开启master进程,用来监控worker进程
master = true
# 工作进程的最大数目,worker数量
processes = 4
# 结束后是否清理文件
vacuum = true
6、启动uwsgi
uwsgi -d --ini myweb_uwsgi.ini --uid 666
-d:守护进程进行
--ini:指定配置文件路径
--uid:指定uid
# 使用root启动会给一个警告!
7、编辑Nginx配置文件
[root@web01 ~]# vim /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、域名解析 windows下
9、测试配置文件
nginx -t
10、重启Nginx配置
systemctl restart nginx
[root@db01 ~]# yum install mariadb* -y
[root@db01 ~]# systemctl start mariadb
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)
测试连接成功
# 上传代码
[root@db01 ~]# unzip bbs.zip
[root@db01 ~]# mv bbs /opt/
# 数据库迁移之前清空/opt/bbs/app01/migrations目录下的py文件只留下双下init.py
[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 /opt/bbs/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'
}
}
# 修改Django版本和安装pymysql插件,修改配置文件,是为了满足创建数据库迁移文件准备
# 创建数据库迁移文件
[root@web01 bbs]# python3 manage.py makemigrations
# 数据库迁移
[root@web01 bbs]# python3 manage.py migrate
# 数据库就有表了
[root@web01 bbs]# vim /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
# 配置nginx前,启动uwsgi
[root@web01 bbs]# uwsgi -d --ini myweb_uwsgi.ini --uid 666
# 配置Nginx
[root@web01 bbs]# vim /etc/nginx/conf.d/bbs.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 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 bbs]# systemctl restart nginx
10.windows hosts文件域名解析
【写错请指正,搭建好有点丑~】
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.cnblogs.com/48xz/p/15766841.html
内容来源于网络,如有侵权,请联系作者删除!