Apache HTTP Server 安装、配置与高级功能详解

Apache核心功能

  1. 托管静态网页和动态内容(如 PHP、Python 等)。
  2. 支持 HTTP/2、HTTPS 等现代协议。
  3. 提供虚拟主机配置,支持多域名托管。
  4. 日志记录、访问控制和流量管理。

应用场景

  1. 企业级网站和应用程序部署。
  2. 作为反向代理或负载均衡器。
  3. 与 MySQL、PHP 组成 LAMP/LNMP 栈开发动态网站。

Apache安装

bash 复制代码
[root@nfs-server ~]# dnf install httpd -y
[root@nfs-server ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

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 复制代码
[root@nfs-server ~]# echo 172.25.254.100 > /var/www/html/index.html
[root@nfs-server ~]# cat /var/www/html/index.html
172.25.254.100

测试:

bash 复制代码
[root@nfs-server ~]# curl  172.25.254.100
172.25.254.100
b默认发布文件的修改
bash 复制代码
[root@nfs-server ~]# vim /etc/httpd/conf/httpd.conf
168 <IfModule dir_module>
169     DirectoryIndex lee.html index.html
170 </IfModule>

[root@nfs-server ~]# systemctl reload httpd
[root@nfs-server ~]# echo lee > /var/www/html/lee.html

测试:

bash 复制代码
[root@nfs-server ~]# curl  172.25.254.100
lee
[root@nfs-server ~]# cat /var/www/html/lee.html
lee
[root@nfs-server ~]# rm -fr /var/www/html/lee.html
[root@nfs-server ~]# curl  172.25.254.100
172.25.254.100

2.修改默认发布目录

a.生成新的默认发布目录和默认发布文件
bash 复制代码
[root@nfs-server ~]# mkdir  /var/web/html
[root@nfs-server ~]# echo /var/web/html > /var/web/html/index.html
b.配置使用新的默认发布目录
bash 复制代码
[root@nfs-server ~]# vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"
DocumentRoot "/var/web/html"
<Directory "/var/web">					#对/var/web目录授权,否则服务拒绝访问
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

[root@nfs-server ~]# systemctl restart httpd		

测试:

bash 复制代码
[root@nfs-server ~]# curl   172.25.254.100
/var/web/html

四.修改服务端口

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

DocumentRoot "/var/www/html"
#DocumentRoot "/var/web/html"
47 Listen 8080

[root@nfs-server ~]# systemctl restart httpd

测试:

bash 复制代码
[root@nfs-server ~]# netstat -antlupe | grep httpd
tcp6       0      0 :::8080                 :::*                    LISTEN      0          79486      30895/httpd
[root@nfs-server ~]# curl  172.25.254.100:8080
172.25.254.100

五.目录的访问控制

1.基于IP的访问控制

在访问http://172.25.254.100/lee 只能让172.25.254.1主机访问其他人不能

bash 复制代码
[root@nfs-server ~]# mkdir  /var/www/html/lee
[root@nfs-server ~]# echo /var/www/html/lee > /var/www/html/lee/index.html

[root@nfs-server ~]# vim /etc/httpd/conf/httpd.conf
Listen 80
<Directory "/var/www/html/lee">
  Order deny,allow
  Deny from ALL
  Allow from 172.25.254.1
</Directory>
[root@nfs-server ~]# systemctl restart http

测试:

bash 复制代码
[root@nfs-server ~]# curl  172.25.254.100/lee
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
</body></html>


#在windows中的浏览器中访问http://172.25.254.100/lee 是可以的

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

当访问http://172.25.254.100/timinglee目录时,需要通过用户认证否则拒绝访问

bash 复制代码
[root@nfs-server ~]# mkdir  -p /var/www/html/timinglee
[root@nfs-server ~]# echo /var/www/html/timinglee > /var/www/html/timinglee/index.html

#建立认证文件
[root@nfs-server ~]# htpasswd -cm /etc/httpd/.htpasswd lee
New password:
Re-type new password:
Adding password for user lee

#配置apache目录访问认证
[root@nfs-server ~]# vim /etc/httpd/conf/httpd.conf
	
<Directory "/var/www/html/timinglee">
  AuthUserFile /etc/httpd/.htpasswd
  AuthName "Please input username and password"
  AuthType basic
  Require valid-user
</Directory>

[root@nfs-server ~]# systemctl restart httpd

测试:

bash 复制代码
[root@nfs-server ~]# curl  172.25.254.100/timinglee/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>

[root@nfs-server ~]# curl  172.25.254.100/timinglee/ -ulee:lee
/var/www/html/timinglee

六.Apache服务器的虚拟主机

服务Ip是非常有限的资源,Apache默认只能发布一个站点,但是企业可能有多个站点要发布,那么就需要Apache的虚拟机技术

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

www.timinglee.org 访问默认

bbs.timinglee.org /var/www/virtual/timinglee.org/bbs

news.timinglee.org /var/www/virtual/timinglee.org/news

bash 复制代码
[root@nfs-server ~]# mkdir  /var/www/virtual/timinglee.org/news -p
[root@nfs-server ~]# mkdir  /var/www/virtual/timinglee.org/bbs -p
[root@nfs-server ~]# echo news.timinglee.org > /var/www/virtual/timinglee.org/news/index.html
[root@nfs-server ~]# echo bbs.timinglee.org /var/www/virtual/timinglee.org/bbs/index.html
bbs.timinglee.org /var/www/virtual/timinglee.org/bbs/index.html
[root@nfs-server ~]# echo bbs.timinglee.org > /var/www/virtual/timinglee.org/bbs/index.html

2.配置虚拟主机

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

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

<VirtualHost *:80>
    ServerName bbs.timinglee.org
    DocumentRoot /var/www/virtual/timinglee.org/bbs
    CustomLog /etc/httpd/logs/bbs.log combined
</VirtualHost>

测试:

bash 复制代码
#在哪里测试就在那个主机中做解析
[root@nfs-server ~]# vim /etc/hosts
172.25.254.100      nfs-server www.timinglee.org   news.timinglee.org  bbs.timinglee.org


[root@nfs-server ~]# curl  www.timinglee.org
172.25.254.100
[root@nfs-server ~]# curl  bbs.timinglee.org
bbs.timinglee.org
[root@nfs-server ~]# curl  news.timinglee.org
news.timinglee.org

七.Apache 的https访问

1.生成证书

bash 复制代码
[root@nfs-server ~]# mkdir  /etc/httpd/certs
[root@nfs-server ~]# openssl req  -newkey rsa:2048 -nodes -sha256 -keyout /etc/httpd/certs/timinglee.org.key -x509 -days 365 -out /etc/httpd/certs/timinglee.org.crt
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Shannix
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@nfs-server ~]# ls /etc/httpd/certs/
timinglee.org.crt  timinglee.org.key

