Apache HTTP Server 是全球最受欢迎的网页服务器软件之一,它具有稳定、可靠、功能丰富等特点。本文将为您详细介绍如何在 Rocky Linux 8.3 上安装 Apache 服务,并进行详细配置,以搭建一个高效网站服务器。
1. 安装 Apache 服务
1.1 安装 Apache 包
首先,您需要使用 Rocky Linux 的包管理器
dnf
来安装 Apache。以下是安装命令:
sudo dnf install httpd
1.2 启动 Apache 服务
安装完成后,启动 Apache 服务:
sudo systemctl start httpd
1.3 设置 Apache 服务开机自启
为了在系统重启后自动启动 Apache 服务,您需要设置开机自启:
sudo systemctl enable httpd
2. 配置 Apache 服务
2.1 访问 Apache 默认网站
在浏览器中输入您的服务器 IP 地址,您应该能看到 Apache 的默认欢迎页面。
2.2 修改默认网站配置
默认情况下,Apache 的配置文件位于
/etc/httpd/conf/httpd.conf
。为了修改默认网站配置,您需要编辑该文件。
sudo nano /etc/httpd/conf/httpd.conf
在配置文件中找到以下行:
#DocumentRoot “/var/www/html”
将注释取消,并将
DocumentRoot
的值修改为您自己的网站根目录,例如:
DocumentRoot “/var/www/mywebsite”
保存并关闭文件。
2.3 创建虚拟主机
如果您需要配置多个网站,可以创建虚拟主机。以下是创建虚拟主机的步骤:
创建虚拟主机配置文件:
sudo nano /etc/httpd/conf.d/mywebsite.conf
在配置文件中添加以下内容:
ServerAdmin webmaster@mywebsite.com
ServerName mywebsite.com
ServerAlias www.mywebsite.com
DocumentRoot “/var/www/mywebsite”
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
保存并关闭文件。
2.4 重新加载 Apache 服务
在修改配置文件后,重新加载 Apache 服务以应用更改:
sudo systemctl reload httpd
3. 安全配置
3.1 限制访问
为了提高网站安全性,您可以通过
Allow
和
Deny
指令来限制访问。以下示例中,我们限制了所有 IP 地址的访问,只允许来自特定 IP 地址的访问:
Order allow,deny
Allow from 192.168.1.100
Deny from all
3.2 SSL 配置
为了提高数据传输安全性,您可以为网站配置 SSL。以下是使用
certbot
生成 SSL 证书的步骤:
安装
certbot
:
sudo dnf install certbot python3-certbot-apache
运行
certbot
来生成 SSL 证书:
sudo certbot –apache
按照提示完成证书申请流程后,Apache 将自动配置 SSL,并重新加载服务。
4. 总结
本文详细介绍了如何在 Rocky Linux 8.3 上安装 Apache 服务,并进行详细配置。通过本文,您可以轻松搭建一个高效、安全的网站服务器。祝您使用愉快!