
一、狀態(tài)模塊:
curl -H Host:status.oldboy.com 172.16.1.7
官網(wǎng)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.狀態(tài)模塊詳解
Active connections: 1
server accepts handled requests
22 22 22
Reading: 0 Writing: 1 Waiting: 0
Active connections: 1 當(dāng)前的連接數(shù)量(已建立連接)
server accepts:22 服務(wù)器接收到的請(qǐng)求數(shù)量
server handled: 22 服務(wù)器接收處理的請(qǐng)求數(shù)量
server request:20 用戶一共向服務(wù)器發(fā)出多少請(qǐng)求Reading:0 當(dāng)前nginx正在讀取的用戶請(qǐng)求頭的數(shù)量
Writing:1 當(dāng)前nginx正在響應(yīng)用戶請(qǐng)求的數(shù)量
Waiting:1 當(dāng)前等待被nginx處理的 請(qǐng)求數(shù)量
二、權(quán)限控制
allow和deny
簡(jiǎn)單驗(yàn)證
【簡(jiǎn)單驗(yàn)證】htpasswd

安裝軟件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;
}
創(chuàng)建密碼文件,修改權(quán)限600 屬主屬組為nginx
[09:14 root@web01 /etc/nginx]# htpasswd -b -c /etc/nginx/htpasswd oldboy oldboy
[09:15 root@web01 /etc/nginx]# ll htpasswd
-rw-r--r-- 1 root root 48 Jun 6 09:15 htpasswd
[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
瀏覽器查看
輸入賬號(hào)密碼都為oldboy:
三、Location規(guī)則
先看圖:




常見規(guī)則:
將狀態(tài)碼取出來:
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.配置模塊內(nèi)容:
修改前請(qǐng)備份
return 200 "location ~* .(gif|jpg|jpeg) \n";這一行最后不要加$符號(hào),不識(shí)別正則符號(hào)。
修改后重啟 檢查語法
[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 /usr/share/nginx/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.測(cè)試一下
= 精確
^~ 不匹配正則
~* 不區(qū)分大小寫正則匹配
/documents 匹配路徑
/ 默認(rèn)
[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)
#驗(yàn)證/documents與~* 的優(yōu)先級(jí)
[root@web01 /etc/nginx/conf.d]# curl 10.0.0.7/documents/oldboy.jpg
location ~* \.(gif|jpg|jpeg)
#驗(yàn)證 ~* 與 ^~ 優(yōu)先級(jí)
[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動(dòng)態(tài)頁面
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服務(wù)
2.查看進(jìn)程號(hào)ss -lntup |grep mysql
3.查看進(jìn)程 ps -ef |grep mysql
4.然后執(zhí)行mysql命令進(jìn)入
※①查看系統(tǒng)中所有數(shù)據(jù)庫的命令
show databases;
結(jié)尾要加分號(hào)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
MariaDB [(none)]>
※查看數(shù)據(jù)庫表(行)
MariaDB [(none)]> show tables from mysql;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
24 rows in set (0.00 sec)
※②查看系統(tǒng)中所有的用戶信息
查看user表中的user和host字段
select user,host from mysql.user;
顯示出user和host兩個(gè)字段 在mysql數(shù)據(jù)庫中 的user表中進(jìn)行查找。
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)]>
MariaDB [(none)]> #select * from wordpress.wp_posts\G #查看所有表讓內(nèi)容對(duì)齊
MariaDB [(none)]> #select * from wordpress.wp_posts limit 1 \G #只查看一列讓內(nèi)容對(duì)齊
MariaDB [(none)]> #select user,host from mysql.user; #查看user表中的user和host字段(查看系統(tǒng)中所有的用戶信息)
MariaDB [(none)]> #use mysql; #切換到數(shù)據(jù)庫表
MariaDB [mysql]> #select user,host from user; #進(jìn)入表后就不用加絕對(duì)路徑mysql,直接查看相對(duì)路徑的user和host字段
※③創(chuàng)建一個(gè)新的數(shù)據(jù)庫wordpress
create database wordpress;
最后為數(shù)據(jù)庫名字
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)]>
※④創(chuàng)建用戶 并給權(quán)限,密碼,允許誰登錄
grant all on wordpress.* to 'wordpress'@'172.16.1.%' identified by '123456';
grant all on wordpress.* to 'wordpress'@'localhost' identified by '123456';
>grant 創(chuàng)建用戶并給權(quán)限
>all 所有權(quán)限
>on 指定的是
>wordpress.* wordpress數(shù)據(jù)庫的.所有表
>
>'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)
※⑤刪除(謹(jǐn)慎操作)
刪除表: drop database lcx;
刪除用戶: drop user lcx@'172.16.1.%';
刪除空用戶: drop user ''@'localhost';
※⑥刷新權(quán)限信息
flush privileges;
修改用戶信息之后需要更新權(quán)限信息
※⑦ctrl c退出mysql,重新進(jìn)入新創(chuàng)建的數(shù)據(jù)庫用戶
mysql -uwordpress -p
-p后直接輸入密碼也可以
-u與-p后不能加空格
-u:連接MySQL服務(wù)器的用戶名
-p:連接MySQL服務(wù)器的密碼
※⑧數(shù)據(jù)庫備份
- 打包 + 定時(shí)任務(wù) +rsync
- 備份:
備份全部表:
mysqldump -uroot -p -A >/root/all.sql
備份wordpress表:
mysqldump -uwordpress -p123456 -A >/root/wordpress.sql
備份并壓縮
mysqldump -uroot -p -A|gzip >/root/all-gzip.sql.gz
- 將備份的數(shù)據(jù)庫恢復(fù): mysql -uroot -p </root/all.sql
3.搭建PHP環(huán)境
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 服務(wù)
systemctl restart php-fpm.service
4.查看端口號(hào)與進(jìn)程(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站點(diǎn)目錄下,添加以php結(jié)尾的文件
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();
?>
瀏覽器查看網(wǎng)頁http://10.0.0.7/info.php
如果訪問沒有顯示出來,主要原因就是Nginx沒有將請(qǐng)求拋給PHP,主要去看nginx的配置
首先保證02-blog.conf配置文件是在第一位,把其他的配置模塊先壓縮或注釋掉。
第二種就是配置的內(nèi)容是否有錯(cuò)誤,是否生效
PHP與MySQL之間:

6.在blog站點(diǎn)目錄下創(chuàng)建第二個(gè)文件mysqli.php
[root@web01 /blog]# vim mysqli.php
<?php
$servername = "localhost";
$username = "wordpress";
$password = "123456";
// 創(chuàng)建連接
$conn = mysqli_connect($servername, $username, $password);
// 檢測(cè)連接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "php連接MySQL數(shù)據(jù)庫成功";
?>
然后用瀏覽器訪問網(wǎng)站
image.png
7.搭建wordpress博客
官網(wǎng):https://cn.wordpress.org/
wordpress開源博客壓縮包鏈接:
https://pan.baidu.com/s/1KOI3FZV8VY22rxD731pp0g 提取碼: 7naq
image.png
1.解壓后講wordpress下的所有內(nèi)容移動(dòng)到blog站點(diǎn)目錄下
修改權(quán)限blog站點(diǎn)目錄的屬主屬組為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.瀏覽器輸入網(wǎng)址http://10.0.0.7
填寫用戶名密碼
輸入信息
登錄賬戶密碼
9.登錄后我們回到虛擬機(jī)查看數(shù)據(jù)庫
show tables from wordpress;
已經(jīng)創(chuàng)建好了數(shù)據(jù)庫中的表
10.最后我們的博客就搭建好了,可以進(jìn)入博客寫文章了
博客的內(nèi)容都放到了數(shù)據(jù)庫里
我們可以在數(shù)據(jù)庫中查看一下博客網(wǎng)站的內(nèi)容
- MariaDB [(none)]> select * from wordpress.wp_posts\G
\G讓內(nèi)容對(duì)齊- MariaDB [(none)]> select * from wordpress.wp_posts limit 1 \G 只顯示一列
MariaDB [(none)]> select * from wordpress.wp_posts\G
11.鼠標(biāo)右鍵復(fù)制圖片地址然后查看圖片地址
再去虛擬機(jī)中查看一下此路徑下有什么
有重復(fù)的圖片是因?yàn)樽詣?dòng)給裁剪為各種尺寸的圖片了
總結(jié):
1.nginx location 認(rèn)證
2.LNMP原理
3.部署LNMP過程
作業(yè):
將搭建LNMP服務(wù),每個(gè)服務(wù)都是單獨(dú)的一臺(tái)服務(wù)器
1.把數(shù)據(jù)庫服務(wù)器單獨(dú)放一臺(tái)服務(wù)器上
2.把存儲(chǔ)服務(wù)器掛載到另外一臺(tái)服務(wù)器上
未完待續(xù)...mysqli.php


























