一、nginx模块
1.目录索引模块
[ root@web01 ~]
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
}
}
2.访问限制模块
1)语法
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
2)配置
[ root@web01 ~]
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
access_log /var/log/nginx/www.autoindex.com.log main;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
allow 10.0.0.1;
deny all;
}
}
3.访问控制模块
1)语法
Syntax: auth_basic string | off;
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 ~]
New password:
Re-type new password:
Adding password for user lhd
3)配置
[ root@web01 ~]
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
auth_basic "请输入用户名和密码:" ;
auth_basic_user_file /etc/nginx/auth_basic;
}
}
4.nginx状态模块
1)语法
Syntax: stub_status;
Default: —
Context: server, location
2)配置
[ root@web01 ~]
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
index index.html;
}
location = /basic_status {
stub_status;
}
}
3)页面
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
4)监控网站的PV
[ root@web01 ~]
61
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 ~]
limit_conn_zone $remote_addr zone= conn_zone:10m;
server {
listen 80;
server_name www.autoindex.com;
charset utf8; ;
limit_conn conn_zone 1;
location / {
root /code/autoindex;
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 ~]
limit_conn_zone $remote_addr zone= conn_zone:10m;
limit_req_zone $remote_addr zone= req_zone:10m rate= 1r/s;
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
limit_conn conn_zone 1;
limit_req zone= req_zone;
location / {
root /code/autoindex;
index index.html;
}
}
3)测试
[ root@web01 ~]
二、nginx的location配置
使用Nginx Location可以控制访问网站的路径,但一个server可以有多个location配置, 多个location的优先级该如何区分
1.语法
Syntax: location [ = | ~ | ~* | ^~ ] uri { .. . }
location @name { .. . }
Default: —
Context: server, location
2.location匹配符
匹配符 匹配规则 优先级 = 精确匹配 1 ^~ 以某个字符串开头 2 ~ 区分大小写的正则匹配 3 ~* 不区分大小写的正则匹配 3 / 通用匹配,任何请求都会匹配到 4
3.优先级验证
[ root@web01 ~]
server {
listen 80;
server_name linux.test.com;
location / {
default_type text/html;
return 200 "location /" ;
}
location = / {
default_type text/html;
return 200 "location =/" ;
}
location ~ / {
default_type text/html;
return 200 "location ~/" ;
}
}
4.Locaiton应用场景
location / {
.. .
}
location ~ \.php$ {
.. .
}
location ~ \.jsp$ {
.. .
}
location ~* .*\.( jpg| gif| png| js| css) $ {
.. .
}
http://linux.test.com/1.PHP
http://linux.test.com/1.JPG
http://linux.test.com/1.jsp
http://linux.test.com/1.Gif
http://linux.test.com/1.PnG
http://linux.test.com/1.JsP
三、LNMP架构
1.简介
LNMP是一套技术的组合,L= Linux、N= Nginx、M~= MySQL、P~= PHP
不仅仅只有这些服务,还有很多
redis\elasticsearch\kibana\logstash\zabbix\git\jenkins\kafka\hbase\hadoop\spark\flink
2.LNMP架构工作方式
首先Nginx服务是不能处理动态请求,那么当用户发起动态请求时, Nginx又是如何进行处理的。
1.静态请求:请求的内容是静态文件就是静态请求
1)静态文件:文件上传到服务器,永远不会改变的文件就是静态文件
2)html就是一个标准的静态文件
2.动态请求:请求的内容是动态的就是动态请求
1)不是真实存在服务器上的内容,是通过数据库或者其他服务拼凑成的数据
当用户发起http请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回,如果是动态请求Nginx则通过fastcgi协议转交给后端的PHP程序处理,具体如下图所示
3.访问流程
1.浏览器输入域名,浏览器会拿着域名取DNS服务器解析
2.DNS服务器会将域名解析成IP
3.浏览器会去与IP对应服务器建立TCP\IP连接
4.连接建立完成,会向服务器发起请求,请求nginx
5.nginx会判断请求是动态的还是静态的
location \.jpg$ {
root /code;
}
location \.php$ {
fastcgi_pass 127.0.0.1:9000;
.. . .. .
}
6.如果是静态请求,nginx去code目录获取,直接返回
7.如果是动态请求,nginx会通过fastcgi协议连接PHP服务的php-fpm管理进程
8.php-fpm管理进程会下发工作给 wrapper工作进程
9.wrapper工作进程判断是不是简单的php内容
10.如果只是php内容则使用php解析器解析后直接返回
11.如果还需要读取数据库,wrapper工作进程会去数据库读取数据,再返回数据
12.数据流转过程:
1)请求:浏览器 > 负载均衡 > nginx > php-fpm > wrapper > mysql
2)响应:mysql > wrapper > php-fpm > nginx > 负载均衡 > 浏览器
四、LNMP架构搭建
1.搭建nginx
1)配置官方源
[ root@web01 ~]
[ nginx-stable]
name= nginx stable repo
baseurl= http://nginx.org/packages/centos/7/$basearch /
gpgcheck= 1
enabled= 1
gpgkey= https://nginx.org/keys/nginx_signing.key
module_hotfixes= true
2)安装nginx
[ root@web01 ~]
3)配置nginx
[ root@web01 ~]
user www;
4)创建用户
[ root@web01 ~]
[ root@web01 ~]
5)启动nginx
[ root@web01 ~]
[ root@web01 ~]
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[ root@web01 ~]
root 9953 1 0 11:17 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 9954 9953 0 11:17 ? 00:00:00 nginx: worker process
2.安装PHP
1)安装方式一(不推荐)
[ root@nginx ~]
[ root@nginx ~]
[ php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
[ root@nginx ~]
2)安装方式二(推荐)
1.上传包
[ root@web01 ~]
[ root@web01 ~]
-rw-r--r--. 1 root root 19889622 Nov 22 15:52 php.tar.gz
2.解压包
[ root@web01 ~]
3.本地安装php的rpm包
[ root@web01 ~]
3)配置php
[ root@web01 ~]
user = www
group = www
4)启动服务
[ root@web01 ~]
[ root@web01 ~]
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
5)验证启动
[ root@web01 ~]
root 10195 1 0 11:29 ? 00:00:00 php-fpm: master process ( /etc/php-fpm.conf)
www 10196 10195 0 11:29 ? 00:00:00 php-fpm: pool www
www 10197 10195 0 11:29 ? 00:00:00 php-fpm: pool www
www 10198 10195 0 11:29 ? 00:00:00 php-fpm: pool www
www 10199 10195 0 11:29 ? 00:00:00 php-fpm: pool www
www 10200 10195 0 11:29 ? 00:00:00 php-fpm: pool www
3.搭建交作业页面
1)配置nginx
[ root@web01 ~]
server {
listen 80;
server_name linux.zuoye.com;
location / {
root /code/zuoye;
index index.html;
}
}
2)创建站点目录
[ root@web01 ~]
3)上传代码
[ root@web01 ~]
[ root@web01 zuoye]
[ root@web01 zuoye]
-rw-r--r--. 1 root root 26995 Nov 22 16:47 kaoshi.zip
[ root@web01 zuoye]
[ root@web01 zuoye]
-rw-r--r--. 1 root root 38772 Apr 27 2018 bg.jpg
-rw-r--r--. 1 root root 2633 May 4 2018 index.html
-rw-r--r--. 1 root root 52 May 10 2018 info.php
-rw-r--r--. 1 root root 1192 Jan 10 2020 upload_file.php
4)修改代码中上传作业位置
[ root@web01 ~]
$wen = "/code/zuoye/upload" ;
5)授权
[ root@web01 zuoye]
6)重启服务
[ root@web01 ~]
7)配置hosts访问测试
10.0.0.7 linux.zuoye.com
http://linux.zuoye.com/
上传代码出错,报错405,因为nginx没办法处理php代码程序
4.关联nginx与php
1)关联语法
Syntax: fastcgi_pass address;
Default: —
Context: location, if in location
Syntax: fastcgi_param parameter value [ if_not_empty] ;
Default: —
Context: http, server, location
Syntax: fastcgi_index name;
Default: —
Context: http, server, location
2)配置
[ root@web01 ~]
server {
listen 80;
server_name linux.zuoye.com;
location / {
root /code/zuoye;
index index.html;
}
location ~* \.php$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME /code/zuoye/$fastcgi_script_name ;
include fastcgi_params;
}
}
3)访问页面测试
1.访问页面
http://linux.zuoye.com/
2.上传图片文件
成功
3.上传一个大文件
失败,413报错,说文件过大
[ root@web01 ~]
http {
.. . .. .
client_max_body_size 200m;
.. . .. .
}
[ root@web01 ~]
[ root@web01 ~]
upload_max_filesize = 200M
post_max_size = 200M
[ root@web01 ~]
4.重新上传文件测试
成功
5.搭建mariadb
1)安装
[ root@web01 ~]
2)启动服务
[ root@web01 ~]
[ root@web01 ~]
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
3)验证启动
[ root@web01 ~]
mysql 11006 10841 1 12:06 ? 00:00:00 /usr/libexec/mysqld --basedir= /usr --datadir= /var/lib/mysql --plugin-dir= /usr/lib64/mysql/plugin --log-error= /var/log/mariadb/mariadb.log --pid-file= /var/run/mariadb/mariadb.pid --socket= /var/lib/mysql/mysql.sock
4)连接
[ root@web01 ~]
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server
Copyright ( c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [ ( none) ] > show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_sche ma |
| test |
+--------------------+
4 rows in set ( 0.00 sec)
5)设置数据库密码
[ root@web01 ~]
[ root@web01 ~]
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.68-MariaDB MariaDB Server
Copyright ( c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [ ( none) ] >
6.测试PHP和mariadb关联
1)编写php测试连接数据库的代码
[ root@web01 ~]
< ?php
$servername = "localhost" ;
$username = "root" ;
$password = "123" ;
// 创建连接
$conn = mysqli_connect( $servername , $username , $password ) ;
// 检测连接
if ( ! $conn ) {
die( "Connection failed: " . mysqli_connect_error( )) ;
}
echo "小哥哥,php可以连接MySQL..." ;
?>
< img style= 'width:100%;height:100%;' src= https://blog.driverzeng.com/zenglaoshi/php_mysql.png>
2)访问测试
http://linux.zuoye.com/test.php
五、搭建wordpress博客
1.上传代码
[ root@web01 code]
[ root@web01 code]
-rw-r--r--. 1 root root 11098483 Sep 12 17:52 wordpress-5.0.3-zh_CN.tar.gz
2.解压代码
[ root@web01 code]
3.授权
[ root@web01 code]
4.配置nginx
[ root@web01 code]
server {
listen 80;
server_name linux.wp.com;
location / {
root /code/wordpress;
index index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name ;
include fastcgi_params;
}
}
5.重启访问
[ root@web01 code]
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[ root@web01 code]
6.访问测试
10.0.0.7 linux.wp.com
http://linux.wp.com/
7.创建数据库
[ root@web01 code]
MariaDB [ ( none) ] > create database wordpress;
Query OK, 1 row affected ( 0.00 sec)
MariaDB [ ( none) ] > show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
+--------------------+
5 rows in set ( 0.00 sec)
8.根据页面提示操作