-1

I'm create a script to start my Laravel 5.8 project

#Install dependencies
composer self-update
composer install

# create .env base on .env.example
cat .env.example > .env

#permission
chmod -R 777 .env

#create the key
php artisan key:generate --force

cat .env

#set permission
chmod -R 777 bootstrap/ vendor/ storage/ public/

sleep 1

#clear cache
php artisan config:cache
php artisan cache:clear
composer dump-autoload
php artisan clear-compiled

php artisan key:generate

python -mwebbrowser http://127.0.0.1:8000
php artisan serve

This line does not seem to work.

php artisan key:generate --force

Look at my .env

 ⚡️  bheng  cat .env                                                                             
APP_ENV=local                                                                                  
APP_URL=http://bheng.test/                                                                     
APP_DEBUG=true                                                                                 
APP_KEY=***                                                                                    
CODE=###                                                                                       
                                                                                               
#---------------------------------------------- DATABASE                                       
                                                                                               
DB_CONNECTION=mysql                                                                            
DB_HOST=4.2.2.1                                                                            
DB_DATABASE=b                                                                          
DB_USERNAME=dev                                                                                
DB_PASSWORD=123
DB_PORT=3306

#---------------------------------------------- EMAIL

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_FROM=8863b0c62fbcff
MAIL_PASSWORD=***

#----------------------------------------------

GOOGLE_WEB_API_KEY=***

Update

⚡️  bheng  php artisan key:generate --force
Application key set successfully.
⚡️  bheng  cat .env | grep KEY
APP_KEY=***                                                                                    
GOOGLE_WEB_API_KEY=***                                                                         
⚡️  bheng  ls
Procfile           composer.lock      package-lock.json  readme.md          storage/
app/               config/            package.json       resources/         vendor/
artisan            database/          phpspec.yml        routes/
bootstrap/         gulpfile.js        phpunit.xml        server.php
composer.json      npm-debug.log      public/            start.sh*
⚡️  bheng  L
total 1584
drwx------+ 25 bheng  staff   800B Oct 13 13:55 ../
-rw-r--r--   1 bheng  staff   796B Oct 13 13:55 .env.example
-rw-r--r--   1 bheng  staff    61B Oct 13 13:55 .gitattributes
-rw-r--r--   1 bheng  staff   556B Oct 13 13:55 .gitignore
drwxr-xr-x   7 bheng  staff   224B Oct 13 13:55 .idea/
-rw-r--r--   1 bheng  staff    43B Oct 13 13:55 Procfile
drwxr-xr-x  26 bheng  staff   832B Oct 13 13:55 app/
-rw-r--r--   1 bheng  staff   1.6K Oct 13 13:55 artisan
drwxr-xr-x   7 bheng  staff   224B Oct 13 13:55 bootstrap/
-rw-r--r--   1 bheng  staff   1.3K Oct 13 13:55 composer.json
-rw-r--r--   1 bheng  staff   200K Oct 13 13:55 composer.lock
drwxr-xr-x  17 bheng  staff   544B Oct 13 13:55 config/
drwxr-xr-x   6 bheng  staff   192B Oct 13 13:55 database/
-rw-r--r--   1 bheng  staff   1.6K Oct 13 13:55 gulpfile.js
-rw-r--r--   1 bheng  staff    13K Oct 13 13:55 npm-debug.log
-rw-r--r--   1 bheng  staff   515K Oct 13 13:55 package-lock.json
-rw-r--r--   1 bheng  staff   1.0K Oct 13 13:55 package.json
-rw-r--r--   1 bheng  staff    87B Oct 13 13:55 phpspec.yml
-rw-r--r--   1 bheng  staff   777B Oct 13 13:55 phpunit.xml
drwxr-xr-x  20 bheng  staff   640B Oct 13 13:55 public/
-rw-r--r--   1 bheng  staff   2.5K Oct 13 13:55 readme.md
drwxr-xr-x   5 bheng  staff   160B Oct 13 13:55 resources/
drwxr-xr-x   5 bheng  staff   160B Oct 13 13:55 routes/
-rw-r--r--   1 bheng  staff   560B Oct 13 13:55 server.php
-rwxr-xr-x   1 bheng  staff   957B Oct 13 13:55 start.sh*
drwxr-xr-x   5 bheng  staff   160B Oct 13 13:55 storage/
drwxr-xr-x  12 bheng  staff   384B Oct 13 13:55 .git/
drwxr-xr-x  45 bheng  staff   1.4K Oct 13 13:55 vendor/
drwxr-xr-x  30 bheng  staff   960B Oct 13 13:55 ./
-rwxr-xr-x   1 bheng  staff   796B Oct 13 13:56 .env*
⚡️  bheng
Community
  • 1
  • 1
code-8
  • 54,650
  • 106
  • 352
  • 604

4 Answers4

10

Number 1: Never set 777 permission for any reason especially for web-facing app. You don't want external script manipulating your web-server for you.

Number 2: Since the issue with adding the generated key is not easily traceable, you could get the generated key into the terminal with php artisan key:generate --show and manually override it in your .env.

You could write a script like this that helps to create and override your .env file's APP_KEY value:

#!/bin/sh

while IFS="" read -r p || [ -n "$p" ]
do
  if printf '%s' "$p" | grep -Eq '^APP_KEY'; then
    key=$(php artisan key:generate --show)
    echo "APP_KEY=$key" >> .envv
    printf '%s\n' "APP_KEY generated and set"
  else
    #printf '%s\n' "$p"
    echo "$p" >> .envv
  fi

done < .env

cp .envv .env
rm .envv
  • if printf '%s' "$p" | grep -Eq '^APP_KEY'; then checks if the line starts with APP_KEY
  • Then generates the key, appends it into temp file .envv
  • ... then override .env file with the temporary file

PS: I am not an expert in bash/sh/dash scripting so this might not be performance friendly, but just to prove the idea. The initial problem I suspect comes from changing permission of files

4

Steps to Reproduce your App Key:

  1. Create a .env file without an APP_KEY= line.
  2. Run php artisan key:generate.
  3. Run grep APP_KEY .env - It will generate no output.

I Hope this gonna work for you. Thank you

dqureshiumar
  • 810
  • 1
  • 7
  • 19
0

I think the problem is that your group don't have write permissions.

-rwxr-xr-x   1 bheng  staff   796B Oct 13 13:56 .env*

Thats identifiable by the missing second w on the -rwxrwxrwx, witch by the way, represents the permissions for the owner, the group and others (in that order)

So, instead of chmod 777 i suggest:

chmod g+rwx . -R (this will give read, write and execution permissions to the group, on the current directory recursively)

Give it a try, hope it helps :)

Renato Gomes
  • 126
  • 4
-4

if you can't use key:generate, maybe this will help you

$router->get('/key', function() {
    return str_random(32);
});

and please, don't use 777, read more : How to set up file permissions for Laravel?

Elonelon
  • 68
  • 1
  • 12
  • 2
    How is this fix my problem ? ‍♂️ – code-8 Oct 12 '19 at 19:09
  • @kyo how ? it will generate random key from your browser, and then you can add that key to your .env file. If key:generate doesn't work, then you have a problem with your laravel installer. – Elonelon Oct 13 '19 at 20:47
  • Are you sure, that is not because of the permission ? I listed my permission on the post. – code-8 Oct 13 '19 at 23:01
  • try to create new project and run key:generate, or maybe add manual key generate from my answer. And yes, i also use MacOS Mojave, everything is fine, i can do anything from VS Code / Terminal. – Elonelon Oct 14 '19 at 19:47