苏州好的做网站的公司,信息技术教学网站开发,网站建设需要申请服务器吗,可直接进入网站的代码目录 写在前面Nginx03案例1 模拟视频下载网站自动索引autoindex基础认证auth_basic模块状态stub_status模块模块小结 案例2 动态网站#xff08;部署php代码#xff09;概述常见的动态网站的架构LNMP架构流程数据库Mariadb安装安全配置基本操作 PHP安装php修改配置文件 Nginx… 目录 写在前面Nginx03案例1 模拟视频下载网站自动索引autoindex基础认证auth_basic模块状态stub_status模块模块小结 案例2 动态网站部署php代码概述常见的动态网站的架构LNMP架构流程数据库Mariadb安装安全配置基本操作 PHP安装php修改配置文件 NginxWordpress编辑Nginx安装wordpress访问主页 写在前面 这是Nginx第三篇内容为Nginx自动索引模块、基础认证模块、状态模块、动态资源介绍、LNMP介绍与实验等。 上篇笔记 Nginx02-Nginx虚拟主机介绍、日志介绍、Location规则介绍 Nginx03
案例1 模拟视频下载网站 需求 浏览器打开显示目录结构autoindex模块部分文件夹需登录认证功能auth_basic模块统计nginx服务访问状态stub_status模块域名v.test.com目录/app/code/v ngx模块是众多ngx指令的集合.
自动索引autoindex
首页不存在时会使用autoindex模块
autoindex on 开启目录索引功能(显示站点目录下的文件的列表,首页文件不存在.)autoindex_localtime on 显示本地时间.autoindex_exact_size off是否显示精确的文件的大小. off表示以人类可读形式显示大小 实现浏览器打开显示目录结构文件模拟即可 # 设置子配置文件
[rootfront conf.d]# cat v.test.com.conf
server {listen 80;server_name v.test.com;root /app/code/v;error_log /var/log/nginx/v.test.com-error.log notice;access_log /var/log/nginx/v.test.com-access.log main;autoindex on; #开启目录索引功能首页文件不存在时显示站点目录下的文件的列表charset utf8; # 中文字符autoindex_localtime on; # 系统本地时间autoindex_exact_size off; # 人类可读的大小显示location / {index index.html;}
}# 新建目录
[rootfront conf.d]# mkdir -p /app/code/v
[rootfront conf.d]# touch /app/code/v/test{1..10}.mp4# 检查语法并重启服务
[rootfront 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
[rootfront conf.d]# systemctl reload nginx
基础认证auth_basic模块
nginx中最基础的认证模块
auth_basic “请输入密码:”; #输出提示,根据不同浏览器,可能不显示.auth_basic_user_file conf/htpasswd; #指定用户名,密码文件 登录认证 登陆的用户有auth目录需要登录才能访问直接退出会401报错 # 修改子配置文件
[rootfront conf.d]# cat v.test.com.conf
server {listen 80;server_name v.test.com;root /app/code/v;error_log /var/log/nginx/v.test.com-error.log notice;access_log /var/log/nginx/v.test.com-access.log main;autoindex on;charset utf8;autoindex_localtime on;autoindex_exact_size off;location / {index index.html;}location /auth/ { # 新增locationauth_basic 提示-请输入密码; #认证提示auth_basic_user_file /etc/nginx/user; #认证密码文件路径}
}# 新建目录
[rootfront conf.d]# mkdir -p /app/code/v/auth/
[rootfront conf.d]# touch /app/code/v/auth/auth{1..5}.mp5# 新建密码文件
## 安装httpd-tools需要用到htpasswd工具
[rootfront conf.d]# yum install -y httpd-tools## 创建加密的密码文件并新增用户
[rootfront conf.d]# htpasswd -bc /etc/nginx/user test test #-b不使用交互模式-c新增一个密码文件(若有内容会清空)
Adding password for user test
[rootfront conf.d]# htpasswd -b /etc/nginx/user tassel tassel
Adding password for user tassel
[rootfront conf.d]# cat /etc/nginx/user
test:$apr1$DYOgLaoY$GKNgriUjduo/r7s5ous4v.
tassel:$apr1$iQF6rDr0$.Pwe8qmNOi6jxUMD4deI4.## 修改权限
[rootfront conf.d]# chmod 600 /etc/nginx/user
[rootfront conf.d]# chown nginx.nginx /etc/nginx/user# 语法检查并重启
[rootfront 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
[rootfront conf.d]# systemctl reload nginx状态stub_status模块 显示nginx当前状态信息。显示nginx服务的状态,用户访问的状态. stub_status; #显示nginx # 编写子配置文件
[rootfront conf.d]# cat v.test.com.conf
server {listen 80;server_name v.test.com;root /app/code/v;error_log /var/log/nginx/v.test.com-error.log notice;access_log /var/log/nginx/v.test.com-access.log main;autoindex on;charset utf8;autoindex_localtime on;autoindex_exact_size off;location / {index index.html;}location /auth/ {auth_basic 提示-请输入密码;auth_basic_user_file /etc/nginx/user;}location /status/ { # 新增此部分注意此部分是uri而不是一个文件夹所以可以不用新建该路径文件夹stub_status; #启用统计功能注意显示所有站点而非仅当前虚拟主机}
}# 语法检查并重启
[rootfront 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
[rootfront conf.d]# systemctl reload nginx# 内容解析
## 压测
[rootfront conf.d]# ab -n 999999 -c 3 -H Host:v.test.com http://192.168.100.148/
# ab是Apache Bench的缩写它是一个用于测试Web服务器性能的工具。ab可以发送多个HTTP请求到服务器并报告服务器的响应时间、传输速度等性能指标。命令行中的参数含义如下
# -n 999999指定总共发送的请求次数为999999次。
# -c 3指定并发连接数为3即同时有3个请求发送到服务器。
# -H Host:v.test.com添加一个HTTP请求头设置Host头的值为v.test.com。这通常用于测试虚拟主机的性能或者当服务器根据Host头来处理请求时。
# http://192.168.100.148/指定要测试的服务器的URL。这里是一个本地网络中的IP地址表示ab将向该地址发送请求。## status下内容
Active connections: 3
server accepts handled requests61576 61576 61579
Reading: 0 Writing: 1 Waiting: 2
# Active connections: 当前活动状态的连接数。这是当前Nginx正在处理的连接数量包括正在读取请求、正在写入响应或正在等待客户端发送下一个请求的连接。
# server accepts handled requests下的三个参数
# accepts: 总共接受的连接数。这是自从Nginx启动以来接受的连接总数。
# handled: 总共处理的连接数。通常这个数字和accepts相同因为Nginx能够处理所有接受的连接。
# requests: 总共处理的请求数。这是自从Nginx启动以来处理的总请求数量。这个数字可能会比accepts大因为同一个连接可能会发送多个请求例如在一个Keep-Alive连接中。
# Reading: 当前正在读取请求头的连接数。这是Nginx正在从客户端读取请求头的连接数量。
# Writing: 当前正在写入响应的连接数。这是Nginx正在向客户端发送响应的连接数量。
# Waiting: 当前等待请求的空闲连接数。这是打开着但是目前没有读取或写入活动的连接数量。这些连接可能处于Keep-Alive状态等待客户端发送下一个请求。
状态模块中的指标
指标说明Active connections当前已经建立的连接数est和等待数量体现Nginx并发能力server accepts已经接收到客户端的连接总数handled服务端已经处理的连接requests客户端发出请求的总数reading正在读取的请求头连接数量writing正在进行的响应的连接数量waiting排队数量反映排队情况
模块小结
nginx模块就是nginx指令的集合
模块模块中的核心指令目录索引模块autoindex on;认证功能模块auth_basic_user_file;访问控制模块allow,deny状态模块stub_statusnginx 核心模块root,location,error_log,server_name,listennginx 日志模块access_log,log_formaterror_log
案例2 动态网站部署php代码
概述
网站架构说明与特点性能1句话说明静态网站网站仅仅包含HTML、CSS样式、JS脚本、图片、视频等静态资源。只需要web服务器即可nginx可以承受较高的访问量。不支持动态的功能如注册、评论功能单一。高浏览器端解析(客户端解析),服务端仅仅负责发送.动态网站动态网站一般是通过开发语言实现Java、PHP、Python、Golang等。动态资源页面需要服务器进行处理nginxphp/tomcat数据库。一般动态请求需要服务端进行处理与解析,把结果给用户.
区分静态/动态资源url中包含或?一般都是动态资源
常见的动态网站的架构 L: Linux, N: Nginx, M: Mysql, A: Apache, W: Windows PHPLNMP(LEMP), LAMP, WNMP/WAMPJava: LNMT(Tomcat,Jetty,Weblogic,Jboss)Python: LNMP(Python,uwsgi)Golang: LNMG(Golang)C/C
LNMP
架构流程
用户通过http协议发送请求Nginx分流动/静态资源静态自己处理动态请求丢给PHP - Nginx通过fastcgi协议把动态请求丢给PHP - PHP处理动态请求若需要数据则连接数据库Mysql
数据库Mariadb
安装
yum install -y mariadb-server
# mariadb-server 服务端
# mariadb 客户端systemctl start mariadb
systemctl enable mariadb# 验证
[rootdb01 ~]# ss -tunlp | grep mysql
tcp LISTEN 0 80 *:3306 *:* users:((mysqld,pid14433,fd21))
[rootdb01 ~]# ps -ef | grep mysql
mysql 14433 1 0 20:35 ? 00:00:00 /usr/libexec/mysqld --basedir/usr
root 14974 14928 0 20:39 pts/0 00:00:00 grep --colorauto mysql安全配置
mysql_secure_installation #仅仅刚安装的时候运行.仅首次运行即可.
#用于设置root密码,清理用户和清理临时库.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.You already have a root password set, so you can safely answer n.Change the root password? [Y/n] y #输入y
New password: #输入密码
Re-enter new password: #再次输入密码
Password updated successfully!
Reloading privilege tables..... Success!Remove anonymous users? [Y/n] y #y删除数据库中的匿名用户... Success!Disallow root login remotely? [Y/n] y #y禁止root远程登陆... Success!Remove test database and access to it? [Y/n] y #y删除test测试用数据库Reload privilege tables now? [Y/n] y #y更新权限信息表... Success!
基本操作
进入数据库
mysql -uroot -p密码 [-h IP]
#不要有空格也可以不输入密码等交互式时输入数据库基本概念请自行了解库-表-字段(属性/投影/列)-记录(元组/行) 查看
# 查看数据库
MariaDB [(none)] show databases;
--------------------
| Database |
--------------------
| information_schema |
| mysql |
| performance_schema |
--------------------
3 rows in set (0.001 sec)# 查看表
MariaDB [(none)] show tables from mysql;
---------------------------
| Tables_in_mysql |
---------------------------
| column_stats |
| columns_priv |
| db |
| event |
...#查看某些字段(列)筛选记录(行)
## 筛选mysql数据库中user表的user和host列的记录
## -G行的内容以列显示
MariaDB [(none)] select user,host from mysql.user ;
-----------------
| user | host |
-----------------
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
-----------------
3 rows in set (0.000 sec)
创建
# 创建数据库
MariaDB [(none)] create database test;
Query OK, 1 row affected (0.000 sec)MariaDB [(none)] show databases;
--------------------
| Database |
--------------------
| information_schema |
| mysql |
| performance_schema |
| test |
--------------------
4 rows in set (0.001 sec)# 创建用户进行授权
MariaDB [(none)] grant all on test.* to test% identified by test;
Query OK, 0 rows affected (0.000 sec)
## grant 权限 on 数据库.表 to 用户登录白名单 identified by 密码;
## 一般localhost表示只能在数据库本地使用.
## 可以通过192.168.100.% 进行授权局域网访问. 其他局域网机器可以访问数据库.
## 只给个 % 表示所有人可以访问(不安全). %不包含localhost.
### ALL: 所有可用的权限
### CREATE: 创建库、表和索引
### LOCK_TABLES: 锁定表
### ALTER: 修改表
### DELETE: 删除表
### UPDATE: 更新数据
### INSERT: 插入表或列
### SELECT: 检索表或列的数据
### CREATE_VIEW: 创建视图
### SHOW_DATABASES: 列出数据库
### DROP: 删除库、表和视图删除
# 删除数据库
MariaDB [(none)] drop database test;
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)] show databases;
--------------------
| Database |
--------------------
| information_schema |
| mysql |
| performance_schema |
--------------------
3 rows in set (0.000 sec)# 删除用户revoke删除授权
MariaDB [(none)] drop user test%;
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)] select user,host from mysql.user;
-----------------
| user | host |
-----------------
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
-----------------
3 rows in set (0.001 sec)PHP 选择php 7.x 安装php
# 安装php
## 若是centos7可以输入以下yum源和安装命令
[rootfront conf.d]# cat /etc/yum.repos.d/php.repo
[webtatic-php]
name php Repository
baseurl http://us-east.repo.webtatic.com/yum/el7/x86_64
enabled 1
gpgcheck 0[rootfront conf.d]# yum install -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## centos8可以直接使用默认的yum源安装
[rootfront conf.d]# yum install -y php*
[rootfront conf.d]# php -v
PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologieswith Zend OPcache v7.2.24, Copyright (c) 1999-2018, by Zend Technologies# 启动php
[rootfront conf.d]# systemctl enable php-fpm --now
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
修改配置文件 php默认使用的时apache但要求用nginx所以修改配置文件使其使用nginx yum安装的php默认listen监听的是socket要修改成127.0.0.1:9000 [rootfront conf.d]# egrep -n ^user|^group /etc/php-fpm.d/www.conf
24:user apache
26:group apache
[rootfront conf.d]# egrep -n ^listen /etc/php-fpm.d/www.conf
38:listen /run/php-fpm/www.sock# 修改配置文件
[rootfront conf.d]# sed -i s/user apache/user nginx/g /etc/php-fpm.d/www.conf
[rootfront conf.d]# sed -i s/group apache/group nginx/g /etc/php-fpm.d/www.conf
[rootfront conf.d]# egrep -n ^user|^group /etc/php-fpm.d/www.conf
24:user nginx
26:group nginx[rootfront conf.d]# sed -i s/listen /run/php-fpm/www.sock/listen 127.0.0.1:9000/g /etc/php-fpm.d/www.conf
sed: -e expression #1, char 18: unknown option to s
[rootfront conf.d]# sed -i s|listen /run/php-fpm/www.sock|listen 127.0.0.1:9000|g /etc/php-fpm.d/www.conf
[rootfront conf.d]# egrep -n ^listen /etc/php-fpm.d/www.conf
38:listen 127.0.0.1:9000NginxWordpress
编辑Nginx
# 编写子配置文件
[rootfront conf.d]# cat blog.test.com.conf
server {listen 80;server_name blog.test.com;root /app/code/blog;error_log /var/log/nginx/blog.test.com-error.log notice;access_log /var/log/nginx/blog.test.com-access.log main;location / {index index.php;}location ~* \.php$ {# foward to phpfastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;# 修改以下fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}
}# 安装wordpress 官方下载链接https://wordpress.org/download/releases/#branch-61 我选用的是6.1.1 # 解压并移动到对应站点目录
unzip wordpress-6.1.1.zip
mv wordpress/* /app/code/blog/# 修改权限
[rootfront conf.d]# chown -R nginx.nginx /app/code/blog访问主页 wordpress的教程网上特别多这里不赘述只是作为动态资源的演示