Linux ❀ RHCE自研教学笔记 - Redhat 8.2 HTTP服务教研笔记

Posted 国家级干饭型选手°

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux ❀ RHCE自研教学笔记 - Redhat 8.2 HTTP服务教研笔记相关的知识,希望对你有一定的参考价值。


HTTP - HyperText Transport Protocol 超文本传输协议:此协议采用了C/S模型,客户端向服务器发送一个请求,请求头包含请求的方法、URL、协议版本等信息,服务器收到该请求后,回复一个状态应答作为响应;
服务端口:TCP 80
Web服务器:指望着服务器,此服务器可以向浏览器等软件提供文档,也可以放置网站文件,供读者浏览或下载,目前最主流的Web服务器为Apache、Microfost、Internet信息服务器;

WWW - World Wide Web 环球信息网:中文名称为“万维网”,分为Web客户端与Web服务器程序,WWW可以让Web客户端(经常为浏览器软件)访问浏览Web服务器资源,是一个由许多互相链接的超文本组成的系统,通过互联网访问;

请求头部
HTTP请求由三部分组成,分别是请求行、消息报头、请求正文
Method Request-URL HTTP-Version CRLF

  • Method表示请示方法;
  • Request-URL是一个统一资源标识符;
  • HTTP-Version:请求的HTTP协议版本;
  • CRLF:表示回车和换行(除了作为结尾的CRLF外,不允许出现单独的CR或LF字符)

响应头部
HTTP响应报文由三部分组成:响应行、响应消息报头、响应正文
HTTP-Version Status-Code Reason-Phrase CRLF

  • HTTP-Version:表示服务器HTTP协议的版本;
  • Status-Code:服务器发回的响应状态码;
  • Reason-Phrase:状态码的文本描述;

响应状态码

  • 100 - 继续发送请求,未被拒绝;
  • 200 - 请求正常;
  • 301/302 - 重定向;
  • 400 - 客户端请求错误;
  • 500 - 服务器内部错误;

请求方式

  • GET:请求获取Request-URL所标识的资源;
  • POST:在Request-URL所标识的资源后附加新的数据;
  • HEAD:请求获取由Request-URL所标识的资源的响应消息报头;
  • PUT:请求服务器存储一个资源,并用Request-URL作为其标识;
  • DELETE:请求服务器删除Request-URL所标识的资源;
  • TRACE:请求服务器回送收到的请求信息,主要用户测试或诊断;
  • CONNECT:保留;
  • OPTIONS:请求查询服务器的性能,或者查询与资源相关的选项和需求;

案例截图

URL 统一资源定位符:在Web服务器资源内,由一个全局统一资源标识符标识其所有资源,这些资源通过HTTP协议传输给访问用户,用户则通过点击链接来获取这些资源;
scheme://host[:port] [abs_path]?[query-string1]&[query-string2]
以常见的URL为例子,格式如下:

http://   115.231.230.183:80   /space?   uid=14804563
scheme        host            port     path       query-string   

在URL中,各项的意义如下:

  • scheme:表明底层使用的协议,如HTTP/HTTPS;
  • host:HTTP服务器的IP地址或者域名;
  • port:HTTP默认端口为80,HTTPS默认端口为443,默认可以省略,非默认需要注明;
  • path:访问资源的路径,在服务器以www开头的路径,通常表示访问文件的地点;
  • query-string:发送给HTTP服务器的数据,此时,应用GET方式传输才有效,多个数据可用&进行分割,实现多组数据同时传输;

1、服务安装


[root@redhat8 ~]# yum install -y httpd			
#在RHEL 8中yum为dnf的一种软连接,后续安装均使用dnf,请务必注意!
[root@redhat8 ~]# dnf install -y httpd
Complete!

yum命令与dnf命令文件的位置如下

[root@redhat8 ~]# which dnf
/usr/bin/dnf
[root@redhat8 ~]# which yum
/usr/bin/yum

服务包配置确认

[root@redhat8 ~]# rpm -qa httpd
httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64
[root@localhost ~]# rpm -qc httpd
/etc/httpd/conf.d/autoindex.conf
/etc/httpd/conf.d/userdir.conf
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf.modules.d/00-base.conf
/etc/httpd/conf.modules.d/00-dav.conf
/etc/httpd/conf.modules.d/00-lua.conf
/etc/httpd/conf.modules.d/00-mpm.conf
/etc/httpd/conf.modules.d/00-optional.conf
/etc/httpd/conf.modules.d/00-proxy.conf
/etc/httpd/conf.modules.d/00-systemd.conf
/etc/httpd/conf.modules.d/01-cgi.conf
/etc/httpd/conf/httpd.conf
/etc/httpd/conf/magic
/etc/logrotate.d/httpd
/etc/sysconfig/htcacheclean

tree命令是RHEL 8新增的命令(非常方便查看目录下的文件信息)

