[Development environment] If you cannot migrate php artisan with Operation not permitted, check the permissions!

permission.png

Hi, this is Takafumi.

When the development of the system I'm currently involved in was over, I thought, "I'm about to prepare the staging environment," and tried to execute the command php aritisan migrate to create a table.

Operation not permitted

"I can't migrate!"

Today's article solves the problem of not being able to migrate in a staging environment prepared using AWS EC2!

solution

Become the owning user & give write permission to the owning group

What i did

1. First read the error message.

UnexpectedValueException  : The stream or file "/var/www/mcfhfs/storage/logs/laravel-2019-11-20.log" could not be opened: failed to open stream: Permission denied
  at /var/www/mcfhfs/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:111
    107|             restore_error_handler();
    108|             if (!is_resource($this->stream)) {
    109|                 $this->stream = null;
    110|
  > 111|                 throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: '.$this->errorMessage, $this->url));
    112|             }
    113|         }
    114|
    115|         if ($this->useLocking) {
  Exception trace:
  1   Monolog\Handler\StreamHandler::write()
      /var/www/mcfhfs/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php:120
  2   Monolog\Handler\RotatingFileHandler::write()
      /var/www/mcfhfs/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php:42
  Please use the argument -v to see more details.

Looking at the error message, it says "You do not have permission to write to the log file." Apparently you don't have write permission to the log file.

2. Check & change log file permissions

When I checked the permissions with ls -la, it was rw ---- r ---- r --.

In this case, it is "owning user (reading / writing) --owning group (reading) --other user (reading)". I'm not the owning user, I belong to the owning group, so I only have read permissions.

"Then, why not add write permission (w) to the permissions of the owning group?"

So run chmod 664 laravel-2019-11-20.log. As a result, ʻOperation not permitted` was displayed.

Upon examination, it seems that file permissions can only be changed by the file owner. (Reference: https://marunouchi-tech.i-studio.co.jp/3341)

3. Recreate the file

"Then, why don't I become the owner of the file?"

Delete the relevant log file and create it again. After confirming that the file owner is yourself, execute migrate again.

php artisan migrate:fresh
Dropped all tables successfully.
Migration table created successfully.

"success!"

4. Change the permissions so that you can also write from the web server

I thought this was the solution, and when I tried to view the page by connecting to the staging environment from the browser, the message "You do not have write permission for the log file" was displayed again. Apparently it is necessary to give write permission from the web server.

chmod 664 laravel-2019-11-20.log
ls -la
-rw-rw-r--

Now that the owning user and owning group have write permission, access the site again. It was displayed safely!

■ Summary

-Permissions can only be changed by the file owner. ・ Don't forget to grant write permission from the web server! -Check the authority when "Operation not permitted" or "Permission denied"!

I've never had an error with permissions, so I'm glad I got to know this. It is safe to get Permission denied with this. Well then!

Recommended Posts

[Development environment] If you cannot migrate php artisan with Operation not permitted, check the permissions!
What to do if you get angry with swapon failed: Operation not permitted
Prepare the development environment with anyenv
About the development environment you are using
If you want to enter the virtual environment with jupyter, nb_conda_kernels is recommended
Build the fastest Django development environment with docker-compose