How to execute "php artisan migrate" and other Laravel commands in remote server?
Asked Answered
S

5

7

I'm using Laravel 4.2 in remote server and I want to execute Laravel commands php artisan migrate but I don't know how.

Saffron answered 14/9, 2014 at 21:5 Comment(2)
Is your remote server Linux? Do a web search for "server SSH tutorial", that should cover it. Check with your hosting provider that SSH access is provided. If not you might have to run these commands via a script, which is not ideal.Porphyritic
(Please note that questions this brief, and not featuring any evidence of prior research, will usually be closed here).Porphyritic
C
10

You can ssh to the server and perform the command, you can add your servers public key to the remote server to do this without a password. I made a bash script with the following code which I can then execute manually via command line or from a program, lets say I call it myscript.sh with the following code

ssh [email protected] << EOF
cd /var/www/app/;
php artisan migrate --force; // force prevents artisan from asking for a yes/no on production
exit;
EOF

now I can write 'sh myscript.sh' and it will run migrations on the remote server.

Cottbus answered 31/12, 2016 at 21:26 Comment(0)
S
1

The preferred way to do this:

  1. ssh into your server (for example, username root & server ip 1.1.1.1: ssh [email protected]).
  2. go to your project folder (for example: cd /var/....)
  3. run the command php artisan migrate.

Original post (not the best way):

You can setup a cronjob like this:

* * * * * /usr/bin/php /var/www/app/artisan schedule:run 

This will run every minute and run the migration.

You can change this to everything you want.

If you want to open a url what while be excecuting the command.

Use this code:

Artisan::call('migrate');

Hope this works!

Stenotypy answered 1/1, 2017 at 1:14 Comment(2)
Why would you use cronjob to run migrate every minute?Nitrous
It's an example, he could also call another command like: php artisan schedule:runStenotypy
M
0

For the completeness of this article, you do this with Windows host using PS remote (enable first) then...

$Username   = "{domain}\{domain account}"
$PasswordSS = ConvertTo-SecureString '{domain account password}' -AsPlainText -Force
$Cred       = New-Object System.management.Automation.PSCredential $Username,$PasswordSS

Invoke-Command -ComputerName {server name} -ScriptBlock { cd d:\wwwroot\{website};php artisan migrate } -Credential $Cred

This will return the result to your local machine.

I use this in VSTS release deployments all the time.

Misconduct answered 4/5, 2018 at 8:49 Comment(0)
M
0

With Mac terminal

Step 1 sh [email protected] e.g 181.6.41.221

Step 2 Enter your password

Step 3 cd /home/admin/web/yourdomain.com/public_html

Step 4 Laravel command: "php artisan migrate"

Without the "quote"

Ask your host for your IP address and Password if not known by you.

Mook answered 16/8, 2022 at 11:19 Comment(0)
M
-5

The solution to this problem could be running once a php code (on the server) like this:

<?php 
// installation.php file
echo exec('php /var/www/laravel-app/artisan migrate:install');

and than you need to visit installation.php in your browser.
After migration you should remove the installation file, so nobody can execute it again

Miscellany answered 14/6, 2016 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.