温馨提示×

PHPStorm Ubuntu版如何配置Web服务器

小樊
41
2025-12-28 15:13:03
栏目: 云计算

在 Ubuntu 上配置 PHPStorm 的 Web 服务器

一 内置 Web 服务器快速运行

  • 打开项目:File → Open,选择你的项目目录。
  • 新建运行配置:Run → Edit Configurations → 点击左上角 + → 选择 PHP Built-in Web Server
  • 关键参数:
    • Name:自定义名称。
    • Host:如 localhost
    • Port:如 8080(确保未被占用)。
    • Document root:项目根目录的绝对路径,例如 /home/username/myproject
  • 应用并运行:Apply → OK → 点击绿色运行按钮;浏览器访问 http://localhost:8080
  • 停止:工具栏红色方块,或终端 Ctrl+C
  • 防火墙放行(如启用 UFW):执行命令:sudo ufw allow 8080

二 使用 Apache 或 Nginx 作为外部服务器

  • 安装与启动服务(二选一或两者皆装):
    • Apache:sudo apt install apache2sudo systemctl start apache2sudo systemctl enable apache2
    • Nginx:sudo apt install nginxsudo systemctl start nginxsudo systemctl enable nginx
  • 部署代码:将项目放到 Web 根目录(常见为 /var/www/html),或配置虚拟主机指向你的项目目录,并设置合适权限。
  • 在 PHPStorm 中创建运行配置:Run → Edit Configurations → + → PHP Web Page,配置 Start URL(如 http://localhost/ 或项目入口),保存后运行/调试。

三 调试配置与 Xdebug 3 步骤

  • 安装调试器:执行 sudo apt install php-xdebug
  • 配置 php.ini(Xdebug 3 推荐):
    • 确认扩展已启用:zend_extension=xdebug.so
    • 启用调试模式与监听:xdebug.mode=debug
    • 指定本机调试客户端:xdebug.client_host=127.0.0.1
    • 指定调试端口:xdebug.client_port=9003
  • 重启 Web 服务或 PHP-FPM:
    • Apache:sudo systemctl restart apache2
    • Nginx + PHP-FPM:sudo systemctl restart php{version}-fpmsudo systemctl restart nginx
  • PHPStorm 设置:
    • 解释器:File → Settings → Languages & Frameworks → PHP → 指定 PHP Executable(如 /usr/bin/php)。
    • Servers:Languages & Frameworks → PHP → Servers → +,填写 Host(如 localhost)、端口(如 80/443),调试器选 Xdebug,端口 9003
    • 运行/调试:Run → Edit Configurations → + → PHP Web Page,选择服务器与 Start URL,点击调试(绿色蜘蛛)并在代码行设置断点。

四 常见问题与排查

  • 端口被占用:更换 Run Configuration 的 Port(如 8081),或释放占用端口;必要时执行 sudo ufw allow <端口> 放行。
  • 502 或 “php-cgi not found”:安装 php-cgi 或相应模块(如 libapache2-mod-php),并确保 PHP 以 CGI/FPM 方式可用。
  • 页面空白或 No input file specified:核对 Document root 是否指向包含入口文件(如 public/index.php)的目录;检查 Web 服务器配置中的根目录与重写规则。
  • Xdebug 无法连接:确认 xdebug.mode=debugxdebug.client_host=127.0.0.1xdebug.client_port=9003 与 PHPStorm 的 Debug 端口一致;确保防火墙未阻断 9003

0