讲师: 白树明
分类
源码管理;自动部署; Web服务器;
Web环境部署与上线流程
环境搭建
服务器环境搭建
Nginx
是什么?
轻量级服务器;资源占用相比Apache小;
能做什么?
@Web服务器@;反向代理服务器功能;IMAP / POP3/ SMTP代理服务器
Web服务器: 相当于一个中间件,开发写出来的代码由它共享并且展示给互联网广大用户
种类: `Apache`衍生: `tengine`[淘宝], `openresrt`[章亦春]等
资源文档:
官网:[[nginx news](https://nginx.org/)]官方文档:[[nginx documentation](https://nginx.org/en/docs/)]中文文档:[[Nginx中文文档](https://www.nginx.cn/doc/)]
准备工作
yum -y install gcc make wget pcre-devel zlib zlib-develyum -y install lsof elinks
查看外部配置信息

网卡配置信息
[root@localhost network-scripts]# sudo vim ifcfg-ens33TYPE=EthernetPROXY_METHOD=noneBROWSER_ONLY=noBOOTPROTO=staticDEFROUTE=yesIPADDR="192.168.124.9"NETMASK="255.255.255.0"GATEWAY="192.168.124.2"IPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_FAILURE_FATAL=noIPV6_ADDR_GEN_MODE=stable-privacyNAME=ens33UUID=608dbc8e-3115-4f58-95ae-a5fadbc3aedbDEVICE=ens33ONBOOT=no
安装过程
- 下载
yum -y install wget # 看是否安装了wget包wget https://nginx.org/download/nginx-1.21.3.tar.gz -P /usr/src # 从官网版本黏贴du -h /usr/src/nginx-1.21.3.tar.gz # 查看大小tar xf nginx-1.21.3.tar.gz # 解压cd nginx-1.21.3 # 切换工作路径&& make && make install # 检查配置,指定安装目录并且编译安装
- 配置
1)检查环境是否满足安装条件,解决依赖
yum -y install prce-devel zlib zlib-devel
2)指定安装方式 配置文件 命令文件放在那里
./configure --prefix=/usr/local/nginx
3)开启模块功能 内置,第三方
4)指定安装位置
编译
make
安装
make install
[root@vvkt7whznuckhiz2-0723575 nginx-1.21.3]# ./configure --help--help print this message--prefix=PATH set installation prefix--sbin-path=PATH set nginx binary pathname--modules-path=PATH set modules path--conf-path=PATH set nginx.conf pathname--error-log-path=PATH set error log pathname--pid-path=PATH set nginx.pid pathname--lock-path=PATH set nginx.lock pathname
总结
# 安装位置cd /usr/local/nginx# 解压位置cd /usr/src/nginx以及版本号
使用
检查端口占用[默认端口为80]
netstat -tunlp | grep "80"lsof -i :80netstat -ntpl # 检查正在监听的端口
启动
/usr/local/nginx/sbin/nginxlsof -i :80 # 有输出表示有占用
验证是否安装成功
yum -y install elinkselinks http://192.168.1.9 --dump
配置文件详解
亦称变量文件,保存软件运行时需要的各种参数;
配置文件存储的路径
/usr/local/nginx/conf/nginx.conf
完整配置文件
#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# 启动子进程程序的默认用户#user nobody;# 一个主进程和多个工作进程;工作进程是单进程多线程的,且不需要特殊授权即可运行;这里定义的是工作进程的数量;worker_processes 1;# 全局错误日志的位置以及日志格式#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;# 设置主进程号#pid logs/nginx.pid;events {# 每个工作进程最大的并发数worker_connections 1024;}# http服务器的设置http {# 设定mime类型,类型由mime.type文件定义include 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"';# remote_addr 和 http_x_forwarded_for 用以记录客户端的ip地址;# remote_user 用来记录客户端用户名称;# time_local 记录时区;# request 记录请求的url与http协议;# status 用来记录请求状态;成功则为200;# body_bytes_sent 记录发送给客户端文件主体内容的大小;# http_referer 记录从哪个页面链接访问过来的;# http_user_agent 记录浏览器相关信息#全局访问日志路径#access_log logs/access.log main;# [指令]sendfile指定nginx是否调用sendfile函数(zero copy方式)来输出文件,对于普通应用,必须设为on.sendfile on;# 此选项允许或者禁止使用socket的TCP_CORK选项,此选项仅在使用sendfile的时候使用#tcp_nopush on;# 长连接超时时间#keepalive_timeout 0;keepalive_timeout 65;# 开启压缩#gzip on;server {# 虚拟主机使用的端口listen 80;# 虚拟主机域名server_name localhost;# 字符集#charset koi8-r;# 自定义虚拟主机的访问日志路径,可以不使用全局的日志路径#access_log logs/host.access.log main;# 定义Web根路径location / {# 根目录路径root html;# 索引页index index.html index.htm;}# 根据错误码,返回对应的页面#error_page 404 /404.html;# 定义页面路径# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# 定义反向代理服务器,数据服务器时 lamp模型# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# 定义PHP为本机服务的模型# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##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;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one## 拒绝apache DR目录以及子目录下的.htaccessw#location ~ /\.ht {# deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;}http {include 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 logs/access.log main;sendfile on;#tcp_nopush on;
分支1—查验Nginx启动用户
lsof -i :80COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEnginx 25421 root 6u IPv4 136364 0t0 TCP *:http (LISTEN)nginx 25422 nobody 6u IPv4 136364 0t0 TCP *:http (LISTEN)
分支2-创建只能用于启动程序的用户
# 创建目标用户useradd -s /sbin/nologin -r www第一个参数为:指定登录时使用的shell,默认为空第二个参数为:该用户为系统管理员# 编辑配置文件指定启动用户vim nginx.conf # 将配置文件更为以下内容user www;worker_processes 1;# 关闭进程并且查看是否关闭成功killall nginxlsof -i :80# 重启并且查看启动用户[root conf]# cd ..[root@ nginx]# ./sbin/nginx[root@ nginx]# lsof -i :80COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEnginx 25625 root 6u IPv4 137912 0t0 TCP *:http (LISTEN)nginx 25626 www 6u IPv4 137912 0t0 TCP *:http (LISTEN)
分支3-查看PID存放的位置
cat logs/nginx.pid25625
分支4-查看有谁对服务器进行过访问
cat logs/access.log192.168.1.9 - - [03/Oct/2021:20:41:57 +0800] "GET / HTTP/1.1" 200 615 "-" "ELinks/0.12pre6 (textmode; Linux; -)"
默认网站
默认网页的存放位置
ls50x.html index.htmlpwd/usr/local/nginx/html
准备子网页内容
[root@vvkt7whznuckhiz2-0723575 nginx]# cd html[root@vvkt7whznuckhiz2-0723575 html]# mkdir a[root@vvkt7whznuckhiz2-0723575 html]# mkdir b[root@vvkt7whznuckhiz2-0723575 html]# mkdir c[root@vvkt7whznuckhiz2-0723575 html]# echo aaaa > a/index.html[root@vvkt7whznuckhiz2-0723575 html]# echo bbbb > b/index.html[root@vvkt7whznuckhiz2-0723575 html]# echo cccc > c/index.html
设置允许主机
48 location /a {49 allow 122.198.1.9;50 allow 10.19.1.26;51 deny all;52 }
关闭进程以及重新启动
killall nginx../sbin/nginxlsof -i :3389elinks http://122.198.1.9/a:3389
报错
1.nginx: [emerg] invalid parameter "localhost" in /usr/local/nginx/conf/nginx.conf:49
delete allow localhost;指定本机IP地址;
管理;优化;负载均衡;缓存;
