第十一周-day48-状态模块、location规则与LNMP服务博客搭建

在这里插入图片描述

#一、状态模块:

 curl -H Host:status.oldboy.com 172.16.1.7

官网http://nginx.org/en/docs/http/ngx_http_stub_status_module.html#stub_status

1.重启刷新:
[08:46 root@web01 ~]# systemctl reload nginx
[08:46 root@web01 ~]# curl -H Host:status.oldboy.com 172.16.1.7
Active connections: 1 
server accepts handled requests
 22 22 22 
Reading: 0 Writing: 1 Waiting: 0 
[08:46 root@web01 ~]# systemctl restart nginx
[08:46 root@web01 ~]# curl -H Host:status.oldboy.com 172.16.1.7
Active connections: 1 
server accepts handled requests
 1 1 1 
Reading: 0 Writing: 1 Waiting: 0 

※2.状态模块详解

Active connections: 1 
server accepts handled requests
 22 22 22 
Reading: 0 Writing: 1 Waiting: 0 

Active connections: 1 当前的连接数量(已建立连接)

server accepts:22 服务器接收到的请求数量
server handled: 22 服务器接收处理的请求数量
server request:20 用户一共向服务器发出多少请求

Reading:0 当前nginx正在读取的用户请求头的数量
Writing:1 当前nginx正在响应用户请求的数量
Waiting:1 当前等待被nginx处理的 请求数量

二、权限控制

allow和deny
简单验证

【简单验证】

安装软件httpd-tools

[09:00 root@web01 /etc/nginx]# rpm -qa httpd-tools
httpd-tools-2.4.6-89.el7.centos.x86_64

2.修改status模块

auth_basic “Auth access Blog Input your Passwd!”;
auth_basic_user_file /etc/nginx/htpasswd;

[09:09 root@web01 /etc/nginx]# vim conf.d/status.conf 
server {
        listen 80;
        server_name  status.oldboy.com;
        stub_status;
        access_log off;
        auth_basic "Auth access Blog Input your Passwd!";
        auth_basic_user_file /etc/nginx/htpasswd;

#       allow 172.16.1.0/24;
#       deny all;
}

创建密码文件,修改权限600 属主属组为nginx

[09:14 root@web01 /etc/nginx]# htpasswd -b -c /etc/nginx/auth_conf oldboyedu oldboyedu
[09:15 root@web01 /etc/nginx]# ll auth_conf 
-rw-r--r-- 1 root root 48 Jun  6 09:15 auth_conf
[09:24 root@web01 /etc/nginx]# chmod 600 htpasswd 
[09:25 root@web01 /etc/nginx]# chown nginx.nginx htpasswd 
[09:25 root@web01 /etc/nginx]# ll htpasswd 
-rw------- 1 nginx nginx 45 Jun  6 09:19 htpasswd

浏览器查看

输入账号密码都为oldboy:

三、Location规则

先看图:




###常见规则:


将状态码取出来:

1.第一种方法:

需要将多余的定向到空
curl -I 10.0.0.7 2>/dev/null|awk ‘NR==1{print $2}’

[09:55 root@web01 /etc/nginx]# curl -I 10.0.0.7
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Thu, 06 Jun 2019 01:55:13 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Wed, 05 Jun 2019 01:00:48 GMT
Connection: keep-alive
ETag: "5cf71440-f"
Accept-Ranges: bytes

[09:55 root@web01 /etc/nginx]# curl -I 10.0.0.7|awk 'NR==1{print $2}'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0    15    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
200
[09:55 root@web01 /etc/nginx]# curl -I 10.0.0.7 2>/dev/null|awk 'NR==1{print $2}'
200

2.第二种方法:

curl -sw “%{http_code}\n” -o /dev/null baidu.com

[09:59 root@web01 /etc/nginx]# curl -sw "%{http_code}\n" -o /dev/null baidu.com
200

安装需要的软件包:

rpm压缩包链接: https://pan.baidu.com/s/1jmr-c3i1EUzRtkH9IGEv9Q 提取码: ge4j

yum remove php-mysql-5.4 php php-fpm php-common
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb

yum install -y mariadb-server 

3.配置模块内容:

修改前请备份
return 200 “location ~* .(gif|jpg|jpeg) \n”;这一行最后不要加$符号,不识别正则符号。
修改后重启 检查语法

