Skip to content

Instantly share code, notes, and snippets.

@BonfaceKilz
Last active March 20, 2018 12:32
Show Gist options
  • Save BonfaceKilz/1cc9bee4200b232694b99cae901805fc to your computer and use it in GitHub Desktop.
Save BonfaceKilz/1cc9bee4200b232694b99cae901805fc to your computer and use it in GitHub Desktop.
Setting proper laravel permissions. Otherwise, server will be down

If you don't set permissions right, your server will be down. If too restrictive, i.e. your server(e.g nginx or apache) has no write permissions for some things, content won't be served. If too liberal, you will pawned and alot of bad things will maybe happen. Here's how I set the permissions(should be added to playbooks if possible).

There are 2 ways of setting up the permissions:

  1. Make webserver owner of all files [Recommended]
  2. Giving yourself ownership

1. Make webserver owner of all files

sudo chown -R www-data:www-data /path/to/dir
sudo usermod -a -G www-data <yourusername>

sudo find /path/to/your/laravel/root/dir -type f -exec chmod 644 {} \;
sudo find /path/to/your/laravel/root/dir -type d -exec chmod 755 {} \;

sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

2. User as owner

sudo chown -R my-user:www-data /path/to/your/laravel/root/directory
sudo find /path/to/your/laravel/root/dir -type f -exec chmod 664 {} \;
sudo find /path/to/your/laravel/root/dir -type d -exec chmod 775 {} \;

sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

Notes:

If you run things like passport, a common source of error is 500 caused by not setting an oauth token. This is fixed by running:

php artisan passport:install

In the above examples, the exec command was used with a ;(so you need to type a \; to avoid interpretion by the shell) or a +. The diffference is that with ;, the command is called once per file; and with a +, it is called just as few times as possible(usually once, but there is a maximum length for a command line, so it might be split up) with all the file names.

Important

Do not give any of your files 777 file permissions, otherwise you will be pawned. Alot of people have ignorantly suggested this on many websites as a quick hack to get things running on the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment