Apache HTTP 服务器配置

一、Apache 安装

bash 复制代码
[root@localhost ~]# dnf install httpd -y
[root@localhost ~]# systemctl enable --now httpd

二、Apache 服务基本信息

项目 内容
软件名 httpd
端口 HTTP 80 / HTTPS 443
主配置文件 /etc/httpd/conf/httpd.conf
子配置文件目录 /etc/httpd/conf.d/
服务启动脚本 httpd.service
默认发布目录 /var/www/html
默认发布文件 index.html

三、Apache 的基本配置

1. 发布文件

a. 默认发布文件的生成

bash 复制代码
echo 172.25.254.100 > /var/www/html/index.html

cat /var/www/html/index.html

测试:

bash 复制代码
[root@localhost ~]# curl 192.168.17.138
192.168.17.138

b. 默认发布文件的修改

修改主配置文件,调整 DirectoryIndex 的优先级:

bash 复制代码
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf

<IfModule dir_module>
     DirectoryIndex test.html  index.html
</IfModule>

[root@localhost ~]# systemctl reload httpd
[root@localhost ~]# echo test > /var/www/html/test.html

[root@localhost ~]# curl 192.168.17.138
test



2. 修改默认发布目录

a. 生成新的默认发布目录和默认发布文件

bash 复制代码
[root@localhost ~]# mkdir /var/web/html -p
[root@localhost ~]# echo 123 > /var/web/html/index.html

修改主配置文件:

apache 复制代码
DocumentRoot "/var/web/html"
<Directory "/var/web">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
bash 复制代码
[root@localhost ~]# systemctl restart httpd

测试:

bash 复制代码
[root@localhost ~]# curl 192.168.17.138
123

四、修改服务端口

bash 复制代码
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf

将文件中的 Listen 80 修改为:

apache 复制代码
Listen 8080

测试:

bash 复制代码
[root@localhost ~]# netstat -antlupe | grep httpd

五、目录的访问控制

1. 基于 IP 的访问控制

需求: 访问 http://192.168.17.138/test1 时,只允许 192.168.17.131 主机访问,其他主机拒绝。

bash 复制代码
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
apache 复制代码
<Directory "/var/www/html/test1">
    Order deny,allow
    Deny from ALL
    Allow from 192.168.17.131
</Directory>
bash 复制代码
[root@localhost ~]# systemctl restart httpd



2. 基于用户认证的访问控制

需求: 访问 http://192.168.17.138/test2 目录时,需要通过用户名密码认证。

第一步:创建密码文件

bash 复制代码
[root@localhost ~]# htpasswd -cm /etc/httpd/.htpasswd 123
New password: 
Re-type new password: 
Adding password for user 123

⚠️ 注意:如果文件已存在,不要用 -c 参数(-c 会创建新文件并覆盖旧文件)。

第二步:配置认证

bash 复制代码
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
apache 复制代码
<Directory "/var/www/html/test2">
    AuthUserFile /etc/httpd/.htpasswd
    AuthName "Please input username and passwd"
    AuthType basic
    Require valid-user
</Directory>



六、Apache 虚拟主机

需求: 发布三个站点:www.qwe.comnews.qwe.combbs.qwe.com

1. 建立各个站点的默认发布目录和文件

bash 复制代码
[root@bogon /]# mkdir /var/www/virtual/qwe.com/news -p
[root@bogon /]# mkdir /var/www/virtual/qwe.com/bbs
[root@bogon /]# mkdir /var/www/virtual/qwe.com/www
bash 复制代码
[root@bogon /]# echo hello1 > /var/www/virtual/qwe.com/news/index.html
[root@bogon /]# echo hello2 > /var/www/virtual/qwe.com/bbs/index.html
[root@bogon /]# echo hello3 > /var/www/virtual/qwe.com/www/index.html

2. 配置虚拟主机

bash 复制代码
[root@bogon ~]# vim /etc/httpd/conf.d/vhosts.conf
apache 复制代码
<VirtualHost _default_:80>
        DocumentRoot /var/www/html
        CustomLog /etc/httpd/logs/default.log combined
