Cannot write to images directory | cloud hosting with Laravel forge - laravel

I have a problem when deploying Laravel an application using Laravel forge. I try to generate fake images using faker package in Laravel but,
Cannot write to directory "/home/forge/my.domain/public/storage/images/products/cover_img"
at vendor/fakerphp/faker/src/Faker/Provider/Image.php:98
94▕ ) {
95▕ $dir = null === $dir ? sys_get_temp_dir() : $dir; // GNU/Linux / OS X / Windows compatible
96▕ // Validate directory path
97▕ if (!is_dir($dir) || !is_writable($dir)) {
➜ 98▕ throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $dir));
99▕ }
100▕
101▕ // Generate a random filename. Use the server address so that a file
102▕ // generated at the same time on a different server won't have a collision.
in the factory file,
'cover_img' => $this->faker->image(public_path('storage/images/products/cover_img'), 640, 480, null, false),
This is my first time using cloud hosting. when using shared hosting I can give permission to create a folder or can create a folder manually. please help me to solve this problem. Thank You!
***UPDATE ***
I change a few codes and again tried.
if(!File::exists(public_path().'/storage/images/products/cover_img'))
{ File::makeDirectory(public_path().'/storage/images/products/cover_img', 0777,true);
}
Now it has error when deploy using Laravel forge,
ErrorException
mkdir(): Permission denied
If someone can help me to solve this I really appreciate it.

Finally, I've figured out the issue, I think First I did the total wrong way because I tried to seed when deploying using forge with php artisan migrate:fresh --seed code. Then came that error. below has the wrong code.
cd /home/forge/dev.mydomain.com
git pull origin $FORGE_SITE_BRANCH
$FORGE_COMPOSER install --no-interaction --prefer-dist --optimize-autoloader
( flock -w 10 9 || exit 1
echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock
if [ -f artisan ]; then
$FORGE_PHP artisan migrate --seed
fi
So, finally, I did just deploy with default settings in the forge and I log in to serve using terminal, Then I run the php artisan migrate --seed command and I was successful without any error. This was the step I followed,
Generate SSH key using git bash
ssh-keygen
Added ssh key into the forge
run this command in git bash terminal
ssh forge#ip address in the server
then installed the oh my zsh using this code,
sh -c "$(curl -fsSL
https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
then went to my directory in the host using terminal,
cd dev.maydomain.com
and final run seed,
php artisan migrate --seed

Related

Unable to run migration via artisan command

I have a Laravel 7 project and pushed it to a live server for production but I can't run migration successfully. I always get an error 'access denied' error.
I can confirm that the command sees the .env file and the connection details are all correct. When I ssh into the server and run mysql command using same parameters saved in the .env file, connection is successful. Adding the details into workbench and SequelPro also works so I am not sure why php artisan migrate doesn't work
Run the following command:
php artisan tinker
Tinker is Laravel's own repl.
It will prompt you to enter the commands. Here you can check and print the value of the environment variables by entering string inside env method.
>>> env('DB_DATABASE')
and so on for the other DB parameters.
Hope this helps.
For more help you can check out the official Github repository of tinker:
https://github.com/laravel/tinker

laravel 6.9 localhost keep loading than Not found

I download a fresh laravel project from someone for testing.
After setting up and running using php artisan serve, the locahost (http://localhost:8000/) keep loading slowly then It shows message 404 not found.
I have tried to solve using command line php artisan serve --port=1010 and run http://localhost:1010/ on broswer but It didn't work.
And I also used php -S localhost:8000 -t public or php -S 127.0.0.1:8000 -t public after run localhost:8000 on broswer but it still didn't work.
any idea?
Check once there is storage/framework/cache/data and storage/framework/sessions directory created or not, if those doesn't exist then create manually.

Laravel Forge Scheduler - Run Command

When using Laravel Forge's scheduler, you can trigger all the cronjob by e.g. running:
php /home/forge/default/artisan schedule:run
However, when wanting to execute a sole command, it will fail and says "fatal: not a git repository (or any of the parent directories): .git".
Via SSH, I would execute the command like this:
ssh to the server
cd path/
php artisan command:name
So how would I declare it in Laravel Forge's scheduler?
php /home/forge/default/artisan command:name
Does not work.
The solution is simply to remove the slash after /path/artisan - it is /path artisan.

AWS code deploy after install whoami issues

I'm migrating away from Envoyer to AWS code deploy to auto-deploy my Laravel app.
So I added this to my afterInstall script:
cd /project directory
composer install
if [ -f artisan ]
then
php artisan migrate --force
fi
if [ -f artisan ]
then
php artisan config:cache
php artisan queue:restart
fi
But the deployment fails with this error
MessageScript at specified location: scripts/after-install.sh run as
user user failed with exit code 1 Log TailLifecycleEvent -
AfterInstall Script - scripts/after-install.sh [stderr]No passwd entry
for user 'user'
which is weird. The AMI instance I've created already has the default user login as well as ubuntu as sudoers:
grep -Po '^sudo.+:\K.*$' /etc/group
ubuntu,forge
So why is it asking for a password?
I had to change the runAs section in the appspec file
in the script specified in "location".
AfterInstall:
- location: scripts/after-install.sh
runas: forge // used to be 'user'

Laravel Envoy on fortrabbit

I've setup a Laravel app on a Fortrabbit server. I can do the following
$ ssh user#server
$ cd htdocs
$ php artisan migrate
Which works perfectly fine.
But I'm trying to use Envoy for tasks like this. So I've made a simple task:
#servers(['test' => 'user#server'])
#task('task:test', ['on' => 'test'])
cd htdocs
php artisan migrate
#endtask
However, I encounter the following issue when doing so:
$ envoy run task:test
[user#server]: \(><)/ Welcome to the rabbit hole. \(><)/
[user#server]: [PDOException] SQLSTATE[HY000] [2002] No such file or directory
It might be worth noting that the db connection uses credentials from ENV variables set in the Fortrabbit interface. If i SSH into the server and do env, all the variables are listed as they should be. But when doing
$ ssh user#server env
I only get a small portion of the Fortrabbit server ENV variables.
I can't figure out what could be the cause of the error, and I haven't been able to find anything when asking Google. Could anybody shed a bit of light on this? Thanks alot.
As #ukautz said, the session scripts are not executed by envoy, that's why some of your environment variables are missing.
Here's what I did:
#servers(['dev'=>"<<user#server>>"])
#task('list')
. /etc/profile.envvars
env
#endtask
That showed all ENV variables, including those set by you on the dashboard !
Your script should work with this:
#servers(['test' => 'user#server'])
#task('task:test', ['on' => 'test'])
. /etc/profile.envvars
cd htdocs
php artisan migrate
#endtask
I suggest to check if your environment dedection works as expected.
$ php artisan env
If not, your can force the environment for artisan commands, like so:
$ php artisan migrate --env=test_server

Resources