Almost always, when I am starting a Codeigniter 4 project, I am getting the following error:
"CodeIgniter\Cache\Exceptions\CacheException Cache unable to write to "/path/to/codeigniter-project/writable/cache/."
SYSTEMPATH/Cache/Handlers/FileHandler.php at line 61
which points at the below line:
throw CacheException::forUnableToWrite($this->path);
What is the best way to solve this?
CodePudding user response:
After doing a research, I've concluded into two solutions.
Solution 1 (fast solution):
The very quick way to just solve the above issue is to go to the command tool (e.g. terminal) and change the permissions of the folder to be writable and also to change the owner of the folder to be the one that is used from your server application. For example, I am using Apache and the user for apache is www-data
:
chmod -R 755 writable/
chown -R www-data:www-data writable/
or for macOS:
chmod -R 755 writable/
chown -R _www:_www writable/
Pros:
- A quick copy-paste solution
- Those commands, most of the times works out of the box
- Even if you don't have access to command tool, you can change the permissions and ownership of the folder through FTP (without the need of commands)
Cons:
- You always need to add those commands into a new project
- You may experience issues on development when you are trying to access the folder with your username
Solution 2 (suggested):
Add the www-data
or _www
(for macOS) to be at the same group role as your user.
for Linux and Apache:
sudo usermod -aG www-data your_username
or for macOS and Apache:
sudo dseditgroup -o edit -a your_username -t user _www
If you are still getting the error also make sure that you have the folder as writable with the following command:
chmod -R 755 writable/
I don't know why but it may also need to do a restart of the Apache.
Pros:
- It works for new projects with no extra effort
- You don't need to change the owner of the folder so you can have access to the folder for development
- Once this change is available to the server or locally, you don't need to use
sudo
anymore if thechmod
command is required
Cons:
- It requires more effort and it is not that straight forward for beginners
- It needs more permissions to the server
CodePudding user response:
The OP has a good answer. Though sometimes you don't have access to the terminal or a secure shell (ssh
), especially in a shared hosting environment to be able to run those commands.
I got a similar issue while working on a new project. I noticed that this normally happened because my application failed to write session files in the writable/session
folder of my root project directory because of permission restrictions. I was using the CodeIgniter\Session\Handlers\FileHandler
as my configured "session storage driver".
I managed to solve it by instructing the application to write these temporary files in the server's tmp
directory.
This is by default in the /tmp
directory.
Excerpt from php-src/php.ini-production
; Directory where the temporary files should be placed.
; Defaults to the system default (see sys_get_temp_dir)
;sys_temp_dir = "/tmp"
Solution
I opened my .env
file residing at the root of my project directory and added the line below.
app.sessionSavePath = '/tmp/my_project_name/ci4_session'
If you don't have this .env
file, create one starting with this default file content:
Notice the dot (.
) in front of the filename.
Addendum
Similarly, in your case, the application fails to write cache files in the writable/cache
folder of your root project directory because of permission restrictions. You most certainly are using file
as your cache handler.
Solution
Open your .env
file residing at the root of your project directory and add the line below.
cache.storePath = '/tmp/my_project_name/ci4_cache'
TIP(S)
These and more configurations can be found/modified in your .env
file using the rules below:
- The configuration class file name, in lower case. I.e
cache
. - Followed by a dot(
.
). - Then the
public
configuration class's property name. I.estorePath
. - Steps 1,2,3 combined makes,
cache.storePath
, which becomes your.env
file's key declaration. Then an equals sign=
. And lastly, the.env
file's value ('/tmp/my_project_name/ci4_cache'
) declaration. Finally coming up with,cache.storePath = '/tmp/my_project_name/ci4_cache'
.
The configuration classes can be found in the app/Config
folder of your root project directory. I.e:
app/Config/Cache.php
app/Config/App.php