Linux-Nginx基础

Nginx基础

一、WEB服务

web服务:网站服务,部署并启动了这个服务,你就可以搭建一个网站

web中间件: 等同于web服务

中间件:范围更加广泛,指的负载均衡之后的服务

数据库中间件:数据库缓存,消息队列

二、常见的网站服务

网站服务 说明 官网
Nginx 大部分使用nginx,Engine X http:nginx.org/en/docs/
Tengine 基础Nginx二次开发,淘宝开源,更多内置模块
Openresty 基于Nginx二次开发,加强Lua功能与模块
Tomcat/Jboss/Jetty/Weblogic 运行java环境的,web服务
PHP 运行php环境,需要nginx

三、 极速上手指南

常用的版本:1.22.1

选用稳定版,或上一个稳定版

1.配置yum源

[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

2.检查安装

[root@web01 ~]# rpm -qa nginx
nginx-1.22.1-1.el7.ngx.x86_64
[root@web01 ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.22.1
/usr/share/doc/nginx-1.22.1/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx

3.目录结构

温馨提示:

Nginx不同的安装方法: 目录,文件会有所区别

目录结构 说明
/etc/nginx nginx各种配置目录
/etc/nginx/nginx.conf 主配置文件
/etc/nginx/conf.d/ 自配置文件
/etc/nginx/conf.d/default.conf 默认的子配置文件
/usr/sbin/nginx nginx命令
/usr/share/nginx/html nginx默认的站点目录,网站的根目录
/var/log/nginx nginx日志:访问日志,错误日志,跳转日志
其他目录和文件 说明
/etc/logrotate.d/nginx 日志切割(防止文件过大)
/etc/nginx/mime.types 媒体类型
/etc/nginx/fastcgi_params nginx+php
/etc/nginx/uwsgi_params nginx+python
/usr/lib/systemd/system/nginx.service systemctl配置文件
/var/cache/nginx/ 缓存目录

4.日常启动与管理

[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
#检查服务状态
[root@web01 ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-02-08 12:07:53 CST; 8h ago
     Docs: http://nginx.org/en/docs/
  Process: 7501 ExecReload=/bin/sh -c /bin/kill -s HUP $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)
 Main PID: 1875 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1875 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─7505 nginx: worker process

Feb 08 12:07:53 web01 systemd[1]: Starting nginx - high performance web server...
Feb 08 12:07:53 web01 systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory
Feb 08 12:07:53 web01 systemd[1]: Started nginx - high performance web server.
Feb 08 16:43:42 web01 systemd[1]: Reloading nginx - high performance web server.
Feb 08 16:43:42 web01 systemd[1]: Reloaded nginx - high performance web server.
Feb 08 17:10:02 web01 systemd[1]: Reloading nginx - high performance web server.
Feb 08 17:10:02 web01 systemd[1]: Reloaded nginx - high performance web server.
Feb 08 17:18:07 web01 systemd[1]: Reloading nginx - high performance web server.
Feb 08 17:18:07 web01 systemd[1]: Reloaded nginx - high performance web server.

#检查端口与进程
[root@web01 ~]# ss -lnutp |grep nginx
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=7505,fd=6),("nginx",pid=1875,fd=6))
[root@web01 ~]# ps -ef|grep nginx
root       1875      1  0 14:07 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      7505   1875  0 18:25 ?        00:00:00 nginx: worker process
root      10365   9302  0 20:22 pts/0    00:00:00 grep --color=auto nginx

#命令行访问
[root@web01 ~]# curl 10.0.0.7
[root@web01 ~]# curl -v 10.0.0.7

四、配置文件

1.主配置文件详解

[root@web01 ~]# cat /etc/nginx/nginx.conf 
#指定nginx所属用户(虚拟用户)
user  nginx;
#工具人进程数量  处理用户请求的进程,当前数量是自动
worker_processes  auto;
#nginx错误日志位置
error_log  /var/log/nginx/error.log notice;
#pid文件
pid        /var/run/nginx.pid;


events {
#工具人进程可以处理多少个连接(每个工具人进程最大的连接数)
    worker_connections  1024;
}


http {
	#引用的媒体类型
    include       /etc/nginx/mime.types;
    #默认的媒体类型
    default_type  application/octet-stream;
	#指定nginx访问日志格式,名字叫main
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
	#使用main格式,记录访问日志
    access_log  /var/log/nginx/access.log  main;
	#提高nginx性能
    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
	#文件包含或引用,用于在nginx配置文件中调取其他文件,引用nginx子配置文件
    include /etc/nginx/conf.d/*.conf;
}

2.子配置文件

[root@web01 ~]# egrep -v  '^$|#' /etc/nginx/conf.d/default.conf 
server {
	#监听指定端口,一般是80端口
    listen       80;
    #网站的域名
    server_name  localhost;
    #用于匹配用户请求的uri,location /表示默认
    location / {
    	#用于指定站点目录
        root   /usr/share/nginx/html;
        #指定首页文件,只输入域名的时候展示的页面
        index  index.html index.htm;
    }
    #指定出现500 502 503 504错误的时候显示50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
网站中常用必会指令 说明
listen 指定监听端口
server_name 指定域名,多个通过空格分隔
location 匹配请求中的uri(资源地址)
root 指定站点目录(网站的根目录)
index 指定站点的首页文件,用户访问的时候不加上任何文件,展示首页文件
error_log 指定错误状态码与对应的错误页面

五、部署第1个cxk网站

网站要求 说明
域名 cxk.com
站点目录 /app/code/cxk
代码来源 链接:https://pan.baidu.com/s/1uxJZzRyOHr_cOoEfJIZStw
提取码:63ie
--来自百度网盘超级会员V6的分享

1.配置文件

[root@web01 ~]# cat /etc/nginx/conf.d/czk.com.conf 
server{
	listen 80;
	server_name cxk.com;
	root /app/code/cxk;
	location /{
		index index.html;
	
	}
	
}

2.业务代码部署

#创建目录
[root@web01 ~]# mkdir -p /app/code/cxk
#解压代码
[root@web01 ~]# unzip cxk.zip -d /app/code/cxk/

#配置linux或windows hosts解析
#windows:C:\Windows\System32\drivers\etc\hosts
#linux: /etc/hosts
10.0.0.7 cxk.com

六、Nginx处理用户请求流程

1.案例与思考

思考之前搭建的cxk.com网站,使用域名访问或ip访问都是没有问题的.

如果把配置文件名改为 game.cxk.com.conf使用域名或ip访问或怎么样?

结果:

  • 域名访问没有任何问题

  • 使用ip访问则显示静态页面,另外一个网页了

2.详细原理

使用域名访问网站流程:

  1. DNS解析:域名--->IP地址

  2. 连接80端口,tcp3次握手与网站的80端口建立连接

  3. http请求报文:请求方法,URI,HOST等信息

  4. Nginx处理

    • http请求,http区域处理
    • 不同的server{} 区域(子配置文件)处理
      • 端口
      • 域名:用户请求的域名与子配置文件server_name进行匹配
      • 匹配成功,就让对应的子配置文件进行处理
      • 根据自配置文件的root,location规则,index进行处理查找文件
      • 把找到的文件发回给用户
  5. http响应报文

    • 状态码:200 ok
    • server信息
    • 其他信息
    • 文件内容
  6. 客户收到文件内容,浏览器解析,进行展示

    补充:

    ip访问或不存在的域名

    • 看看是否有默认的server处理 listen 80 default_server;
      • 如果有则返回对应的server{}
      • 如果没有则按照配置文件的顺序进行选择第一个处理

image

七、虚拟主机

虚拟主机:相当于是1个网站,在nginx中通过server{}区域实现

nginx中虚拟主机有不同的类型

1.概述与分类

虚拟主机的分类 说明 应用场景
基于域名的虚拟主机 不同的域名访问不同的站点 生产环境最常用的
基于端口的虚拟主机 不同的端口访问不同的站点 保护,设置特殊端口,一般1024以上
基于ip的虚拟主机 不同的ip访问不同的站点 保护,用户只能通过某个ip连接进来,用来限制网站只能通过指定的ip进行访问

2.基于域名的虚拟主机

不同的域名访问不同的主机

#创建bird.com网站,站点目录/app/code/bird
[root@web01 /etc/nginx]# cat conf.d/bird.com.conf 
server{
	listen 80;
	server_name bird.com;
	root /app/code/bird;
	error_log  /var/log/nginx/bird.com-error.log notice;
	access_log  /var/log/nginx/bird.com-access.log  main;

	location / {
		index index.html;
	}
}



curl -H Host:bird.com   http:10.0.0.7
#1.完成后不创建站点目录进行访问看看报什么错误.
提示404
#2.创建站点目录后再访问看看报什么错误.
提示403
#3.创建首页文件和内容,访问看看显示什么
访问正常


-H:修改请求头里面的内容

3. 基于端口的虚拟主机

不同的端口访问不同的网站

#修改bird.com端口为81
server{
	listen 81;
	server_name bird.com;
	root /app/code/bird;
	error_log  /var/log/nginx/bird.com-error.log notice;
	access_log  /var/log/nginx/bird.com-access.log  main;

	location / {
		index index.html;
	}
}

4.基于ip的虚拟主机

#buy.com/admin 只能内网访问
[root@web01 /etc/nginx]# cat conf.d/buy.com.conf 
server{
	listen 80;
	server_name buy.com;
	root /app/code/buy;
	location /{
		index index.html;
	}
	location /admin/{
		allow 172.16.1.0/24;
		deny all;	
		index index.html;
	}
}

[root@web01 /etc/nginx]# curl -H Host:buy.com http://10.0.0.7
welcom to buy page!!!!
[root@web01 /etc/nginx]# curl -H Host:buy.com http://10.0.0.7/admin/
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>
[root@web01 /etc/nginx]# curl -H Host:buy.com http://172.16.1.7/admin/
welcom to admin !!!!

#地址的最后没有加/
[root@web01 /etc/nginx]# curl -H Host:buy.com http://172.16.1.7/admin
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>

八、Nginx日志

nginx指令帮助

1.概述

日志 使用建议 定义 使用
错误日志 发生故障的时候可以查看 通过错误级别定义 error_log
访问日志 记录着用户什么时候访问,网站那些页面,客户端信息 通过log_format定义访问日志的格式 access_log

2.错误日志

指定错误日志的位置和错误级别(日志级别)

error_log指令
格式:error_log 文件名 错误日志级别
指定放在哪里:main,http,mail,stream,server,location
错误日志级别:从左到右,越来越粗糙 debug,info,notice,warn,error,crit,alert,emerg

补充:

  • emerg:当系统不稳定时,用于紧急消息
  • alert:生成严重问题的警报消息。
  • crit:用于紧急情况下立即处理。
  • crit: Used for Critical issues for immediately dealing.
  • error:处理页面时,可能会发生错误。
  • warn:用于警告消息
  • notice:您也可以忽略的通知日志。
  • info:有关信息,消息
  • debug:指向用于调试信息的错误位置
#每个虚拟主机指定自己独立的错误日志
#先注释主配置文件的错误日志配置
[root@web01 /etc/nginx]# grep -n error nginx.conf 
5:#error_log  /var/log/nginx/error.log notice;
[root@web01 /etc/nginx/conf.d]# cat czk.com.conf 
server{
	listen 80;
	server_name cxk.com;
	root /app/code/cxk;
	error_log  /var/log/nginx/cxk.com-error.log notice;  #配置错误日志

	location /{
		index index.html;
	
	}
	
}

3.访问日志

log_format指定访问日志的格式
log_format 格式名字 格式
放在哪里: http
[root@web01 /etc/nginx/conf.d]# tail /var/log/nginx/access.log
10.0.0.1 - - [09/Feb/2023:16:21:27 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://buy.com/admin/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
Nginx访问日志格式(nginx内置变量) 说明
$remote_addr 客户端ip地址 10.0.0.1
$remote_user 用户名(空,进行认证用户)
$time_local 时间 09/Feb/2023:16:21:27 +0800
$request 请求报文的起始行 GET /admin/ HTTP/1.1
$status http状态码 403
$body_bytes_sent 响应给客户的文件的大小,
响应报文的主体大小 单位字节
555
$http_referer 从哪里跳转,访问到这个网站的 http://buy.com/admin/
$http_user_agent 客户端代理(浏览器) Mozilla/5.0 (Windows NT 10.0; WOW64)
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/86.0.4240.198 Safari/537.36
$http_x_forwarded_for XFF开头,负载中用,记录用户真实ip
#日志格式的定义
[root@web01 /etc/nginx]# cat nginx.conf 

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

#注释主配置文件
[root@web01 /etc/nginx]# grep access nginx.conf 
#    access_log  /var/log/nginx/access.log  main;

#给每个用户指定自己独立的日志
[root@web01 /etc/nginx/conf.d]# cat czk.com.conf 
server{
	listen 80;
	server_name cxk.com;
	root /app/code/cxk;
	error_log  /var/log/nginx/cxk.com-error.log notice;
	access_log  /var/log/nginx/cxk.com-access.log  main;   #配置访问日志

	location /{
		index index.html;
	}		
}

#改完记得重启nginx服务
访问日志其他选项 说明
访问日志进行压缩 gzip需要通过zcat,zless,zgrep进行查看
进行缓存 buffer=32k 先把日志写入到内存中,定期写入到磁盘
定义刷新时间 flush=10s
[root@web01 /etc/nginx/conf.d]# cat czk.com.conf 
server{
	listen 80;
	server_name cxk.com;
	root /app/code/cxk;
	error_log  /var/log/nginx/cxk.com-error.log notice;
	access_log  /var/log/nginx/cxk.com-access.log  main gzip buffer=32k  flush=10s;   #配置访问日志

	location /{
		index index.html;
	}		
}

九、Location规则

1.location概述

nginx的location规则:

  • 在nginx用于匹配用户请求中的uri,nginx对用户请求中的uri进行判断
  • 如果用户请求的uri是xxx,则做xxx

URI VS URL

URL: https://nginx.org/en/docs/

URI: /en/docs

2.案例:搭建大型直播购物网站

域名:buy.cn
站点目录:/app/code/buy/ 首页文件index.html  
后台管理页面:/app/code/buy/admin/index.html
要求后台只能内网访问:172.16.1.0/24网段.

[root@web01 /etc/nginx/conf.d]# cat buy.com.conf 
server{
	listen 80;
	server_name buy.com;
	root /app/code/buy;
	location /{
		index index.html;
	}
	location /admin/{
		allow 172.16.1.0/24;
		deny all;	
		index index.html;
	}
}

[root@web01 ~]# mkdir -p /app/code/buy/admin/
[root@web01 ~]# cat /app/code/buy/index.html 
welcom to buy page!!!!
[root@web01 ~]# cat /app/code/buy/admin/index.html 
welcom to admin !!!!

[root@web01 ~]# curl -H Host:buy.com http://172.16.1.7/admin/
welcom to admin !!!!
[root@web01 ~]# curl -H Host:buy.com http://10.0.0.7/admin/
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>

3.案例 :搭建bird小鸟飞飞网站

#案例06:搭建bird小鸟飞飞网站,给网站加速,设置缓存,网站中html,js,css结尾的文件缓存1天,图片缓存1小时.

[root@web01 ~]# cat /etc/nginx/conf.d/bird.com.conf 
server{
	listen 80;
	server_name bird.com;
	root /app/code/bird;
	error_log  /var/log/nginx/bird.com-error.log notice;
	access_log  /var/log/nginx/bird.com-access.log  main;

	location / {
		index index.html;
	}
	location ~* \.(html|js|css)$ {
		expires max; 
		#max最大,10年
	}
	location ~* \.(jpg|jpeg|png|gif|bmp)$ {
		expires 1h;
	}
}

补充:

浏览器打开缓存的方法:

打开Cache-Control

image

4. location 规则小结

location规则 说明
location / 默认规则,保底,location规则在进行匹配的时候,其他的规则都匹配失败了,这时候匹配默认的规则
loaction /image/ {} 用于匹配请求的uri
location ~ \.(jpg|jpeg)$ {} 支持正则,区分大小写
location ~* \.(jpg|jpeg)$ {} 支持正则,不区分大小写
location ^~ /image/ 不支持正则,仅仅匹配普通字符,很少使用,优先匹配
location =/50x.html 不支持正则,精确匹配
location @名字{} 命名的location一般用于return /error_log跳转

location匹配的时候优先级

优先级 符号
1 =
2 ^~
3 ~ ~*
4 /image/
5 /

十、案例:一个高清的下载站

1.搭建要求

  1. 浏览器打开后,显示目录结构(如上图)。
  2. 增加svip功能。
  3. 增加统计功能,统计nginx服务的状态,访问情况。

2.基本要求

域名:v.cn

站点目录:/app/code/v/

自动索引功能(列表站点目录的内容),首页文件不存在

autoindex模块
autoindex on:开启目录索引功能(显示站点目录下的文件的列表,首页不存在)
autoindex_localtime on: 显示本地时间
autoindex_exact_size off:是否显示精确的文件的大小,off表示以人类可读形式显示大小
[root@web01 /etc/nginx/conf.d]# cat v.cn.conf 
server{
	listen 80;
	server_name v.cn;
	root /app/code/v;
	
	error_log /var/log/nginx/v.cn-error.log notice;
	access_log /var/log/nginx/v.cn-access.log main;
	charset utf8;
	
	autoindex on;								
	autoindex_localtime on;
	autoindex_exact_size off;
	location / {
		index index.html;
	}
}


[root@web01 ~]# ll /app/code/v/
total 6728
-rw-r--r-- 1 root root       0 Feb 10 09:24 test01.mp4
-rw-r--r-- 1 root root       0 Feb 10 09:24 test02.mp4
-rw-r--r-- 1 root root       0 Feb 10 09:24 test03.mp4
-rw-r--r-- 1 root root       0 Feb 10 09:24 test04.mp4
-rw-r--r-- 1 root root       0 Feb 10 09:24 test05.mp4
-rw-r--r-- 1 root root       0 Feb 10 09:24 test06.mp4
-rw-r--r-- 1 root root       0 Feb 10 09:24 test07.mp4
-rw-r--r-- 1 root root       0 Feb 10 09:24 test08.mp4
-rw-r--r-- 1 root root       0 Feb 10 09:24 test09.mp4
-rw-r--r-- 1 root root 6888896 Feb 10 09:36 test10.mp4

3.增加vip认证功能

增加一个svip目录,里面创建文件.

增加认证功能(用户名和密码).

auth_basic模块

[root@web01 /etc/nginx/conf.d]# cat v.cn.conf 
server{
	listen 80;
	server_name v.cn;
	root /app/code/v;
	
	error_log /var/log/nginx/v.cn-error.log notice;
	access_log /var/log/nginx/v.cn-access.log main;
	charset utf8;
	
	autoindex on;
	autoindex_localtime on;
	autoindex_exact_size off;
	location / {
		index index.html;
	}
	location /vip/ {
		#输出提示,根据不同的浏览器,可能不显示
		auth_basic "请输入密码:";
		#指定用户名,密码文件
		auth_basic_user_file /etc/nginx/user;
	}
}
[root@web01 ~]# yum install -y httpd-tools
#-b 增加用户     #-c 创建文件
[root@web01 ~]# htpasswd -bc /etc/nginx/user admin  admin
[root@web01 ~]# chmod 600 /etc/nginx/user
[root@web01 ~]# chown nginx.nginx /etc/nginx/user
[root@web01 ~]# mkdir /app/code/v/vip/
[root@web01 ~]# touch /app/code/v/vip/vip{01..10}.mp4

#点击vip目录就需要密码了,密码会进行缓存,下次就不会要输入了

4. 增加统计功能

#stub_status模块/指令,显示nginx服务的状态,用户访问的状态.
[root@web01 ~]# cat /etc/nginx/conf.d/v.cn.conf 
server{
	listen 80;
	server_name v.cn;
	root /app/code/v;
	
	error_log /var/log/nginx/v.cn-error.log notice;
	access_log /var/log/nginx/v.cn-access.log main;
	charset utf8;
	
	autoindex on;
	autoindex_localtime on;
	autoindex_exact_size off;
	location / {
		index index.html;
	}
	location /vip/ {
		auth_basic "请输入密码:";
		auth_basic_user_file /etc/nginx/user;
	}
	location /status/ {
		stub_status;
	}
}

Active connections: 1 
server accepts handled requests
 64 64 397 
Reading: 0 Writing: 1 Waiting: 0 
ab 压力测试功能
ab  -n 9999999  -c 3    -H Host:v.cn    
http:10.0.0.7/
nginx状态模块中的指标说明 说明
Active connections 当前已经建立的连接数和等待数量 体现:nginx并发
server accepts 已经接收到客户端的连接总数
handled 服务端已经处理的连接
requests 客户端发出请求的总数
reading 正在读取的请求头连接数量
writing 正在进行的响应的连接数量
waiting 排队数量

十一、动态网站架构LNMP

开发书写了一个php代码发给我们部署下。

  1. 测试能否直接部署到nginx中?
  2. 需要什么服务,是什么原理?
  3. 准备服务并测试。
  4. 部署代码。

1.概述

网站架构 说明与特点 性能 说明
静态网站 网站仅仅包含:html,css样式,js脚本,图片,视频
nginx直接处理静态资源
只要是web服务器即可:nginx,可以承受较高的访问量
不支持动态的功能,注册,评论,静态网站功单一
浏览器端解析(客户端解析),服务端仅仅负责发送
动态网站 动态网站一般是通过开发语言实现:java,PHP,python,Golang 动态资源页面需要,服务器进行处理,处理后把结果返回给用户 动态请求需要服务端进行处理与解析,把结果给用户

如何区分静态资源和动态资源

通过url初步判断,网站的类型

url中包含&或?一般是动态网站

2. 常见的动态网站的架构

PHP LNMP,LAMP,WNMP/WAMP(P--->PHP,W--->Windows,A--->Apache)
JAVA LNMT(Tomcat,Jetty,Weblogic,Jboss)
Python LNMP(Python,uwsgi)
Golang LNMG(Golang)

3. LNMP架构细节

image

3.1 数据库

db01数据库:10.0.0.51/172.16.1.51

[root@db01 ~]# yum -y install mariadb-server
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable mariadb

#数据库安全配置,仅仅刚安装的时候运行,仅首次运行即可,用于设置root密码,清理用户和临时数据库
[root@db01 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y		#删除数据库中的匿名用户(没有用户名的用户,容易导致安全问题)			
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y  #是否禁止root远程登录

 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y  #是否删除test测试用的数据库
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y	#是否更新权限信息表
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@db01 ~]# mysql -uroot -p1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 190
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)]> 

数据库基本概念

库:database 相当于1个execl文件

表:table 相当于是1个execl文件的1个sheet

字段: 列 某列的名字,字段名字

记录:行 某个人的数据

#查看系统所有的数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

#查看库里的表
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)

#显示mysql库的user表的user列和host列 (列字段),查看数据库的用户信息
MariaDB [(none)]> select user,host from mysql.user;
+------+------------+
| user | host       |
+------+------------+
| root | 127.0.0.1  |
| test | 172.16.1.% |
| root | ::1        |
| root | localhost  |
| test | localhost  |
+------+------------+
5 rows in set (0.00 sec)

#\G  显示的时候好看些
select  * from test.wp_posts \G;

#创建数据库
MariaDB [(none)]> create database test;

#创建用户,进行授权
grant 所有权限   on 数据库.* to  '用户名'@'白名单'
MariaDB [(none)]> grant all on test.* to 'test'@'localhost' identified by '1';
MariaDB [(none)]> grant all on test.* to 'test'@'172.16.1.%' identified by '1';

#查看用户授权
show grants for  'test'@'172.16.1.%'; 

#删除数据库
MariaDB [(none)]> drop database test;

#删除用户
drop user 'test'@'172.16.1.%'; 

#刷新用户权限表
flush privileges;

#测试远程连接
mysql -utest -p1  -h 172.16.1.51

数据库白名单说明:

白名单表示用户可以从哪里进行登录与使用数据库.

一般localhost表示只能在数据库本地使用.

可以通过172.16.1.% 进行授权局域网访问.

其他局域网机器可以访问数据库.

只给个%表示所有人可以访问(不安全). %不包含localhost.

3.2 php

#yum安装
/etc/yum.repos.d/php.repo
[webtatic-php]
name = php Repository
baseurl = http:useast.repo.webtatic.com/yum/el7/x86_64
enabled = 1
gpgcheck = 0

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

#rpm安装
[root@web01 ~]# yum -y localinstall *.rpm

#启动服务nginx,php-fpm
[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm

#检查
[root@web01 ~]# ss -lntup |grep php 
tcp    LISTEN     0      128    127.0.0.1:9000                  *:*                   users:(("php-fpm",pid=34844,fd=9),("php-fpm",pid=34635,fd=9),("php-fpm",pid=34634,fd=9),("php-fpm",pid=34632,fd=9),("php-fpm",pid=34568,fd=9),("php-fpm",pid=34266,fd=9),("php-fpm",pid=33441,fd=9),("php-fpm",pid=33440,fd=9),("php-fpm",pid=33439,fd=9),("php-fpm",pid=33438,fd=9),("php-fpm",pid=33437,fd=9),("php-fpm",pid=33436,fd=7))

[root@web01 ~]# ps -ef |grep php 
root      33436      1  0 17:44 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
nginx     33437  33436  0 17:44 ?        00:00:03 php-fpm: pool www
nginx     33438  33436  0 17:44 ?        00:00:03 php-fpm: pool www
nginx     33439  33436  0 17:44 ?        00:00:01 php-fpm: pool www
nginx     33440  33436  0 17:44 ?        00:00:02 php-fpm: pool www
nginx     33441  33436  0 17:44 ?        00:00:01 php-fpm: pool www
nginx     34266  33436  0 18:23 ?        00:00:02 php-fpm: pool www
nginx     34568  33436  0 18:38 ?        00:00:01 php-fpm: pool www
nginx     34632  33436  0 18:42 ?        00:00:00 php-fpm: pool www
nginx     34634  33436  0 18:42 ?        00:00:00 php-fpm: pool www
nginx     34635  33436  0 18:42 ?        00:00:00 php-fpm: pool www
nginx     34844  33436  0 18:51 ?        00:00:00 php-fpm: pool www
root      37055  36060  0 20:18 pts/0    00:00:00 grep --color=auto php
#统一nginx与php用户
[root@web01 ~]# egrep -n '^user|^group' /etc/php-fpm.d/www.conf
8:user = nginx
10:group = nginx

3.3 nginx配置

#nginx配置文件
[root@web01 ~]# cat /etc/nginx/conf.d/blog.cn.conf 
server{
          listen 80;
	  server_name blog.cn;
	  root /app/code/blog;
	  error_log /var/log/nginx/blog-error.log notice ; 
	  access_log /var/log/nginx/blog-access.log main;
	  
	  location / {
		index  index.php;
	  }
#这个location的目的是把请求的php文件的绝对路径找出来交给php处理
	  location ~*  \.php$ {
		#传递给php
		fastcgi_pass   127.0.0.1:9000;
		fastcgi_index  index.php;
		#下面内容需要修改
		#fastcgi_param  SCRIPT_FILENAME  /app/code/blog$fastcgi_script_name;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		include        fastcgi_params;


	  }
}

补充:

fastcgi_param:内置变量需要存放php文件的绝对路径

$document_root: 站点目录

$fastcgi_script_name: 请求中的uri

3.4 部署代码与设置权限

[root@web01 ~]# unzip wordpress-6.1.1.zip -d /app/code/
[root@web01 ~]# cd /app/code/
[root@web01 ~]# mv wordpress/  blog/
[root@web01 ~]# chown -R nginx.nginx blog/

3.5 故障

  1. 权限问题

image

  1. 部署wordpress的时候页面是英文问题解决方案。
  • ​ 检查nginx子配置文件去掉charset utf8;这个选项,wordpress不需要加。
  • 先通过英文的安装wordpress,然后进入后台修改语言为简体中文即可。

3.6 排错流程

  • Linux:防火墙
  • Nginx:检查配置,处理流程检查,测试nginx
  • PHP:处理动态请求
  • Mysql: 测试PHP+数据库
  1. 检查Nginx是否正常运行

    检查端口
    检查进程
    检查配置
    站点目录下创建test.html  ,curl访问
    
  2. nginx是否把动态页面转发php,php是否解析

    站点目录下创建testinfo.php
    [root@web01 /app/code/blog]# cat testinfo.php 
    <?php
    	phpinfo();
    ?>
    
    #注:测试完记得删除,要不容易泄露系统信息
    

image

  1. 检查php连接数据库是否正常

    #推荐方式
    mysql -u用户   -p密码   -h 数据库的ip
    show databases; 
    
    
    #书写php页面
    [root@web01 /app/code/blog]# cat testmysql.php 
    <?php
    //数据库地址
    $db_host='172.16.1.51';
    //数据库用户名
    $db_user='test';
    $db_pass='1';
    //数据库名字
    $db_name='test';
    
    $link_id=mysqli_connect($db_host,$db_user,$db_pass,$db_name);
    if($link_id){
      echo "mysql successful! 数据库名字:$db_name 数据库用户:$db_user 数据库密码:$db_pass" ;
    }else{
    	echo "connection failed!\n" ;
    }
    ?>
    
    [root@web01 /app/code/blog]# curl -H Host:blog.cn http://10.0.0.7/testmysql.php
    mysql successful! 数据库名字:test 数据库用户:test 数据库密码:1
    
    #一般会在conf自配置文件中加上限制
    location ~* (testinfo|testmysq).php$ {
       allow 10.0.0.0/24;
       allow 172.16.1.0/24;
       deny all;
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
    }
    
    

a

本文作者:缘之世界

本文链接:https://www.cnblogs.com/world-of-yuan/p/17142648.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @ 2023-02-22 21:03  wh459086748  阅读(377)  评论(2编辑  收藏  举报
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起