I have a ec2 image and get the following error when trying to create a new laravel project.
[ErrorException] mkdir(): Permission denied
Here is the command:
composer create-project laravel/laravel mydir
I can write to the folder with my ec2-user username but do I need to add permission for composer to write?
If you receive an error telling you that you do not have permissions to create a directory or to write a file to a directory, this is likely an indication that your script is attempting to write to a directory that you do not own.
Quick Answer:
For security reasons, lets keep root
as the owner and just allow ourselves to read, write, and execute within the area which we will be working in.
Change directories
cd /var/www/
Change group ownership
sudo chown -Rv root:$USER .
Add priviledges to our group
sudo chmod -Rv g+rw .
For the grand finale, go ahead and create your new laravel project
composer create-project laravel/laravel
projectName--prefer-dist
Congratulations, We're done. :)
In-Depth Explanation
Explanation behind the solution:
By default you have two users; root
and $USER
(this is the global variable, but the actual name is whatever you put as your username). Every user created comes with a group as well. So if my username is john
, I have a group called john
.
There are many ways to solve this problem but I perfer this route for a few reasons:
I still want root
to be the owner of the files for security reason.
I am working in a localhost so there shouldn't be any other (guest)users writing on my files.
In order to achieve this we will need to edit the group ownership and add the proper permissions. We will leave the others
as is and only allow them to read and execute the files.
So lets get started:
I like being in the directory that I am making changes to in order to avoid mistakes so first we will need to enter the the directory which we will be working in.
cd /var/www/
We will need to change the group ownership of the directories which we will be working in. Starting from the directory which we are in and all the future directories and files we will create underneath this directory. So basically any children directories from now on will be own by our group.
sudo chown -Rv root:$USER .
chown
= command to change owner.
-R
= Recursive - This is basically stating to perform the same command to all the directories within this directory.
-v
= verbose - we are stating here to keep us updated by showing us what is actually happening.
root:$USER
= This is were we are setting the ownership. The first word before the colon(:) states theroot
will be the single owner. The second word after the colon(:) states that the group owner will be$USER
(the current user).
.
= This just means 'here, in this directory'.
root
to be the primary owner while ONLY allowing ourself's to create changes(of course, besides root
). As I mentioned before, this does not allow other users to create any changes what so ever.
sudo chmod -Rv g+rw .
chmod
= command to change modifications, in this case, privileges.
-Rv
= Recursive & Verbose
g
= this states who will receive the modifications. In our case g-group Other options are u-user and o-other.
+
= symbolizes add
r
= symbolizes read
w
= symbolizes write
composer create-project laravel/laravel
projectName` --prefer-dist
Note: If this is not working, enter the command ls -al | grep $USER
while inside the /var/www/
directory.
If you get this:
drwxrw-r-x
You are missing the executable(x
) privilege for the group. The first x
is for the user and last x
is for others. There should be middle x
for the group.
Run the following command and you should be all set to go:
sudo chmod -Rv g+x .
Now if you run ls -al | grep $USER
, you should get:
drwxrwxr-x
add "www" group and add your user to this group
sudo groupadd www
sudo usermod -a -G www ec2-user
logout/login
set ownership and write permissions
sudo chown -R root:www /var/www
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} +
find /var/www -type f -exec sudo chmod 0664 {} +
Referencing: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/install-LAMP.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With