第一部分安装Nginxbash# 1. 更新软件源sudo apt update# 2. 安装 Nginxsudo apt install nginx -y# 3. 确认安装成功nginx -v# 会输出类似nginx version: nginx/1.18.0# 4. 启动 Nginxsudo systemctl start nginx# 5. 设置开机自启服务器重启后自动运行sudo systemctl enable nginx# 6. 查看运行状态sudo systemctl status nginx# 看到 active (running) 就成功了第二部分创建你的第一个网站# 1. 创建网站目录sudo mkdir -p /var/www/mysite# 2. 创建首页文件sudo tee /var/www/mysite/index.html EOF!DOCTYPE htmlhtmlheadtitle我的运维实战/titlestylebody { font-family: Arial; text-align: center; padding: 50px; }h1 { color: #2c3e50; }p { color: #7f8c8d; }/style/headbodyh1 我的第一个 Nginx 网站/h1p部署时间2026-09-25/p p运维工程师成长之路/p /body/html第三部分配置Nginx 站点# 1. 创建站点配置文件sudo tee /etc/nginx/sites-available/mysite EOFserver {listen 8080;server_name localhost;root /var/www/mysite;index index.html;location / {try_files $uri $uri/ 404;}}EOF# 2. 启用站点创建软链接到 sites-enabledsudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/# 3. 测试配置是否正确这一步必做sudo nginx -t# 看到 syntax is ok 和 test is successful 就对了# 4. 重新加载 Nginx让配置生效sudo systemctl reload nginx第四部分验证网站# 1. 本机测试curl -I http://localhost:8080# 看到 HTTP/1.1 200 OK 就成功了# 2. 查看页面内容curl http://localhost:8080# 会输出你写的 HTML# 3. 在浏览器访问# 打开浏览器输入 http://localhost:8080# 你会看到 我的第一个 Nginx 网站
