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

Apache HTTP Server 从安装到配置

一、Apache 是什么?

Apache(全称 Apache HTTP Server)是当前最流行的开源Web服务器软件之一,由Apache软件基金会维护。它以稳定性高、模块化设计灵活的配置著称,支持Linux、Windows等多平台,是搭建个人博客、企业官网乃至复杂Web应用的首选工具。

#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

# 1. 安装Apache
sudo dnf install httpd -y# 2. 放行防火墙(允许HTTP/HTTPS流量)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload# 3. 启动并设置开机自启
sudo systemctl enable --now httpd# 4.生成默认测试页文件
echo 172.25.254.100 > /var/www/html/index.html# 5.测试:
curl 172.25.254.100
172.25.254.100

三、Apache的基本配置信息

1.端口修改

#修改配置文件
[root@apache ~]# vim /etc/httpd/conf/httpd.conf
47 Listen 8080#刷新服务
[root@apache ~]# systemctl reload httpd#设定火墙通过
[root@apache ~]# firewall-cmd --permanent --add-port=8080/tcp
success[root@apache ~]# firewall-cmd --reload#检测
[root@apache ~]# netstat -antlupe | grep httpd
tcp6       0      0 :::8080                 :::*                   LISTEN      0 78081      32315/httpd#访问:
[root@apache ~]# curl 172.25.254.100:8080
172.25.254.100

 2.默认发布目录

修改selinux  ——开着的话会有所影响
grubby --update-kernel ALL --args selinux=0reboot ——重启getenforce
Disabled#建立默认发布目录
[root@apache ~]# mkdir /web/html -p#修改配置文件
[root@apache ~]# vim /etc/httpd/conf/httpd.conf
125 DocumentRoot "/web/html" #指定默认发布目录位置
126 <Directory "/web/html"> 
127         Require all granted #对于目录访问进行授权
128 </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
172 <IfModule dir_module>
173     DirectoryIndex lee.html index.html
174 </IfModule>#重启服务
[root@apache ~]# systemctl reload httpd#测试:
[root@apache ~]# curl 172.25.254.100:8080
/web/html/lee's page

4.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#编辑主配置文件
[root@apache ~]# vim /etc/httpd/conf.d/ssl.conf86 SSLCertificateFile /etc/httpd/certs/timinglee.org.crt95 SSLCertificateKeyFile /etc/httpd/certs/timinglee.org.key#重启服务
root@apache ~]# systemctl reload httpd
[root@apache ~]# netstat -antlupe | grep httpd
tcp6       0     0 :::443                 :::*                   LISTEN     0 85111     33518/httpd
tcp6       0     0 :::80                   :::*                   LISTEN     0 80172     33518/httpd#在浏览器中访问
https://服务器ip

5.apache的虚拟主机

修改selinux  ——有所影响
grubby --update-kernel ALL --args selinux=1reboot ——重启getenforce
Enforcing#为每个发布站点建立默认发布目录
[root@apache ~]# mkdir -p /var/www/virtual/timinglee.org/news
[root@apache ~]# mkdir -p /var/www/virtual/timinglee.org/bbs#为每个站点建立默认发布文件
[root@apache ~]# echo new.timinglee.org > /var/www/virtual/timiniglee.org/news/index.html
[root@apache ~]# echo bbs.timinglee.org > /var/www/virtual/timiniglee.org/bbs/index.html#修改配置文件
[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/timiniglee.org/bbs/
</VirtualHost><VirtualHost *:80>ServerName news.timinglee.orgDocumentRoot /var/www/virtual/timiniglee.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 mariadb.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.xdnf.cn/news/15295.html

相关文章:

  • 【Linux仓库】虚拟地址空间【进程·陆】
  • 未来软件开发的新方向:从工程到智能的深度演进
  • Claude Code:完爆 Cursor 的编程体验
  • 剑指offer——链表:从尾到头打印链表
  • 上位机知识篇---SD卡U盘镜像
  • [论文阅读] 人工智能 + 软件工程 | LLM辅助软件开发:需求如何转化为代码?
  • 链表算法之【判断链表中是否有环】
  • 千辛万苦3面却倒在性格测试?这太离谱了吧!
  • 【C++】内联函数inline以及 C++入门(4)
  • 自动评论+AI 写作+定时发布,这款媒体工具让自媒体人躺赚流量
  • C++(STL源码刨析/List)
  • PyTorch中的torch.argmax()和torch.max()区别
  • 标准化模型格式ONNX介绍:打通AI模型从训练到部署的环节
  • 基于Springboot+UniApp+Ai实现模拟面试小工具二:后端项目搭建
  • 上位机知识篇---安装包架构
  • java集合类
  • 输入流挂起
  • 人脸图像生成(DCGAN)
  • Java线程进阶-并发编程
  • python的病例管理系统
  • halcon 求一个tuple的极值点
  • 性能狂飙 Gooxi 8卡5090服务器重新定义高密度算力
  • 深入剖析Spring Bean生命周期:从诞生到消亡的全过程
  • JavaSE——Object
  • Linux驱动基本概念(内核态、用户态、模块、加载、卸载、设备注册、字符设备)
  • DSSA(Domain-Specific Software Architecture)特定领域架构
  • 台球 PCOL:极致物理还原的网页斯诺克引擎(附源码深度解析)
  • Leaflet面试题及答案(21-40)
  • 2025年体育科学与健康大数据国际会议(ICSSHBD 2025)
  • OpenAI 将推 AI Agent 浏览器:挑战 Chrome,重塑上网方式