[root@redhat8 ~]# tree /etc/httpd/
/etc/httpd/
├── conf
│   ├── httpd.conf
│   └── magic
├── conf.d
│   ├── autoindex.conf
│   ├── README
│   ├── userdir.conf
│   ├── vhost.conf
│   └── welcome.conf
├── conf.modules.d
│   ├── 00-base.conf
│   ├── 00-dav.conf
│   ├── 00-lua.conf
│   ├── 00-mpm.conf
│   ├── 00-optional.conf
│   ├── 00-proxy.conf
│   ├── 00-systemd.conf
│   ├── 01-cgi.conf
│   ├── 10-h2.conf
│   ├── 10-proxy_h2.conf
│   └── README
├── logs -> ../../var/log/httpd
├── modules -> ../../usr/lib64/httpd/modules
├── run -> /run/httpd
└── state -> ../../var/lib/httpd

登录日志存放位置:

[root@localhost ~]# ll /var/log/httpd/
total 8
-rw-r--r--. 1 root root 1625 Aug 25 14:43 access_log
-rw-r--r--. 1 root root 2505 Aug 25 14:31 error_log

2、服务配置文件内容详解


(1)配置文件介绍

  • 主配置目录:/etc/httpd/conf
  • 子配置目录:/etc/httpd/conf.d(apache是一个模块化的一个配置服务,所以我们可以根据每个模块进行一些配置,不仅对程序本身进行模块化配置对配置文件也进行了模块化配置)
    默认网站存放路径:/var/www/html/
