CentOS 7作为一款稳定、安全且开源的Linux发行版,被广泛应用于企业级Web服务器搭建,其基于Red Hat Enterprise Linux(RHEL)的源代码构建,提供了强大的包管理工具(YUM)、长期支持周期以及丰富的社区资源,使其成为中小型企业和个人开发者的首选平台之一,以下将从环境准备、基础配置、服务安装、安全加固及性能优化等方面,详细介绍CentOS 7 Web服务器的搭建与运维要点。

环境准备与基础系统配置
在搭建Web服务器前,需确保系统满足基本要求:建议至少2GB内存、20GB以上硬盘空间,以及稳定的网络连接,通过ip addr或ifconfig命令查看服务器IP地址,并使用ping测试网络连通性,更新系统至最新状态,执行以下命令:
sudo yum update -y sudo reboot
重启后,配置主机名以方便管理,
sudo hostnamectl set-hostname web-server
关闭防火墙和SELinux(生产环境建议谨慎配置):
sudo systemctl stop firewalld sudo systemctl disable firewalld sudo setenforce 0 sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
若需启用防火墙,可使用firewall-cmd开放HTTP(80)和HTTPS(443)端口:

sudo systemctl start firewalld sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
安装与配置Web服务
安装Apache HTTP服务器
Apache是最流行的Web服务器软件之一,支持模块化扩展和虚拟主机配置,通过YUM安装:
sudo yum install httpd -y
安装完成后,启动服务并设置开机自启:
sudo systemctl start httpd sudo systemctl enable httpd
测试服务是否运行正常,在浏览器中访问服务器IP地址,若显示“Apache 2 Test Page”则表示成功。
安装Nginx(可选)
若需更高性能的Web服务器,可安装Nginx,其反向代理和负载均衡能力更优,安装命令:

sudo yum install epel-release -y sudo yum install nginx -y sudo systemctl start nginx sudo systemctl enable nginx
Apache与Nginx可通过不同端口共存,避免冲突。
配置虚拟主机
虚拟主机允许单台服务器托管多个网站,以Apache为例,创建虚拟主机配置文件:
sudo vim /etc/httpd/conf.d/example.com.conf
``` 替换为实际域名和目录):
```apache
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
ErrorLog /var/log/httpd/example.com_error.log
CustomLog /var/log/httpd/example.com_access.log combined
</VirtualHost>
创建网站目录并设置权限:
sudo mkdir -p /var/www/example.com sudo chown -R apache:apache /var/www/example.com sudo chmod -R 755 /var/www/example.com
重启Apache使配置生效:
sudo systemctl restart httpd
数据库与PHP环境安装
动态网站通常需要数据库和脚本支持,以LAMP(Linux+Apache+MySQL+PHP)架构为例:
安装MariaDB(MySQL分支)
sudo yum install mariadb-server mariadb -y sudo systemctl start mariadb sudo systemctl enable mariadb
执行安全脚本设置root密码:
sudo mysql_secure_installation
根据提示配置root密码、移除匿名用户、禁止远程root登录等。
安装PHP及扩展
sudo yum install php php-mysql php-gd php-mbstring -y
安装后重启Apache以加载PHP模块:
sudo systemctl restart httpd
创建PHP测试文件/var/www/html/info.php为<?php phpinfo(); ?>,访问http://服务器IP/info.php若显示PHP配置信息则成功。
安全加固措施
配置Fail2ban防暴力破解
安装Fail2ban并配置规则:
sudo yum install fail2ban -y sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
编辑jail.local,添加以下内容:
[apache] enabled = true port = http,https filter = apache-auth logpath = /var/log/httpd/error_log maxretry = 3 bantime = 3600
启动服务:
sudo systemctl start fail2ban sudo systemctl enable fail2ban
使用SSL证书加密网站流量
通过Let's Encrypt免费证书实现HTTPS:
sudo yum install certbot python-certbot-apache -y sudo certbot --apache -d example.com
根据提示配置邮箱和重定向选项,证书自动续期无需手动干预。
定期备份与日志监控
使用rsync备份网站文件和数据库:
sudo rsync -avz /var/www/ /backup/www/ sudo mysqldump -u root -p database_name > /backup/database_name.sql
通过logrotate管理日志文件,防止日志过大:编辑/etc/logrotate.d/httpd,确保配置正确。
性能优化建议
启用Apache压缩模块
sudo yum install mod_deflate -y
编辑/etc/httpd/conf.d/deflate.conf,添加:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>
重启Apache生效。
配置缓存与静态文件处理
在Apache配置中启用缓存:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
</IfModule>
资源限制与监控
使用htop或nmon监控服务器资源,通过ulimit限制用户进程数,避免资源耗尽,对于高并发场景,可考虑安装Nginx作为Apache的反向代理,静态文件由Nginx直接处理,动态请求转发至Apache。
相关问答FAQs
Q1: CentOS 7如何查看Apache的错误日志?
A: Apache的错误日志默认位于/var/log/httpd/error_log,可通过以下命令查看实时日志内容:
tail -f /var/log/httpd/error_log
若需查看特定时间段的日志,可结合grep过滤关键字,
grep "PHP Fatal error" /var/log/httpd/error_log
Q2: 如何在CentOS 7中禁用不需要的服务以提升安全性?
A: 使用systemctl list-unit-files --type=service列出所有服务,通过以下命令禁用不必要的服务(如telnet、 cups等):
sudo systemctl stop telnet.socket sudo systemctl disable telnet.socket sudo systemctl stop cups sudo systemctl disable cups
禁用后可通过systemctl is-enabled 服务名确认状态,仅保留必要的基础服务(如httpd、mariadb、network等)可减少攻击面。
