温馨提示×

ubuntu phpstorm如何绑定域名

小樊
40
2026-10-13 19:46:14
栏目: 云计算

在 Ubuntu 下用 PhpStorm 绑定/调试域名,通常分两种场景:

  1. 本地开发:把域名指向本地项目(hosts + Web 服务器)
  2. PhpStorm 里配置域名用于运行/调试(Run/Debug Configuration)

下面按常见做法一步步说。


一、把自定义域名指向本地(最重要的一步)

1️⃣ 修改 hosts 文件

sudo nano /etc/hosts

添加一行(示例):

127.0.0.1   test.local

保存后测试:

ping test.local

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

✅ Apache 示例

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

内容:

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

    <Directory /home/youruser/projects/demo/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/demo/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 systemctl restart nginx

二、PhpStorm 中绑定域名

方法 1:Deployment(推荐)

  1. Settings → Deployment
  2. 点 + → 选 Local or mounted folder
  3. 配置:
    • Web server root URL
      http://test.local
      
  4. Mapping 设置:
    • Local path:项目目录
    • Web path:/

✅ 这样 PhpStorm 就知道域名对应哪个项目


方法 2:Run / Debug Configuration

  1. 顶部 Run → Edit Configurations
  2. 选 PHP Web Page
  3. 设置:
    • Server:test.local
    • Start URL:/
  4. 浏览器会自动打开:
    http://test.local
    

三、PhpStorm 内置服务器(不推荐生产)

如果你只是想快速跑:

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

访问:

http://test.local:8000

⚠️ 仍需 hosts 指向 127.0.0.1


四、常见问题

❌ 域名访问 403

  • 检查 DocumentRoot 权限
  • Apache:Require all granted
  • Nginx:目录有读权限

❌ PhpStorm 打不开浏览器

  • 检查 Settings → Tools → Web Browsers

如果你用的是 Laravel / Symfony / Docker,或者想 HTTPS + PhpStorm 调试,告诉我,我可以给你更具体的配置。

0 踩