[root@redhat8 ~]# cat /etc/httpd/conf/httpd.conf 
ServerRoot "/etc/httpd"		        /服务配置文件的根设置apache安装的绝对路径;
Listen 80							/监听端口为80;
Include conf.modules.d/*.conf
User apache							/进程属组;
Group apache
ServerAdmin root@localhost			/服务管理员邮箱;
<Directory />						/目录标签;
    AllowOverride none		
    Require all denied				/禁止访问根;
</Directory>
DocumentRoot "/var/www/html"		/文档根目录;
<Directory "/var/www">
    AllowOverride None
    Require all granted
<Directory "/var/www/html">
    Options Indexes FollowSymLinks   /选定索引跟踪软链接;
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html	     /索引目录为index.html;
</IfModule>
<Files ".ht*">	   					/安全策略控制文件都是.ht开头;
    Require all denied
</Files>
LogLevel warn
<IfModule log_config_module>
    LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\"" 
  (%h等,查看使用 man date )
combined
    LogFormat "%h %l %u %t \\"%r\\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
    # will make a new request for the document at its new location.
    # ScriptAliases are essentially the same as Aliases, except that
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8					/字符集;
<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on						/支持发送文件;
IncludeOptional conf.d/*.conf			/加载子配置文件;

(2)主配置文件参考

[root@redhat8 ~]# cat /usr/share/doc/httpd/httpd-vhosts.conf 
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com	
    DocumentRoot "/var/www/dummy-host.example.com"				/提供http服务的目录;
    ServerName dummy-host.example.com							/服务器主机名和端口号;
    ServerAlias www.dummy-host.example.com						/服务器主机别名和端口号;
    ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"	/服务器错误日志目录;
    CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common	/日志文件;
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/var/www/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
    CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>

查看服务状态

[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-08-25 16:22:03 CST; 5s ago
     Docs: man:httpd.service(8)
 Main PID: 9753 (httpd)
   Status: "Started, listening on: port 80"
    Tasks: 213 (limit: 49452)
   Memory: 44.4M
   CGroup: /system.slice/httpd.service
           ├─9753 /usr/sbin/httpd -DFOREGROUND
           ├─9755 /usr/sbin/httpd -DFOREGROUND
           ├─9756 /usr/sbin/httpd -DFOREGROUND
           ├─9757 /usr/sbin/httpd -DFOREGROUND
           └─9758 /usr/sbin/httpd -DFOREGROUND

Aug 25 16:22:03 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
Aug 25 16:22:03 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
Aug 25 16:22:03 localhost.localdomain httpd[9753]: Server configured, listening on: port 80

3、服务案例简要配置


(1)默认页面配置

[root@localhost ~]# systemctl restart httpd
#关闭防火墙与Selinux
[root@redhat8 ~]# systemctl stop firewalld.service 
[root@redhat8 ~]# setenforce 0
[root@redhat8 ~]# getenforce 
Permissive

[root@redhat8 ~]# curl -k http://192.168.58.134

结果验证

Apache欢迎页面的配置文件位置:
[root@localhost ~]# cat /etc/httpd/conf.d/welcome.conf 
<LocationMatch "^/+$">
    Options -Indexes
    ErrorDocument 403 /.noindex.html
</LocationMatch>

<Directory /usr/share/httpd/noindex>
    AllowOverride None		/网站 .htaccess 文件被忽略,另一个文件覆盖现有配置文件;
    Require all granted		/无条件允许访问;
</Directory>
Alias /.noindex.html /usr/share/httpd/noindex/index.html

Apache欢迎页面的具体文件位置:

[root@localhost ~]# cat /usr/share/httpd/noindex/index.html

(2)修改http服务web页面显示内容

[root@redhat8 ~]# cat /etc/httpd/conf.d/vhost.conf
<directory /www>
	allowoverride none
	require all granted
</directory>
<virtualhost 192.168.58.134:80>
	servername 192.168.58.134
	documentroot /www/134
</virtualhost>
[root@redhat8 ~]# mkdir -pv /www/134
[root@redhat8 ~]# echo this is test > /www/134/index.html
[root@redhat8 ~]# systemctl restart httpd
#修改配置文件需要重启http服务

验证结果:

[root@redhat8 ~]# curl -k http://192.168.58.134
this is test

(3)基于IP地址+端口号进行访问http服务

[root@localhost ~]# ss -lntup | grep 80
tcp    LISTEN   0        128                     *:80                   *:*      users:(("httpd",pid=3473,fd=4),("httpd",pid=3472,fd=4),("httpd",pid=3471,fd=4),("httpd",pid=3468,fd=4))
#检测某个端口是否被某个进程使用

[root@redhat8 ~]# cat /etc/httpd/conf.d/vhost.conf 
<directory /www>
	allowoverride none
	require all granted
</directory>
listen 8888
<virtualhost 192.168.58.134:8888>
	servername 192.168.58.134
	documentroot /www/8888
</virtualhost>
[root@redhat8 ~]# mkdir -pv /www/8888
[root@redhat8 ~]# echo this is 8888 > /www/8888/index.html
[root@redhat8 ~]# systemctl restart httpd

验证结果:

[root@redhat8 ~]# curl -k http://192.168.58.134:8888
this is 8888

(4)基于不同域名配置http服务

[root@redhat8 ~]# vim /etc/httpd/conf.d/vhost.conf 
<directory /www>
        allowoverride none
        require all granted
</directory>
<virtualhost 192.168.58.134:80>
        servername www.ceshi.com
        serveralias www.test.com
        documentroot /www/80
</virtualhost>
[root@redhat8 ~]# mkdir -pv /www/80
[root@redhat8 ~]# echo this is ceshi domain > /www/80/index.html
[root@redhat8 ~]# cat /etc/hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.58.134	www.ceshi.com
192.168.58.134	www.test.com
[root@redhat8 ~]# systemctl restart httpd

验证结果:

[root@redhat8 ~]# curl -k www.test.com
this is ceshi domain
[root@redhat8 ~]# curl -k www.ceshi.com
this is ceshi domain

(5)虚拟目录下的http服务

[root@localhost ~]# vim /etc/httpd/conf.d/vhost.conf
<virtualhost 192.168.14.131:80>
        servername 192.168.14.131
        documentroot /var/www/jiami
        alias /xxx /var/www/xxx
</virtualhost>
<directory /var/www>
        allowoverride none
        require all granted
</directory>

[root@localhost ~]# mkdir /var/www/xxx
[root@localhost ~]# echo this is xxx > /var/www/xxx/index.html 
[root@localhost ~]# systemctl restart httpd

结果验证:

[root@localhost ~]# curl -k http://192.168.14.131/xxx/
this is xxx

(6)基于用户控制访问http服务

[root@localhost ~]# htpasswd -c /etc/httpd/mymima xiaoming
New password: redhat
Re-type new password: redhat
Updating password for user xiaoming
[root@localhost ~]# htpasswd /etc/httpd/mymima xiaohong
New password: redhat
Re-type new password: redhat
Updating password for user xiaohong
[root@localhost ~]# cat /etc/httpd/mymima 
xiaoming:$apr1$E9thblMP$jDdb6S5iW5ydGkENJzL0e0
xiaohong:$apr1$2nODEikn$y6E9vD4wdw9tr6lIIByg60

[root@localhost ~]# vim /etc/httpd/conf.d/vhost.conf 
<virtualhost 192.168.14.131:80>
	servername 192.168.14.131
	documentroot /var/www/jiami
</virtualhost>
<directory /var/www>
	authtype basic
	authname "please login:"	
	authuserfile /etc/httpd/mymima
	require user xiaoming xiaohong
</directory>
[root@localhost ~]# systemctl restart httpd


[root@localhost ~]# curl -k http://192.168.14.131 -u xiaoming
Enter host password for user 'xiaoming':redhat
一个简单的https服务页面 
[root@localhost ~]# curl -k http://192.168.14.131 -u xiaohong
Enter host password for user 'xiaohong':redhat
一个简单的https服务页面

以上是关于Linux ❀ RHCE自研教学笔记 - Redhat 8.2 HTTP服务教研笔记的主要内容,如果未能解决你的问题,请参考以下文章

Linux ❀ RHCE自研教学笔记 - Redhat 8.2 SFTP服务教研笔记

Linux ❀ RHCE自研教学笔记 - Redhat 8.2 SFTP服务教研笔记

Linux ❀ RHCE自研教学笔记 - Redhat 8.2 Nmcli服务教研笔记

Linux ❀ RHCE自研教学笔记 - Redhat 8.2 Nmcli服务教研笔记

Linux ❀ RHCE自研教学笔记 - Redhat 8.2 HTTP服务教研笔记

Linux ❀ RHCE自研教学笔记 - Redhat 8.2 FTP服务教研笔记