欢迎光临
我们一直在努力

服务器搭建linux

准备工作
在Linux环境下搭建服务器前,需完成以下准备:

# Ubuntu/Debian编辑配置文件
sudo nano /etc/netplan/01-netcfg.yaml
# CentOS编辑文件
sudo nmtui

  • 创建非root用户并赋予sudo权限:
    adduser username  
    usermod -aG sudo username  

  • 基础服务部署

    1. 更新系统与安装必要工具

      # Ubuntu/Debian
      sudo apt update && sudo apt upgrade -y
      sudo apt install curl wget ufw -y
      # CentOS
      sudo dnf update -y
      sudo dnf install epel-release curl wget firewalld -y

    2. 配置防火墙

      • UFW(Ubuntu/Debian)
        sudo ufw allow 22/tcp  # 开放SSH端口  
        sudo ufw enable  
      • Firewalld(CentOS)
        sudo firewall-cmd --permanent --add-service=ssh  
        sudo firewall-cmd --reload  
    3. 部署Web服务器(以Nginx为例)

      # Ubuntu/Debian  
      sudo apt install nginx -y  
      sudo systemctl start nginx  
      # CentOS  
      sudo dnf install nginx -y  
      sudo systemctl enable --now nginx  

      验证:浏览器访问服务器IP,显示Nginx欢迎页即成功。


    安全加固(E-A-T核心要点)

    1. SSH安全配置

      • 禁用root登录:
        sudo nano /etc/ssh/sshd_config  
        # 修改 PermitRootLogin no  
      • 启用密钥认证:
        ssh-keygen -t ed25519  # 本地生成密钥  
        ssh-copy-id username@server_ip  # 上传公钥  
    2. 定期更新与监控

      • 设置自动更新:

        # Ubuntu/Debian  
        sudo apt install unattended-upgrades  
        sudo dpkg-reconfigure unattended-upgrades  
        # CentOS  
        sudo dnf install dnf-automatic  
        sudo systemctl enable --now dnf-automatic.timer  
      • 安装监控工具(如htop、netdata)实时查看资源占用。

        rsync -avz /path/to/source user@remote:/path/to/destination


    优化与维护

    1. 启用Swap文件(内存不足时)

      sudo fallocate -l 2G /swapfile  # 创建2GB Swap  
      sudo chmod 600 /swapfile  
      sudo mkswap /swapfile  
      sudo swapon /swapfile  
      # 永久生效:将“/swapfile none swap sw 0 0”写入/etc/fstab  
    2. 日志管理

      • 使用logrotate自动压缩旧日志:
        sudo nano /etc/logrotate.d/nginx  
        # 添加规则:  
        /var/log/nginx/*.log {  
            daily  
            missingok  
            rotate 14  
            compress  
            delaycompress  
            notifempty  
            create 0640 www-data adm  
        }  

    参考资料

    • Ubuntu官方文档:https://ubuntu.com/server/docs
    • CentOS安装指南:https://docs.centos.org
    • Let’s Encrypt SSL配置:https://letsencrypt.org/getting-started/
    • Nginx性能优化:https://www.nginx.com/resources/wiki/

    (完)

    未经允许不得转载:九八云安全 » 服务器搭建linux