如何在远程服务器中执行“php artisan migrate”和其他 Laravel 命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25838018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute "php artisan migrate" and other Laravel commands in remote server?
提问by Youssef Had
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.
我在远程服务器中使用 Laravel 4.2,我想执行 Laravel 命令,php artisan migrate
但我不知道如何执行。
回答by Matkey
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 连接到服务器并执行命令,您可以将服务器公钥添加到远程服务器,无需密码即可执行此操作。我使用以下代码制作了一个 bash 脚本,然后我可以通过命令行或程序手动执行该脚本,假设我使用以下代码将其称为 myscript.sh
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.
现在我可以编写'sh myscript.sh',它将在远程服务器上运行迁移。
回答by Robin Dirksen
The preferred way to do this:
执行此操作的首选方法:
- ssh into your server (for example, username root & server ip 1.1.1.1:
ssh [email protected]
). - go to your project folder (for example:
cd /var/....
) - run the command
php artisan migrate
.
- ssh 进入您的服务器(例如,用户名 root 和服务器 IP 1.1.1.1:)
ssh [email protected]
。 - 去你的项目文件夹(例如:
cd /var/....
) - 运行命令
php artisan migrate
。
Original post (not the best way):
原帖(不是最好的方法):
You can setup a cronjob like this:
您可以像这样设置一个 cronjob:
* * * * * /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.
如果你想在执行命令的同时打开一个 url。
Use this code:
使用此代码:
Artisan::call('migrate');
Hope this works!
希望这有效!
回答by MiloTheGreat
For the completeness of this article, you do this with Windows host using PS remote (enable first) then...
为了本文的完整性,您使用 PS 远程(先启用)在 Windows 主机上执行此操作,然后...
$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.
我一直在 VSTS 发布部署中使用它。
回答by David
The solution to this problem could be running once a php code (on the server) like this:
这个问题的解决方案可能是运行一次这样的 php 代码(在服务器上):
<?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
而不是您需要installation.php
在浏览器中访问。
迁移后你应该删除安装文件,这样没有人可以再次执行它