网站搭建速查表
本文档汇总网站搭建过程中的常用命令、配置和工具,方便快速查阅。
域名相关
域名查询命令
# 查询域名 DNS 解析
nslookup example.com
# 使用指定 DNS 服务器查询
nslookup example.com 8.8.8.8
# 详细 DNS 查询
dig example.com
# 查询特定记录类型
dig example.com A
dig example.com MX
dig example.com TXT
# 查询域名 WHOIS 信息
whois example.com
DNS 记录类型
| 记录类型 | 用途 | 示例 |
|---|---|---|
| A | 指向 IPv4 地址 | @ IN A 192.0.2.1 |
| AAAA | 指向 IPv6 地址 | @ IN AAAA 2001:db8::1 |
| CNAME | 域名别名 | www IN CNAME example.com |
| MX | 邮件服务器 | @ IN MX 10 mail.example.com |
| TXT | 文本记录 | @ IN TXT "v=spf1 -all" |
| NS | 名称服务器 | @ IN NS ns1.example.com |
| SRV | 服务记录 | _sip._tcp IN SRV 10 60 5060 sip.example.com |
常用 DNS 服务器
| 服务商 | 首选 DNS | 备用 DNS |
|---|---|---|
| 阿里云 | 223.5.5.5 | 223.6.6.6 |
| 腾讯云 | 119.29.29.29 | 182.254.116.116 |
| 8.8.8.8 | 8.8.4.4 | |
| Cloudflare | 1.1.1.1 | 1.0.0.1 |
服务器相关
SSH 连接
# 使用密码连接
ssh root@server_ip
# 使用密钥连接
ssh -i ~/.ssh/id_rsa root@server_ip
# 指定端口连接
ssh -p 2222 root@server_ip
# 生成 SSH 密钥对
ssh-keygen -t rsa -b 4096
# 复制公钥到服务器
ssh-copy-id root@server_ip
文件传输
# 上传文件
scp local_file root@server:/remote/path/
# 上传目录
scp -r local_dir root@server:/remote/path/
# 使用 rsync 同步
rsync -avz local_dir/ root@server:/remote/path/
# 排除文件
rsync -avz --exclude '*.log' local_dir/ root@server:/remote/path/
系统管理
# 查看系统信息
uname -a
cat /etc/os-release
# 查看 CPU 信息
lscpu
cat /proc/cpuinfo
# 查看内存使用
free -h
# 查看磁盘使用
df -h
# 查看进程
ps aux | grep nginx
# 查看端口占用
netstat -tlnp
ss -tlnp
# 查看系统负载
uptime
top
htop
防火墙配置
firewalld(CentOS/RHEL):
# 查看状态
systemctl status firewalld
# 开放端口
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
# 开放服务
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
# 重载配置
firewall-cmd --reload
# 查看规则
firewall-cmd --list-all
ufw(Ubuntu/Debian):
# 启用防火墙
ufw enable
# 开放端口
ufw allow 80
ufw allow 443
ufw allow 22
# 查看状态
ufw status
# 删除规则
ufw delete allow 80
Nginx 配置
常用命令
# 测试配置
nginx -t
# 重载配置
nginx -s reload
# 启动/停止/重启
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
# 查看状态
systemctl status nginx
# 查看日志
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
基础配置模板
server {
listen 80;
listen 443 ssl http2;
server_name example.com www.example.com;
root /var/www/html;
index index.html index.php;
# SSL 配置
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
# HTTP 重定向 HTTPS
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
# 静态文件缓存
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
# PHP 处理
location ~ \.php$ {
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 反向代理
location /api/ {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# HSTS
add_header Strict-Transport-Security "max-age=63072000" always;
}
负载均衡配置
upstream backend {
server 192.168.1.10:8000 weight=3;
server 192.168.1.11:8000 weight=2;
server 192.168.1.12:8000 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
SSL 证书
Certbot 命令
# 申请证书(自动配置 Nginx)
certbot --nginx
# 申请证书(仅获取证书)
certbot certonly --nginx
# 申请证书(Webroot 方式)
certbot certonly --webroot -w /var/www/html -d example.com
# 申请证书(DNS 验证)
certbot certonly --manual --preferred-challenges dns -d example.com
# 测试续期
certbot renew --dry-run
# 手动续期
certbot renew
# 查看证书列表
certbot certificates
# 删除证书
certbot delete --cert-name example.com
OpenSSL 命令
# 生成私钥
openssl genrsa -out private.key 2048
# 生成 CSR
openssl req -new -key private.key -out request.csr
# 查看证书信息
openssl x509 -in certificate.pem -text -noout
# 测试 SSL 连接
openssl s_client -connect example.com:443
# 检查证书过期时间
echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates
数据库
MySQL 常用命令
# 登录 MySQL
mysql -u root -p
# 创建数据库
CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# 创建用户
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'password';
# 授权
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
# 备份数据库
mysqldump -u root -p mydb > backup.sql
# 恢复数据库
mysql -u root -p mydb < backup.sql
# 导出表结构
mysqldump -u root -p --no-data mydb > schema.sql
Redis 常用命令
# 连接 Redis
redis-cli
# 带密码连接
redis-cli -a password
# 查看所有键
KEYS *
# 清空数据库
FLUSHDB
FLUSHALL
# 查看信息
INFO
# 监控命令
MONITOR
Docker 常用命令
# 构建镜像
docker build -t myapp:latest .
# 运行容器
docker run -d -p 80:80 --name myapp myapp:latest
# 查看容器
docker ps
docker ps -a
# 查看日志
docker logs myapp
docker logs -f myapp
# 进入容器
docker exec -it myapp bash
# 停止/启动容器
docker stop myapp
docker start myapp
# 删除容器
docker rm myapp
# 删除镜像
docker rmi myapp:latest
# Docker Compose
docker-compose up -d
docker-compose down
docker-compose logs -f
docker-compose ps
Git 常用命令
# 初始化仓库
git init
# 克隆仓库
git clone https://github.com/user/repo.git
# 添加文件
git add .
git add file.txt
# 提交
git commit -m "message"
# 推送
git push origin main
# 拉取
git pull origin main
# 查看状态
git status
# 查看日志
git log --oneline
# 创建分支
git branch feature
git checkout -b feature
# 合并分支
git checkout main
git merge feature
静态网站生成器
Hugo
# 创建站点
hugo new site mysite
# 创建文章
hugo new posts/my-post.md
# 本地预览
hugo server -D
# 构建
hugo --minify
# 构建(包含未来日期)
hugo --buildFuture
Hexo
# 初始化
hexo init mysite
cd mysite
npm install
# 创建文章
hexo new "My Post"
# 生成静态文件
hexo generate
# 本地预览
hexo server
# 部署
hexo deploy
进程管理
Systemd
# 启动服务
systemctl start nginx
# 停止服务
systemctl stop nginx
# 重启服务
systemctl restart nginx
# 重载配置
systemctl reload nginx
# 查看状态
systemctl status nginx
# 开机自启
systemctl enable nginx
# 禁用自启
systemctl disable nginx
# 查看日志
journalctl -u nginx
journalctl -u nginx -f
PM2(Node.js)
# 启动应用
pm2 start app.js --name myapp
# 查看进程
pm2 list
# 查看日志
pm2 logs myapp
# 重启应用
pm2 restart myapp
# 停止应用
pm2 stop myapp
# 删除应用
pm2 delete myapp
# 开机自启
pm2 startup
pm2 save
监控与日志
系统监控
# 实时监控
top
htop
# I/O 监控
iostat
iotop
# 网络监控
iftop
nethogs
# 磁盘监控
df -h
du -sh /var/www/*
日志查看
# 实时查看日志
tail -f /var/log/nginx/access.log
# 查看最后 N 行
tail -n 100 /var/log/nginx/error.log
# 搜索日志
grep "error" /var/log/nginx/error.log
grep -r "error" /var/log/
# 统计访问量
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10
性能测试
# 使用 ab 测试
ab -n 1000 -c 10 https://example.com/
# 使用 wrk 测试
wrk -t12 -c400 -d30s https://example.com/
# 使用 curl 测试响应时间
curl -o /dev/null -s -w "Time: %{time_total}s\n" https://example.com/
常用端口
| 端口 | 服务 |
|---|---|
| 20 | FTP 数据 |
| 21 | FTP 控制 |
| 22 | SSH |
| 23 | Telnet |
| 25 | SMTP |
| 53 | DNS |
| 80 | HTTP |
| 110 | POP3 |
| 143 | IMAP |
| 443 | HTTPS |
| 3306 | MySQL |
| 5432 | PostgreSQL |
| 6379 | Redis |
| 8080 | HTTP 代理/备用 HTTP |
| 27017 | MongoDB |
备案相关
备案查询
备案号格式
- ICP 备案号:京ICP备XXXXXXXX号
- ICP 证号:京ICP证XXXXXX号
- 公安备案号:京公网安备 XXXXXXXXXXXXX号
有用的链接
Google Search Console
验证网站所有权
DNS 记录验证:
主机记录:@
记录类型:TXT
记录值:google-site-verification=XXXXXXXXXXXXX
HTML 标签验证:
<meta name="google-site-verification" content="XXXXXXXXXXXXX" />
HTML 文件验证:
将验证文件上传到网站根目录,确保可通过 https://example.com/googleXXXXX.html 访问。
Sitemap 格式
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/</loc>
<lastmod>2024-01-01</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
</urlset>
robots.txt 常用配置
# 允许所有爬虫访问
User-agent: *
Allow: /
# 禁止访问特定目录
Disallow: /admin/
Disallow: /private/
# 指定 sitemap 位置
Sitemap: https://example.com/sitemap.xml
索引检查命令
site:example.com
在 Google 搜索框中输入,查看网站收录情况。
Google AdSense
广告代码
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX"
crossorigin="anonymous"></script>
自动广告
在 <head> 中添加上述代码,Google 自动放置广告。
广告单元示例
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
data-ad-slot="XXXXXXXXXX"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
必备页面清单
/about/- 关于我们/privacy-policy/- 隐私政策/contact/- 联系方式/disclaimer/- 免责声明
Google Analytics
跟踪代码
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
事件跟踪
<button onclick="gtag('event', 'click', {
'event_category': 'Button',
'event_label': 'Download',
'value': 1
});">下载</button>
核心网页指标
| 指标 | 良好 | 需改进 | 较差 |
|---|---|---|---|
| LCP | ≤ 2.5s | 2.5-4s | > 4s |
| FID | ≤ 100ms | 100-300ms | > 300ms |
| CLS | ≤ 0.1 | 0.1-0.25 | > 0.25 |