上一篇
服务器搭建linux
- 行业动态
- 2025-04-13
- 5
搭建Linux服务器需选择合适发行版(如Ubuntu、CentOS),通过ISO镜像安装系统,配置网络与防火墙,安装必要服务(Apache/Nginx、MySQL等),设置SSH密钥登录及权限管理,确保系统安全更新,定期备份数据,优化性能参数即可部署稳定高效的服务器环境。
准备工作
在Linux环境下搭建服务器前,需完成以下准备:
选择Linux发行版
- 推荐系统:Ubuntu Server、CentOS Stream或Debian,因社区支持广泛且文档丰富。
- 硬件要求:根据服务类型(如网站托管、数据库等)选择CPU、内存及存储配置,静态网站可选用1核1GB配置,数据库服务器建议2核4GB起步。
获取系统镜像
- 从官网下载ISO镜像:
- Ubuntu:https://ubuntu.com/download/server
- CentOS:https://www.centos.org/download/
- 使用工具(如Rufus或Ventoy)制作启动盘。
- 从官网下载ISO镜像:
安装Linux系统
启动安装程序
- 插入启动盘,重启服务器并进入BIOS/UEFI,设置从USB启动。
- 选择“Install”或“Graphical Install”进入安装向导。
分区与磁盘配置
- 推荐方案:
/boot
:1GB(用于引导文件)。swap
:与物理内存相同(适用于小内存服务器)。- :剩余空间(根目录)。
- 选择LVM(逻辑卷管理)以便未来扩展分区。
- 推荐方案:
网络与用户配置
- 设置静态IP(保障服务器稳定访问):
# Ubuntu/Debian编辑配置文件 sudo nano /etc/netplan/01-netcfg.yaml # CentOS编辑文件 sudo nmtui
- 创建非root用户并赋予sudo权限:
adduser username usermod -aG sudo username
- 设置静态IP(保障服务器稳定访问):
基础服务部署
更新系统与安装必要工具
# 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
配置防火墙
- 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
- UFW(Ubuntu/Debian):
部署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核心要点)
SSH安全配置
- 禁用root登录:
sudo nano /etc/ssh/sshd_config # 修改 PermitRootLogin no
- 启用密钥认证:
ssh-keygen -t ed25519 # 本地生成密钥 ssh-copy-id username@server_ip # 上传公钥
- 禁用root登录:
定期更新与监控
设置自动更新:
# 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或BorgBackup定期备份至远程存储。
- 示例命令:
rsync -avz /path/to/source user@remote:/path/to/destination
优化与维护
启用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
日志管理
- 使用logrotate自动压缩旧日志:
sudo nano /etc/logrotate.d/nginx # 添加规则: /var/log/nginx/*.log { daily missingok rotate 14 compress delaycompress notifempty create 0640 www-data adm }
- 使用logrotate自动压缩旧日志:
参考资料
- 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/
(完)