🗣 Wd提问: linux服务器的搭建
🤖 Ai回答: 搭建 Linux 服务器是一个涉及多个步骤的过程,具体取决于你打算搭建什么类型的服务器(如 Web 服务器、数据库服务器、FTP 服务器、邮件服务器、VPN 服务器等)。下面我将为你提供一个通用且完整的 Linux 服务器搭建指南,以最常见的 Web 服务器(LAMP/LEMP) 为例,适用于 Ubuntu 或 CentOS 等主流发行版。
✅ 一、准备工作
1、选择 Linux 发行版
推荐用于服务器的发行版:
Ubuntu Server LTS(推荐新手,社区支持好)
CentOS Stream / Rocky Linux / AlmaLinux(企业级稳定)
Debian(稳定、轻量)
本文以 Ubuntu 22.04 LTS 为例。
2、准备硬件或云服务器
本地:安装虚拟机(VMware、VirtualBox)或直接安装系统
云端:阿里云、腾讯云、AWS、Google Cloud、DigitalOcean 等购买云服务器(ECS)
3、连接服务器
通过 SSH 连接(Windows 可用 PuTTY、Windows Terminal;Mac/Linux 用终端):
bash
ssh username@your_server_ip
首次连接会提示接受密钥,输入密码登录。
✅ 二、基础安全配置
1、更新系统
bash
sudo apt update && sudo apt upgrade -y
2、创建非 root 用户(安全最佳实践)
bash
sudo adduser yourusername
sudo usermod -aG sudo yourusername 添加到 sudo 组
退出后用新用户登录:
bash
exit
ssh yourusername@your_server_ip
3、配置 SSH 密钥登录(禁用密码登录)
在本地生成密钥对(如未生成):
bash
ssh-keygen -t ed25519 -C your_email@65.hk
复制公钥到服务器:
bash
ssh-copy-id yourusername@your_server_ip
编辑 SSH 配置文件:
bash
sudo nano /etc/ssh/sshd_config
修改以下内容:
ini
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
重启 SSH 服务:
bash
sudo systemctl restart ssh
⚠️ 确保你已成功用密钥登录后再关闭密码登录,否则可能被锁!
4、配置防火墙(UFW)
bash
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full' 如果用 Nginx
sudo ufw allow 3306 MySQL(可选,仅内网访问)
sudo ufw enable
sudo ufw status
5、安装 Fail2Ban(防暴力破解)
bash
sudo apt install fail2ban -y
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
✅ 三、搭建 LAMP / LEMP 服务器
方案 A:LAMP(Linux + Apache + MySQL + PHP)
1、安装 Apache
bash
sudo apt install apache2 -y
sudo systemctl enable apache2
sudo systemctl start apache2
访问 `http://your_server_ip`,看到 It works! 表示成功。
2、安装 MySQL
bash
sudo apt install mysql-server -y
sudo mysql_secure_installation
按提示设置 root 密码、移除匿名用户、禁止远程 root 登录等。
可选:允许远程访问(仅限内网):
bash
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
修改 bind-address = 0.0.0.0
sudo systemctl restart mysql
3、安装 PHP
bash
sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-zip -y
测试 PHP:
bash
echo <?php phpinfo(); ? | sudo tee /var/www/html/info.php
访问 `http://your_server_ip/info.php` 查看 PHP 信息。
✅ 完成后删除 info.php(安全风险):
bash
sudo rm /var/www/html/info.php
方案 B:LEMP(Linux + Nginx + MySQL + PHP-FPM)【推荐】
1、安装 Nginx
bash
sudo apt install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
2、安装 MySQL(同上)
bash
sudo apt install mysql-server -y
sudo mysql_secure_installation
3、安装 PHP-FPM
bash
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip -y
4、配置 Nginx 使用 PHP-FPM
创建站点配置文件:
bash
sudo nano /etc/nginx/sites-available/yourdomain.com
内容示例:
nginx
server {
listen 80;
server_name your_domain_or_ip;
root /var/www/yourdomain.com/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
启用站点:
bash
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t 测试配置
sudo systemctl reload nginx
5、创建网站目录并测试
bash
sudo mkdir -p /var/www/yourdomain.com/html
echo <?php echo '<h1Hello from PHP!</h1'; ? | sudo tee /var/www/yourdomain.com/html/index.php
sudo chown -R www-data:www-data /var/www/yourdomain.com/html
sudo chmod -R 755 /var/www/yourdomain.com/html
访问 `http://your_server_ip` 查看效果。
✅ 四、配置域名与 DNS(可选)
1、购买域名(如阿里云、Namecheap)
2、在 DNS 管理中将 A 记录指向你的服务器公网 IP
3、配置 Nginx 的 `server_name yourdomain.com;`
4、可使用 Let’s Encrypt 免费 SSL 证书(推荐):
bash
sudo apt install certbot python3-certbot-nginx -y
sudo certbot nginx -d yourdomain.com
自动配置 HTTPS,重定向 HTTP → HTTPS。
✅ 五、其他常用服务(按需安装)
| 服务 | 命令 |
| FTP 服务器 | `sudo apt install vsftpd` |
| Docker | `curl -fsSL https://get.docker.com | sh` |
| Redis | `sudo apt install redis-server` |
| Node.js | `curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash && sudo apt install nodejs` |
| Git | `sudo apt install git` |
| 后台进程管理 | `sudo apt install pm2`(Node.js)或 `systemd` |
✅ 六、监控与维护
1、查看服务状态
bash
systemctl status nginx
systemctl status mysql
systemctl status php8.1-fpm
2、日志查看
bash
tail -f /var/log/nginx/error.log
tail -f /var/log/mysql/error.log
tail -f /var/log/auth.log SSH 登录记录
3、设置自动更新(可选)
bash
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
4、备份策略
定期备份网站文件和数据库:
bash
mysqldump -u root -p yourdb backup.sql
tar -czf website_backup.tar.gz /var/www/yourdomain.com/html/
使用 `cron` 定时任务自动备份:
bash
crontab -e
添加:0 2 * * * /path/to/backup_script.sh
✅ 七、安全加固总结(必做)
| 措施 | 说明 |
| ✅ 禁用 root 登录 SSH | 防止暴力破解 |
| ✅ 使用密钥登录 | 比密码更安全 |
| ✅ 配置防火墙 UFW | 只开放必要端口 |
| ✅ 安装 Fail2Ban | 自动封禁恶意 IP |
| ✅ 定期更新系统 | `apt update && upgrade` |
| ✅ 删除测试文件 | 如 info.php、默认页面 |
| ✅ 使用 HTTPS | Let’s Encrypt 免费证书 |
| ✅ 限制文件权限 | 网站目录属主为 www-data,权限 755/644 |
📣 商家广告 :
👍
0
👎
IP地址: 27.242.112.88
搜索次数: 0
提问时间: 2025-11-10 22:45:29
🛒 域名购买 :
uj.gs 5.hi.cn zhong.tw twks.cn qn.gs zf.gs 15.gold y-b.net 91.gold sou.gg
❓️ 热门提问 :
php工程师如何做技术调研 国外的vps有哪些 个人搭建云服务器 北京 idc 机房 云空间有必要吗 VPS搭建教程反向代理 mercury服务器 自己开服务器 bgp多线路 wordpress做商城网站 豌豆Ai站群搜索引擎系统
🌐 域名评估 :
hh.tw.cn 815.gs catl.bj.cn yb.gs y-u.com.cn pjnb.cn cnstarway.com vivo.he.cn drone.xz.cn hh.xz.cn
⛏ 最新挖掘 :
凯里酸汤鱼 贵阳烤脑花 都匀毛尖茶 花溪牛肉粉 贵阳烤生蚝 贵阳烤豆腐 贵阳纸包鱼 贵阳特色菜
🖌 热门作画 :
🤝 关于我们:
豌豆Ai
域名
建站
站群
留痕
推广
评估
源码
开发Ai
工具
日记
价格
加盟
广告
流量
留言
联系
🗨 加入群聊 :
🔗 友情链接 :
租香港vps
사이트 순위
ai提问
🧰 站长工具 :Ai工具 whois查询 搜索
📢 温馨提示 :本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持 :本站由豌豆Ai 提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。