Linux服务器搭建与配置全指南
准备工作与环境选择
在搭建Linux服务器之前,需明确服务器用途(如Web服务、数据库、文件共享等),并选择合适的Linux发行版,对于新手,Ubuntu Server或CentOS Stream是较为友好的选择,两者社区支持完善,文档丰富,若追求稳定性,CentOS 7/8或Rocky Linux更适合生产环境;若需要高性能计算,可考虑Debian或Gentoo。

硬件方面,建议根据负载选择配置:小型Web服务至少2核CPU、4GB内存;数据库服务需8GB以上内存及SSD存储;虚拟化或容器化场景建议16GB以上内存,网络环境需确保公网IP(若需外网访问)或内网静态IP,并提前规划防火墙策略。
系统安装与基础配置
-
安装系统
通过官方ISO镜像制作启动U盘,进入安装界面后选择“Install Linux”模式,分区建议采用LVM(逻辑卷管理),便于后续动态调整存储空间;根分区(/)分配50-100GB,swap设为内存的1-2倍(若内存>8GB可适当减半),剩余空间作为/home或/data分区,安装过程中需设置root密码(建议复杂密码)并创建普通用户(避免直接使用root操作)。 -
网络配置
安装完成后,编辑/etc/netplan/01-netcfg.yaml(Ubuntu)或/etc/sysconfig/network-scripts/ifcfg-eth0(CentOS),配置静态IP:network: version: 2 ethernets: eth0: dhcp4: no addresses: [192.168.1.100/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 114.114.114.114]执行
sudo netplan apply(Ubuntu)或sudo systemctl restart network(CentOS)使配置生效。 -
系统更新与安全加固
更新系统包:- Ubuntu/Debian:
sudo apt update && sudo apt upgrade -y - CentOS/RHEL:
sudo dnf update -y
禁用root远程登录:编辑/etc/ssh/sshd_config,将PermitRootLogin设为no,重启SSH服务:sudo systemctl restart sshd,配置SSH密钥登录:生成密钥对(ssh-keygen),将公钥(~/.ssh/id_rsa.pub)内容追加到服务器的~/.ssh/authorized_keys,并设置权限600。
- Ubuntu/Debian:
核心服务配置
-
Web服务(Nginx+PHP)
安装Nginx:sudo apt install nginx -y(Ubuntu)或sudo dnf install nginx -y(CentOS),启动并设置开机自启:sudo systemctl enable --now nginx。
配置虚拟主机:在/etc/nginx/sites-available/创建新配置文件(如example.com),写入:
server { listen 80; server_name example.com www.example.com; root /var/www/example.com; index index.php index.html; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; } }创建网站目录并赋权:
sudo mkdir -p /var/www/example.com && sudo chown -R www-data:www-data /var/www/example.com,启用站点:sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/,测试并重载Nginx:sudo nginx -t && sudo systemctl reload nginx。
安装PHP:sudo apt install php8.1-fpm php8.1-mysql php8.1-gd -y(Ubuntu),确保PHP-FPM与Nginx配置版本匹配。 -
数据库服务(MySQL)
安装MySQL:sudo apt install mysql-server -y(Ubuntu)或sudo dnf install mysql-server -y(CentOS),安全初始化:sudo mysql_secure_installation,设置root密码、移除匿名用户、禁止root远程登录等。
创建数据库与用户:登录MySQL(sudo mysql),执行:CREATE DATABASE example_db; CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'StrongPassword!'; GRANT ALL PRIVILEGES ON example_db.* TO 'example_user'@'localhost'; FLUSH PRIVILEGES; EXIT;
-
防火墙配置
使用UFW(Ubuntu)或firewalld(CentOS)限制端口访问:- Ubuntu:
sudo ufw allow 22/tcp(SSH)、sudo ufw allow 80/tcp(HTTP)、sudo ufw allow 443/tcp(HTTPS),启用sudo ufw enable。 - CentOS:
sudo firewall-cmd --permanent --add-service=ssh、sudo firewall-cmd --permanent --add-service=http、sudo firewall-cmd --permanent --add-service=https,重载sudo firewall-cmd --reload。
- Ubuntu:
监控与维护
-
日志管理
配置日志轮转:编辑/etc/logrotate.d/nginx(Nginx日志)或/etc/logrotate.d/mysql(MySQL日志),设置按天轮转、保留30天日志。
实时监控日志:tail -f /var/log/nginx/access.log(Nginx访问日志)或tail -f /var/log/mysql/error.log(MySQL错误日志)。 -
性能监控
安装监控工具:sudo apt install htop iotop nethogs -y(Ubuntu)或sudo dnf install htop iotop nethogs -y(CentOS),使用htop查看进程资源占用,iotop监控磁盘I/O,nethogs查看网络流量。
集成Prometheus+Grafana:若需长期监控,可部署Prometheus采集指标,Grafana可视化展示,适用于生产环境集群监控。 -
备份策略
配置自动备份:使用rsync或tar备份数据库与网站文件,通过cron定时执行,每日凌晨2点备份数据库到/backup/mysql:
0 2 * * * mysqldump -u root -pPassword example_db > /backup/mysql/example_db_$(date +\%Y\%m\%d).sql
备份文件需异地存储,可结合云存储(如AWS S3)或远程服务器同步。
优化与安全进阶
-
系统优化
调整内核参数:编辑/etc/sysctl.conf,优化TCP连接(如net.core.somaxconn=1024、net.ipv4.tcp_tw_reuse=1),执行sudo sysctl -p生效。
禁用不必要服务:sudo systemctl list-unit-files --state=enabled查看开机自启服务,关闭不需要的服务(如avahi-daemon)。 -
安全加固
安装Fail2ban:防暴力破解SSH服务,sudo apt install fail2ban -y,配置/etc/fail2ban/jail.local:[sshd] enabled = true port = 22 maxretry = 3 bantime = 3600
定期更新系统:设置自动更新(Ubuntu:
sudo apt install unattended-upgrades -y),或通过cron定期执行dnf update(CentOS)。
通过以上步骤,可完成从基础安装到服务配置、监控维护的Linux服务器搭建流程,实际部署中需根据业务需求调整参数,并定期检查系统状态与安全漏洞,确保服务器稳定运行。


















