温馨提示×

ubuntu phpstorm怎么绑定域名

小樊
48
2026-09-17 03:52:32
栏目: 云计算

在 Ubuntu + PhpStorm 里“绑定域名”通常有两种常见场景,下面分别说明:


一、本地开发:把自定义域名指向本机(最常用)

1️⃣ 修改 hosts 文件(把域名指向 127.0.0.1)

sudo nano /etc/hosts

添加一行(示例):

127.0.0.1   test.local

保存退出(Ctrl+O,Enter,Ctrl+X)

这样浏览器访问 http://test.local 就会访问本机


2️⃣ 配置 Web 服务器(Apache / Nginx)

✅ 如果用 Apache

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/test.local.conf
sudo nano /etc/apache2/sites-available/test.local.conf

内容示例:

<VirtualHost *:80>
    ServerName test.local
    DocumentRoot /home/youruser/projects/test/public

    <Directory /home/youruser/projects/test/public>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

启用站点:

sudo a2ensite test.local.conf
sudo systemctl restart apache2

✅ 如果用 Nginx

sudo nano /etc/nginx/sites-available/test.local

内容示例:

server {
    listen 80;
    server_name test.local;
    root /home/youruser/projects/test/public;

    index index.php index.html;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    }
}

启用:

sudo ln -s /etc/nginx/sites-available/test.local /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

3️⃣ PhpStorm 中绑定项目路径

  • 打开项目
  • Settings → PHP → Server
  • 添加:
    • Name: test.local
    • Host: test.local
    • Port: 80
    • Debugger: Xdebug
  • 勾选 Use path mappings
  • 映射:
    本地路径  →  /home/youruser/projects/test
    

二、让 PhpStorm 的“内置服务器”用域名

1️⃣ 创建 PhpStorm Built-in Server

  • Run → Edit Configurations
  • 添加 PHP Built-in Web Server
  • Host: test.local
  • Port: 8000

2️⃣ hosts 文件仍需配置

127.0.0.1 test.local

访问:

http://test.local:8000

三、常见问题

❌ 浏览器打不开

  • 检查 hosts
  • 检查 Apache/Nginx 是否重启
  • sudo ufw status

❌ Xdebug 不生效

  • PhpStorm 中 PHP → Debug 端口一致
  • php.ini 中:
xdebug.mode=debug
xdebug.client_host=127.0.0.1
xdebug.client_port=9003

如果你愿意,可以告诉我:

  • 你用的是 Apache 还是 Nginx
  • PHP 版本
  • 是否是 Docker / Laravel / ThinkPHP

我可以给你一套完全对应的配置。

0 踩