网站服务应用实践(nginx,php配置)
Day-25-网站服务应用实践(web)
01 网站服务概念介绍
1)日常浏览网站是如何看到页面
第一个步骤:输入域名信息进行域名解析 -DNS解析原理
第二个步骤:和网站服务器建立TCP三次握手过程
第三个步骤:客户端向服务端发送请求报文 (HTTP请求报文)
第四个步骤:服务端向客户端发送响应报文 (HTTP响应报文--HTML代码信息)
第五个步骤:浏览器解析识别HTML代码信息,进行渲染过程
...
第三个步骤:客户端向服务端发送请求报文 (HTTP请求报文)
第四个步骤:服务端向客户端发送响应报文 (HTTP响应报文--HTML代码信息)
第五个步骤:浏览器解析识别HTML代码信息,进行渲染过程
...
第六个步骤:和网站服务器断开TCP四次挥手过程
2)网站请求与响应过程:(暴力破解)
HTTP请求报文:
4个部分组成:
① 请求的起始行:1行
请求方法 URL 协议版本
· 请求方法:
get: 从服务端获取指定数据信息 (图片 视频 音频 代码文件-字符/排版-HTML 样式-CSS 特效-JS)
post: 客户端要将信息提交到服务端(注册 登录 评论)
put: 将文档信息进行上传提交 (本地有些资源需要上传) -- 利用-上传漏洞
HEAD: 请求响应HTTP首部信息,主要用于测试使用
DELETE: 请求服务器删除Request-URI所表示的资源 用户主观行为
MOVE: 请求服务器将指定的页面移至另一个网络地址。 管理员行为
· URL(Uniform Resource Locator,统一资源定位器) 信息:访问网站域名信息 == URL www.baidu.com *****
· URI(Uniform Resource Identifier,统一资源标识符) 信息:具体访问资源信息 == URI *****
· 协议版本:应用层协议 *****
HTTP 协议(Hyper Text Transfer Protocol 超文本传输协议 - 实现网页显示)
HTTPS协议 传输超文本数据安全性(secure)
协议版本选择:HTTP-0.9(使用TCP短连接使用通讯) HTTP-1.1(使用TCP长连接使用通讯) HTTP-2.0(支持TCP连接并发处理能力)
② 请求头部信息:
总结:具体请求数据资源功能描述信息(是否压缩 是否缓存 是否加密...)
③ 空行
④ 请求主体内容:
post 方法应用时:会含有请求主体信息 (注册 登录--表单操作)
get 方法应用时:不会含有请求主体信息
HTTP响应报文:
4个部分组成:
① 响应的起始行
协议信息 响应状态码
200(ok) -- 正常做响应
PS:服务端可以正常做出响应
301:永久调整(域名信息永久变化 -- www.360buy.com -- www.jd.com)
302:临时跳转(非法网站应用更多 -- 色情网站 -- www.abc.com -- www.bcd.com -- www.hgf.com)
304:走缓存了(主要用于缓存图片 视频等信息,实现快速加载页面)
PS:响应信息做了跳转处理或者缓存处理
404 -- 客户端请求资源无法找到
403 -- 客户端请求信息错误
401 -- 客户端请求没有认证通过
PS:客户端请求出现问题
500:服务端出现异常,反馈的状态(服务器负载过高 响应超时 后端网络出现问题)
PS:服务端出现异常(程序-运维 业务-开发 安全)
② 响应头部信息
总结:具体响应数据资源功能描述信息(是否压缩 是否缓存 是否加密...)
③ 空行
④ 响应主体内容:
响应HTML代码信息
3)利用什么服务可以实现网站访问
web服务程序:apache nginx tomcat php jboss IIS ... 处理客户端的HTTP请求信息
分类:
处理静态请求服务:nginx apache (不需要访问后端存储服务的请求) 看网站页面
处理动态请求服务:php tomcat (需要后端存储服务的请求) 和网站有交互过程
======================================================================================
02 网站服务部署过程
静态服务程序部署:nginx
安装方式:
方式一:yum安装nginx程序 (关注)
第一个步骤:优化系统环境
① 优化selinux安全程序--关闭
临时关闭
[root@xiaoQ ~ 16:22]# getenforce
[root@xiaoQ ~ 16:38]# setenforce 0
永久关闭(需要重启系统)
[root@xiaoQ ~ 16:38]# vim /etc/selinux/config
7 SELINUX=disabled
② 优化系统防火墙服务-关闭
[root@xiaoQ ~ 16:39]# systemctl stop firewalld
[root@xiaoQ ~ 16:40]# systemctl disable firewalld
③ 优化系统时间信息
[root@xiaoQ ~ 16:40]# ntpdate ntp1.aliyun.com
27 Mar 16:41:51 ntpdate[10207]: step time server 120.25.115.20 offset -0.821817 sec
④ 优化系统yum下载源信息
cd /etc/yum.repos.d/
mkdir -p /etc/yum.repos.d/backup
find /etc/yum.repos.d/ -type f -exec mv {} /etc/yum.repos.d/backup ;
ll /etc/yum.repos.d/
total 4
drwxr-xr-x 2 root root 4096 Mar 27 16:44 backup
进行下载源优化:
Base
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
epel
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
nginx
vim /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
第二个步骤:下载nginx软件程序
yum install -y nginx
第三个步骤:运行启动程序
systemctl start nginx
systemctl enable nginx
ps -ef|grep nginx
root 10283 1 0 16:59 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf 主进程
nginx 10284 10283 0 16:59 ? 00:00:00 nginx: worker process 子进程
nginx 10285 10283 0 16:59 ? 00:00:00 nginx: worker process
nginx 10286 10283 0 16:59 ? 00:00:00 nginx: worker process
nginx 10287 10283 0 16:59 ? 00:00:00 nginx: worker process
netstat -lntup|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10283/nginx: master
测试访问程序:
浏览器访问:http:\虚拟主机地址 --- 看到nginx程序欢迎页面
方式二:编译安装nginx程序 (了解)
第一个步骤:安装nginx的依赖包(pcre-devel openssl-devel)---假设不进行安装
yum install -y pcre-devel openssl-devel
第二个步骤:下载nginx软件---1.10.2 复制链接地址(统一位置进行下载)
mkdir -p /root/tools
cd /root/tools
wget -q http://nginx.org/download/nginx-1.22.1.tar.gz
第三个步骤:编译安装软件步骤
解压要编译安装的软件(解压软件---配置(./configure)---做菜(编译 make)---上菜(安装 make install))
tar xf nginx-1.22.1.tar.gz
cd nginx-1.22.1
useradd -s /sbin/nologin -M www <--- 创建web服务程序www用户
./configure --prefix=/application/nginx-1.22.1 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
(编译参数说明后续补充说明)
make
make install
ln -s /application/nginx-1.22.1 /application/nginx <--- 安装完成一个软件要做一个软链接
第四个步骤:启动nginx软件程序进行测试
vim /etc/profile
export PATH="/applicaton/nginx/sbin/:$PATH"
source /etc/profile
nginx
浏览器访问 http:\虚拟主机地址
至此软件编译安装完毕;
动态服务程序部署:php
方式一:通过网络下载安装程序-在线安装(说明)
yum install epel-release -y
wget https://mirror.webtatic.com/yum/el7/webtatic-release.rpm --no-check-certificate
rpm -Uvh webtatic-release.rpm
rpm -e $(rpm -qa|grep php)
yum install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd -y
yum install php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd -y
yum install php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb -y
方式二:通过本地下载安装程序-离线安装
上传离线程序包- php.tar.gz
tar xvPf php.tar.gz
yum install -y libXpm-3.5.12-2.el7_9.x86_64 (可以先不操作)
yum localinstall -y find /var/cache/yum/ -name "*rpm"
如果不加参数P: 会在当前目录中 生成一个var子目录
如果加上参数P:会将压缩包里面的数据,解压缩到原本目录中 (/var/cache/yum/)
启动运行php服务程序
systemctl start php-fpm
[root@xiaoQ ~ 17:37]# ps -ef|grep php
root 10495 1 0 17:37 ? 00:00:00 php-fpm: master process (/etc/php-fpm.conf)
apache 10496 10495 0 17:37 ? 00:00:00 php-fpm: pool www
apache 10497 10495 0 17:37 ? 00:00:00 php-fpm: pool www
apache 10498 10495 0 17:37 ? 00:00:00 php-fpm: pool www
apache 10499 10495 0 17:37 ? 00:00:00 php-fpm: pool www
apache 10500 10495 0 17:37 ? 00:00:00 php-fpm: pool www
netstat -lntup|grep php
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 10495/php-fpm: mast
======================================================================================
03 网站服务应用配置
1)查看nginx程序安装后生成数据文件信息
/etc/nginx -- nginx程序主目录
/etc/nginx/nginx.conf -- nginx程序主配置文件
/etc/nginx/conf.d/ -- 在此目录中,会存储nginx程序虚拟主机(不同网站)配置文件
/etc/nginx/mime.types -- 媒体资源类型文件(定义nginx能处理哪些请求资源)
/var/log/nginx -- 日志信息存储路径(访问日志-分析 错误日志-分析错误)
/usr/share/nginx/html -- 网站服务站点目录(用户访问资源统一存储路径)
2)编写nginx程序配置文件
主配置文件
ps -ef|grep nginx 查看有关nginx的进程信息
sysctemctl restart nginx 重启nginx服务
ps -auf|grep nginx
[root@xiaoQ ~ 17:54]# grep -Ev "^$|#" /etc/nginx/nginx.conf
user nginx;
-- 定义nginx服务进程管理用户
worker_processes auto;
-- 定义nginx服务子进程数量(属于优化配置,可以承载更多的并发访问)
error_log /var/log/nginx/error.log notice;
-- 定义错误日志文件存储路径
pid /var/run/nginx.pid;
-- 定义服务pid文件存储路径
events {
worker_connections 1024;
-- 设置默认每个子进程可以处理并发连接数量(属于优化配置,可以承载更多的并发访问)
}
http {
include /etc/nginx/mime.types;
-- 加载媒体资源类型文件
default_type application/octet-stream;
-- 默认处理数据类型
log_format oldboy '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
-- 定义访问日志格式信息
$remote_addr: 记录客户端地址信息(知道访问者区域)
$remote_user: 当网站需要认证访问时,记录登录用户信息
[$time_local]: 记录用户访问网站时间
$request: 记录用户请求头信息
$status: 响应状态码信息
$body_bytes_sent 响应数据内容大小信息
$http_referer 从哪访问过来的(了解)(网站盗链问题???)
$http_user_agent 显示用户访问网站客户端信息 识别客户端不同 显示不同的页面
user_agent -- iphone/安卓 www.baidu.com -- m.baidu.com
user_agent -- chorm/IE www.baidu.com
access_log /var/log/nginx/access.log oldboy;
sendfile on;
keepalive_timeout 65;
-- tcp长连接超时时间
include /etc/nginx/conf.d/*.conf;
}
修改user 为www
useradd www -M -s /sbin/nologin
vim /etc/nginx/nginx.conf
[root@eden ~]# vim /etc/nginx/nginx.conf
user www;
worker_processes 8;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
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"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
~
~
~
~
~
~
~
systemctl restat nginx 重启nginx服务
虚拟主机配置文件
grep -Ev /etc/nginx/conf.d/default.conf
vim /etc/nginx/conf.d/default.conf --- 虚拟主机模板配置文件
server { --- 编写不同的虚拟主机信息 www bbs
listen 80;
-- 指定监听的端口或地址信息
server_name localhost;
-- 指定网站域名信息
#access_log /var/log/nginx/www.access.log main;
-- 可以将不同网站的访问日志分类保存
#root /usr/share/nginx/html;
root /html;
-- 指定网站资源存储的站点目录
index index.html index.htm ;
-- 默认加载网站资源信息(首页文件)
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
-- 优雅显示错误信息
}
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
-- 和动态请求有关的配置信息
}
netstat -lntup|grep nginx 查看网络状态
配置文件编写规则:
① 配置文件由指令与指令块构成
② 每条指令以;分号结尾,指令与参数间以空格符号分隔
③ 指令块以{}大括号将多条指令组织在一起
④ 使用#符号添加注释,提高可读性
⑤ include语句允许组合多个配置文件以提升可维护性
⑥ 使用$符号使用变量
⑦ 部分指令的参数支持正则表达式(内容信息匹配)
编写www虚拟主机配置文件:
server {
listen 10.0.0.100:80;
server_name www.oldboy.com;
root /html/www;
index index.html index.htm;
}
编写完nginx配置文件需要进行检查核实:
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
PS:只有检查配置通过后,nginx服务才能正常启动或重启
mkdir -p /html/www
chown -R www.www /html/www
[root@xiaoQ www 09:41]# cat index.html
| 01 | 国产视频 |
| 02 | 欧美视频 |
| 03 | 日韩视频 |
访问网站显示403错误:
- 访问服务地址信息被阻止
- 访问首页文件不存在
- 建议不要使用微软自带浏览器进行访问
3)多个虚拟主机配置文件编写实践
方式一:`行多个虚拟主机的访问
编写多个虚拟主机配置文件信息
cat www.conf
server {
listen 10.0.0.100:80;
server_name www.oldboy.com;
root /html/www;
index index.html index.htm;
}
cat bbs.conf
server {
listen 10.0.0.100:80;
server_name bbs.oldboy.com;
root /html/bbs;
index index.html index.htm;
}
cat blog.conf
server {
listen 10.0.0.100:80;
server_name blog.oldboy.com;
root /html/blog;
index index.html index.htm;
}
mkdir -p /html/{www,bbs,blog}
chown -R www.www /html/
echo "www.oldboy.com" >/html/www/index.html
echo "bbs.oldboy.com" >/html/bbs/index.html
echo "blog.oldboy.com" >/html/blog/index.html
systemctl restart nginx
进行本地域名解析:
10.0.0.100 www.oldboy.com bbs.oldboy.com blog.oldboy.com
方式二:基于端口进行多个虚拟主机的访问
[root@xiaoQ conf.d 10:18]# cat *.conf
server {
listen 10.0.0.100:82;
server_name bbs.oldboy.com;
root /html/bbs;
index index.html index.htm;
}
server {
listen 10.0.0.100:83;
server_name blog.oldboy.com;
root /html/blog;
index index.html index.htm;
}
server {
listen 10.0.0.100:81;
server_name www.oldboy.com;
root /html/www;
index index.html index.htm;
}
systemctl restart nginx
方式三:基于地址进行多个虚拟主机的访问
内部管理系统 不希望外网访问
cat www.conf
server {
listen 10.0.0.100:80; -- 地址外网 可以被外网用户直接访问
server_name www.oldboy.com;
root /html/www;
index index.html index.htm;
}
cat bbs.conf
server {
listen 172.16.10.100:82; -- 地址私网 可以被内部管理人员访问
server_name bbs.oldboy.com;
root /html/bbs;
index index.html index.htm;
}
10.0.0.100 模拟用户访问网站
172.16.10.100 实现访问内部管理平台
4)虚拟主机区域配置应用
nginx配置文件解释官网nginx.org/en/docs/dirindex.html
location区域配置:可以实现根据匹配的信息,做更灵活的处理
location可以实现多个uri信息匹配:
使用语法:location [ = | ~ | ~* | ^~ ] uri { ... }
location = / { -- 实现精准匹配
return 402;
}
location / { -- 默认匹配
return 401;
}
location /documents/ { -- 根据指定资源目录进行匹配
return 403;
}
location ^~ /images/ { -- 利用^~进行匹配 优先匹配(不检查正则表达式)
return 404;
}
location ~* .(gif|jpg|jpeg)$ { -- 不区分大小写
return 500;
}
location ~ .(gif|jpg|jpeg)$ { -- 区分大小写匹配
return 501;
}
======================================================================================
04 网站服务实际应用
利用nginx实现真实网站搭建
· 静态网站搭建:nginx(安装 虚拟主机配置)
配置信息:
server {
listen 10.0.0.100:80;
server_name game.oldboy.com;
root /html/game;
index index.html index.htm;
}
mkdir /html/game
上传游戏网站代码:
chown -R www.www /html/game/
chown -R www.www /html/game/
配置本地解析信息
10.0.0.100 game.oldboy.com
· 动态网站搭建 交互过程
情况一:不需要和数据库交互
编写配置文件
server {
listen 10.0.0.100:80;
server_name pan.oldboy.com;
root /html/pan;
index index.php index.html index.htm;
location ~ \.php$ {
root /html/pan;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
mkdir -p /html/pan
上传网站程序代码:内网网盘业务程序代码
chown -R www.www /html/pan/
修改php程序进程管理用户
vim /etc/php-fpm.d/www.conf
8 user = www
10 group = www
systemctl restart php-fpm
systemctl restart nginx
本地域名解析配置
情况二:需要和数据库交互
nginx -- php -- 存储/读取 数据库服务读取 LNMP架构构建
搭建博客平台:
编写配置文件
server {
listen 10.0.0.100:80;
server_name blog.oldboy.com;
root /html/blog;
index index.php index.html index.htm;
location ~ .php$ {
root /html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
创建连接数据库用户:
create user blog@'localhost' identified by '123456';
create database blog;
grant all on blog.* to blog@'localhost';
mkdir -p /html/blog
上传博客网站程序代码:
chown -R www. /html/blog/
systemctl restart nginx
本地域名解析配置

浙公网安备 33010602011771号