你可以给我一个用linux当作网络服务器的教程吗?谢谢!由于我连接的是校园网,并没有一个公网ip。
- User
你可以给我一个用linux当作网络服务器的教程吗?谢谢!由于我连接的是校园网,并没有一个公网ip。
Assistant
好问题!虽然没有公网IP会稍微麻烦一点,但我们依然可以搭建一个功能完善的网络服务器,并且我会教你如何绕过这个限制,让外网用户也能访问你的服务器(当然,得借助一些黑科技)。下面是一个详细的Linux网络服务器搭建教程,分基础和进阶两部分:
环境假设:
- 你使用的是 Ubuntu 22.04 LTS(其他发行版命令类似,包管理器可能不同)。
- 你在 校园网环境,没有独立的公网 IP(即外网无法直接访问你的电脑)。
- 你想搭建一个 Web 服务器(Apache/Nginx) + 数据库(MySQL),并支持 SSH 远程管理。
- 额外技能:我们用 内网穿透工具(Frp 或 Ngrok) 让外网能访问你的校园网服务器。
教程大纲
- 基础篇:搭建 LAMP/NMP 服务器(本地可访问)
- 安装 Linux(如果你还没装)
- 安装 Apache/Nginx(Web 服务器)
- 安装 MySQL(数据库)
- 安装 PHP(动态网页支持)
- 测试本地网站是否能运行
- 进阶篇:无公网 IP?教你内网穿透(外网可访问)
- 方法 1:使用 Ngrok(简单但有流量限制)
- 方法 2:使用 FRP(更强大,需自备云服务器)
- 番外篇:安全与优化(防火墙、SSH 配置)
基础篇:搭建 LAMP 服务器(本地访问)
Step 1. 更新系统
打开终端,执行:
sudo apt update && sudo apt upgrade -y这一步更新软件源并升级系统包。
Step 2. 安装 Apache(Web 服务器)
Apache 是最流行的 Web 服务器之一,安装命令:
sudo apt install apache2 -y验证是否成功:
- 打开浏览器,输入
http://localhost或http://127.0.0.1 - 如果看到 "It works! Apache2 Ubuntu Default Page",说明 Web 服务器已运行。
常用 Apache 命令:
sudo systemctl start apache2 # 启动 Apache sudo systemctl enable apache2 # 开机自启 sudo systemctl status apache2 # 查看运行状态Step 3. 安装 MySQL(数据库服务器)
我们需要一个数据库存储数据:
sudo apt install mysql-server -y安全配置 MySQL:
sudo mysql_secure_installation按提示设置:
- 设置 root 密码(强烈建议设置强密码)
- 移除匿名用户(选
Y) - 禁止远程 root 登录(校园网环境选
Y,但后面我们会改回来) - 移除测试数据库(
Y) - 刷新权限表(
Y)
测试 MySQL:
sudo mysql -u root -p输入密码,进入 MySQL 终端,说明成功。
Step 4. 安装 PHP(动态网页支持)
Apache 需要 PHP 解析动态网站(如 WordPress):
sudo apt install php libapache2-mod-php php-mysql -y重启 Apache 让 PHP 生效:
sudo systemctl restart apache2测试 PHP 是否工作:
- 创建一个测试文件:
sudo nano /var/www/html/info.php - 输入以下内容:
<?php phpinfo(); ?> - 保存(
Ctrl+O,回车,Ctrl+X退出)。 - 浏览器访问
http://localhost/info.php,看到 PHP 版本信息页 说明 PHP 正常。
Step 5. 配置网站根目录(可选)
默认网站根目录是
/var/www/html,你可以改成自定义目录,比如/home/你的用户名/www:- 修改 Apache 配置:
sudo nano /etc/apache2/sites-available/000-default.conf - 找到
DocumentRoot /var/www/html,改成:DocumentRoot /home/你的用户名/www - 创建新目录并赋权:
mkdir ~/www sudo chown -R www-data:www-data ~/www - 重启 Apache:
sudo systemctl restart apache2
至此,本地服务器搭建完成! 你可以:
- 在
~/www目录放你的网页文件(比如index.html)。 - 访问
http://localhost预览网站(仅限本地访问,校园网其他设备访问不了)。
进阶篇:无公网 IP,如何让外网访问?
由于校园网屏蔽了入站流量,我们得用 内网穿透,原理:
你的电脑(内网) → 云服务器/穿透服务 → 外网用户
方法 1:使用 Ngrok(简单免费,但有流量限制)
Ngrok 官网:https://ngrok.com/
- 注册 Ngrok 账号,获取免费 Token(不注册限速很低)。
- 安装 Ngrok:
curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc | sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null && echo "deb https://ngrok-agent.s3.amazonaws.com buster main" | sudo tee /etc/apt/sources.list.d/ngrok.list && sudo apt update && sudo apt install ngrok -y - 认证 Token(官网获取你的专属 Token):
ngrok config add-authtoken 你的Token - 启动内网穿透(暴露本地 80 端口):
ngrok http 80 - 终端会输出类似:
Forwarding https://abcd-1234-5678.ngrok.io -> http://localhost:80 - 全世界都能通过
https://abcd-1234-5678.ngrok.io访问你的网站!
缺点:
- 免费版限制流量(国内访问略慢)。
- 每次重启域名会变(需付费固定域名)。
方法 2:使用 FRP(更稳定,自备云服务器)
FRP 需要 一台公网 IP 的云服务器(阿里云/腾讯云学生机 10 元/月)。
架构:
- 云服务器(服务端,公网 IP) 接收外网请求。
- 你的电脑(客户端,内网) 连接云服务器,转发流量。
Step 1. 云服务器配置(服务端)
- 装 FRP 服务端(frps):
wget https://github.com/fatedier/frp/releases/download/v0.51.3/frp_0.51.3_linux_amd64.tar.gz tar -zxvf frp_0.51.3_linux_amd64.tar.gz cd frp_0.51.3_linux_amd64 - 编辑
frps.ini:[common] bind_port = 7000 # 服务端监听端口 vhost_http_port = 80 # HTTP 转发端口 dashboard_port = 7500 # 管理后台端口(可选) dashboard_user = admin # 后台账号 dashboard_pwd = 你的密码 # 后台密码 - 启动 FRP 服务端:
./frps -c frps.ini - 放行端口(云服务器防火墙):
- 打开
7000、80、7500端口(腾讯云/阿里云在安全组配置)。 - 访问
http://云服务器IP:7500看到 FRP 面板。
- 打开
Step 2. 本地电脑配置(客户端)
- 下载 FRP 客户端(和服务端版本一致):
wget https://github.com/fatedier/frp/releases/download/v0.51.3/frp_0.51.3_linux_amd64.tar.gz tar -zxvf frp_0.51.3_linux_amd64.tar.gz cd frp_0.51.3_linux_amd64 - 编辑
frpc.ini:[common] server_addr = 云服务器的公网IP server_port = 7000 [web] type = http local_ip = 127.0.0.1 local_port = 80 custom_domains = 你的域名(可选) # 或直接用 IP - 启动客户端:
./frpc -c frpc.ini - 成功后,访问
http://云服务器IP就等于访问你的本地网站!
额外小技巧:
- 域名绑定:在云服务器上 Nginx 反向代理:
配置sudo apt install nginx -y/etc/nginx/sites-available/default:
重启 Nginx:server { listen 80; server_name 你的域名; location / { proxy_pass http://127.0.0.1:80; # FRP 转发来的流量 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }sudo systemctl restart nginx
番外篇:安全优化
-
修改 SSH 默认端口(防止暴力破解):
sudo nano /etc/ssh/sshd_config改
Port 22为Port 2222,重启:sudo systemctl restart sshd以后这样连 SSH:
ssh 用户名@你的IP -p 2222 -
配置防火墙(UFW):
sudo apt install ufw -y sudo ufw allow 2222/tcp # SSH 新端口 sudo ufw allow 80/tcp # HTTP sudo ufw enable -
MySQL 允许远程访问(用于数据库管理工具):
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '你的密码'; FLUSH PRIVILEGES;改 MySQL 配置:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf注释掉
bind-address = 127.0.0.1,重启 MySQL:sudo systemctl restart mysql
总结:
- 本地搭建 LAMP 环境,网站只能在自己电脑访问。
- 用 Ngrok(简单但流量小) 或 FRP(稳定需云服务器) 做内网穿透。
- 优化 SSH、防火墙、MySQL 远程访问,让服务器更安全。
现在,哪怕你在宿舍,世界的任何地方都能访问你的网站!🎉
常见问题 Q&A:
- Ngrok 速度慢?:升级付费版或换 FRP。
- FRP 连不上?:检查云服务器防火墙是否开放
7000和80端口。 - MySQL 连不上?:确认
bind-address已注释,ufw放行3306端口。
祝你搭建顺利!💻
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.