Ubuntu 本地建站完整教程(双域名:games1.990011.cn + games2.990011.cn,局域网 IP:192.168.1.15)
Ubuntu 本地建站完整教程(双域名:games1.990011.cn + games2.990011.cn,局域网 IP:192.168.1.15)
适配你的局域网 IP:192.168.1.15,包含:基础环境搭建、单站点部署、新增多站点完整步骤、hosts 绑定、HTTPS 部署,全程复制命令即可运行,新手零踩坑。
一、整体环境说明
-
系统:Ubuntu 桌面 / 服务器版通用
-
服务:Nginx(网页服务)+ PHP(动态网页)+ MySQL(数据库)
-
域名:games1.990011.cn、games2.990011.cn
-
局域网 IP:192.168.1.15
-
用途:本地建站、局域网所有设备访问、后续可对接公网 SSL 证书
第一步:更新系统 & 安装全套网站环境
打开 Ubuntu 终端,逐条复制运行
1. 更新系统软件源
==【sudo apt update && sudo apt upgrade -y】============
sudo apt update && sudo apt upgrade -y
2. 安装 Nginx(网页核心服务)
==【sudo apt install nginx -y】============
sudo apt install nginx -y
验证运行状态:
==【sudo apt install php-fpm php-mysql -y】============
sudo systemctl status nginx
出现active (running)即正常运行。
3. 安装 PHP 环境(动态网页必备)
==【sudo apt install php-fpm php-mysql -y】============
sudo apt install php-fpm php-mysql -y
4. 安装 MySQL 数据库
==【sudo apt install mysql-server -y】============
sudo apt install mysql-server -y
5. 放行防火墙(局域网设备可访问)
==【sudo ufw allow 'Nginx Full'
sudo ufw reload】============
sudo ufw allow 'Nginx Full' sudo ufw reload
第二步:创建第一个网站:games1.990011.cn
1. 新建网站专属目录(存放网页 / 源码)
规范存放位置,不和系统文件混杂,权限开放给当前用户:
==【# 网站根目录:games1站点
sudo mkdir -p /var/www/games1
# 赋予当前用户读写权限,方便修改文件、上传源码
sudo chown -R $USER:$USER /var/www/games1
sudo chmod -R 755 /var/www/games1
】============
# 网站根目录:games1站点 sudo mkdir -p /var/www/games1 # 赋予当前用户读写权限,方便修改文件、上传源码 sudo chown -R $USER:$USER /var/www/games1 sudo chmod -R 755 /var/www/games1
2. 写入测试网页(验证环境正常)
==【nano /var/www/games1/index.php】============
nano /var/www/games1/index.php
粘贴以下代码:
==【<?php
echo "<h1>✅ games1.990011.cn 本地网站部署成功!</h1>";
echo "<p>服务器局域网IP:192.168.1.15</p>";
echo "<p>当前系统时间:".date("Y-m-d H:i:s")."</p>";
echo "<p>PHP环境运行正常,可上传网站源码</p>";
?>
】============
<?php echo "<h1>✅ games1.990011.cn 本地网站部署成功!</h1>"; echo "<p>服务器局域网IP:192.168.1.15</p>"; echo "<p>当前系统时间:".date("Y-m-d H:i:s")."</p>"; echo "<p>PHP环境运行正常,可上传网站源码</p>"; ?>
保存退出:Ctrl+O→ 回车 →Ctrl+X
3. 配置 Nginx 站点(绑定 games1 域名)
==【sudo nano /etc/nginx/sites-available/games1】============
sudo nano /etc/nginx/sites-available/games1
直接复制以下完整配置:
nginx
==【server {
listen 80;
server_name localhost games1.990011.cn;
# 网站文件存放路径(对应上面创建的目录)
root /var/www/games1;
index index.php index.html index.htm;
# 局域网全局访问规则
location / {
try_files $uri $uri/ =404;
}
# PHP脚本解析规则
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
】============
server { listen 80; server_name localhost games1.990011.cn; # 网站文件存放路径(对应上面创建的目录) root /var/www/games1; index index.php index.html index.htm; # 局域网全局访问规则 location / { try_files $uri $uri/ =404; } # PHP脚本解析规则 location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; } }
Ubuntu24.04 系统:将php8.1-fpm替换为php8.3-fpm
保存退出:Ctrl+O→ 回车 →Ctrl+X
4. 启用站点并生效
==【# 启用站点配置
sudo ln -s /etc/nginx/sites-available/games1 /etc/nginx/sites-enabled/
# 测试配置是否有误
sudo nginx -t
# 重载Nginx,网站正式上线
sudo systemctl reload nginx
】============
# 启用站点配置 sudo ln -s /etc/nginx/sites-available/games1 /etc/nginx/sites-enabled/ # 测试配置是否有误 sudo nginx -t # 重载Nginx,网站正式上线 sudo systemctl reload nginx
看到test is successful即配置无误。
5. 绑定域名到本机 hosts(局域网解析生效)
==【sudo nano /etc/hosts】============
sudo nano /etc/hosts
在文件末尾添加:
==【192.168.1.15 games1.990011.cn】============
192.168.1.15 games1.990011.cn
保存退出:Ctrl+O→ 回车 →Ctrl+X
6. 访问测试
-
本机浏览器:http://games1.990011.cn
-
局域网其他设备:同 WiFi 下访问http://192.168.1.15或域名,均可打开网站
第三步:【重点】新增第二个网站:games2.990011.cn(完整新增站点步骤)
1. 新建第二个网站目录(独立存放 games2 源码)
==【# 新建games2站点目录
sudo mkdir -p /var/www/games2
# 赋予读写权限
sudo chown -R $USER:$USER /var/www/games2
sudo chmod -R 755 /var/www/games2
】============
# 新建games2站点目录 sudo mkdir -p /var/www/games2 # 赋予读写权限 sudo chown -R $USER:$USER /var/www/games2 sudo chmod -R 755 /var/www/games2
核心规则:一个站点一个独立目录,互不干扰,方便管理。
2. 写入 games2 测试网页
==【nano /var/www/games2/index.php】============
nano /var/www/games2/index.php
粘贴代码:
php
==【<?php
echo "<h1>✅ games2.990011.cn 第二个网站部署成功!</h1>";
echo "<p>服务器局域网IP:192.168.1.15</p>";
echo "<p>双站点运行正常,可分别上传不同网站源码</p>";
?>
】============
<?php echo "<h1>✅ games2.990011.cn 第二个网站部署成功!</h1>"; echo "<p>服务器局域网IP:192.168.1.15</p>"; echo "<p>双站点运行正常,可分别上传不同网站源码</p>"; ?>
保存退出。
3. 配置 Nginx,绑定 games2 域名
==【sudo nano /etc/nginx/sites-available/games2】============
sudo nano /etc/nginx/sites-available/games2
sudo apt update && sudo apt upgrade -y
sudo apt install nginx -y
sudo systemctl status nginx
sudo apt install php-fpm php-mysql -y
sudo apt install mysql-server -y
sudo ufw allow 'Nginx Full' sudo ufw reload
sudo mkdir -p /var/www/games1
# 赋予当前用户读写权限,方便修改文件、上传源码
sudo chown -R $USER:$USER /var/www/games1
sudo chmod -R 755 /var/www/games1
】============
# 网站根目录:games1站点 sudo mkdir -p /var/www/games1 # 赋予当前用户读写权限,方便修改文件、上传源码 sudo chown -R $USER:$USER /var/www/games1 sudo chmod -R 755 /var/www/games1
nano /var/www/games1/index.php
echo "<h1>✅ games1.990011.cn 本地网站部署成功!</h1>";
echo "<p>服务器局域网IP:192.168.1.15</p>";
echo "<p>当前系统时间:".date("Y-m-d H:i:s")."</p>";
echo "<p>PHP环境运行正常,可上传网站源码</p>";
?>
】============
<?php echo "<h1>✅ games1.990011.cn 本地网站部署成功!</h1>"; echo "<p>服务器局域网IP:192.168.1.15</p>"; echo "<p>当前系统时间:".date("Y-m-d H:i:s")."</p>"; echo "<p>PHP环境运行正常,可上传网站源码</p>"; ?>
sudo nano /etc/nginx/sites-available/games1
nginx
==【server {
listen 80;
server_name localhost games1.990011.cn;
# 网站文件存放路径(对应上面创建的目录)
root /var/www/games1;
index index.php index.html index.htm;
# 局域网全局访问规则
location / {
try_files $uri $uri/ =404;
}
# PHP脚本解析规则
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
】============
server { listen 80; server_name localhost games1.990011.cn; # 网站文件存放路径(对应上面创建的目录) root /var/www/games1; index index.php index.html index.htm; # 局域网全局访问规则 location / { try_files $uri $uri/ =404; } # PHP脚本解析规则 location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; } }
sudo ln -s /etc/nginx/sites-available/games1 /etc/nginx/sites-enabled/
# 测试配置是否有误
sudo nginx -t
# 重载Nginx,网站正式上线
sudo systemctl reload nginx
】============
# 启用站点配置 sudo ln -s /etc/nginx/sites-available/games1 /etc/nginx/sites-enabled/ # 测试配置是否有误 sudo nginx -t # 重载Nginx,网站正式上线 sudo systemctl reload nginx
sudo nano /etc/hosts
192.168.1.15 games1.990011.cn
sudo mkdir -p /var/www/games2
# 赋予读写权限
sudo chown -R $USER:$USER /var/www/games2
sudo chmod -R 755 /var/www/games2
】============
# 新建games2站点目录 sudo mkdir -p /var/www/games2 # 赋予读写权限 sudo chown -R $USER:$USER /var/www/games2 sudo chmod -R 755 /var/www/games2
nano /var/www/games2/index.php
echo "<h1>✅ games2.990011.cn 第二个网站部署成功!</h1>";
echo "<p>服务器局域网IP:192.168.1.15</p>";
echo "<p>双站点运行正常,可分别上传不同网站源码</p>";
?>
】============
<?php echo "<h1>✅ games2.990011.cn 第二个网站部署成功!</h1>"; echo "<p>服务器局域网IP:192.168.1.15</p>"; echo "<p>双站点运行正常,可分别上传不同网站源码</p>"; ?>
sudo nano /etc/nginx/sites-available/games2
粘贴完整配置:
nginx
==【server {
listen 80;
server_name localhost games2.990011.cn;
# games2专属网站目录
root /var/www/games2;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
】============
server { listen 80; server_name localhost games2.990011.cn; # games2专属网站目录 root /var/www/games2; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; } }
4. 启用 games2 站点
sudo ln -s /etc/nginx/sites-available/games2 /etc/nginx/sites-enabled/
# 测试配置
sudo nginx -t
# 重载生效
sudo systemctl reload nginx
】============
# 启用站点 sudo ln -s /etc/nginx/sites-available/games2 /etc/nginx/sites-enabled/ # 测试配置 sudo nginx -t # 重载生效 sudo systemctl reload nginx
5. hosts 绑定 games2 域名
sudo nano /etc/hosts
末尾追加一行:
==【192.168.1.15 games2.990011.cn】============
6. 双站点访问验证
- http://games1.990011.cn→ 打开第一个网站
- http://games2.990011.cn→ 打开第二个网站
第四步:统一部署 HTTPS(对接你之前的 SSL 证书,绿色安全锁)
1. 修改 games1 的 Nginx 配置
sudo nano /etc/nginx/sites-available/games1
替换为 HTTPS 完整配置:
nginx
==【# HTTP强制跳转HTTPS
server {
listen 80;
server_name localhost games1.990011.cn;
return 301 https://$host$request_uri;
}
# HTTPS正式站点
server {
listen 443 ssl;
server_name localhost games1.990011.cn;
# 你的SSL证书路径
ssl_certificate /etc/nginx/ssl/4bai.cn/fullchain.cer;
ssl_certificate_key /etc/nginx/ssl/4bai.cn/4bai.cn.key;
root /var/www/games1;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
】============
# HTTP强制跳转HTTPS server { listen 80; server_name localhost games1.990011.cn; return 301 https://$host$request_uri; } # HTTPS正式站点 server { listen 443 ssl; server_name localhost games1.990011.cn; # 你的SSL证书路径 ssl_certificate /etc/nginx/ssl/4bai.cn/fullchain.cer; ssl_certificate_key /etc/nginx/ssl/4bai.cn/4bai.cn.key; root /var/www/games1; index index.php index.html; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; } }
2. 修改 games2 的 Nginx 配置
sudo nano /etc/nginx/sites-available/games2
替换为:
nginx
==【server {
listen 80;
server_name localhost games2.990011.cn;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name localhost games2.990011.cn;
ssl_certificate /etc/nginx/ssl/4bai.cn/fullchain.cer;
ssl_certificate_key /etc/nginx/ssl/4bai.cn/4bai.cn.key;
root /var/www/games2;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
】============
server { listen 80; server_name localhost games2.990011.cn; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name localhost games2.990011.cn; ssl_certificate /etc/nginx/ssl/4bai.cn/fullchain.cer; ssl_certificate_key /etc/nginx/ssl/4bai.cn/4bai.cn.key; root /var/www/games2; index index.php index.html; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; } }
3. 重载 Nginx 生效
sudo nginx -t sudo systemctl reload nginx
第五步:【通用模板】新增第 N 个网站(复制即用)
步骤 1:新建独立网站目录
sudo mkdir -p /var/www/新站点名
sudo chown -R $USER:$USER /var/www/新站点名
sudo chmod -R 755 /var/www/新站点名
】============
# 替换为你的新域名,例:games3 sudo mkdir -p /var/www/新站点名 sudo chown -R $USER:$USER /var/www/新站点名 sudo chmod -R 755 /var/www/新站点名
步骤 2:新建 Nginx 站点配置
sudo nano /etc/nginx/sites-available/新站点名
粘贴模板,替换域名和目录:
nginx
==【server {listen 80;
server_name localhost 你的新域名;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name localhost 你的新域名;
ssl_certificate /etc/nginx/ssl/4bai.cn/fullchain.cer;
ssl_certificate_key /etc/nginx/ssl/4bai.cn/4bai.cn.key;
root /var/www/新站点名;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
】============
server { listen 80; server_name localhost 你的新域名; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name localhost 你的新域名; ssl_certificate /etc/nginx/ssl/4bai.cn/fullchain.cer; ssl_certificate_key /etc/nginx/ssl/4bai.cn/4bai.cn.key; root /var/www/新站点名; index index.php index.html; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; } }
步骤 3:启用站点
sudo nginx -t
sudo systemctl reload nginx
】============
sudo ln -s /etc/nginx/sites-available/新站点名 /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx
步骤 4:绑定 hosts
sudo nano /etc/hosts # 末尾添加:192.168.1.15 你的新域名
第六步:网站文件管理 & 日常操作
1. 网站文件存放位置(直接上传源码 / 修改网页)
- games1 站点:/var/www/games1/
- games2 站点:/var/www/games2/
直接把网站源码、图片、程序放到对应目录,即可运行。
2. Nginx 常用命令
sudo systemctl start nginx
# 停止网站
sudo systemctl stop nginx
# 重启网站
sudo systemctl restart nginx
# 重载配置(不中断访问)
sudo systemctl reload nginx
】============
# 启动网站 sudo systemctl start nginx # 停止网站 sudo systemctl stop nginx # 重启网站 sudo systemctl restart nginx # 重载配置(不中断访问) sudo systemctl reload nginx
3. 局域网其他设备访问
192.168.1.15 games1.990011.cn games2.990011.cn