目录
[1. PHP 简介](#1. PHP 简介)
[1.1 前后端交互示例:登录页面](#1.1 前后端交互示例:登录页面)
[2. PHP 版本](#2. PHP 版本)
[3. PHP 运行环境安装](#3. PHP 运行环境安装)
[1. 更新系统](#1. 更新系统)
[2. 安装编译 Nginx 需要的软件包](#2. 安装编译 Nginx 需要的软件包)
[二、源码安装 Nginx](#二、源码安装 Nginx)
[1. 创建源码目录](#1. 创建源码目录)
[2. 下载 Nginx 源码](#2. 下载 Nginx 源码)
[3. 解压源码包](#3. 解压源码包)
[三、创建 Nginx 运行用户](#三、创建 Nginx 运行用户)
[四、配置 Nginx 编译参数](#四、配置 Nginx 编译参数)
[五、编译并安装 Nginx](#五、编译并安装 Nginx)
[六、配置 Nginx 命令软链接](#六、配置 Nginx 命令软链接)
[七、配置 systemd 管理 Nginx](#七、配置 systemd 管理 Nginx)
[八、安装 MySQL](#八、安装 MySQL)
[1. 安装 MySQL](#1. 安装 MySQL)
[2. 启动 MySQL](#2. 启动 MySQL)
[3. 进入 MySQL](#3. 进入 MySQL)
[4. 设置 root 密码](#4. 设置 root 密码)
[九、安装 PHP-FPM](#九、安装 PHP-FPM)
[1. 安装 PHP 和常用扩展](#1. 安装 PHP 和常用扩展)
[2. 查看 PHP 版本](#2. 查看 PHP 版本)
[3. 查看 PHP-FPM 服务](#3. 查看 PHP-FPM 服务)
[十、配置 PHP-FPM 监听 9000 端口](#十、配置 PHP-FPM 监听 9000 端口)
[1. 编辑 PHP-FPM 配置文件](#1. 编辑 PHP-FPM 配置文件)
[2. 找到 listen 配置](#2. 找到 listen 配置)
[3. 重启 PHP-FPM](#3. 重启 PHP-FPM)
[4. 检查 9000 端口](#4. 检查 9000 端口)
[十二、配置 Nginx 连接 PHP-FPM 9000 端口](#十二、配置 Nginx 连接 PHP-FPM 9000 端口)
[十三、检查并重启 Nginx](#十三、检查并重启 Nginx)
[十五、测试 MySQL 和 PHP 是否正常连接](#十五、测试 MySQL 和 PHP 是否正常连接)
[1. 创建数据库和用户](#1. 创建数据库和用户)
[2. 创建 PHP 数据库测试文件](#2. 创建 PHP 数据库测试文件)
[3. 浏览器访问](#3. 浏览器访问)
[1. Nginx](#1. Nginx)
[2. PHP-FPM](#2. PHP-FPM)
[3. MySQL](#3. MySQL)
[1. 访问 PHP 页面出现 502 Bad Gateway](#1. 访问 PHP 页面出现 502 Bad Gateway)
[2. 访问 PHP 文件变成下载](#2. 访问 PHP 文件变成下载)
[3. 页面显示 File not found](#3. 页面显示 File not found)
[4. 9000 端口没有监听](#4. 9000 端口没有监听)
[5. 80 端口被占用](#5. 80 端口被占用)
1. PHP 简介
PHP 的全称是:PHP: Hypertext Preprocessor,中文通常翻译为"超文本预处理器"。
PHP 是常见的后端编程语言之一,主要作用是:
-
接收前端页面提交的数据;
-
在服务器端进行业务逻辑处理;
-
操作文件、数据库、会话等资源;
-
根据处理结果生成动态 HTML 页面并返回给浏览器。
前端常见语言主要是:
-
HTML:负责页面结构;
-
CSS:负责页面样式;
-
JavaScript:负责页面交互。
后端语言有很多,例如 PHP、Java、Python、Go、Node.js 等。
PHP 曾经大量用于网站开发,很多网站系统、论坛、博客、后台管理系统都可以使用 PHP 开发。
1.1 前后端交互示例:登录页面
前端页面负责收集用户名和密码,后端 PHP 文件负责接收数据并进行判断。
前端页面: login.html
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>示例小站</title>
</head>
<body>
<form action="checklogin.php" method="get">
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="登录">
</form>
</body>
</html>
后端页面: checklogin.php
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>论坛</title>
</head>
<body>
<?php
$uname = $_GET['username'];
$pwd = $_GET['password'];
if ($uname == 'demo_user') {
echo "<h1>欢迎 $uname 回来</h1>";
echo "<h2>您的密码为: $pwd</h2>";
} else {
echo "<h1>sorry,用户名或者密码不正确!</h1>";
}
?>
</body>
</html>
这个例子体现了基本流程:
-
用户在浏览器中访问
login.html; -
用户输入用户名、密码并提交表单;
-
浏览器把表单数据发送给
checklogin.php; -
PHP 通过
$_GET接收参数; -
PHP 判断用户名是否正确;
-
PHP 生成对应的 HTML 内容返回给浏览器。
注意:上面的代码只是用于理解前后端交互。真实项目中不能直接把密码输出到页面,也不能使用这种简单的明文判断方式做登录认证。
php特点:
| PHP 特点 | 简单解释 | 示例/说明 |
|---|---|---|
| 弱类型语言 | 变量不需要提前声明类型,PHP 会自动判断和转换类型 | $a = 10; 后面也可以写 $a = "hello"; |
| 解释型语言 | PHP 代码不需要提前编译,由 PHP 解释器执行 | 修改 .php 文件后,一般刷新页面即可看到效果 |
| 服务端脚本语言 | PHP 主要运行在服务器端,浏览器看到的是执行后的结果 | PHP 生成 HTML,再返回给浏览器 |
| 动态语言 | 变量类型、代码执行结果可以在运行过程中变化 | $x = 1; $x = "admin"; |
| 可以嵌入 HTML | PHP 代码可以直接写在 HTML 页面中 | <h1><?php echo "hello"; ?></h1> |
变量以 $ 开头 |
PHP 中定义和使用变量时,变量名前必须加 $ |
$name = "admin"; |
| 数组非常灵活 | PHP 数组既可以当普通数组,也可以当键值对数组使用 | $arr = ["admin", "test"]; / $user["name"] |
2. PHP 版本
PHP 主要版本历史:
| 版本 | 时间 | 说明 |
|---|---|---|
| PHP 1.0 | 1995 | 早期版本 |
| PHP 2.0 | 1995 | 早期版本 |
| PHP 3.0 | 1998 左右 | PHP 逐步发展成熟 |
| PHP 4.0 | 2000 | 早期大量使用版本 |
| PHP 5.0 | 2004 | 经典版本,常见分支 5.0 - 5.6 |
| PHP 7.0 | 2015 | 性能提升明显,常见分支 7.0 - 7.4 |
| PHP 8.0 | 2020 | 新语法、新特性更多 |
学习阶段建议:
新学习可以直接使用 PHP 7.x 或 PHP 8.x;
旧项目维护中可能遇到 PHP 5.x;
注意不同 PHP 版本之间可能存在语法、函数、扩展兼容性差异。
3. PHP 运行环境安装
PHP 代码不能像 HTML 文件一样直接在浏览器中运行,通常需要 Web 服务器和 PHP 解释器配合。
PHP 运行环境请求处理流程
浏览器 发送 HTTP 请求 → Web 服务器 Nginx / Apache → PHP 解释器 PHP-FPM → 数据库 MySQL
PHP 执行完成后生成 HTML,由 Web 服务器返回给浏览器。
常见运行环境:
-
Windows:可以使用集成环境(已经把多个软件组件提前整合好的开发/运行环境),例如 phpStudy等;
-
Linux:可以搭建 Apache/Nginx + PHP-FPM + MySQL(自己手动搭建起所需的服务构成的环境);、
基于ubuntu搭建lnmp的具体操作步骤:
L:Linux,也就是 Ubuntu N:Nginx,Web 服务器 M:MySQL,数据库 P:PHP,后端脚本语言
可以,下面给你一版 Ubuntu 源码安装 Nginx,并配置 LNMP 的步骤。
这里默认:
-
系统:Ubuntu 22.04 / 24.04
-
Nginx:源码编译安装
-
PHP:使用 apt 安装 PHP-FPM
-
MySQL:使用 apt 安装
-
Nginx 安装目录:
/usr/local/nginx -
网站目录:
/var/www/test
Nginx 官方说明中,源码安装时通过 ./configure 指定安装路径、配置文件路径、日志路径、运行用户等参数,最后使用 make 编译安装。(Nginx)
下面给你一版 完整 LNMP 搭建步骤,场景是:
-
Ubuntu 系统
镜像下载的链接是:Download Ubuntu Desktop | Ubuntu
-
Nginx:源码安装
-
MySQL:apt 安装
-
PHP-FPM:apt 安装
-
Nginx 和 PHP-FPM 通过
127.0.0.1:9000端口关联
也就是最终结构:
浏览器
↓
Nginx:80端口
↓
PHP-FPM:127.0.0.1:9000
↓
MySQL
Nginx 源码编译安装主要通过 ./configure 指定安装目录、配置文件路径、模块等参数,然后 make && make install 编译安装;
Nginx 官方文档也说明了这些源码构建参数的作用。(Nginx)
一、准备系统环境
1. 更新系统
bash
sudo apt update
sudo apt upgrade -y
2. 安装编译 Nginx 需要的软件包
php
sudo apt install -y gcc make build-essential \
libpcre3 libpcre3-dev \
zlib1g zlib1g-dev \
openssl libssl-dev \
wget curl vim net-tools lsof
这些依赖大致作用:
| 软件包 | 作用 |
|---|---|
gcc |
编译 C 语言源码 |
make |
执行编译流程 |
libpcre3-dev |
支持 Nginx 正则匹配 |
zlib1g-dev |
支持 gzip 压缩 |
libssl-dev |
支持 HTTPS |
wget |
下载源码包 |
二、源码安装 Nginx
1. 创建源码目录
php
cd /usr/local/src
2. 下载 Nginx 源码
Nginx 官方下载页会区分 mainline version 和 stable version ,源码包统一放在官方 download 目录中。(Nginx)
这里以稳定版为例:
bash
sudo wget https://nginx.org/download/nginx-1.30.2.tar.gz
3. 解压源码包
bash
sudo tar -zxvf nginx-1.30.2.tar.gz
cd nginx-1.30.2
三、创建 Nginx 运行用户
Ubuntu 一般默认有 www-data 用户。
查看:
bash
id www-data
如果没有,就创建:
bash
sudo useradd -r -s /sbin/nologin www-data
四、配置 Nginx 编译参数
执行:
bash
sudo ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/usr/local/nginx/logs/nginx.lock \
--user=www-data \
--group=www-data \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_gzip_static_module
参数说明:
| 参数 | 作用 |
|---|---|
--prefix=/usr/local/nginx |
指定 Nginx 安装目录 |
--sbin-path |
指定 Nginx 主程序路径 |
--conf-path |
指定 Nginx 主配置文件路径 |
--pid-path |
指定 Nginx PID 文件 |
--user=www-data |
指定 worker 进程运行用户 |
--group=www-data |
指定 worker 进程运行组 |
--with-http_ssl_module |
支持 HTTPS |
--with-http_v2_module |
支持 HTTP/2 |
--with-http_stub_status_module |
支持状态查看 |
--with-http_realip_module |
获取真实客户端 IP |
--with-http_gzip_static_module |
支持 gzip 静态压缩 |
五、编译并安装 Nginx
sudo make
sudo make install
查看安装目录:
ls /usr/local/nginx
正常会看到:
conf html logs sbin
查看 Nginx 版本:
/usr/local/nginx/sbin/nginx -v
查看编译参数:
/usr/local/nginx/sbin/nginx -V
六、配置 Nginx 命令软链接
为了后面可以直接使用 nginx 命令:
sudo ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
测试:
nginx -v
七、配置 systemd 管理 Nginx
源码安装的 Nginx 默认不能直接用 systemctl 管理,所以需要手动创建服务文件。
sudo vim /etc/systemd/system/nginx.service
写入:
bash
[Unit]
Description=The NGINX HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重新加载 systemd:
sudo systemctl daemon-reload
启动 Nginx:
sudo systemctl start nginx
设置开机自启:
sudo systemctl enable nginx
查看状态:
sudo systemctl status nginx
八、安装 MySQL
1. 安装 MySQL
sudo apt install -y mysql-server
2. 启动 MySQL
sudo systemctl start mysql
sudo systemctl enable mysql
查看状态:
sudo systemctl status mysql
3. 进入 MySQL
sudo mysql
4. 设置 root 密码
在 MySQL 中执行:
sql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Root@123456';
FLUSH PRIVILEGES;
EXIT;
以后登录:
mysql -uroot -p
输入密码:
Root@123456
九、安装 PHP-FPM
PHP-FPM 是 PHP 官方提供的 FastCGI 进程管理器 ,常用于和 Nginx 配合处理 PHP 请求。(PHP)
1. 安装 PHP 和常用扩展
sudo apt install -y php php-fpm php-mysql php-cli php-curl php-mbstring php-xml php-zip php-gd
2. 查看 PHP 版本
php -v
3. 查看 PHP-FPM 服务
systemctl list-units | grep php
你可能会看到类似:
php8.1-fpm.service
php8.3-fpm.service
假设你的版本是 PHP 8.3,后面就用
php8.3-fpm。如果你的版本是 PHP 8.1,就把后面的8.3改成8.1。
十、配置 PHP-FPM 监听 9000 端口
默认情况下,Ubuntu 的 PHP-FPM 通常监听的是 .sock 文件,比如:
/run/php/php8.3-fpm.sock
现在我们要改成监听:
127.0.0.1:9000
1. 编辑 PHP-FPM 配置文件
以 PHP 8.3 为例:
sudo vim /etc/php/8.3/fpm/pool.d/www.conf
如果你是 PHP 8.1:
sudo vim /etc/php/8.1/fpm/pool.d/www.conf
2. 找到 listen 配置
找到:
listen = /run/php/php8.3-fpm.sock
改成:
listen = 127.0.0.1:9000
建议再检查下面这项,如果有,就保持或添加:
listen.allowed_clients = 127.0.0.1这样表示只允许本机 Nginx 连接 PHP-FPM。
不要写成:
listen = 0.0.0.0:9000因为 9000 端口只需要给本机 Nginx 使用,不应该暴露到外网。
3. 重启 PHP-FPM
sudo systemctl restart php8.3-fpm
如果是 PHP 8.1:
sudo systemctl restart php8.1-fpm
4. 检查 9000 端口
ss -lntp | grep 9000
正常结果类似:
LISTEN 0 4096 127.0.0.1:9000
说明 PHP-FPM 已经监听了本机 9000 端口。
十一、创建网站目录
sudo mkdir -p /var/www/test
创建 PHP 测试页面:
sudo vim /var/www/test/index.php
写入:
php
<?php
echo "<h1>LNMP 搭建成功</h1>";
echo "<p>Nginx 源码安装</p>";
echo "<p>PHP-FPM 通过 9000 端口连接</p>";
echo "<p>当前 PHP 版本:" . PHP_VERSION . "</p>";
?>
修改权限:
sudo chown -R www-data:www-data /var/www/test
sudo chmod -R 755 /var/www/test
十二、配置 Nginx 连接 PHP-FPM 9000 端口
源码安装 Nginx 的主配置文件是:
/usr/local/nginx/conf/nginx.conf
先备份:
sudo cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
编辑配置文件:
sudo vim /usr/local/nginx/conf/nginx.conf
改成下面内容:
php
user www-data;
worker_processes auto;
error_log /usr/local/nginx/logs/error.log;
pid /usr/local/nginx/logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /usr/local/nginx/logs/access.log;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root /var/www/test;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
重点就是这一段:
php
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
解释:
| 配置 | 作用 |
|---|---|
location ~ \.php$ |
匹配所有 .php 文件 |
fastcgi_pass 127.0.0.1:9000 |
把 PHP 请求交给 PHP-FPM 的 9000 端口 |
fastcgi_index index.php |
默认 PHP 首页 |
SCRIPT_FILENAME |
告诉 PHP-FPM 要执行哪个 PHP 文件 |
include fastcgi_params |
引入 FastCGI 参数 |
Nginx 的 fastcgi_pass 可以把请求传递给 FastCGI 服务,例如 127.0.0.1:9000 这种形式。(Nginx)
十三、检查并重启 Nginx
检查配置:
sudo nginx -t
如果看到:
syntax is ok
test is successful
说明配置没问题。
重启 Nginx:
sudo systemctl restart nginx
或者重载:
sudo systemctl reload nginx
十四、访问测试
查看服务器 IP:
ip a
或者:
hostname -I
浏览器访问:
http://服务器IP
如果页面显示:
LNMP 搭建成功
Nginx 源码安装
PHP-FPM 通过 9000 端口连接
当前 PHP 版本:xxx
说明 LNMP 搭建成功。
十五、测试 MySQL 和 PHP 是否正常连接
1. 创建数据库和用户
登录 MySQL:
mysql -uroot -p
执行:
sql
CREATE DATABASE testdb DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'Test@123456';
GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
| 命令 | 作用 | 简单说明 |
|---|---|---|
CREATE DATABASE testdb DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; |
创建数据库 | 创建 testdb 数据库,并设置字符集为 utf8mb4 |
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'Test@123456'; |
创建用户 | 创建用户 testuser,只能本机登录,密码是 Test@123456 |
GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost'; |
授权 | 把 testdb 数据库的所有权限授予 testuser |
FLUSH PRIVILEGES; |
刷新权限 | 让刚才的用户和权限配置生效 |
EXIT; |
退出 MySQL | 退出 MySQL 命令行 |
2. 创建 PHP 数据库测试文件
sudo vim /var/www/test/dbtest.php
写入:
php
<?php
$host = "localhost";
$user = "testuser";
$password = "Test@123456";
$dbname = "testdb";
$conn = new mysqli($host, $user, $password, $dbname);
if ($conn->connect_error) {
die("数据库连接失败:" . $conn->connect_error);
}
echo "数据库连接成功!";
?>
3. 浏览器访问
http://服务器IP/dbtest.php
如果看到:
数据库连接成功!
说明:
Nginx + PHP-FPM + MySQL
已经全部打通。
十六、完整服务管理命令
1. Nginx
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx
sudo systemctl status nginx
检查 Nginx 配置:
sudo nginx -t
查看 Nginx 版本:
nginx -v
查看 Nginx 编译参数:
nginx -V
2. PHP-FPM
以 PHP 8.3 为例:
sudo systemctl start php8.3-fpm
sudo systemctl stop php8.3-fpm
sudo systemctl restart php8.3-fpm
sudo systemctl status php8.3-fpm
查看 9000 端口:
ss -lntp | grep 9000
3. MySQL
sudo systemctl start mysql
sudo systemctl stop mysql
sudo systemctl restart mysql
sudo systemctl status mysql
登录:
mysql -uroot -p
十七、关键配置文件位置
| 文件/目录 | 作用 |
|---|---|
/usr/local/nginx/ |
源码安装 Nginx 的主目录 |
/usr/local/nginx/sbin/nginx |
Nginx 主程序 |
/usr/local/nginx/conf/nginx.conf |
Nginx 主配置文件 |
/usr/local/nginx/conf/fastcgi_params |
FastCGI 参数文件 |
/usr/local/nginx/logs/access.log |
Nginx 访问日志 |
/usr/local/nginx/logs/error.log |
Nginx 错误日志 |
/var/www/test/ |
测试网站目录 |
/etc/php/8.3/fpm/pool.d/www.conf |
PHP-FPM 监听配置 |
/etc/php/8.3/fpm/php.ini |
PHP-FPM 的 PHP 配置 |
/var/log/mysql/ |
MySQL 日志目录 |
十八、常见报错处理
1. 访问 PHP 页面出现 502 Bad Gateway
原因通常是 PHP-FPM 没有启动,或者 9000 端口没有监听。
检查:
sudo systemctl status php8.3-fpm
ss -lntp | grep 9000
解决:
sudo systemctl restart php8.3-fpm
sudo systemctl restart nginx
2. 访问 PHP 文件变成下载
说明 Nginx 没有正确把 PHP 请求交给 PHP-FPM。
检查 Nginx 配置中是否有:
php
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
3. 页面显示 File not found
通常是 SCRIPT_FILENAME 配置错误。
确保这一行是:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
并且 root 指向正确目录:
root /var/www/test;
4. 9000 端口没有监听
检查 PHP-FPM 配置:
sudo vim /etc/php/8.3/fpm/pool.d/www.conf
确认是:
listen = 127.0.0.1:9000
然后重启:
sudo systemctl restart php8.3-fpm
5. 80 端口被占用
查看谁占用了 80 端口:
sudo lsof -i:80
或者:
ss -lntp | grep :80
如果之前用 apt 安装过 Nginx 或 Apache,可能需要停止:
sudo systemctl stop apache2
sudo systemctl disable apache2
十九、最终总结
完整流程就是:
-
安装编译环境
-
下载 Nginx 源码
-
编译安装 Nginx 到
/usr/local/nginx -
配置 systemd 管理 Nginx
-
安装 MySQL
-
安装 PHP-FPM
-
修改 PHP-FPM:
listen = 127.0.0.1:9000 -
配置 Nginx:
fastcgi_pass 127.0.0.1:9000 -
创建 PHP 测试页面
-
浏览器访问测试
-
创建 PHP 连接 MySQL 测试页面
核心记忆:
PHP-FPM 负责执行 PHP;
Nginx 不直接执行 PHP;
Nginx 通过
fastcgi_pass 127.0.0.1:9000把 PHP 请求交给 PHP-FPM。
- Docker:可以使用 PHP 官方镜像或 LNMP/LAMP 环境镜像。
Docker 可以理解为:把程序运行需要的环境,打包成一个"小盒子"。
然后不管你把这个"小盒子"放到 Windows、Linux,还是服务器上,只要这个机器安装了 Docker,就可以把环境快速跑起来。
学习阶段常见做法:
-
安装集成环境;
-
启动 Web 服务和 PHP 服务;
-
将 PHP 文件放到网站根目录;
-
通过浏览器访问,例如
http://127.0.0.1/index.php。

课堂或实验环境中,如遇到端口占用、防火墙拦截、杀毒软件拦截等问题,需要根据实际情况排查。