Laravel/Homestead环境下PhpMyAdmin访问及migrate命令报错求助
Hey there, let’s break down these two issues you’re facing with Homestead—they’re definitely connected, so fixing one will likely knock out the other too!
1. Fixing the phpMyAdmin Subpath 404 Issue
First, let’s clarify why this is happening: Homestead sets up phpMyAdmin as a standalone site by default, mapped to phpmyadmin.test—not as a subdirectory of your Laravel project. That’s why mylaravelproject.test/phpmyadmin throws a 404; there’s no Nginx configuration or Laravel route handling that path.
You’ve got two solid options:
- Option 1 (Easiest, use the default setup): Stick with accessing phpMyAdmin via
http://phpmyadmin.test. This is the intended workflow—just double-check your local hosts file has an entry forphpmyadmin.testpointing to Homestead’s default IP (192.168.10.10). - Option 2 (Add subpath access if you prefer):
- SSH into your Homestead VM:
homestead ssh - Open your project’s Nginx config (usually at
/etc/nginx/sites-available/mylaravelproject.test) - Add this location block inside the
serverblock (adjust the PHP version to match your Homestead setup):location /phpmyadmin { alias /usr/share/phpmyadmin; index index.php index.html index.htm; location ~ ^/phpmyadmin/(.+\.php)$ { alias /usr/share/phpmyadmin/$1; fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; } } - Test the Nginx config for errors:
sudo nginx -t - Reload Nginx to apply changes:
sudo systemctl reload nginx
- SSH into your Homestead VM:
2. Fixing the Migrate Connection Refusal Error
This error almost always comes from a mismatch between your local Laravel .env config and the Homestead VM’s database setup. Here’s how to fix it:
- Step 1: Update your
.envfile
Open your project’s.envand make sure these values match Homestead’s defaults (adjust if you set custom DB credentials inHomestead.yaml):DB_CONNECTION=mysql DB_HOST=192.168.10.10 # Critical: use the VM's IP, NOT localhost DB_PORT=3306 DB_DATABASE=homestead # Or your custom database name DB_USERNAME=homestead DB_PASSWORD=secret - Step 2: Verify the database exists in the VM
SSH into Homestead, log into MySQL, and check if your target database is there:
If it’s missing, create it withmysql -u homestead -psecret SHOW DATABASES;CREATE DATABASE your_database_name; - Step 3: Run migrate correctly
You have two reliable ways to run migrations:- Local machine: With the
.envfixed, just runphp artisan migratelocally—Homestead forwards the VM’s MySQL port to your host by default. - Inside the VM: SSH into Homestead, navigate to your project folder (
cd /home/vagrant/code/mylaravelproject), then runphp artisan migrate. This avoids any network routing hiccups.
- Local machine: With the
Why These Issues Are Linked
Both problems boil down to understanding how Homestead isolates services:
phpmyadmin.testworks because it points directly to the VM, so it connects to the VM’s MySQL instance.- The subpath 404 happens because Nginx doesn’t know to route
mylaravelproject.test/phpmyadminto the phpMyAdmin directory. - The migrate error occurs if your
.envusesDB_HOST=localhost—your local machine tries to connect to its own MySQL (which isn’t running the Homestead database), hence the refusal. Fixing the.envto use the VM’s IP aligns your migrate command with the same database phpMyAdmin uses.
内容的提问来源于stack exchange,提问作者Brad

