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
相关推荐
治愈系贝壳1 小时前
ARM --- day 6 时钟
linux·arm开发·学习
Molesidy2 小时前
【GUI】【AWTK】基于开阳平台的GUI界面设计环境搭建
linux·gui·awtk
RisunJan2 小时前
Linux命令-semanage(SELinux 策略管理)
linux·运维·服务器
流浪0012 小时前
Linux系统篇 21:文件(五)——动静态库、ELF 底层原理全解
linux·运维·服务器
Dawn-bit2 小时前
Linux救援模式和系统恢复
linux·运维·服务器·云计算·运维开发
十八岁牛爷爷3 小时前
Linux 进程与进程状态・三层级深度解析
linux·运维·服务器
chexus3 小时前
21. 深入 Nginx HTTP 缓存源码:CDN功能
nginx·http·缓存
不会就选b3 小时前
linux之进程管理(二)--替换
linux·运维·服务器