[root@web01 /etc/nginx/conf.d]# cp 01-www.conf 01-www.conf.bak
[root@web01 /etc/nginx/conf.d]# vim 01-www.conf
server {
    listen       80;
    server_name  www.oldboy.com;
    root   html/www;
    location / {
       return 200  "location / \n";
    }
    location = / {
        return 200 "location = \n";
    }
    location /documents/ {
        return 200 "location /documents/ \n";
    }
    location ^~ /images/ {
        return 200 "location ^~ /images/ \n";
    }
    location ~* \.(gif|jpg|jpeg)$ {
        return 200 "location ~* \.(gif|jpg|jpeg) \n";
    }
    access_log off;
}
[10:53 root@web01 /etc/nginx/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
[10:54 root@web01 /etc/nginx/conf.d]# systemctl reload nginx 

4.测试一下

=     精确
^~    不匹配正则
~*    不区分大小写正则匹配
/documents    匹配路径
/      默认

[root@web01 /etc/nginx/conf.d]# curl 10.0.0.7
location = 

[root@web01 /etc/nginx/conf.d]# curl 10.0.0.7/
location = 

[root@web01 /etc/nginx/conf.d]# curl 10.0.0.7/oldboy.html
location / 

[root@web01 /etc/nginx/conf.d]# curl 10.0.0.7/documents/alex.txt
location /documents/ 

[root@web01 /etc/nginx/conf.d]# curl 10.0.0.7/lidao/documents/alex.txt
location / 

[root@web01 /etc/nginx/conf.d]# curl 10.0.0.7/oldboy.jpg
location ~* \.(gif|jpg|jpeg) 

#验证/documents与~* 的优先级
[root@web01 /etc/nginx/conf.d]# curl 10.0.0.7/documents/oldboy.jpg
location ~* \.(gif|jpg|jpeg) 

#验证 ~*^~ 优先级
[root@web01 /etc/nginx/conf.d]# curl 10.0.0.7/images/oldboy.jpg
location ^~ /images/ 


※四、LNMP博客搭建


1.配置/etc/nginx/conf.d/下的 02-blog.conf 文件

1.提前备份02-blog.conf
2.添加php动态页面
3.重启并检查

[12:01 root@web01 /etc/nginx/conf.d]# cp 02-blog.conf 02-blog.conf.bak
[12:01 root@web01 /etc/nginx/conf.d]# vim 02-blog.conf
server   {
    listen       80;
    server_name  blog.oldboy.com;
    access_log  /var/log/nginx/access_blog.log  main;
    root   /usr/share/nginx/html/blog;
    location / {
    index  index.php index.html index.htm;
    }
   location ~* \.(php|php5)$ {
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       include        fastcgi_params;
   }

}
[12:07 root@web01 /etc/nginx/conf.d]# systemctl reload nginx
[12:07 root@web01 /etc/nginx/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

2.配置MySQL

1.提前安装MySQL,重启MySQL的mariadb服务
2.查看进程号ss -lntup |grep mysql
3.查看进程 ps -ef |grep mysql
4.然后执行mysql命令进入

[12:10 root@web01 /etc/nginx/conf.d]# systemctl start mariadb.service 
[12:10 root@web01 /etc/nginx/conf.d]#  ss -lntup |grep mysql 
tcp    LISTEN     0      50        *:3306                  *:*                   users:(("mysqld",pid=8793,fd=13))
[12:10 root@web01 /etc/nginx/conf.d]#  ps -ef |grep mysql 
mysql      8631      1  0 12:09 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql      8793   8631 11 12:09 ?        00:00:02 /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
root       8883   7472  0 12:10 pts/1    00:00:00 grep --color=auto mysql
[12:10 root@web01 /etc/nginx/conf.d]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-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;

结尾要加分号

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)

MariaDB [(none)]> 

※②查看系统中所有的用户信息

select user,host from mysql.user;
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
|      | localhost |
| root | localhost |
|      | web01     |
| root | web01     |
+------+-----------+
6 rows in set (0.00 sec)

MariaDB [(none)]> 

※③创建一个新的数据库wordpress

create database wordpress;

最后为数据库名字

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)

MariaDB [(none)]> 

※④创建用户 并给权限,密码,允许谁登录

grant all on wordpress.* to ‘wordpress’@‘172.16.1.%’ identified by ‘123456’;
grant all on wordpress.* to ‘wordpress’@‘localhost’ identified by ‘123456’;

>grant   创建用户并给权限
>all     所有权限
>on      指定的是
>wordpress.*    wordpress数据库的.所有表
>
>'wordpress'@'localhost'     ‘用户名'@'ip.登录' (%==*)
>identified by '123456';      密码是 '123456';
MariaDB [(none)]> grant all on wordpress.* to 'wordpress'@'172.16.1.%' identified by '123456';
Query OK, 0 rows affected (0.04 sec)

MariaDB [(none)]> grant all on wordpress.* to 'wordpress'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select user,host from mysql.user;
+-----------+------------+
| user      | host       |
+-----------+------------+
| root      | 127.0.0.1  |
| wordpress | 172.16.1.% |
| root      | ::1        |
|           | localhost  |
| root      | localhost  |
| wordpress | localhost  |
|           | web01      |
| root      | web01      |
+-----------+------------+
8 rows in set (0.00 sec)

