Linux搭建服务器配置步骤有哪些?新手必看指南!
- 云服务器
- 2025-12-14
- 4
在Linux系统中搭建服务器配置是一个涉及多个步骤的过程,需要根据实际需求选择合适的操作系统、安装必要的服务、进行安全配置以及优化性能,以下将详细介绍从基础环境准备到服务部署的完整流程,包括常见服务的配置方法及注意事项。
基础环境准备
-
选择操作系统
优先选择服务器版Linux,如Ubuntu Server、CentOS Stream或Debian,这些版本经过优化,稳定性高且资源占用较低,以Ubuntu Server 22.04为例,通过官方ISO镜像安装,安装过程中选择“OpenSSH server”以支持远程管理。
-
更新系统
安装完成后,执行以下命令更新系统包:
sudo apt update && sudo apt upgrade y -
配置静态IP地址
编辑网络配置文件(如/etc/netplan/01netcfg.yaml),设置静态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, 8.8.4.4]执行sudo netplan apply使配置生效。

安装必要服务
Web服务器(以Nginx为例)
sudo apt install nginx y sudo systemctl start nginx sudo systemctl enable nginx
配置文件位于/etc/nginx/sitesavailable/,可通过修改default文件配置虚拟主机,
server { listen 80; server_name example.com; root /var/www/html; index index.html; }
测试配置并重启服务:sudo nginx t && sudo systemctl restart nginx。
数据库服务(以MySQL为例)
sudo apt install mysqlserver y sudo mysql_secure_installation # 初始化安全配置
登录MySQL创建数据库和用户:

FTP服务(以vsftpd为例)
sudo apt install vsftpd y sudo nano /etc/vsftpd.conf # 修改配置文件,启用本地用户并限制访问目录
关键配置项:
anonymous_enable=NO local_enable=YES chroot_local_user=YES allow_writeable_chroot=YES
重启服务:sudo systemctl restart vsftpd。
安全配置
-
配置防火墙
使用ufw管理端口规则:
-
禁用root远程登录
编辑SSH配置文件/etc/ssh/sshd_config,设置PermitRootLogin no,重启SSH服务:sudo systemctl restart sshd。

-
定期更新系统
设置自动更新:sudo apt install unattendedupgrades y,编辑/etc/apt/apt.conf.d/50unattendedupgrades启用自动更新。
-
调整内核参数
编辑/etc/sysctl.conf,优化网络连接数:
net.core.somaxconn = 65535 net.ipv4.tcp_max_syn_backlog = 65535执行sudo sysctl p生效。
-
监控资源使用
安装htop或glances:sudo apt install htop y,实时查看CPU、内存及磁盘I/O情况。
性能优化
常见服务配置对比
| 服务类型 | 常用软件 | 默认端口 | 配置文件位置 |
|---|---|---|---|
| Web服务器 | Nginx/Apache | 80/443 | /etc/nginx/sitesavailable/ |
| 数据库 | MySQL/PostgreSQL | 3305/5432 | /etc/mysql/mysql.conf.d/ |
| FTP | vsftpd/proftpd | 21 | /etc/vsftpd.conf |
FAQs
Q1: 如何检查Nginx配置是否正确?
A1: 使用命令sudo nginx t测试配置文件语法,若显示syntax is ok和test is successful,则配置正确;否则根据错误提示修改文件后重启服务。
Q2: 如何限制FTP用户仅能访问指定目录?
A2: 在vsftpd配置文件中设置chroot_local_user=YES,并创建用户时指定家目录,sudo useradd d /home/ftpuser s /sbin/nologin ftpuser,确保目录权限为755,避免用户无法写入。