在Linux系统中开启服务器是一个涉及系统准备、基础配置、服务部署及安全加固的系统性过程,本文将详细从硬件与系统选型、安装步骤、基础配置、服务启动、安全优化等方面展开说明,帮助用户完成服务器的搭建与启用。
硬件与系统准备
在开启Linux服务器前,需先明确硬件需求与系统选择,硬件方面,根据服务器用途(如Web服务、数据库、应用部署)配置CPU、内存、存储:小型Web服务建议至少2核CPU、4GB内存、100GB SSD;数据库服务需8GB以上内存及高速存储,系统选择上,推荐使用稳定版发行版,如Ubuntu Server 22.04 LTS(社区支持广,文档丰富)或CentOS Stream 9(企业级稳定,RHEL兼容),两者均提供长期安全更新。
下载对应系统镜像后,通过Rufus(Windows)或dd
命令(Linux,如dd if=/path/to.iso of=/dev/sdb bs=4M
)制作启动U盘,确保服务器能从U盘启动。
系统安装步骤
- 启动安装程序:将U盘插入服务器,开机进入BIOS设置启动顺序为U盘优先,启动后选择“Install Ubuntu Server”或“Install CentOS”。
- 语言与键盘布局:选择安装语言(如English)和键盘布局(如US English)。
- 网络配置:若服务器有固定IP,可在此处配置静态IP(IP地址、子网掩码、网关、DNS),或选择DHCP动态获取(后续再调整)。
- 磁盘分区:
- 自动分区:选择“Erase disk and install Ubuntu”或“Automatic Partitioning”,适合新手;
- 手动分区:推荐分区方案:
/boot
:1GB(ext4,存放引导文件);swap
:内存的2倍(不超过8GB,用于虚拟内存);- 剩余空间的50%(ext4,根目录);
/home
:剩余空间(ext4,存放用户数据)。
- 用户创建:设置普通用户名(如
admin
)、密码,并勾选“Make this user administrator”(赋予sudo权限),禁用root直接登录(安全考虑)。 - 安装完成:等待安装结束,选择“Restart Now”,移除U盘进入系统。
基础系统配置
-
更新系统:首次登录后,更新软件包列表及已安装软件:
- Ubuntu/Debian:
sudo apt update && sudo apt upgrade -y
- CentOS/RHEL:
sudo yum update -y
- Ubuntu/Debian:
-
网络配置(静态IP):
- Ubuntu 22.04:编辑
/etc/netplan/01-netcfg.yaml
,配置如下:network: version: 2 ethernets: ens33: # 替换为实际网卡名 dhcp4: no addresses: [192.168.1.100/24] # IP地址/子网掩码 gateway4: 192.168.1.1 # 网关 nameservers: addresses: [8.8.8.8, 114.114.114.114] # DNS
执行
sudo netplan apply
生效。 - CentOS 9:使用
nmcli
命令:sudo nmcli connection modify "ens33" ipv4.method manual ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.dns "8.8.8.8 114.114.114.114" sudo nmcli connection up "ens33"
- Ubuntu 22.04:编辑
-
时区与时间同步:
sudo timedatectl set-timezone Asia/Shanghai # 设置时区 sudo apt install ntpdate -y # Ubuntu安装时间同步工具 sudo yum install ntp -y # CentOS安装 sudo ntpdate cn.pool.ntp.org # 同步时间
常用服务部署与启动
服务器需根据业务需求部署服务(如Web、数据库、FTP等),以下以Nginx(Web服务)、MySQL(数据库)为例,说明安装与启动流程:
常用服务配置对比表
服务名称 | 安装命令(Ubuntu) | 安装命令(CentOS) | 启动命令 | 配置文件路径 | 常用管理命令(重启/停止/状态) |
---|---|---|---|---|---|
Nginx | sudo apt install nginx |
sudo yum install nginx |
sudo systemctl start nginx |
/etc/nginx/nginx.conf |
systemctl restart nginx /stop nginx /status nginx |
Apache | sudo apt install apache2 |
sudo yum install httpd |
sudo systemctl start apache2 |
/etc/apache2/apache2.conf |
systemctl restart apache2 /stop apache2 /status apache2 |
MySQL | sudo apt install mysql-server |
sudo yum install mysql-server |
sudo systemctl start mysql |
/etc/mysql/mysql.conf.d/mysqld.cnf |
systemctl restart mysql /stop mysql /status mysql |
SSH | 默认已安装 | 默认已安装 | sudo systemctl start sshd |
/etc/ssh/sshd_config |
systemctl restart sshd /stop sshd /status sshd |
示例:Nginx部署与配置
- 安装Nginx:
sudo apt install nginx -y # Ubuntu # 或 sudo yum install nginx -y # CentOS
- 启动并设置开机自启:
sudo systemctl start nginx sudo systemctl enable nginx # 开机自启
- 配置测试:浏览器访问服务器IP(如
http://192.168.1.100
),若显示“Welcome to nginx!”则安装成功。 - 修改默认站点:编辑
/etc/nginx/sites-available/default
,修改root
指向网站目录(如/var/www/html
),保存后执行sudo nginx -s reload
重载配置。
安全加固
-
SSH安全配置:
- 编辑
/etc/ssh/sshd_config
:Port 2222 # 修改默认端口22(防暴力破解) PermitRootLogin no # 禁止root直接登录 PasswordAuthentication no # 禁止密码登录,仅允许密钥 PubkeyAuthentication yes
重启SSH服务:
sudo systemctl restart sshd
。 - 生成SSH密钥对(本地客户端执行):
ssh-keygen -t rsa
,将公钥(~/.ssh/id_rsa.pub
)上传到服务器~/.ssh/authorized_keys
。
- 编辑
-
防火墙配置:
- Ubuntu:使用
ufw
(Uncomplicated Firewall):sudo ufw allow 22 # 允许SSH(若修改端口则为2222) sudo ufw allow 80 # 允许HTTP sudo ufw allow 443 # 允许HTTPS sudo ufw enable # 启用防火墙
- CentOS:使用
firewalld
:sudo firewall-cmd --permanent --add-service=ssh # 允许SSH sudo firewall-cmd --permanent --add-service=http # 允许HTTP sudo firewall-cmd --reload # 重载规则
- Ubuntu:使用
-
定期更新与日志监控:
- 设置自动更新:Ubuntu编辑
/etc/apt/apt.conf.d/10periodic
,设置APT::Periodic::Unattended-Upgrade "1"
;CentOS使用yum-cron
:sudo yum install yum-cron -y && sudo systemctl enable yum-cron
。 - 监控日志:查看系统日志
/var/log/syslog
(Ubuntu)或/var/log/secure
(SSH登录日志),安装logwatch
分析日志:sudo apt install logwatch -y
,执行logwatch --range today --mailto admin@example.com
接收日报。
- 设置自动更新:Ubuntu编辑
性能优化(可选)
- 资源限制:编辑
/etc/security/limits.conf
,添加:* soft nofile 65536 # 最大打开文件数 * hard nofile 65536
- 内核参数优化:编辑
/etc/sysctl.conf
,添加:net.ipv4.tcp_tw_reuse = 1 # 重用TIME_WAIT连接 net.ipv4.ip_local_port_range = 1024 65535 # 扩大端口范围
执行
sudo sysctl -p
生效。
相关问答FAQs
问题1:如何查看Linux服务器当前运行的所有服务状态?
解答:使用systemctl
命令可查看服务状态。
- 查看所有已启动的服务:
systemctl list-units --type=service --state=running
- 查看特定服务状态(如Nginx):
systemctl status nginx
- 查看失败的服务:
systemctl --failed
问题2:如何设置服务开机自启动?如何取消自启动?
解答:通过systemctl enable
和systemctl disable
命令管理开机自启动。
- 设置开机自启动(如Nginx):
sudo systemctl enable nginx
- 取消开机自启动:
sudo systemctl disable nginx
- 检查服务是否已设置自启动:
systemctl is-enabled nginx
(返回enabled
表示已设置,disabled
表示未设置)。
原创文章,发布者:酷番叔,转转请注明出处:https://cloud.kd.cn/ask/25192.html