第一步:准备工作
在开始之前,请确保你已经:

- 拥有一台 Ubuntu 服务器:可以是云服务器(如阿里云、腾讯云、AWS、Vultr 等)或本地虚拟机(如 VirtualBox, VMware)。
- 拥有管理员权限:通过
sudo命令执行需要管理员权限的操作。 - 服务器已更新:保持系统软件包为最新版本是一个好习惯。
sudo apt update sudo apt upgrade -y
第二步:选择并安装 Web 服务器
你可以选择 Nginx 或 Apache,两者都是优秀的 Web 服务器,但它们的设计哲学略有不同:
- Nginx:以其高并发、低内存消耗和反向代理功能而闻名,处理静态文件(如 HTML, CSS, 图片)性能极高。
- Apache:历史悠久,模块丰富,配置灵活,兼容性极好,通常被认为比 Nginx 更容易上手。
对于初学者,两者都可以,这里我们分别介绍如何安装。
方案 A:安装 Nginx
-
安装 Nginx
sudo apt install nginx -y
-
启动并启用 Nginx 服务
(图片来源网络,侵删)start:立即启动服务。enable:设置服务在开机时自动启动。sudo systemctl start nginx sudo systemctl enable nginx
-
检查 Nginx 状态
sudo systemctl status nginx
如果看到绿色的
active (running)字样,说明服务已成功启动。
方案 B:安装 Apache
-
安装 Apache
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) 流量通过防火墙,Ubuntu 默认使用 ufw (Uncomplicated Firewall)。
-
允许流量
sudo ufw allow 'Nginx Full' # 如果安装的是 Nginx # 或者 sudo ufw allow 'Apache Full' # 如果安装的是 Apache
Nginx Full同时允许 HTTP 和 HTTPS 流量。Apache Full也是如此。
-
启用防火墙(如果尚未启用)
sudo ufw enable
输入
y确认。 -
验证防火墙状态
sudo ufw status
你应该能看到允许的规则中包含了
Nginx Full或Apache Full。
第四步:测试默认页面
你可以通过服务器的公网 IP 地址来访问 Web 服务器了。
-
获取你的服务器公网 IP
curl -4 ifconfig.me # 或者 curl icanhazip.com
-
在浏览器中访问 打开浏览器,输入
http://<你的公网IP>。- 如果你安装的是 Nginx,你应该看到 "Welcome to nginx!" 的欢迎页面。
- 如果你安装的是 Apache,你应该看到 "Apache2 Ubuntu Default Page" 的页面。
如果看到了这些页面,恭喜你!你的 Web 服务器已经成功运行了。
第五步:部署你的第一个网站
默认的网站目录是 /var/www/html,我们将创建一个新的网站目录。
创建网站目录
假设我们的网站域名是 example.com。
sudo mkdir -p /var/www/example.com/html
mkdir -p会创建所有必需的父目录。
设置目录权限
Web 服务器进程(www-data 用户)需要读取这些文件,我们需要将目录的所有权赋予这个用户。
sudo chown -R www-data:www-data /var/www/example.com/html
-R表示递归地更改所有子文件和子目录的所有权。
创建一个测试首页
使用 nano 或你喜欢的编辑器创建一个 index.html 文件。
sudo nano /var/www/example.com/html/index.html
在文件中粘贴以下内容:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">我的第一个网站</title>
</head>
<body>
<h1>你好,世界!</h1>
<p>欢迎来到我的网站,部署在 Ubuntu 服务器上!</p>
</body>
</html>
保存并退出(在 nano 中是 Ctrl+X,然后按 Y,再按 Enter)。
配置虚拟主机
虚拟主机允许你在同一台服务器上托管多个网站,我们需要为 example.com 创建一个配置文件。
如果你使用的是 Nginx:
-
创建新的配置文件
sudo nano /etc/nginx/sites-available/example.com
-
粘贴以下配置内容
server { listen 80; server_name example.com www.example.com; root /var/www/example.com/html; index index.html index.htm; location / { try_files $uri $uri/ =404; } }server_name:替换为你的实际域名,如果你没有域名,可以先使用你的公网 IP。root:指向你的网站根目录。
-
启用配置文件 创建一个符号链接到
sites-enabled目录。sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
-
测试并重载 Nginx
# 测试配置文件语法是否正确 sudo nginx -t # 如果测试通过,重载 Nginx 以应用新配置 sudo systemctl reload nginx
如果你使用的是 Apache:
-
创建新的配置文件 Apache 的配置文件通常以
.conf并放在sites-available目录下。sudo nano /etc/apache2/sites-available/example.com.conf
-
粘贴以下配置内容
<VirtualHost *:80> ServerAdmin webmaster@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>ServerName和ServerAlias:同 Nginx。DocumentRoot:指向你的网站根目录。
-
启用配置文件 使用
a2ensite工具。sudo a2ensite example.com.conf
-
启用
rewrite模块(可选,但推荐)sudo a2enmod rewrite
-
检查 Apache 配置并重载
# 检查配置文件语法 sudo apache2ctl configtest # 如果显示 "Syntax OK",重载 Apache sudo systemctl reload apache2
访问你的网站
在浏览器中访问 http://example.com(或你的公网 IP),你应该能看到你刚刚创建的 "你好,世界!" 页面了。
第六步:进阶配置(可选)
安装 PHP 支持(用于动态网站)
如果你的网站需要 PHP,WordPress 或其他 CMS,你需要安装 PHP-FPM。
-
安装 PHP 和 PHP-FPM
# 对于 Nginx sudo apt install php-fpm php-mysql php-mbstring php-xml php-curl php-zip -y # 对于 Apache sudo apt install libapache2-mod-php php-mysql php-mbstring php-xml php-curl php-zip -y
-
配置 Nginx 处理 PHP 文件 编辑你的 Nginx 配置文件 (
/etc/nginx/sites-available/example.com),在location /块下方添加:location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; # 注意版本号,可能是 8.1, 8.0 等 }php8.2-fpm.sock的版本号需要根据你安装的 PHP 版本进行调整,可以用ls /var/run/php/查看。
-
配置 Apache 处理 PHP 文件 Apache 通常会自动处理,但需要确保
index.php在DirectoryIndex指令中。 编辑主配置文件/etc/apache2/sites-available/000-default.conf或你的虚拟主机配置文件,确保有:<Directory /var/www/html> # ... 其他配置 ... DirectoryIndex index.php index.html index.htm </Directory> -
重启/重载 Web 服务器
# Nginx sudo systemctl restart nginx # Apache sudo systemctl restart apache2
配置 HTTPS(使用 Let's Encrypt)
为你的网站启用 HTTPS 是非常重要的,可以使用免费的 Let's Encrypt 证书。
-
安装 Certbot Certbot 是一个自动获取和续签 Let's Encrypt 证书的工具。
sudo apt install certbot python3-certbot-nginx -y # 如果使用 Nginx # 或者 sudo apt install certbot python3-certbot-apache -y # 如果使用 Apache
-
获取并安装证书 运行以下命令,根据提示输入你的邮箱并同意条款,Certbot 会自动检测你的 Web 服务器配置并完成所有设置。
# 对于 Nginx sudo certbot --nginx -d example.com -d www.example.com # 对于 Apache sudo certbot --apache -d example.com -d www.example.com
-
测试自动续签 Let's Encrypt 证书有效期为 90 天,Certbot 会自动设置一个定时任务来续签,你可以用以下命令测试:
sudo certbot renew --dry-run
如果没有输出错误,说明续签工作正常。
恭喜你!你已经成功完成了在 Ubuntu 上配置 Web 服务器的全过程:
- 安装:选择了 Nginx 或 Apache。
- 基础配置:启动服务,配置防火墙。
- 部署:创建了网站目录,设置了权限,并部署了静态页面。
- 高级配置:学习了如何配置虚拟主机、安装 PHP 支持,以及如何启用 HTTPS。
现在你有了一个功能齐全、安全的 Web 服务器,可以开始部署你的网站项目了!
