Ubuntu 建站教程
2026-07-26
摘要:
Ubuntu 建站教程
本文将从零开始,教你在 Ubuntu Server 上搭建一个完整的网站运行环境,包括 Nginx、MySQL/MariaDB、PHP(LEMP 栈)、SSL 证书配置以及网站部署。
一、准备工作
1.1 系统要求
- Ubuntu Server 22.04 LTS / 24.04 LTS
- 至少 1GB 内存
- 20GB 磁盘空间
- Root 权限或 sudo 用户
- 域名已解析到服务器 IP
1.2 购买服务器
推荐选择以下云服务商:
| 服务商 | 特点 | 最低配置 |
|---|---|---|
| 阿里云 ECS | 国内访问快,有轻量应用服务器 | 2C2G |
| 腾讯云 Lighthouse | 性价比高,适合个人站长 | 2C2G |
| 华为云 HECS | 弹性云服务器 | 2C1G |
| 搬瓦工 Bandwagon | 海外 VPS,免备案 | 1C1G |
二、基础环境配置
2.1 连接服务器
ssh root@你的服务器IP
# 或使用普通用户
ssh username@你的服务器IP
2.2 更新系统
sudo apt update && sudo apt upgrade -y
2.3 创建普通用户(安全建议)
adduser webmaster
usermod -aG sudo webmaster
2.4 配置 SSH 安全
编辑 /etc/ssh/sshd_config:
sudo vim /etc/ssh/sshd_config
修改以下配置:
Port 2222 # 修改默认端口(可选)
PermitRootLogin no # 禁止 root 登录
PasswordAuthentication no # 禁止密码登录(使用密钥)
PubkeyAuthentication yes # 允许密钥登录
重启 SSH 服务:
sudo systemctl restart sshd
⚠️ 修改 SSH 配置前,请确保已添加自己的 SSH 公钥,否则会被锁在服务器外!
2.5 配置防火墙(UFW)
sudo ufw allow 2222/tcp # 如果你修改了 SSH 端口
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
sudo ufw --force enable
sudo ufw status
2.6 配置主机名
sudo hostnamectl set-hostname your-domain.com
编辑 /etc/hosts:
echo "127.0.1.1 your-domain.com your-domain" | sudo tee -a /etc/hosts
2.7 配置时区
sudo timedatectl set-timezone Asia/Shanghai
timedatectl status
2.8 优化系统参数
echo "fs.file-max = 65535" | sudo tee -a /etc/sysctl.conf
cat << EOF | sudo tee -a /etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
EOF
sudo sysctl -p
三、安装 Nginx
3.1 安装 Nginx
sudo apt install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
3.2 验证 Nginx 运行
systemctl status nginx
curl -I http://localhost
3.3 Nginx 配置文件结构
/etc/nginx/
├── nginx.conf # 主配置文件
├── sites-available/ # 站点配置(可用)
├── sites-enabled/ # 站点配置(启用)
└── conf.d/ # 附加配置
3.4 创建网站目录
sudo mkdir -p /var/www/your-domain.com/html
sudo mkdir -p /var/www/your-domain.com/logs
sudo chown -R $USER:$USER /var/www/your-domain.com
3.5 创建 Nginx 站点配置
sudo vim /etc/nginx/sites-available/your-domain.com
写入以下内容:
server {
listen 80;
listen [::]:80;
server_name your-domain.com www.your-domain.com;
root /var/www/your-domain.com/html;
index index.html index.htm index.php;
access_log /var/www/your-domain.com/logs/access.log;
error_log /var/www/your-domain.com/logs/error.log;
location / { try_files $uri $uri/ =404; }
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}
location ~ /\.ht { deny all; }
}
3.6 启用站点并测试
sudo ln -s /etc/nginx/sites-available/your-domain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
3.7 创建测试页面
echo '<h1>Hello World!</h1><p>Ubuntu 建站教程</p>' > /var/www/your-domain.com/html/index.html
四、安装 MariaDB(MySQL)
4.1 安装 MariaDB
sudo apt install mariadb-server mariadb-client -y
sudo systemctl enable mariadb
sudo systemctl start mariadb
4.2 安全初始化
sudo mysql_secure_installation
4.3 创建网站数据库
sudo mysql -u root -p
CREATE DATABASE mywebsite DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'your-strong-password';
GRANT ALL PRIVILEGES ON mywebsite.* TO 'webuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
五、安装 PHP
5.1 安装 PHP 及常用扩展
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip php-bcmath php-redis -y
5.2 验证 PHP 版本
php -v
5.3 配置 PHP-FPM
编辑 /etc/php/8.3/fpm/php.ini,修改:
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
memory_limit = 256M
date.timezone = Asia/Shanghai
重启:
sudo systemctl restart php8.3-fpm
六、配置 SSL 证书(HTTPS)
6.1 安装 Certbot
sudo apt install certbot python3-certbot-nginx -y
6.2 获取 SSL 证书
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
6.3 验证自动续期
sudo certbot renew --dry-run
七、部署网站
7.1 手动部署
rsync -avz --delete ./dist/ webmaster@your-server-ip:/var/www/your-domain.com/html/
7.2 使用 Git 部署
cd /var/www/your-domain.com/html
git clone https://github.com/yourusername/your-repo.git .
7.3 部署 WordPress
cd /var/www/your-domain.com/html
wget https://wordpress.org/latest.tar.gz
tar xzf latest.tar.gz --strip-components=1
rm latest.tar.gz
sudo chown -R www-data:www-data .
八、性能优化
8.1 启用 Gzip
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/javascript text/xml image/svg+xml;
8.2 配置浏览器缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js|webp|avif)$ {
expires 30d;
add_header Cache-Control 'public, immutable';
}
8.3 配置安全头
add_header X-Frame-Options 'SAMEORIGIN' always;
add_header X-Content-Type-Options 'nosniff' always;
add_header X-XSS-Protection '1; mode=block' always;
add_header Strict-Transport-Security 'max-age=63072000; includeSubDomains; preload' always;
九、监控与维护
9.1 自动备份
创建 /usr/local/bin/backup.sh:
#!/bin/bash
BACKUP_DIR='/backup'
DATE=$(date +%Y%m%d)
tar czf $BACKUP_DIR/website-$DATE.tar.gz /var/www/
mysqldump -u root -p mywebsite > $BACKUP_DIR/database-$DATE.sql
find $BACKUP_DIR -name '*.tar.gz' -mtime +30 -delete
find $BACKUP_DIR -name '*.sql' -mtime +30 -delete
echo '备份完成: $DATE'
添加到 crontab:
0 3 * * * /bin/bash /usr/local/bin/backup.sh
9.2 安装 Fail2ban
sudo apt install fail2ban -y
sudo systemctl enable fail2ban --now
十、常见问题
10.1 无法访问网站(502)
sudo systemctl restart php8.3-fpm- 检查 Nginx 错误日志:
sudo tail -f /var/log/nginx/error.log
10.2 数据库连接失败
systemctl status mariadb- 检查用户权限
总结
Ubuntu 建站完整流程:
Ubuntu 服务器 → 基础配置 → Nginx → MariaDB → PHP → SSL → 网站部署
这套环境可以运行 WordPress、Halo、Typecho 等各种类型的网站。
Happy Hosting! 🚀