一分钟快速了解Apache

一. Apache 的安装

复制代码
#apache的基本信息
/etc/httpd/conf                      #apache的配置目录
/etc/http/conf.d                     #子配置目录
/etc/httpd/conf/httpd.conf           #主配置文件
/lib/systemd/system/htpd.service     #启动文件
:80                                  #默认端口
/var/www/html                        #默认发布目录
index.html                           #默认发布文件

#安装apache
[root@apache ~]# dnf install httpd -y

#在火墙中放行web服务
[root@apache ~]# firewall-cmd --permanent --add-service=http
success
[root@apache ~]# firewall-cmd --permanent --add-service=https
success

#开启服务
[root@apache ~]# systemctl enable --now httpd

#生成默认测试页文件
[root@apache ~]# echo 172.25.254.100 > /var/www/html/index.html

#测试:
[root@apache ~]# curl 172.25.254.100
172.25.254.100

二. Apache 的基本配置信息

  1. 端口修改

    #修改配置文件
    [root@apache ~]# vim /etc/httpd/conf/httpd.conf
    Listen 8080 #在第47行,原本为Listen 80,修改为监听8080接口

    #刷新服务
    [root@apache ~]# systemctl reload httpd

    #设定火墙通过
    [root@apache ~]# firewall-cmd --permanent --add-port=8080/tcp
    success
    [root@apache ~]# firewall-cmd --reload
    success

    #检测
    [root@apache ~]# netstat -antlupe | grep httpd
    tcp6 0 0 :::8080 :::* LISTEN 0 32453 3698/httpd

    #访问:
    [root@apache ~]# curl 172.25.254.100:8080
    172.25.254.100

  1. 默认发布目录

记得要关闭selinux,或者将selinux设置成Permissive模式(宽容模式)。

不然会报错,访问不了发布的文件。

复制代码
#建立默认发布目录
[root@apache ~]# mkdir /web/html -p

#修改配置文件
[root@apache ~]# vim /etc/httpd/conf/httpd.conf

DocumentRoot "/web/html"         #指定默认发布目录位置,在第125行
<Directory "/web/html">
Require all granted              #对于目录访问进行授权
</Directory>

[root@apache ~]# systemctl restart httpd
[root@apache ~]# echo "/web/html's page" > /web/html/index.html
[root@apache ~]# curl 172.25.254.100:8080
/web/html's page

原文件为下图:

修改后为下图这样:

  1. 默认发布文件

    #建立新的默认发布文件
    [root@apache ~]# echo "/web/html/lee's page" > /web/html/lee.html

    #当没有对配置进行修改时新默认发布文件不会被默认访问
    [root@apache ~]# curl 172.25.254.100:8080
    /web/html's page
    [root@apache ~]# curl 172.25.254.100:8080/lee.html
    /web/html/lee's page

    #修改配置文件
    [root@apache ~]# vim /etc/httpd/conf/httpd.conf
    <IfModule dir_module> #在第172行
    DirectoryIndex lee.html index.html
    </IfModule>

    #重启服务
    [root@apache ~]# systemctl reload httpd

    #测试:
    [root@apache ~]# curl 172.25.254.100:8080
    /web/html/lee's page

原文件配置为下图:

修改后的配置文件为下图如此:

此阶段的实验完成之后记得把配置文件中的命令改回原来那样子,不然例如监听接口还是8080而没有改回80的情况,下面的实验又有用到80接口的情况,就会导致实验出错。

三. https

复制代码
#安装mod_ssl
[root@apache ~]# dnf install mod_ssl -y

#建立证书和key文件目录
[root@apache ~]# mkdir /etc/httpd/certs

