Using “php artisan serve” with xdebug and PHPStorm
This is more of a person note for myself. This posts assumes some knowledge of php, Laravel, artisan, Homebrew, and xdebug.
I’ve been using php artisan serve to serve Laravel applications locally. It’s quick and it works well.
It’s probably not a great solution if you’re working with other people on a project, or if you want to implement a good CD/CI workflow. Having said that, it’s what I’m using today, so I figured I’d document how I got xdebug working with it (on my Mac).
Step 1: Install PHP with xdebug
Actually, I’ll just post my initial “prepare for Laravel development” setup steps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# Install PHP 8 brew install php brew services start php # close and re-open iTerm php -v # Install and switch to PHP 7.4 brew install php@7.4 brew link --force --overwrite php@7.4 brew services start php@7.4 # Install xdebug 3 pecl install xdebug # this should have automatically enabled xdebug in your php ini like: # zend_extension="xdebug.so" brew install mysql@5.7 brew services start mysql@5.7 # close and re-open iTerm brew link --force mysql@5.7 mysql -u root >>> CREATE DATABASE dbname; |
Step 2: Configure xdebug
Find which php.ini file Laravel is using by dropping a phpinfo(); at the top of one of your routes. There may be an easier way, but this is pretty straightforward and won’t lead you to the wrong ini file.
Add these lines to the ini file:
1 2 3 4 5 |
zend_extension=xdebug.so xdebug.mode=debug xdebug.client_host=127.0.0.1 xdebug.client_port=9003 xdebug.start_with_request=yes |
Restart php to make sure the xdebug settings are applied. You can use php artisan serve for this.
Step 3: Start the Server
You can do this several few ways.
If you want your system (or is it Laravel’s artisan?) to pick the port automatically, use php artisan serve .
If you want to change the port, you can either set an env variable: export SERVER_PORT="8080" php artisan serve or you can set SERVER_PORT=8100 in your .env file and use php artisan serve normally.
If you prefer the start the server through PHPStorm, use Run > Edit Configurations… to add a new “PHP Built-in Web Server”:
Step 4: Configure xdebug in PHPStorm:
Open Preferences > PHP > Debug and configure as shown below. I’ve highlighted the most important settings.
Step 5: Test!
Start the server (e.g., php artisan serve )
Click the Run > Start Listening for PHP Debug Connections.
You will need to start and stop when you want to listen or stop listening.
The server will respond faster if you stop listening when you’re not needing to use xdebug.
Next, visit the site in your browser.
You should see a popup like this in PHPStorm, almost immediately:
After you accept you should be able to drop a breakpoint in your code and PHPStorm should catch it.
You may want to rename the auto-configured server in Preferences > PHP > Servers. For example, I changed mine from 127.0.0.1 to 127.0.0.1 mysite .