引言
CentOS Stream 是一个基于 Red Hat Enterprise Linux (RHEL) 的免费操作系统,它提供了最新的 RHEL 版本。部署 Web 服务器是许多系统管理员和开发者的基本任务之一。本文将详细介绍如何在 CentOS Stream 上搭建一个高效的 Web 服务器,包括环境准备、软件安装、配置以及安全加固等步骤。
环境准备
在开始之前,请确保您的 CentOS Stream 系统满足以下条件:
- 系统已更新至最新版本。
- 网络连接正常。
- 有足够的磁盘空间。
更新系统
sudo yum update -y
安装必要的依赖
sudo yum install -y httpd httpd-devel
安装 Apache Web 服务器
Apache HTTP Server 是最流行的 Web 服务器之一,我们将使用它来搭建 Web 服务器。
安装 Apache
sudo yum install -y httpd
启动 Apache 服务
sudo systemctl start httpd
设置 Apache 服务开机自启
sudo systemctl enable httpd
配置 Apache
配置 Apache 以满足您的需求,包括设置虚拟主机、目录索引和错误日志等。
设置虚拟主机
编辑 /etc/httpd/conf/httpd.conf
文件,找到 ServerName
配置项,并设置您的域名。
sudo nano /etc/httpd/conf/httpd.conf
创建虚拟主机配置文件
在 /etc/httpd/conf.d/
目录下创建一个新的配置文件,例如 example.com.conf
。
sudo nano /etc/httpd/conf.d/example.com.conf
添加以下内容:
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
重启 Apache 以应用更改
sudo systemctl restart httpd
安全加固
为了确保 Web 服务器的安全性,以下是一些重要的安全加固措施。
限制 root 用户访问
编辑 /etc/httpd/conf/httpd.conf
文件,找到 AllowOverride
配置项,并设置为 None
。
sudo nano /etc/httpd/conf/httpd.conf
添加以下行:
<Directory />
AllowOverride None
</Directory>
限制目录索引
编辑虚拟主机配置文件,找到 Options
配置项,并移除 Indexes
。
sudo nano /etc/httpd/conf.d/example.com.conf
移除或注释以下行:
Options Indexes
配置 SSL/TLS
为了提高安全性,建议使用 HTTPS。您可以使用 Let’s Encrypt 免费获取 SSL/TLS 证书。
安装 Certbot
sudo yum install -y certbot python2-certbot-apache
运行 Certbot
sudo certbot --apache
Certbot 会自动为您配置 SSL/TLS,并更新虚拟主机配置文件。
总结
通过以上步骤,您已经在 CentOS Stream 上成功搭建了一个基本的 Web 服务器。根据您的需求,您还可以进一步配置和优化您的服务器。记得定期更新系统和软件,以确保服务器的安全性和稳定性。