第十六天学习笔记2024.7.29

web

yum -y install httpd

systemctl start httpd.service 
systemctl stop firewalld 
systemctl disable firewalld 

2、动态⻚⾯与静态⻚⾯的差别
(1)URL不同

静态⻚⾯链接⾥没有“?”

动态⻚⾯链接⾥包含“?”

(2)后缀不同 (开发语⾔不同)

静态⻚⾯⼀般以 .html .htm .xml 为后缀

动态⻚⾯⼀般以 .php .jsp .py等为后缀

(3)内容不同

静态⻚⾯的内容是固定的

动态⻚⾯的内容会因⽤户、浏览器、时间、地点等⽽发⽣变化。

Apache服务器

12、搭建 apache 服务器
 1、安装启动httpd服务
 [root@web ~]# yum -y install httpd   
 [root@web ~]# systemctl start httpd
 2、查看安装情况以及安装的资源文件
 [root@web ~]# rpm -qa | grep httpd
 [root@web ~]# rpm -ql httpd
 [root@web ~]# ls /etc/httpd/
 [root@web ~]# vim /etc/httpd/conf/httpd.conf
 [root@web ~]# ls /var/www/html/
 3、查看端口
 [root@web ~]# netstat -lnput | grep http
 4、设置防火墙
 [root@web ~]# systemctl status firewalld   //检查防火墙状态
 [root@web ~]# systemctl start firewalld   //开启防火墙
 不能直接禁用防火墙,可以单独打开端口/服务
 [root@web ~]# firewall-cmd --list-ports   //查看防火墙打开的端口列表
 [root@web ~]# firewall-cmd --zone=public  --add-port=80/tcp --permanent   //打开防火墙的80端口
 [root@web ~]# firewall-cmd --zone=public  --add-service=http --permanent   //设置防⽕墙放⾏apache
 [root@web ~]# firewall-cmd --list-all   //查看当前区域下防⽕墙所有规则
   public (active)
   target: default
   icmp-block-inversion: no
   interfaces: ens33
   sources: 
   services: ssh dhcpv6-client
   ports: 
   protocols: 
   masquerade: no
   forward-ports: 
   source-ports: 
   icmp-blocks: 
   rich rules: 
 打开服务不会马上生效,需要重启或者重载服务信息
 [root@web ~]# firewall-cmd --reload    //重载防⽕墙规则
 [root@web ~]# firewall-cmd --list-all  //查看当前区域下防⽕墙所有规则
 public (active)
   target: default
   icmp-block-inversion: no
   interfaces: ens33
   sources: 
   services: ssh dhcpv6-client http
   ports: 80/tcp
   protocols: 
   masquerade: no
   forward-ports: 
   source-ports: 
   icmp-blocks: 
   rich rules: 
 5、在资源目录中添加index.html,html服务会自动找到index文件
 [root@web ~]# vim /var/www/html/index.html 
 <!doctype html>
 <html>
        <head>
            <meta charset="utf-8">
            <title>正方形</title>
            <style>
                      div{
                              background-color:red;
                              width:120px;
                              height:120px;
                         }
            </style>
        </head>
        <body>
             <div>正方形</div>
             <img src="1.jpg"
        </body>
 </html>

 nginx 

