Debian Minimal部署Web服务器指南
Debian Minimal是精简版操作系统,无图形界面,需通过命令行部署Web服务器(以Apache/Nginx为例)。以下是详细步骤:
更新系统包
运行以下命令同步软件包索引,确保安装最新版本:
sudo apt update && sudo apt upgrade -y
安装必要工具
若需用ufw管理防火墙,先安装:
sudo apt install ufw -y
执行命令安装Apache2:
sudo apt install apache2 -y
启动Apache服务并设置为开机自动运行:
sudo systemctl start apache2
sudo systemctl enable apache2
检查Apache是否运行正常:
sudo systemctl status apache2
若显示active (running),则表示成功。
允许HTTP(80端口)和HTTPS(443端口)流量:
sudo ufw allow 'Apache Full'
sudo ufw enable # 启用防火墙(若未启用)
在浏览器输入服务器IP地址,若看到Apache默认欢迎页面(如“Apache2 Debian Default Page”),则部署成功。
执行命令安装Nginx:
sudo apt install nginx -y
启动Nginx服务并设置开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
检查Nginx运行状态:
sudo systemctl status nginx
正常状态为active (running)。
允许HTTP和HTTPS流量:
sudo ufw allow 'Nginx Full'
sudo ufw enable
浏览器输入服务器IP,若看到Nginx默认欢迎页面(如“Welcome to nginx!”),则部署成功。
创建虚拟主机配置文件:
sudo nano /etc/apache2/sites-available/example.com.conf
添加以下内容(替换example.com为你的域名,/var/www/example.com为网站根目录):
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
启用虚拟主机:
sudo a2ensite example.com.conf # 启用配置
sudo a2dissite 000-default.conf # 可选:禁用默认站点
sudo systemctl reload apache2 # 重新加载配置
创建网站目录与测试页:
sudo mkdir -p /var/www/example.com
echo "<h1>Welcome to Example.com</h1>" | sudo tee /var/www/example.com/index.html
安装Certbot:
sudo apt install certbot python3-certbot-apache -y
获取并安装证书:
sudo certbot --apache -d example.com -d www.example.com
按提示完成证书安装(自动配置HTTPS重定向)。
测试自动续期:
Certbot会自动添加cron任务,可手动测试:
sudo certbot renew --dry-run
curl localhost测试服务器本地访问。sudo apachectl configtest,Nginx用sudo nginx -t检查。/var/www/)有正确的读写权限(如chown -R $USER:$USER /var/www/example.com)。通过以上步骤,即可在Debian Minimal系统上成功部署Apache或Nginx Web服务器,并根据需求扩展虚拟主机、SSL等功能。