You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacBook M1配置带端口的Apache虚拟主机及Laravel Passport问题

如何让Apache虚拟主机默认绑定8000端口,无需手动输入端口号?

我在PHP Laravel项目中使用Passport,通过Postman发送请求时出现崩溃问题。为解决该问题创建了虚拟主机,目前虚拟主机可正常工作,但必须在地址后手动添加端口号才能访问——比如项目运行地址为127.0.0.1:8000/login,创建的虚拟主机地址是macbookm1.test,必须输入macbookm1.test:8000/login才能访问登录页面。想请教如何让macbookm1.test地址默认指向8000端口?

修改的配置文件

/etc/apache2/httpd.conf

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

/private/etc/apache2/extra/httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/private/var/log/apache2/dummy-host.macbookm1.test"
    ServerName macbookm1.test:80
    ServerAlias www.macbookm1.test
    ErrorLog "/private/var/log/apache2/dummy-host.macbookm1.test-error_log"
    CustomLog "/private/var/log/apache2/dummy-host.macbookm1.test-access_log" common
</VirtualHost>

<VirtualHost *:8000>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/private/var/log/apache2/dummy-host.macbookm1.test"
    ServerName macbookm1.test:8000
    ServerAlias www.macbookm1.test
    ErrorLog "/private/var/log/apache2/dummy-host.macbookm1.test-error_log"

    CustomLog "/private/var/log/apache2/dummy-host.macbookm1.test-access_log" common
</VirtualHost>

<VirtualHost *:8001>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/private/var/log/apache2/dummy-host.macbookm1.test"
    ServerName macbookm1.test:8001
    ServerAlias www.macbookm1.test
    ErrorLog "/private/var/log/apache2/dummy-host.macbookm1.test-error_log"
    CustomLog "/private/var/log/apache2/dummy-host.macbookm1.test-access_log" common
</VirtualHost>

Apache重启错误信息

AH00557: httpd: apr_sockaddr_info_get() failed for macbook-Air 
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

解决方案

1. 修复全局ServerName,消除重启警告

打开/etc/apache2/httpd.conf,添加或修改全局ServerName配置:

ServerName localhost

保存后重启Apache,即可消除AH00557和AH00558警告。

2. 配置反向代理,将80端口请求转发到8000端口

浏览器默认访问80端口(HTTP),要让macbookm1.test直接指向Laravel的8000端口,需将80端口的虚拟主机配置为反向代理:

修改/private/etc/apache2/extra/httpd-vhosts.conf中的80端口虚拟主机:

<VirtualHost *:80>
    ServerAdmin webmaster@macbookm1.test
    ServerName macbookm1.test
    ServerAlias www.macbookm1.test

    # 反向代理转发到Laravel的8000端口
    ProxyPass / http://127.0.0.1:8000/
    ProxyPassReverse / http://127.0.0.1:8000/

    ErrorLog "/private/var/log/apache2/macbookm1.test-error_log"
    CustomLog "/private/var/log/apache2/macbookm1.test-access_log" common
</VirtualHost>

3. 启用Apache反向代理模块

打开/etc/apache2/httpd.conf,取消以下两行的注释(如果未启用):

LoadModule proxy_module libexec/apache2/mod_proxy.so
LoadModule proxy_http_module libexec/apache2/mod_proxy_http.so

4. 清理冗余配置

删除*:8000和*:8001的虚拟主机配置块(除非有其他服务需要),避免配置混乱。

5. 确认hosts文件配置

确保/etc/hosts中存在如下记录:

127.0.0.1 macbookm1.test

6. 重启服务

# 重启Apache
sudo apachectl restart
# 重启Laravel开发服务器(如果使用php artisan serve启动)
php artisan serve --port=8000

完成以上步骤后,访问http://macbookm1.test/login即可自动转发到8000端口的Laravel服务,无需手动输入端口号。

内容的提问来源于stack exchange,提问作者hllibrhm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 17:10:28