一分钟快速了解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
    #在第172行
    DirectoryIndex lee.html index.html

    #重启服务
    [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
相关推荐
ly76892 天前
Kafka 消费者重试与死信:Spring for Apache Kafka 的边界设计
spring·kafka·apache·并发消费·消费者重试·死信主题·位移提交
HoneyMoose2 天前
Ubuntu Apache 配置多站点提示 SSL 错误
ubuntu·apache·ssl
骑着蜗牛撵大象3272 天前
一套引擎撑起树表双模型:Apache IoTDB 2.0 与 TsFile 的选型答卷
apache·时序数据库·iotdb·tsfile·树模型·表模型
凤山老林3 天前
Spring Boot 集成 Apache CXF 发布与消费 SOAP WebService:WSDL、WS-Security
spring boot·后端·apache·webservice·soap·apache cxf
inquisiter3 天前
KernelGpt的部分尝试
java·前端·apache
十碗饭吃不饱3 天前
Apache POI XWPFTableCell 表格单元格排版踩坑总结
apache
betazhou4 天前
Apache Seatunnel 抽取SQL server实时同步到Oracle-多表同步(五)
数据库·oracle·sqlserver·apache·seatunnel
青企通5 天前
电商小程序购物车与库存扣减:SKU 模型、合并规则与防超卖实践
小程序·apache
李纲明6 天前
WordPress 首页正常、内页全 404:固定链接与伪静态规则排查步骤(Apache / Nginx)
nginx·apache·wordpress
betazhou6 天前
Apache seatunel配置-数据同步测试(直接把采集数据打印到控制面板) - SQL server
sqlserver·apache·seatunnel