※⑤ctrl c退出mysql,重新进入新创建的数据库用户

mysql -uwordpress -p

-p后直接输入密码也可以
-u与-p后不能加空格

-u:连接MySQL服务器的用户名
-p:连接MySQL服务器的密码

[12:33 root@web01 /etc/nginx/conf.d]# mysql -uwordpress -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.60-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)]> 

3.搭建PHP环境

1.修改/etc/php-fpm.d/www.conf中的user与group为nginx

2.检查 用户与用户组是否为nginx

egrep -n ‘^user|^group’ /etc/php-fpm.d/www.conf

3.重启 php-fpm.service 服务

systemctl restart php-fpm.service

4.查看端口号与进程(9000)

ss -lntup|grep 9000
ps -ef |grep php

[12:45 root@web01 /etc/nginx/conf.d]# egrep -n '^user|^group' /etc/php-fpm.d/www.conf
8:user = nginx
10:group = nginx
[12:45 root@web01 /etc/nginx/conf.d]#  systemctl restart php-fpm.service 
[12:45 root@web01 /etc/nginx/conf.d]# ss -lntup|grep 9000
tcp    LISTEN     0      128    127.0.0.1:9000                  *:*                   users:(("php-fpm",pid=9013,fd=9),("php-fpm",pid=9012,fd=9),("php-fpm",pid=9011,fd=9),("php-fpm",pid=9010,fd=9),("php-fpm",pid=9009,fd=9),("php-fpm",pid=9008,fd=7))
[12:45 root@web01 /etc/nginx/conf.d]# ps -ef |grep php 
root       9008      1  1 12:45 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
nginx      9009   9008  0 12:45 ?        00:00:00 php-fpm: pool www
nginx      9010   9008  0 12:45 ?        00:00:00 php-fpm: pool www
nginx      9011   9008  0 12:45 ?        00:00:00 php-fpm: pool www
nginx      9012   9008  0 12:45 ?        00:00:00 php-fpm: pool www
nginx      9013   9008  0 12:45 ?        00:00:00 php-fpm: pool www
root       9017   7472  0 12:45 pts/1    00:00:00 grep --color=auto php

Nginx与PHP之间:

5. 切换到blog站点目录下,添加以php结尾的文件

vim info.php
注意php的语法格式

[12:50 root@web01 /etc/nginx/conf.d]# cd /usr/share/nginx/html/blog/
[12:50 root@web01 /usr/share/nginx/html/blog]# vim info.php
<?php
phpinfo();
?>

浏览器查看网页http://10.0.0.7/info.php

如果访问没有显示出来,主要原因就是Nginx没有将请求抛给PHP,主要去看nginx的配置

首先保证02-blog.conf配置文件是在第一位,把其他的配置模块先压缩或注释掉。

第二种就是配置的内容是否有错误,是否生效

6.在blog站点目录下创建第二个文件mysqli.php

[root@web01 /blog]# vim mysqli.php 
<?php
$servername = "localhost";
$username = "wordpress";
$password = "123456";

// 创建连接
$conn = mysqli_connect($servername, $username, $password);

// 检测连接
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "php连接MySQL数据库成功";
?>

###然后用浏览器访问网站
http://10.0.0.7/mysqli.php
image.png

7.搭建wordpress博客

官网:https://cn.wordpress.org/
wordpress开源博客压缩包链接:
https://pan.baidu.com/s/1KOI3FZV8VY22rxD731pp0g 提取码: 7naq
image.png

1.解压后讲wordpress下的所有内容移动到blog站点目录下

修改权限blog站点目录的属主属组为nginx

[root@web01 ~]#  chown  -R nginx.nginx /usr/share/nginx/html/blog/
[root@web01 ~]# ll -d /usr/share/nginx/html/blog/
drwxr-xr-x 5 nginx nginx 4096 Jun  6 21:03 /usr/share/nginx/html/blog/

8.浏览器输入网址http://10.0.0.7


填写用户名密码

输入信息

登录账户密码

9.登录后我们回到虚拟机查看数据库

show tables from wordpress;
已经创建好了数据库中的表

10.最后我们的博客就搭建好了,可以进入博客写文章了

博客的内容都放到了数据库里

我们可以在数据库中查看一下博客网站的内容

MariaDB [(none)]> select * from wordpress.wp_posts\G
\G让内容对齐

MariaDB [(none)]> select * from wordpress.wp_posts\G

11.鼠标右键复制图片地址然后查看图片地址

再去虚拟机中查看一下此路径下有什么

有重复的图片是因为自动给裁剪为各种尺寸的图片了

总结:

1.nginx location 认证
2.LNMP原理
3.部署LNMP过程

作业:

将搭建LNMP服务,每个服务都是单独的一台服务器
1.把数据库服务器单独放一台服务器上
2.把存储服务器(nfs)挂载到另外一台服务器上

未完待续…

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值