插件窝 干货文章 CentOS搭建web服务器的方法及注意事项详解

CentOS搭建web服务器的方法及注意事项详解

sudo 配置 Nginx systemctl 389    来源:    2025-04-12

CentOS搭建Web服务器详解

一、准备工作

  1. 系统要求

    • CentOS 7或8(推荐使用最新稳定版)
    • 至少1GB RAM(生产环境建议2GB以上)
    • 10GB以上磁盘空间
    • root或sudo权限账户
  2. 更新系统

    sudo yum update -y
    sudo yum upgrade -y
    

二、常见Web服务器选择与安装

1. Apache HTTP Server

安装Apache:

sudo yum install httpd -y

基本命令:

sudo systemctl start httpd      # 启动
sudo systemctl enable httpd     # 设置开机启动
sudo systemctl status httpd     # 查看状态

配置文件位置: - 主配置文件: /etc/httpd/conf/httpd.conf - 虚拟主机配置: /etc/httpd/conf.d/

2. Nginx

安装Nginx:

# CentOS 7需要添加EPEL仓库
sudo yum install epel-release -y
sudo yum install nginx -y

基本命令:

sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx

配置文件位置: - 主配置文件: /etc/nginx/nginx.conf - 站点配置: /etc/nginx/conf.d/

三、防火墙配置

开放HTTP/HTTPS端口:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

查看已开放端口:

sudo firewall-cmd --list-all

四、安装PHP(可选)

安装PHP及相关模块:

sudo yum install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring -y

PHP配置文件位置: - 主配置文件: /etc/php.ini - FPM配置文件: /etc/php-fpm.d/www.conf

五、安装数据库(可选)

MySQL/MariaDB

安装MariaDB:

sudo yum install mariadb-server mariadb -y
sudo systemctl start mariadb
sudo systemctl enable mariadb

安全设置:

sudo mysql_secure_installation

六、虚拟主机配置示例

Apache虚拟主机示例

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example
    ErrorLog /var/log/httpd/example_error.log
    CustomLog /var/log/httpd/example_access.log combined
</VirtualHost>

Nginx虚拟主机示例

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/html/example;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

七、SSL证书配置(Let's Encrypt)

安装Certbot:

# Apache
sudo yum install certbot python3-certbot-apache -y

# Nginx
sudo yum install certbot python3-certbot-nginx -y

获取证书:

sudo certbot --apache   # Apache
sudo certbot --nginx    # Nginx

自动续期测试:

sudo certbot renew --dry-run

八、性能优化

  1. 启用Gzip压缩

    • Apache: 修改httpd.conf启用mod_deflate
    • Nginx: 在配置中添加gzip指令
  2. 启用缓存

    • 配置浏览器缓存静态资源
  3. PHP优化

    • 调整php.ini中的内存限制和超时设置
    • 启用OPcache

九、安全注意事项

  1. 文件权限

    • Web根目录权限设置为755
    • 文件所有者设置为非root用户
  2. 禁用不必要模块

    • Apache: 禁用不需要的模块如mod_autoindex
    • Nginx: 删除默认欢迎页面
  3. 定期更新

    sudo yum update -y
    
  4. 日志监控

    • 设置日志轮转
    • 监控异常访问
  5. 禁用SSH root登录

    sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
    sudo systemctl restart sshd
    
  6. 安装fail2ban防止暴力破解

    sudo yum install fail2ban -y
    sudo systemctl enable fail2ban
    sudo systemctl start fail2ban
    

十、常见问题排查

  1. 服务无法启动

    • 检查日志: journalctl -xe/var/log/messages
    • 检查端口冲突: netstat -tulnp
  2. 权限问题

    • 确保SELinux设置正确或临时禁用测试
    sudo setenforce 0
    
  3. 数据库连接问题

    • 检查数据库服务是否运行
    • 检查防火墙设置
    • 检查用户权限

通过以上步骤,您可以在CentOS系统上搭建一个安全、稳定的Web服务器环境。根据实际需求选择Apache或Nginx,并合理配置相关组件。