</VirtualHost>

<VirtualHost *:80>
        ServerName www.qwe.com
        DocumentRoot /var/www/virtual/qwe.com/www
        CustomLog /etc/httpd/logs/www.log combined
</VirtualHost>

<VirtualHost *:80>
        ServerName news.qwe.com
        DocumentRoot /var/www/virtual/qwe.com/news
        CustomLog /etc/httpd/logs/news.log combined
</VirtualHost>

<VirtualHost *:80>
        ServerName bbs.qwe.com
        DocumentRoot /var/www/virtual/qwe.com/bbs
        CustomLog /etc/httpd/logs/bbs.log combined
</VirtualHost>
bash 复制代码
[root@bogon bbs]# systemctl restart httpd

测试: 在客户端配置 hosts 文件

bash 复制代码
root@qwe-virtual-machine:~# vim /etc/hosts

添加以下内容:

复制代码
192.168.17.138  www.qwe.com news.qwe.com bbs.qwe.com

图片说明:


七、HTTPS 配置

1. 生成自签名证书

bash 复制代码
[root@bogon ~]# mkdir /etc/httpd/certs
[root@bogon ~]# openssl req -newkey rsa:2048 -nodes -sha256 \
  -keyout /etc/httpd/certs/qw.org.key \
  -x509 -days 365 \
  -out /etc/httpd/certs/qw.org.crt

2. 安装加密套件

bash 复制代码
[root@bogon ~]# dnf install mod_ssl

3. 配置加密虚拟主机

bash 复制代码
[root@bogon ~]# mkdir /var/www/virtual/qwe.com/login
[root@bogon ~]# echo jiami > /var/www/virtual/qwe.com/login/index.html

[root@bogon ~]# vim /etc/httpd/conf.d/vhosts.conf
apache 复制代码
# HTTP -> HTTPS 强制跳转
<VirtualHost *:80>
        ServerName login.qwe.com
        RewriteEngine On
        RewriteRule ^/(.*)$ https://login.qwe.com/$1 [R=301]
</VirtualHost>

# HTTPS 虚拟主机
<VirtualHost *:443>
        ServerName login.qwe.com
        DocumentRoot /var/www/virtual/qwe.com/login
        CustomLog /ert/httpd/logs/login.log combined
        SSLEngine on
        SSLCertificateFile /etc/httpd/certs/qw.org.crt
        SSLCertificateKeyFile /etc/httpd/certs/qw.org.key
</VirtualHost>

图片说明:

测试:



相关推荐
Gl�ria3 分钟前
Linux 清理日志常用命令
linux·运维·服务器
天道jimmy1 小时前
VulnHub 系列:HA, Wordy
linux·服务器·web安全
右耳朵猫AI2 小时前
Go周刊2026W36 | Go 1.27.1 发布、TinyGo 0.42、HTTP/2 原生迁入 net/http、quic-go 0.62
redis·http·golang
j7~2 小时前
【Linux网络加餐(二)】《进程组,会话,控制终端,作业控制和守护进程》---详解
linux·运维·服务器·守护进程·会话·进程组·控制终端
天道jimmy2 小时前
VulnHub 系列:Prime,1
linux·运维·服务器
辻弋2014 小时前
自动检测硬件生成驱动清单、实时监控FPS、120秒性能记录——30+项优化模块化开关,所有改动可精确复原
服务器·windows·游戏引擎·电脑·开源软件
程序员梅雨4 小时前
Linux & Shell 实用干货
linux·运维·服务器·后端·面试·php
新时代牛马4 小时前
嵌入式 Linux 蓝牙框架完整篇:从 HCI、L2CAP 到BlueZ 用户态
linux·运维·服务器
ONLYOFFICE5 小时前
ONLYOFFICE成为Linux操作系统 – Winux的预装办公套件
linux·运维·服务器
其实防守也摸鱼6 小时前
OpenClaw Windows 踩坑实战指南
运维·服务器·数据库·windows·github·copilot