当前位置: 首页 > news >正文

一分钟快速了解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

2. 默认发布目录

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

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

#建立默认发布目录
[root@apache ~]# mkdir /web/html -p#修改配置文件
[root@apache ~]# vim /etc/httpd/conf/httpd.confDocumentRoot "/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

原文件为下图: 

修改后为下图这样:

3. 默认发布文件

#建立新的默认发布文件
[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.crtCountry 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.orgDocumentRoot /var/www/virtual/timinglee.org/bbs/
</VirtualHost><VirtualHost *:80>ServerName news.timinglee.orgDocumentRoot /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.org2.测试效果
[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
http://www.lryc.cn/news/586397.html

相关文章:

  • Redis集群会有写操作丢失吗?为什么?
  • 动态规划基本操作
  • 从LLM到VLM:视觉语言模型的核心技术与Python实现
  • FastAdmin项目开发三
  • (LeetCode 面试经典 150 题 )3. 无重复字符的最长子串 (哈希表+双指针)
  • 回归(多项式回归)
  • 算法练习6-大数乘法(高精度乘法)
  • Linux系统中部署Redis详解
  • (C++)STL:list认识与使用全解析
  • OpenEuler操作系统测试USB摄像头
  • The Black Heart
  • AOSP Settings模块问题初窥
  • day03-链表part1
  • C++类模版1
  • HTTP和HTTPS部分知识点
  • JAVA开发
  • 【数据结构初阶】--顺序表(三)
  • 广东省省考备考(第四十三天7.12)——数量(第四节课)
  • kettle从入门到精通 第101课 ETL之kettle DolphinScheduler调度kettle
  • 亚矩阵云手机:重构物流供应链,让跨境包裹“飞”得更快更准
  • 配置驱动开发:初探零代码构建嵌入式软件配置工具
  • ESP32使用freertos更新lvgl控件内容
  • TDengine 使用最佳实践(1)
  • Cell2location maps fine-grained cell types in spatial transcriptomics 文章解析
  • 全局唯一id生成
  • JavaScript加强篇——第七章 浏览器对象与存储要点
  • 深度学习-卷积化
  • 深入详解:决策树在医学影像领域心脏疾病诊断的应用及实现细节
  • Vue框架之钩子函数详解
  • ngrok使用