#制作证书
#这些反斜杠\没有特殊含义,在这里是为了将选项分隔开,让看的时候更好分辨
[root@apache ~]# openssl req \
-newkey rsa:2048 \
-nodes \
-sha256 \
-keyout /etc/httpd/certs/timinglee.org.key \
-x509 \
-days 365 \
-out /etc/httpd/certs/timinglee.org.crt

Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Shannxi
Locality Name (eg, city) [Default City]:XI'AN
Organization Name (eg, company) [Default Company Ltd]:timinglee
Organizational Unit Name (eg, section) []:webserver
Common Name (eg, your name or your server's hostname) []:www.timinglee.org
Email Address []:timinglee@timinglee.org

#命令执行完成,证书出现
[root@apache ~]# ls /etc/httpd/certs/
timinglee.org.crt timinglee.org.key

#编辑主配置文件,下面显示的两个SSL前面的数字是编写文件时命令前面的行数,方便看的时候快速找到命令位置
[root@apache ~]# vim /etc/httpd/conf.d/ssl.conf
86 SSLCertificateFile /etc/httpd/certs/timinglee.org.crt
95 SSLCertificateKeyFile /etc/httpd/certs/timinglee.org.key

#重启服务
[root@apache ~]# systemctl reload httpd
[root@apache ~]# netstat -antlupe | grep httpd
tcp6    0    0    :::80        :::*      LISTEN      0   41893      4380/httpd          
tcp6    0    0    :::443       :::*      LISTEN      0   53432      4380/httpd   

#在浏览器中访问
https://服务器ip

原文件配置为下图:

修改后的文件配置为下图:

成功后通过浏览器进行访问:

因为我们的证书没有经过认证,所以会有危险网站的警告信息出现。

可以通过以下步骤查看我们验证的证书。

四. Apache的虚拟主机

复制代码
#为每个发布站点建立默认发布目录
[root@apache ~]# mkdir -p /var/www/virtual/timinglee.org/news
[root@apache ~]# mkdir -p /var/www/virtual/timinglee.org/bbs

#为每个站点建立默认发布文件
[root@apache ~]# echo news.timinglee.org > /var/www/virtual/timinglee.org/news/index.html
[root@apache ~]# echo bbs.timinglee.org > /var/www/virtual/timinglee.org/bbs/index.html

#修改配置文件
# DocumentRoot指定的是默认发布目录
[root@apache ~]# vim /etc/httpd/conf.d/vhosts.conf

<VirtualHost _default_:80>
    DocumentRoot /var/www/html
</VirtualHost>

<VirtualHost *:80>
    ServerName bbs.timinglee.org
    DocumentRoot /var/www/virtual/timinglee.org/bbs/
</VirtualHost>

<VirtualHost *:80>
    ServerName news.timinglee.org
    DocumentRoot /var/www/virtual/timinglee.org/news/
</VirtualHost>

#刷新服务
[root@apache ~]# systemctl reload httpd

#测试:
1.在浏览器所在主机中手动编写本地解析文件
[root@apache ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
#加入虚拟主机解析域名
172.25.254.100 apache.timinglee.org www.timinglee.org news.timinglee.org bbs.timinglee.org

2.测试效果
[root@apache ~]# curl www.timinglee.org
172.25.254.100
[root@apache ~]# curl bbs.timinglee.org
bbs.timinglee.org
[root@apache ~]# curl news.timinglee.org
new.timinglee.org
相关推荐
Fantasy丶夜雨笙歌7 小时前
Apache HTTP Server 从安装到配置
网络协议·http·apache
麟城Lincoln9 小时前
【Linux-云原生-笔记】Apache相关
linux·笔记·云原生·apache·webserver
ZZZKKKRTSAE9 小时前
玩转rhel9 Apache
linux·运维·服务器·apache·web
HashData酷克数据20 小时前
Apache Cloudberry 向量化实践(二):如何识别和定位向量化系统的性能瓶颈?
apache
HashData酷克数据2 天前
流批一体的“奥卡姆剃刀”:Apache Cloudberry 增量物化视图应用解析
apache
lang201509282 天前
什么是Apache Ignite的affinity(亲和性)
apache
Apache IoTDB2 天前
Apache IoTDB V2.0.4 发布|新增用户自定义表函数及多种内置表函数功能
apache·iotdb
TracyCoder1233 天前
Apache Shiro 框架详解
安全·apache·shiro·认证·登录