您好,登录后才能下订单哦!
# Linux下如何搭建VPS
## 前言
虚拟专用服务器(VPS)已成为个人开发者和小型企业的热门选择,它提供了比共享主机更高的控制权,同时比独立服务器更经济实惠。本文将详细介绍在Linux环境下从零开始搭建VPS的全过程,涵盖系统选择、环境配置、安全加固等关键步骤。
## 一、准备工作
### 1.1 选择VPS提供商
推荐主流服务商:
- DigitalOcean(开发者友好)
- Linode(高性价比)
- Vultr(按小时计费)
- AWS Lightsail(亚马逊生态)
### 1.2 基础配置建议
- CPU:至少1核
- 内存:1GB起步(运行GUI需2GB+)
- 存储:25GB SSD起
- 带宽:1TB/月以上流量
### 1.3 系统选择
推荐发行版:
- Ubuntu LTS(长期支持版)
- Debian(稳定性优先)
- CentOS Stream(原RHEL替代)
```bash
# 查看系统信息
lsb_release -a
uname -a
ssh root@your_server_ip
adduser username
usermod -aG sudo username
本地机器生成密钥:
ssh-keygen -t ed25519
上传公钥到服务器:
ssh-copy-id username@server_ip
编辑/etc/ssh/sshd_config
:
PasswordAuthentication no
ChallengeResponseAuthentication no
重启服务:
sudo systemctl restart sshd
sudo apt update && sudo apt upgrade -y # Ubuntu/Debian
sudo dnf update -y # CentOS/RHEL
sudo timedatectl set-timezone Asia/Shanghai
sudo apt install ntp -y # 时间同步服务
UFW(Ubuntu):
sudo ufw allow OpenSSH
sudo ufw enable
Firewalld(CentOS):
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
Apache方案:
sudo apt install apache2 mysql-server php libapache2-mod-php -y
sudo apt install nginx mariadb-server php-fpm php-mysql -y
sudo mysql_secure_installation
编辑/etc/php/8.1/fpm/php.ini
(版本号需调整):
upload_max_filesize = 32M
post_max_size = 48M
memory_limit = 256M
Nginx示例(/etc/nginx/sites-available/example.com):
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.php index.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
Certbot自动安装:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run
sudo apt install fail2ban -y
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
安装DE:
sudo apt install aide -y
sudo aideinit
配置无人值守更新:
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure -plow unattended-upgrades
sudo apt install htop nmon sysstat -y
# 查看最近登录
last
# 检查失败登录
sudo grep "Failed password" /var/log/auth.log
调整内核参数(/etc/sysctl.conf):
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_fin_timeout = 30
检查方向: 1. 防火墙规则 2. SSH服务状态 3. 网络连通性
排查步骤:
sudo systemctl status nginx
sudo tail -n 50 /var/log/nginx/error.log
netstat -tulnp | grep 80
清理方法:
# 查找大文件
sudo find / -type f -size +100M
# 清理旧内核
sudo apt autoremove --purge
通过以上步骤,您已经成功搭建了一个功能完备的Linux VPS。建议定期进行: - 系统更新 - 安全审计 - 数据备份
进阶方向建议: - Docker容器化部署 - Kubernetes集群管理 - CI/CD流水线搭建
注意:本文基于Ubuntu 22.04 LTS编写,其他发行版可能需要调整部分命令。生产环境部署前请务必进行充分测试。 “`
这篇文章共计约1600字,采用Markdown格式编写,包含: 1. 8个主要章节 2. 30+个实用命令片段 3. 配置示例和注意事项 4. 安全建议和优化方案
可根据实际需要调整具体技术栈(如将Nginx替换为Apache)或增加特定应用的部署教程。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。