Today I will tell you how I deploy a new project for Laravel development on Vagrant (Laravel Homestead). I will not tell you what I install and why. This post is just a simple tutorial how to run a new Laravel application in 10 minutes.
If you find something that can be improved, feel free to write about it in the comments or select and click Ctrl+Enter.
This tutorial is not about installing these things, I hope you can install it by yourself (if you don't have it already):
Step 1. Install Laravel
- Open your console, go to your project root folder (using cd command). Terminal in PhpStorm doing this automatically when you launch it.
- Run composer create-project --prefer-dist laravel/laravel . to download the Laravel (do not remove this dot at the end).
Step 2. Install HomeStead
- After the Laravel download is complete, run
composer require laravel/homestead --devto download the Homestead.
- Now we need to install Homestead by running a command.
For Windows: vendor\\bin\\homestead make
php vendor/bin/homestead make
- (Optional) If you want to developing on your custom local domain, open Homestead.yaml (which in your project root folder), find sites section and change map parameter from homestead.test to your domain (for example: test.loc).
If you did that, you need to add new entry 192.168.10.10 test.loc in your /etc/hosts file (in Windows: C:\Windows\System32\drivers\etc). Replace test.loc with your domain.
- Open .env file in your project root folder and change database connection settings:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# For MySQL DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret # For PostgreSQL DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret
- Start the VM by running vagrant up.
Step 3. Configuring PHPStorm
- Go to Settings > Languages & Frameworks > PHP and change PHP Language Level to your current PHP version (you can find this out by logging in to your Vagrant VM via SSH: vagrant ssh > php -v > exit).
After that, at the same page in PHPStorm, find the CLI Interpreter selector and then click on [...] > [+] (add new) > From Docker, Vagrant… > Vagrant > OK.
- Go to Settings > Command Line Tool Support > Click on the [+] (add new).
In Chose Tool param select "Tool Based On Symphone Console" > OK > In alias type artisan, in Path To Script find artisan.php file in your project root folder.
- Mark some folders in PHPStorm by clicking on the right button at the folder > Mark Directory as.
public folder - Resource root
tests folder - Test Sources Root
vendor & storage folders - Excluded
- (Optional) Change your code style to Laravel (Settings > Editor > Code Style > PHP > Set from (blue link on the right) > Laravel).
Step 4. Install Laravel Ide Helper
- Run composer require --dev barryvdh/laravel-ide-helper
- Open /config/app.php and add Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class inside Package Service Providers section.
- Open your composer.json file and put this code inside "scripts" section:
1 2 3 4 5 6 7 8 9 10 11 12 13
"post-update-cmd": [ "Illuminate\\Foundation\\ComposerScripts::postUpdate", "@ide-helper" ], "ide": [ "@ide-helper" ], "ide-helper": [ "@php artisan clear-compiled", "@php artisan ide-helper:generate", "@php artisan ide-helper:meta", "@php artisan ide-helper:models -N" ]
- Run composer ide-helper
Step 5. Configuring XDebug
- Go to Settings > Languages & Frameworks > PHP > Servers > Click on
- In Name and Host parameter insert your domain.
- Check the Use path mappings parameter.
- Set Absolute Path value:
/home/vagrant/code/ for project root folder
/home/vagrant/code/public/ for /public folder
Step 6. Some other things
- Open .gitignore file in your project root folder and put it in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
# Laravel gitignore /node_modules /public/hot /public/storage /storage/*.key /vendor /.idea /.vagrant Homestead.json Homestead.yaml npm-debug.log .env # Larave IDE Helper _ide_helper.php _ide_helper_models.php .phpstorm.meta.php # misc .DS_Store reports/* !reports/.gitkeep npm-debug.log yarn-error.log .phpunit.result.cache /user-customizations.sh /postgres_backup /mysqldump.sql.gz /mysql_backup /aliases /after.sh /Homestead.yaml /Homestead.json/.idea /vendor /.vagrant phpunit.xml .buildpath .settings .project
- Run php artisan storage:link to make a symbolic link from /public/storage/ to /storage/app/public/.
(Optional) Also I usually...
- Install Laravel Debugbar by running composer require barryvdh/laravel-debugbar --dev.
- Generate auth files by running composer require laravel/ui and then php artisan ui <type> --auth. Replace <type> with type that you need (bootstrap, vue, react).
- Install Laravel Telescope by running composer require laravel/telescope and then php artisan telescope:install and then php artisan migrate.
VM control commands
Homestead is actually Vagrant, so we can use Vagrant commands to control it:
- vagrant up to start the VM
- vagrant halt to terminate the VM
- vagrant ssh to connect to the VM via SSH (exit to exit from SSH mode)
- vagrant destroy -f <name/ID of VM> to remove the VM
- vagrant global-status to show all existing VM's
Did you like what you just read?
Give me a "high five" by clicking once, or applaud with quick taps to show how much you liked the material.