nginx安装

 [root@sla ~]# wget https://nginx.org/download/nginx-1.27.0.tar.gz   //下载最新版本的nginx
 [root@sla ~]# yum -y install gcc gcc-c++  //gcc用于编译多种编程语言的编译器  gcc-c++专门用于编译 C++ 程序的 gcc 组件
 [root@sla ~]# yum -y install make  //make用于自动化软件构建过程
 [root@sla ~]# yum -y install openssl-devel //安装“openssl-devel”包可以获得 OpenSSL 库的头文件和静态库文件
 [root@sla ~]# yum -y install pcre-devel  //安装 pcre-devel 包可以让开发者在编写 C 或 C++ 程序时能够使用 PCRE 库来处理正则表达式
 [root@sla ~]# tar -zxvf nginx-1.27.0.tar.gz
 [root@sla ~]# cd  nginx-1.27.0
 [root@sla nginx-1.27.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream
configure,编译安装前的预配置。

--prefix=/usr/local/nginx:指定软件的安装路径,注意不是安装包⽬录,⽽是软件要安装在哪。

--user=nginx:程序⽤户为nginx。

--group=nginx:组为nginx。

--with-http_ssl_module:提供HTTPS⽀持,ssl证书模块。

--with-http_stub_status_module:获取nginx⼯作状态模块。

--with-http_realip_module:获取真实客户端IP。

--with-stream:启⽤TCP/UDP代理模块。

 [root@sla nginx-1.27.0]# make && make install
 [root@sla nginx-1.27.0]# useradd -s /bin/nologin -M nginx   //-M :表示不创建用户的家目录。
 [root@sla nginx-1.27.0]# cd /usr/local/nginx/
 [root@sla nginx]# cp -r conf/ conf.bak   //备份配置文件的目录
 [root@sla nginx]# ./sbin/nginx     //启动nginx服务
 [root@sla nginx]# netstat -lntup | grep nginx    //查看nginx端口
 [root@sla nginx]# firewall-cmd --zone=public --add-port=80/tcp --permanent   //配置防火墙打开80端口
 [root@sla nginx]# firewall-cmd --reload    //重启服务
 [root@sla nginx]# ln -s /usr/local/nginx/sbin/nginx  /usr/bin/    //将软件⽬录下的nginx可执⾏程序软链接到/usr/sbin
 [root@sla nginx]# nginx -s reload  //热重启,仅重载nginx配置⽂件
 之所以指令能在命令行使用,是因为在$PATH目录中能找到这个可执行文件或者是这个可执行文件的链接文件
nginx目录结构介绍
 [root@YH2 nginx-1.22.1]# cd /usr/local/nginx/   //切换到nginx软件⽬录下
 [root@YH2 nginx]# ls 
 conf html logs sbin #nginx软件构成的⽬录
 #conf:存放nginx配置⽂件的⽬录
 #html:存放资源⽂件也就是⽹站⾸⻚(站点根⽬录)的⽬录
 #logs:存放nginx⽇志的⽬录
 #sbin:存放nginx可执⾏程序的⽬录
 [root@YH2 nginx]# ls ./conf     //nginx所有配置⽂件
 fastcgi.conf           nginx.conf
 fastcgi.conf.default   nginx.conf.default
 fastcgi_params         scgi_params
 fastcgi_params.default scgi_params.default
 koi-utf                 uwsgi_params
 koi-win                 uwsgi_params.default
 mime.types             win-utf
 mime.types.default
 #fastcgi.conf:fastcgi 相关参数配置⽂件。
 #fastcgi.conf.default:fastcgi.conf 的原始备份⽂件。
 #fastcgi_params:fastcgi 的参数⽂件。
 #nginx.conf:nginx 默认主配置⽂件。
 [root@YH2 nginx]# ls ./html/      //⽹站根⽬录
 50x.html index.html    //⾸⻚索引⽂件。
 #50x.html:错误⾸⻚
 #index.html:⽹站⾸⻚
 [root@YH2 nginx]# ls ./sbin
 nginx   //nginx可执⾏程序
1、nginx 主配置⽂件

nginx.conf 为 nginx 的主配置⽂件,编译安装的配置⽂件在之前预配置时指定的软件⽬录下,⼀般为 /usr/local/nginx/conf/nginx.conf;

yum 安装的 nginx,主配置 ⽂件位于 /etc/nginx/nginx.conf。 nginx.conf 配置⽂件由指令控制的模块组成。

指令分为简单指令和块指令,⼀个简单指令由名称和参数组成,空格分隔,分号结尾,如:listen 80;。

块指令与简单指令相同的结构,但不是以分号结尾,⽽是以⼤括 号包围的组附加指令结束,如:server { }。

2、 nginx 块指令

全局块:配置nginx全局的指令

events 块:配置 nginx 与⽤户连接的相关指令,如: events { }。

http 块:提供HTTP服务,如:http { }。

server 块:配置虚拟主机,⼀个http可以有多个server,如: server { }。

location 块:匹配URL后做什么动作或者跳转到⽹⻚的哪⾥, 如:location { }。

 [root@YH2 nginx]# vim  /usr/local/nginx/conf/nginx.conf //nginx主配置⽂件
 1 
 2 #user nobody;    //配置运⾏nginx的⽤户和组,(全局块)
 3 worker_processes  1;    //设置worker的进程数量
 4 
 5 #error_log logs/error.log;    //错误⽇志的路径信息
 6 #error_log logs/error.log notice;     //notice等级的错误⽇志路径信息(等级可修改或添加)
 7 #error_log logs/error.log info;     //info等级的错误⽇志路径信息(等级可添加或修改)
 8
 9 #pid       logs/nginx.pid;    //pid的⽂件路径
 10 
 11 
 12 events {      //events块
 13     worker_connections  1024;     //每个进程最多能处理多少个连接
 14 }
 15 
 16 
 17 http {      //http块,设定http服务器,利⽤它的反向代理功能提供负载均衡⽀持
 18     include       mime.types;     //指定⽂件拓展名和⽂件类型映射表
 19     default_type application/octet-stream;     //指定⽂件类型 
 20
 21     #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
 22     #                 '$status $body_bytes_sent "$http_referer" '
 23     #                 '"$http_user_agent" "$http_x_forwarded_for"';
 24 
  //上⾯注释的三⾏为nginx程序内部变量
 25     #access_log logs/access.log main;     //设定访问⽇志的路径及格式
 26 
 27     sendfile       on;    //指定nginx是否调⽤sendfile函数来输出⽂件,对于普通应⽤必须设定为no
 28     #tcp_nopush     on;   //放置⽹络阻塞
 29 
 30     #keepalive_timeout 0;    //连接超过的时间设置
 31     keepalive_timeout  65;
 32 
 33     #gzip on;    //开启gzip压缩
 34 
 35     server {        // server块,配置各种虚拟主机
 36         listen       80; //设置监听端⼝
 37         server_name localhost;    //配置的域名信息,⼀个server可以对应⼀个域名信息
 38 
 39         #charset koi8-r;    //字符集,utf-8为中⽂字符集
 40 
 41         #access_log logs/host.access.log main;    //设定本虚拟主机的访问⽇志和⽇志类型
 42 
 43         location / {     // location块,代表url的跳转,“/”后⾯写⼊的内容代表我们在浏览器地址栏输⼊的域名或IP地址的后续访问路径
 44             root   html;      //服务器的默认⽹站根⽬录位置
 45             index index.html index.htm;      //⾸⻚索引⽂件名称
 46         }
 48         #error_page 404    /404.html;        //错误提示⻚⾯
 49 
 50         # redirect server error pages to the static p   age /50x.html
 51         #
 52         error_page   500 502 503 504 /50x.html;     //错误提示⻚⾯
 53         location = /50x.html {
 54             root   html;
 55         }        //每个花括号都会与前⾯的对应
  ......省略部分注释内容.......
 79     }      //这个花括号对应的是 server 的 
 ...........................................................
 117 }        //这个花括号对应的是 http 的 “{”
8、以 systemctl 控制 nginx
 脚本启动nginx
 [root@sla ~]# vim nginx.sh
 #!/bin/bash
 /usr/local/nginx/sbin/nginx  &> /dev/null
 if [ $? -ne 0 ];then
       echo "nginx正在执行,或者80端口被占用"
 fi
 [root@sla ~]# sh nginx.sh
 配置systemctl控制nginx
 [root@sla ~]# ls /usr/lib/systemd/system
 [root@sla ~]# vim /usr/lib/systemd/system/nginx.service
 [Unit]
 Description=nginx
 After=network.target
 [Service]
 Type=forking
 PIDFile=/usr/local/nginx/logs/nginx.pid
 ExecStart=/usr/local/nginx/sbin/nginx
 ExecReload=/usr/local/nginx/sbin/nginx -s reload
 ExecStop=/usr/local/nginx/sbin/nginx -s stop
 PrivateTmp=Flase
 [Install]
 WantedBy=multi-user.target
 [root@sla ~]# systemctl daemon-reload   //重载systemctl的配置⽂件,以便nginx.service⽣效
 [root@sla ~]# reboot
 [root@sla ~]# systemctl stop nginx   //停用成功
注意:如果直接使⽤ nginx 命令对程序进⾏控制,那么不可以再⽤ systemctl服务控制命令对 nginx 进⾏后续操作。

⽐如:我现在使⽤了 nginx 命令直接启动了 nginx 程序,如果此时我对 nginx 的主配置⽂件做出了修改想要重新载⼊主配置⽂件,那么不能使⽤systemctl reload nginx.service 重载 nginx 配置,或 systemctl stop nginx.service 停⽌ nginx 程序,只能使⽤ nginx -s reload 重载配置,或 nginx -s stop 停⽌服务。

监控模块的应用
 添加监控模块(修改配置文件)
 [root@sla nginx]# vim /usr/local/nginx/conf/nginx.conf   //在location模块下面添加新模块status
          location / {
             root   html;
             index  index.html index.htm;
         }
          location /status {
             stub_status on;
             access_log off;
         }
 [root@sla nginx]# systemctl restart nginx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值