Debian域名如何进行网站搭建
小樊
45
2025-12-18 22:09:33
在 Debian 上用域名搭建网站的完整流程
一 准备与规划
- 准备:一台公网可达的 Debian 服务器(云服务器或物理机)、已注册的 域名、域名解析控制权(DNS 服务商处可添加记录)、放通 TCP 80/443 的网络策略。
- 规划:选择 Web 方案(Apache 或 Nginx)、是否需要 HTTPS/SSL、是否需要 数据库(MySQL/MariaDB) 与 PHP、是否采用 Docker 部署。
二 域名解析到服务器
- 在域名服务商的 DNS 控制台添加记录,将域名指向服务器公网 IPv4(如需 IPv6 可再添加 AAAA 记录)。常见记录类型与用途如下:
| 记录类型 |
主机记录 |
值示例 |
说明 |
| A |
@ |
203.0.113.10 |
将根域名指向服务器 IP |
| A |
www |
203.0.113.10 |
将 www 子域指向服务器 IP |
| CNAME |
www |
example.com |
将 www 作为别名指向根域名 |
- 验证解析是否生效(在服务器或任意终端执行):
- dig 示例:dig +short example.com A
- nslookup 示例:nslookup example.com
- 若你确实需要自建权威 DNS 服务器(非必须),可在 Debian 上安装 BIND9 并配置正向/反向区域,但这通常用于内网或特定场景,大多数网站直接使用云厂商 DNS 即可。
三 安装与配置 Web 服务
- 方案 A Apache 虚拟主机
- 安装与启动
- sudo apt update && sudo apt install apache2
- sudo systemctl start apache2 && sudo systemctl enable apache2
- 创建站点目录与示例页
- sudo mkdir -p /var/www/example.com/public_html
- echo “Welcome to example.com” | sudo tee /var/www/example.com/public_html/index.html
- sudo chown -R www-data:www-data /var/www/example.com
- 新建虚拟主机配置
- sudo nano /etc/apache2/sites-available/example.com.conf
- 示例要点:
- ServerName example.com
- ServerAlias www.example.com
- DocumentRoot /var/www/example.com/public_html
- <Directory …> 中设置 AllowOverride All、Require all granted
- 启用站点与可选禁用默认站点
- sudo a2ensite example.com.conf
- sudo a2dissite 000-default.conf(可选)
- 语法检查与生效
- sudo apache2ctl configtest
- sudo systemctl reload apache2
- 方案 B Nginx 服务器块
- 安装与启动
- sudo apt update && sudo apt install nginx
- sudo systemctl start nginx && sudo systemctl enable nginx
- 创建站点目录与示例页(同上)
- 新建服务器块配置
- sudo nano /etc/nginx/sites-available/example.com.conf
- 示例要点:
- listen 80; server_name example.com www.example.com;
- root /var/www/example.com/public_html; index index.html;
- location / { try_files $uri $uri/ =404; }
- 启用站点
- sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
- sudo nginx -t && sudo systemctl reload nginx
- 防火墙放通(如使用 UFW)
- Apache:sudo ufw allow ‘Apache Full’
- Nginx:sudo ufw allow ‘Nginx Full’
四 部署应用与数据库 LNMP 示例
- 安装数据库(以 MariaDB 为例)
- sudo apt install mariadb-server mariadb-client
- sudo mysql_secure_installation
- 安装 PHP 与常用扩展(以 PHP 7.4 为例,按系统可用版本调整)
- sudo apt install php-fpm php-mysql
- 配置 Nginx 处理 PHP
- 在 server 块中添加:
- location ~ .php$ {
- include snippets/fastcgi-php.conf;
- fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- 重启服务:sudo systemctl restart php7.4-fpm nginx
- 创建测试文件
- echo “” | sudo tee /var/www/example.com/public_html/info.php
- 访问 http://example.com/info.php 验证 PHP 解析是否正常。
五 启用 HTTPS 与常见问题排查
- 使用 Let’s Encrypt 免费证书
- Apache:sudo apt install certbot python3-certbot-apache
- sudo certbot --apache -d example.com -d www.example.com
- Nginx:sudo apt install certbot python3-certbot-nginx
- sudo certbot --nginx -d example.com -d www.example.com
- 证书会自动续期(certbot 定时器),确保 80/443 端口开放。
- 常见问题快速排查
- 域名访问不到:检查 DNS 记录是否已生效(dig/nslookup)、服务器防火墙是否放通 80/443、云安全组策略是否允许入站。
- 403/404:检查 DocumentRoot 路径、Directory 权限(www-data)、是否禁用默认站点(Apache)。
- PHP 不解析:确认 PHP-FPM 运行、Nginx fastcgi_pass 指向正确 .sock 文件、站点配置包含 PHP 处理 location。
- 查看错误日志定位问题:
- Apache:/var/log/apache2/error.log
- Nginx:/var/log/nginx/error.log。