08lnmp架构搭建
关于lnmp架构
l Linux
n Nginx
m MySQL
p Python/PHP
一、nginx中常用模块
1、目录索引模块
[root@web01 ~]# cd /etc/nginx/conf.d
[root@web01 conf.d]# vim autoindex.conf
server {
server_name index.test.com;
listen 80;
# 开启目录索引
autoindex on;
#格式化文件大小
autoindex_exact_size off;
#输出的格式
autoindex_format html;
#使用时区
autoindex_localtime on;
location / {
root /usr/share/nginx;
index index.html;
}
}
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx
浏览器上输入192.168.15.7或者index.test.com访问
#####################整合目录索引模块#####################
[root@web01 nginx]# pwd
/etc/nginx
[root@web01 nginx]# vim autoindex_params
# 开启目录索引
autoindex on;
#格式化文件大小
autoindex_exact_size off;
#输出的格式
autoindex_format html;
#使用时区
autoindex_localtime on;
[root@web01 conf.d]# vim autoindex.conf
server {
server_name index.test.com;
listen 80;
include autoindex_params;
location / {
root /usr/share/nginx;
index index.html;
}
}
2、访问限制模块(禁用IP和开放IP访问)
1)语法
Syntax: allow address | CIDR | unix: | all;
# 地址 网段 sokect文件 所有
Default: —
Context: http, server, location, limit_except
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
2)案例
allow : 允许IP访问
deny :禁止IP访问
案例1:只允许192.168.15.1来访问。
1、允许192.168.15.1来访问
allow 192.168.15.1;
2、禁止其他所有IP来访问
deny all;
案例2:只允许192.168.15.0来访问。
1、允许192.168.15.0来访问
allow 192.168.15.0/24;
2、禁止其他所有IP来访问
deny all;
案例3:要求禁止192.168.15.1来访问。
1、禁止192.168.15.1来访问
deny 192.168.15.1;
2、允许其他所有的IP来访问
allow all;
3)配置
#允许192.168.15.0访问,拒绝所有
[root@web01 conf.d]# vim autoindex.conf
server {
server_name index.test.com;
listen 80;
include autoindex_params;
allow 192.168.15.0/24;
deny all;
location / {
root /usr/share/nginx;
index index.html;
}
}
4)命令行界面访问
[root@web01 ~]# systemctl restart nginx
[root@web01 ~]# curl -H'Host: index.test.com' 192.168.15.7
3、访问控制模块(认证模块)
#下载认证模块
yum install httpd-tools -y
1)语法
Syntax: auth_basic string | off; #如果跟string,就代表开启
Default: auth_basic off;
Context: http, server, location, limit_except
Syntax: auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except
2)配置密码文件
[root@web01 ~]# htpasswd -c /etc/nginx/auth fengmo #-c 创建新的密码文件,追加不使用-c参数。
New password: 123456 #密码输入时为暗文
Re-type new password: 123456
Adding password for user fengmo
3)配置
[root@web01 ~]# cd /etc/nginx/conf.d
[root@web01 conf.d]# vim autoindex.conf
server {
server_name index.test.com;
listen 80;
include autoindex_params;
auth_basic "请输入用户名和密码:"; ##引号内随便输入字符表示打开访问控制
auth_basic_user_file /etc/nginx/auth; #后面跟含有用户名和密码的文件
location / {
root /usr/share/nginx;
index index.html;
}
}
4)重启
[root@web01 conf.d]# systemctl restart nginx
4、nginx状态模块
1)语法
Syntax: stub_status;
Default: —
Context: server, location
2)配置
[root@web01 ~]# vim /etc/nginx/conf.d/autoindex.conf
server {
server_name index.test.com;
listen 80;
include autoindex_params;
auth_basic "请输入用户名和密码:"; ##引号内随便输入字符表示打开访问控制
auth_basic_user_file /etc/nginx/auth; #后面跟含有用户名和密码的文件
location /status {
stub_status; #autoindex.com/status可以访问nginx状态
}
location / {
root /usr/share/nginx;
index index.html;
}
}
3)页面访问
index.test.com/status
Active connections: 2
server accepts handled requests
2 2 27
Reading: 0 Writing: 1 Waiting: 1
4)监控网站的PV
[root@web01 ~]# curl -s http://www.autoindex.com/basic_status | awk 'NR==3 {print $3}'
5、连接限制模块
1)语法
#设置限制的空间
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
limit_conn_zone #设置空间的模块
key #指定空间存储的内容
zone #指定空间
=name #空间名字
:size; #空间的大小
#调用限制的空间
Syntax: limit_conn zone number;
Default: —
Context: http, server, location
limit_conn #调用空间的模块
zone #空间的名字
number; #指定可以同时连接的次数
2)配置
[root@web01 ~]# vim /etc/nginx/conf.d/autoindex.conf
limit_conn_zone $remote_addr zone=conn_zone:10m; #设置一个存储ip地址,空间名字为conn_zone,空间大小为10M的空间
server {
server_name index.test.com;
listen 80;
include autoindex_params;
#调用conn_zone空间,限制每个ip同时只能连接一次
limit_conn conn_zone 1;
location / {
root /usr/share/nginx;
index index.html;
}
}
6、请求限制模块
1)语法
#设置空间的语法
Syntax: limit_req_zone key zone=name:size rate=rate [sync];
Default: —
Context: http
limit_req_zone #设置空间的模块
key #空间存储的内容
zone #指定空间
=name #空间的名字
:size #空间的大小
rate=rate [sync]; #读写速率
#调用的语法
Syntax: limit_req zone=name [burst=number] [nodelay | delay=number];
Default: —
Context: http, server, location
limit_req #调用控件模块
zone=name #指定空间=空间的名字
[burst=number] #允许多请求几次
[nodelay | delay=number]; #延时
2)配置
[root@web01 ~]# vim /etc/nginx/conf.d/autoindex.conf
limit_conn_zone $remote_addr zone=conn_zone:10m;
limit_req_zone $remote_addr zone=req_zone:10m rate=1r/s; #设置一个储存ip地址,储存大小为10m,空间名字req_zone,一秒只能请求一次的空间。
server {
listen 80;
server_name index.test.com;
charset utf8;
limit_conn conn_zone 1;
limit_req zone=req_zone; #调用空间名字是req_zone的空间
#limit_req zone=req_zone burst=5 nodelay; #调用空间名字是req_zone的空间,最大限度可以同时访问五次,没有延迟
location / {
root /usr/share/nginx;
index index.html;
}
}
3)测试
[root@web01 ~]# ab -n 20000 -c 20 http://www.autoindex.com/index.html
-n #设置请求的次数
-c #一次请求并发的次数
二、Nginx代理Python
知识储备:使用django框架
1、安装python3
[root@web01 ~]# yum install python3 -y
2、安装django框架
[root@web01 ~]# pip3 install django==2.2.2
3、创建django项目
[root@web01 opt]# cd /opt/
[root@web01 opt]# django-admin startproject linux
4、在项目中创建应用
[root@web01 opt]# cd linux/
[root@web01 linux]# pwd
/opt/linux
[root@web01 linux]# django-admin startapp application
5、修改配置文件
[root@web01 linux]# vim /opt/linux/linux/settings.py
ALLOWED_HOSTS = ['*']
DATABASES = {}
6、启动,浏览器访问
[root@web01 linux]# pwd
/opt/linux
[root@web01 linux]# python3 manage.py runserver 0.0.0.0:8000
7、浏览器输入192.168.15.7:8000访问
讲解Nginx代理Python
1、创建用户
[root@web01 linux]# groupadd django -g 888
[root@web01 linux]# 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、安装uwsgi和django
[root@web01 linux]# pip3 install uwsgi
[root@web01 linux]# pip3 install django==2.2.2
4、创建Python项目代码
参考上述:使用django框架
5、编辑项目启动配置文件
[root@web01 ~]# cd /opt/linux
[root@web01 linux]# vim myuwsgi.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
uwsgi
参数:
-d : 以守护进程方式运行
--ini : 指定配置文件的路径
[root@web01 linux]# cd /opt/linux
[root@web01 linux]# uwsgi -d --ini myuwsgi.ini
[uWSGI] getting INI configuration from myuwsgi.ini
# 查看进程列表
[root@web01 linux]# ps -ef | grep uwsgi
root 26395 1 3 12:17 ? 00:00:00 uwsgi -d --ini myuwsgi.ini
root 26397 26395 0 12:17 ? 00:00:00 uwsgi -d --ini myuwsgi.ini
root 26398 26395 0 12:17 ? 00:00:00 uwsgi -d --ini myuwsgi.ini
root 26399 26395 0 12:17 ? 00:00:00 uwsgi -d --ini myuwsgi.ini
root 26400 26395 0 12:17 ? 00:00:00 uwsgi -d --ini myuwsgi.ini
7、配置Nginx连接uwsgi
[root@web01 ~]# cd /etc/nginx/conf.d
[root@web01 conf.d]# vim python.conf
# 配置一个网站
server {
# 监听的端口
listen 80;
# 配置域名
server_name py.test.com;
# 配置路径
location / {
# 加载Nginx代理uwsgi的配置项
include uwsgi_params;
# 指定uwsgi的访问地址
uwsgi_pass 127.0.0.1:8000;
# 连接uwsgi的超时时间
uwsgi_read_timeout 2;
# 自定义uwsgi代理项目的路径及配置项
uwsgi_param UWSGI_SCRIPT linux.wsgi;
# 指定python项目的路径
uwsgi_param UWSGI_CHDIR /opt/linux;
# 索引文件
index index.html index.htm;
# 客户端上传文件的最大值
client_max_body_size 35m;
}
}
8、重启Nginx
[root@web01 conf.d]# systemctl restart nginx
9、c盘hosts中添加域名解析:C:\Windows\System32\drivers\etc
192.168.15.7 py.test.com