2.安装加密套件

bash 复制代码
[root@nfs-server ~]# dnf install mod_ssl -y
[root@nfs-server ~]# mkdir  /var/www/virtual/timinglee.org/login -p
[root@nfs-server ~]# echo login.timinglee.org > /var/www/virtual/timinglee.org/login/index.html

3.配置加密

bash 复制代码
[root@nfs-server ~]# vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
    ServerName login.timinglee.org
    RewriteEngine On
    RewriteRule ^/(.*)$ https://login.timinglee.org/$1 [R=301]
</VirtualHost>

<VirtualHost *:443>
    ServerName login.timinglee.org
    DocumentRoot /var/www/virtual/timinglee.org/login
    CustomLog /etc/httpd/logs/login.log combined
    SSLEngine on
    SSLCertificateFile /etc/httpd/certs/timinglee.org.crt
    SSLCertificateKeyFile /etc/httpd/certs/timinglee.org.key
</VirtualHost>
[root@nfs-server ~]# systemctl restart httpd

测试:

bash 复制代码
[root@nfs-server ~]# vim /etc/hosts
172.25.254.100      nfs-server www.timinglee.org   news.timinglee.org  bbs.timinglee.org login.timinglee.org

[root@nfs-server ~]# curl  -kL login.timinglee.org
login.timinglee.or
相关推荐
Android系统攻城狮1 小时前
Linux FFmpeg深度解析之avcodec_flush_buffers调用流程与实战(十九)
linux·运维·服务器·ffmpeg·车载音视频·ffmpeg音视频进阶
水饺编程2 小时前
第2章:Linux 基础,解压缩【.tar.bz2】压缩包文件
linux·c语言·汇编
Zevalin爱灰灰3 小时前
Linux入门第二部分(ARM裸机篇) 第二章 Cortex-A7 MPCore架构与ARM汇编基础
linux·汇编·arm开发·嵌入式
倔强的石头1063 小时前
【Linux指南】进程通信系列(七):System V 信号量 —— 同步互斥的内核原语
linux·运维·服务器
GeW3 小时前
从"补短板"到"强优势"Linux与数据库认证成为产业跃迁人才支点
linux·数据库
PellyKoo5 小时前
【Ubuntu】GTX 1080 Ti 显卡占用为0但功率高达60W的排查与解决
linux·运维·ubuntu
Fruiticecake6 小时前
epoll 回显服务器是怎么工作的:从 accept 队列到就绪链表,以及它和 select / poll 的区别
linux
承渊政道6 小时前
Linux网络学习【从NAT与代理服务理解网络转发、探索内网穿透原理、再到认识DNS与ICMP、最后动手部署并测试内网穿透】
linux·网络·学习·内网穿透·dns·nat
最强小杰6 小时前
minimax-m3 突然报 HTTP 529 怎么办?连续重试为何更难恢复,退避时间该怎么算?
网络·网络协议·http·ai
重生之小比特6 小时前
【Linux系统】17 库制作与原理(下)
linux·运维·服务器