logo

从零开始:搭建网站服务器超详细步骤——部署轻量应用服务器(Centos)

作者:carzy2025.10.10 15:45浏览量:2

简介:本文详细介绍如何通过CentOS系统在轻量应用服务器上搭建网站服务器,涵盖环境准备、安全配置、服务部署及监控维护全流程,适合初学者与开发者参考。

从零开始:搭建网站服务器超详细步骤——部署轻量应用服务器(CentOS)

一、环境准备与服务器选购

1.1 轻量应用服务器选择

轻量应用服务器(Lightweight Application Server)是专为小型网站、开发测试等场景设计的云服务器,具有资源独享、成本低廉的特点。选择时需关注:

  • 配置参数:建议选择至少1核2G内存、50GB系统盘的配置,可满足基础WordPress或静态网站需求。
  • 带宽与流量:根据预期访问量选择带宽(如3-5Mbps),注意流量包是否充足。
  • 地域选择:优先选择靠近目标用户群体的机房,降低延迟。

1.2 CentOS系统安装

购买服务器后,通过控制台选择CentOS 8或CentOS Stream 9镜像(CentOS 8已停止维护,推荐使用Stream版本)。安装过程需注意:

  • 分区方案:采用LVM分区,/boot分配2GB,/分配剩余空间,/swap分配内存的1.5倍(如2G内存服务器分配3GB)。
  • 安全设置:安装时禁用SELinux(临时通过setenforce 0,永久修改/etc/selinux/config中的SELINUX=disabled),减少初期调试障碍。

二、基础环境配置

2.1 网络与防火墙设置

  1. 静态IP配置

    1. # 编辑网络配置文件
    2. vi /etc/sysconfig/network-scripts/ifcfg-eth0
    3. # 修改以下参数
    4. BOOTPROTO=static
    5. IPADDR=192.168.1.100
    6. NETMASK=255.255.255.0
    7. GATEWAY=192.168.1.1
    8. DNS1=8.8.8.8
    9. # 重启网络服务
    10. systemctl restart network
  2. 防火墙规则

    1. # 安装firewalld
    2. yum install firewalld -y
    3. systemctl start firewalld
    4. systemctl enable firewalld
    5. # 开放HTTP/HTTPS/SSH端口
    6. firewall-cmd --permanent --add-service={http,https,ssh}
    7. firewall-cmd --reload

2.2 用户与权限管理

  1. 创建专用用户

    1. useradd -m -s /bin/bash webadmin
    2. passwd webadmin # 设置密码
    3. # 赋予sudo权限
    4. visudo # 在文件末尾添加 "webadmin ALL=(ALL) NOPASSWD:ALL"
  2. SSH密钥登录

    1. # 本地生成密钥对(Windows可用PuTTYgen)
    2. ssh-keygen -t ed25519
    3. # 上传公钥到服务器
    4. ssh-copy-id -i ~/.ssh/id_ed25519.pub webadmin@服务器IP
    5. # 禁用密码登录
    6. vi /etc/ssh/sshd_config
    7. # 修改以下参数
    8. PasswordAuthentication no
    9. ChallengeResponseAuthentication no
    10. # 重启SSH服务
    11. systemctl restart sshd

三、Web服务部署

3.1 Nginx安装与配置

  1. 安装Nginx

    1. # 添加EPEL仓库
    2. yum install epel-release -y
    3. # 安装Nginx
    4. yum install nginx -y
    5. systemctl start nginx
    6. systemctl enable nginx
  2. 虚拟主机配置

    1. # 创建配置文件 /etc/nginx/conf.d/example.com.conf
    2. server {
    3. listen 80;
    4. server_name example.com www.example.com;
    5. root /var/www/example.com/html;
    6. index index.html index.htm;
    7. location / {
    8. try_files $uri $uri/ =404;
    9. }
    10. }
  3. HTTPS配置

    1. # 安装Certbot
    2. yum install certbot python3-certbot-nginx -y
    3. # 获取证书(需提前解析域名
    4. certbot --nginx -d example.com -d www.example.com
    5. # 自动续期测试
    6. certbot renew --dry-run

