Linux ❉ CentOS 7安装http服务器

一 介绍:

        HTTP - HyperText Transport Protocol 超文本传输协议:此协议采用了C/S模型,客户端向服务器发送一个请求,请求头包含请求的方法、URL、协议版本等信息,服务器收到该请求后,回复一个状态应答作为响应。服务端口:TCP 80

        Web服务器:指网站服务器,此服务器可以向浏览器等软件提供文档,也可以放置网站文件,供读者浏览或下载,目前最主流的Web服务器为Apache、Microfost、Internet信息服务器;

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

1 基础知识

(1)请求头部

HTTP请求由三部分组成,分别是请求行、消息报头、请求正文

Method Request-URL HTTP-Version CRLF

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

(2)请求方式: 

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


(3)响应头部

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

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

(4)响应状态码

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

 (5)URL 统一资源定位符

在Web服务器资源内,由一个全局统一资源标识符标识其所有资源,这些资源通过HTTP协议传输给访问用户,用户则通过点击链接来获取这些资源;

scheme://host[:port] [abs_path]?[query-string1]&[query-string2]

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

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

2 URL与URI的区别

 (1)什么是URI

URI是类似一个独一无二的身份标识。准确的说是某个网络资源的特有标识。

任何URI的通用形式都是:

scheme:[// [user:password @] host [:port]] [/] path [?查询] [#片段]



Scheme(方案):该方案列出了具体语法和URI的任何相关协议。方案不区分大小写,后跟冒号。理想情况下,URI方案应该在互联网号码分配机构(IANA)注册,但也可以使用非注册方案。

权限组件:权限组件由多个部分组成:可选的身份验证部分,主机(由注册名称或IP地址组成) , 以及可选的端口号。身份验证部分包含用户名和密码,用冒号分隔,后跟at(@)符号。在@之后是主机名,然后是冒号,然后是一个端口号。请务必注意,IPv4地址必须采用点十进制表示法,并且IPv6地址必须括在括号中。

查询(可选):查询包含一串非分层数据。虽然语法没有明确定义,但通常是由分隔符分隔的属性值对序列,例如&符号或分号。查询通过问号与前一部分分开。

片段(可选):片段包含片段标识符,该标识符为辅助资源提供方向。

举例说明

要求找到一本书(书就是资源),这本书在A省份/B市/C区/D街道/xx栋/392-1住户/1号房间/名字叫做《xxx》 (这里就是模拟我们输入网址进行HTTP请求)
那么此时的 《xxx》 这本书 对于 1号房间 来说就是uri
此时的D街道/xx栋/392-1住户/1号房间/名字叫做《xxx》这本书 对于 A省份/B市/C区 来说就是uri
可以看出uri是不固定的,是相对来说的,具体是什么就看你的参照角度是什么。(不同请求参照角度不一样,所以他们的返回uri有差异)
        由此总结:uri是一个标识,用来区别于其他资源的标识。 (相信你对这句话就更能理解了)

(2)URI和URL的关系

        URL继承了所有URI的内容,所以它比URI更加详细,但是URI是它的父级

1 作用的区别:

URL(统一资源定位符)主要用于链接网页,网页组件或网页上的程序,借助访问方法(http,ftp,mailto等协议)来检索位置资源。
URI(统一资源标识符)用于定义项目的标识,此处单词标识符表示无论使用的方法是什么(URL或URN),都要将一个资源与其他资源区分开来。

2 可以说URL是URI(URL是URI的子集),但URI永远不能是URL

3 协议区别

URL指定要使用的协议类型,而URI不涉及协议规范。

二 安装

[root@slave1 tmp]# yum install -y httpd
······
Installed:
  httpd.x86_64 0:2.4.6-97.el7.centos.2                                                                                                      

Dependency Installed:
  httpd-tools.x86_64 0:2.4.6-97.el7.centos.2                                                                                                

Complete!
# 7版本和8版本略有不同,
# 在RHEL 8中yum为dnf的一种软连接,后续安装均使用dnf,请务必注意!
# yum命令与dnf命令文件的位置如下
[root@redhat8 ~]# which dnf
/usr/bin/dnf
[root@redhat8 ~]# which yum
/usr/bin/yum

服务包配置确认

[root@slave1 tmp]# rpm -qa httpd
httpd-2.4.6-97.el7.centos.2.x86_64
[root@slave1 tmp]# 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-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
/etc/sysconfig/httpd

查看目录下的文件信息

[root@slave1 tmp]# tree /etc/httpd/
/etc/httpd/
├── conf
│   ├── httpd.conf
│   └── magic
├── conf.d
│   ├── autoindex.conf
│   ├── README
│   ├── userdir.conf
│   └── welcome.conf
├── conf.modules.d
│   ├── 00-base.conf
│   ├── 00-dav.conf
│   ├── 00-lua.conf
│   ├── 00-mpm.conf
│   ├── 00-proxy.conf
│   ├── 00-systemd.conf
│   └── 01-cgi.conf
├── logs -> ../../var/log/httpd
├── modules -> ../../usr/lib64/httpd/modules
└── run -> /run/httpd

6 directories, 13 files

查看登陆日志存放位置

三 服务配置文件详解

1 配置文件介绍

  • 主配置目录:/etc/httpd/conf
  • 子配置目录:/etc/httpd/conf.d(apache是一个模块化的一个配置服务,所以我们可以根据每个模块进行一些配置,不仅对程序本身进行模块化配置对配置文件也进行了模块化配置)
    默认网站存放路径:/var/www/html/
[root@slave1 ~]# cat /etc/httpd/conf/httpd.conf
[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@slave1 httpd-2.4.6]# pwd
/usr/share/doc/httpd-2.4.6
[root@slave1 httpd-2.4.6]# cat 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>


原文件内容
<VirtualHost *:@@Port@@>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "@@ServerRoot@@/docs/dummy-host.example.com"
    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 *:@@Port@@>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "@@ServerRoot@@/docs/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@slave1 httpd-2.4.6]# systemctl start httpd
[root@slave1 httpd-2.4.6]# 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 2021-12-14 06:37:45 EST; 1s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 16832 (httpd)
   Status: "Processing requests..."
    Tasks: 6
   Memory: 3.2M
   CGroup: /system.slice/httpd.service
           ├─16832 /usr/sbin/httpd -DFOREGROUND
           ├─16833 /usr/sbin/httpd -DFOREGROUND
           ├─16834 /usr/sbin/httpd -DFOREGROUND
           ├─16835 /usr/sbin/httpd -DFOREGROUND
           ├─16836 /usr/sbin/httpd -DFOREGROUND
           └─16837 /usr/sbin/httpd -DFOREGROUND

Dec 14 06:37:44 slave1 systemd[1]: Starting The Apache HTTP Server...
Dec 14 06:37:45 slave1 httpd[16832]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using... message
Dec 14 06:37:45 slave1 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

四 简要服务案例

        测试时关闭防火墙和selinux

[root@slave1 httpd-2.4.6]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

[root@slave1 httpd-2.4.6]# getenforce
Permissive

1 默认页面配置

[root@slave1 httpd-2.4.6]# curl -k http://192.168.247.131
# 可以看到一堆输出代码,这个在浏览器可以查看

        

 上面这个是欢迎界面,可以在下面的位置进行修改

[root@slave1 httpd-2.4.6]# cat /usr/share/httpd/noindex/index.html 

2 修改web页面显示内容

# 该文件如果没有需要手动创建
[root@slave1 httpd-2.4.6]# cat /etc/httpd/conf.d/vhost.conf
<directory /www>
        allowoverride none
        require all granted
</directory>
<virtualhost 192.168.247.131:80>
        servername 192.168.247.131
        documentroot /www/123
</virtualhost>
[root@slave1 httpd-2.4.6]# mkdir -pv /www/123
mkdir: created directory ‘/www’
mkdir: created directory ‘/www/123’
[root@slave1 httpd-2.4.6]# echo this is test > /www/123/index.html
[root@slave1 httpd-2.4.6]# systemctl restart httpd
# 修改配置文件需要重启http服务
# 验证结果
[root@slave1 httpd-2.4.6]# curl -k 192.168.247.131
this is test

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

# 检测某个端口是否被某个进程使用
[root@slave1 httpd-2.4.6]# ss -tulnp | grep 80
tcp    LISTEN     0      128    [::]:80                 [::]:*                   users:(("httpd",pid=21692,fd=4),("httpd",pid=21691,fd=4),("httpd",pid=21690,fd=4),("httpd",pid=21689,fd=4),("httpd",pid=21688,fd=4),("httpd",pid=21687,fd=4))


[root@slave1 httpd-2.4.6]# systemctl stop httpd
[root@slave1 httpd-2.4.6]# ss -tulnp | grep 80
原来的结果不再显示,说明端口被放开
[root@slave1 httpd-2.4.6]# cat /etc/httpd/conf.d/vhost.conf
<directory /www>
        allowoverride none
        require all granted
</directory>
# 注意这个监听端口要加上
listen 888
<virtualhost 192.168.247.131:888>
        servername 192.168.247.131
        documentroot /www/888
</virtualhost>

# 重启服务
[root@slave1 httpd-2.4.6]# systemctl restart httpd

# 查看有两个端口在使用
[root@slave1 httpd-2.4.6]# ss -tulnp | grep 80
tcp    LISTEN     0      128    [::]:80                 [::]:*                   users:(("httpd",pid=46563,fd=4),("httpd",pid=46562,fd=4),("httpd",pid=46561,fd=4),("httpd",pid=46560,fd=4),("httpd",pid=46559,fd=4),("httpd",pid=46558,fd=4))
[root@slave1 httpd-2.4.6]# ss -tulnp | grep 888
tcp    LISTEN     0      128    [::]:888                [::]:*                   users:(("httpd",pid=46563,fd=6),("httpd",pid=46562,fd=6),("httpd",pid=46561,fd=6),("httpd",pid=46560,fd=6),("httpd",pid=46559,fd=6),("httpd",pid=46558,fd=6))


# 查看不同的端口返回结果
[root@slave1 httpd-2.4.6]# curl -k 192.168.247.131:888
this is 888
# 默认的80 端口返回的依然是原来的欢迎信息
[root@slave1 httpd-2.4.6]# curl -k 192.168.247.131:80
[root@slave1 httpd-2.4.6]# curl -k 192.168.247.131

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

[root@slave1 httpd-2.4.6]# cat /etc/httpd/conf.d/vhost.conf 
<directory /www>
        allowoverride none
        require all granted
</directory>
<virtualhost 192.168.247.131:80>
        servername www.ceshi.com
        serveralias www.test.com
        documentroot /www/80
</virtualhost>

# serveralias是防止人输错网址,对客户输入信息进行小范围容错的

[root@slave1 httpd-2.4.6]# mkdir -pv /www/80
mkdir: created directory ‘/www/80’
[root@slave1 httpd-2.4.6]# echo this is ceshi domain > /www/80/index.html

# 编辑本地host文件
[root@slave1 httpd-2.4.6]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.247.131 www.ceshi.com
192.168.247.131 www.test.com


[root@slave1 httpd-2.4.6]# systemctl restart httpd

# 验证两个域名是否都能使用
[root@slave1 httpd-2.4.6]# curl -k www.ceshi.com
this is ceshi domain
[root@slave1 httpd-2.4.6]# curl -k www.test.com
this is ceshi domain

5 虚拟目录下的http服务

对实际目录进行遮掩

[root@slave1 httpd-2.4.6]# cat /etc/httpd/conf.d/vhost.conf
# 注意此处的链接已经是/var/www,也可以是/var,主要是为了赋权
<directory /var/www>
        allowoverride none
        require all granted
</directory>
<virtualhost 192.168.247.131:80>
        servername 192.168.247.131
        documentroot /var/www/jiami # 后面的这个文件不需要真实存在,也可以是其他名称
        alias /123 /var/www/123 # 将/var/www/123对外伪装为/123
</virtualhost>

[root@slave1 httpd-2.4.6]# mkdir /var/www/123

[root@slave1 httpd-2.4.6]# echo this is 123 > /var/www/123/index.html
[root@slave1 httpd-2.4.6]# systemctl restart httpd

# 结果验证,注意后面加上/123/也就是伪装后的目录
# /123/不能写成/123,最后的/如果去掉就是请求123这个文件,会返回404

[root@slave1 httpd-2.4.6]#  curl -k http://192.168.247.131/123/
this is 123
[root@slave1 httpd-2.4.6]#  curl -k 192.168.247.131/123/
this is 123

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

# newmima这个文件会被自动创建
[root@slave1 httpd-2.4.6]# htpasswd -c /etc/httpd/newmima laozuo
New password: 123456
Re-type new password: 123456
Adding password for user laozuo
[root@slave1 httpd-2.4.6]# cat /etc/httpd/newmima
laozuo:$apr1$qnUNcjKV$pMCk.Who1oRNaia7JbL37/
# 第二次的时候不要加参数-c,要不然前面的密码会被覆盖
[root@slave1 httpd-2.4.6]# htpasswd /etc/httpd/newmima wangjie
New password: 123456
Re-type new password: 123456
Adding password for user wangjie
[root@slave1 httpd-2.4.6]# cat /etc/httpd/newmima
laozuo:$apr1$qnUNcjKV$pMCk.Who1oRNaia7JbL37/
wangjie:$apr1$tShtB7Fb$75nDtTfXZiaP04j7/u1r11

[root@slave1 httpd-2.4.6]# cat /etc/httpd/conf.d/vhost.conf 
<directory /www>
        authtype basic
        authname "please login:"
        authuserfile /etc/httpd/newmima
        require user wangjie laozuo
</directory>
<virtualhost 192.168.247.131:80>
        servername 192.168.247.131
        documentroot /www/123
</virtualhost>

#验证
[root@slave1 httpd-2.4.6]# curl -k 192.168.247.131:80 -u wangjie
Enter host password for user 'wangjie': 123456
this is test
[root@slave1 httpd-2.4.6]# curl -k 192.168.247.131:80 -u laozuo
Enter host password for user 'laozuo': 123456
this is test

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值