3.2 PHP与数据库配置(以WordPress为例)

  1. PHP安装

    1. # 安装PHP 8.0及常用扩展
    2. yum install php php-fpm php-mysqlnd php-gd php-curl -y
    3. systemctl start php-fpm
    4. systemctl enable php-fpm
  2. MariaDB安装

    1. yum install mariadb-server -y
    2. systemctl start mariadb
    3. mysql_secure_installation # 设置root密码并移除匿名用户
    4. # 创建WordPress数据库
    5. mysql -u root -p
    6. CREATE DATABASE wordpress;
    7. GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost' IDENTIFIED BY '强密码';
    8. FLUSH PRIVILEGES;
  3. WordPress部署

    1. # 下载WordPress
    2. wget https://wordpress.org/latest.tar.gz
    3. tar -xzvf latest.tar.gz
    4. mv wordpress /var/www/example.com/html
    5. # 设置目录权限
    6. chown -R webadmin:webadmin /var/www/example.com/html
    7. find /var/www/example.com/html -type d -exec chmod 755 {} \;
    8. find /var/www/example.com/html -type f -exec chmod 644 {} \;

四、性能优化与安全加固

4.1 服务器性能调优

  1. 内核参数优化

    1. # 编辑sysctl配置
    2. vi /etc/sysctl.conf
    3. # 添加以下参数
    4. net.core.somaxconn = 65535
    5. net.ipv4.tcp_max_syn_backlog = 65535
    6. vm.swappiness = 10
    7. # 应用配置
    8. sysctl -p
  2. PHP-FPM调优

    1. # 编辑 /etc/php-fpm.d/www.conf
    2. pm = dynamic
    3. pm.max_children = 50
    4. pm.start_servers = 5
    5. pm.min_spare_servers = 5
    6. pm.max_spare_servers = 10

4.2 安全防护措施

  1. Fail2Ban安装

    1. yum install fail2ban -y
    2. cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
    3. # 编辑jail.local启用SSH防护
    4. [sshd]
    5. enabled = true
    6. maxretry = 3
    7. bantime = 86400
    8. systemctl start fail2ban
    9. systemctl enable fail2ban
  2. 定期安全扫描

    1. # 安装Lynis安全审计工具
    2. yum install epel-release -y
    3. yum install lynis -y
    4. # 执行扫描
    5. lynis audit system

五、监控与维护

5.1 基础监控工具

  1. htop安装

    1. yum install epel-release -y
    2. yum install htop -y
  2. Nginx日志分析

    1. # 安装GoAccess
    2. yum install epel-release -y
    3. yum install goaccess -y
    4. # 生成HTML报告
    5. goaccess /var/log/nginx/access.log -a -o report.html --log-format=COMBINED

5.2 备份策略

  1. 数据库备份

    1. # 创建备份脚本 /root/db_backup.sh
    2. #!/bin/bash
    3. mysqldump -u root -p密码 wordpress > /backup/wordpress_$(date +%F).sql
    4. find /backup/ -name "wordpress_*.sql" -mtime +7 -delete
    5. # 添加cron任务
    6. (crontab -l 2>/dev/null; echo "0 3 * * * /root/db_backup.sh") | crontab -
  2. 文件备份

    1. # 使用rsync同步到远程服务器
    2. rsync -avz --delete /var/www/example.com/html/ 用户名@备份服务器IP:/backup/website/

六、常见问题解决方案

  1. Nginx 502错误

    • 检查PHP-FPM是否运行:systemctl status php-fpm
    • 查看Nginx错误日志:tail -f /var/log/nginx/error.log
  2. WordPress白屏

    • 启用调试模式:编辑wp-config.php,设置define('WP_DEBUG', true);
    • 检查文件权限:确保wp-content目录可写
  3. SSL证书过期

    • 设置自动续期cron任务:
      1. (crontab -l 2>/dev/null; echo "0 3 */15 * * certbot renew --quiet") | crontab -

通过以上步骤,您可以在CentOS系统上构建一个稳定、安全的轻量级网站服务器。实际部署时需根据业务需求调整配置参数,并定期更新系统补丁(通过yum update -y)以保障安全性。对于高流量网站,建议考虑负载均衡架构或升级至计算优化型实例。

相关文章推荐

发表评论

活动