Cakephp: issue with permissions when using the Apc cache - cakephp

When using the file cache, this config let me write/read the cache from a shell script
Cache::config('default', array(
'engine' => 'File',
'mask' => 0666
));
However, when using something like this
Cache::config('default', array(
'engine' => 'Apc',
'mask' => 0666 // I don't think this is used by Apc cache.
));
I get permission errors when saving/reading the cache from a shell script. The Apc cache is working fine when running from regular page loads though. I understand that shell scripts aren't executed from the same user as regular page loads, but I don't know how to set the Apc cache permissions correctly.
Here's my shell script:
class HelloShell extends AppShell {
public function main(){
Cache::write('Hello', 5);
}
}
Here's the error
Warning Error: default cache was unable to write 'hello' to Apc cache in [/home/pi/MyProject/lib/Cake/Cache/Cache.php, line 325]

You need to enable APC for php-cli
http://www.php.net/manual/en/apc.configuration.php#ini.apc.enable-cli
because shell jobs runs as command line php

Well, you just answered your question. Have you tried changing the permissions of the App/tmp/ directory to 777?
#chmod -R 777 app/tmp

Related

Database (database/database.sqlite) does not exist. Database works from artisan tinker

I created my database.sqlite file in the database folder. My .env file contents are :
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=absolute\path\to\database\database.sqlite
DB_USERNAME=admin
DB_PASSWORD=
When I run php artisan tinker and DB::table('users')->get(); I get the info from the database.
My DatabaseController is:
class DatabaseController extends Controller
{
public function main()
{
$users = DB::table('users')->get();
return view('database',compact($users));
}
}
Yet when I request /database path I get the error:
QueryException in Connection.php line 647:
Database (database/database.sqlite) does not exist. (SQL: select * from "users")
UPDATE:
A temporary fix is to change the database.php from the config folder:
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => 'absolute\path\database\database.sqlite',
'prefix' => '',
],
Instead of using env('DB_DATABASE', database_path('database.sqlite')), which returns database/database.sqlite not my absolute path.
You need to use full path, something like:
DB_DATABASE=/home/laravel-project/database/database.sqlite
If you remove DB_DATABASE=... from your .env file and use the path in the config/database.php:
'database' => env('DB_DATABASE', database_path('database.sqlite')),...
(if your database.sqlite file is in database/ folder), it will work, too.
I ran the following commands:
php artisan config:cache
php artisan config:clear
php artisan serve - restarted the server
In config/database.php file:
'sqlite' => [
'driver' => 'sqlite',
'database' => dirname(__DIR__).'/database/database.sqlite',
],
Then run following command:
php artisan config:cache
php artisan config:clear
For those who still face this issue: https://laracasts.com/discuss/channels/general-discussion/database-databasedatabasesqlite-does-not-exist-error-on-running?page=1
Since sqlite only require DB_CONNECTION=sqlite in .env file so you just remove the other:
DB_HOST
DB_PORT
DB_DATABASE
DB_USERNAME
DB_PASSWORD
then save and run migration again. This was how I solved the problem. Cheers!
As Chris said in the comments, the main solution is to completely delete DB_DATABASE from the .env file (in fact, only the first line of the following code is needed).
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USERNAME=admin
DB_PASSWORD=
For Windows, you need to set up your path like this
DB_DATABASE="C:\\xampp\\htdocs\\project\\data\\database.db"
I think, that the problem here was because of Homestead.
Absolute path to the database.sqlite file on local machine is not the same as the virtual machine one has.
In my case a had to set:
DB_DATABASE=/home/vagrant/code/database/database.sqlite
Or, you can just comment out this line and you are ready to go.
#DB_DATABASE=
go to the PHP folder from your C: directory and open the file php.ini.
From there find extension=pdo_sqlite and remove ;
Faced the same problem.
Just had to create the database.sqlite file in the database directory and run my migrations
✅ When using sqlite as your db, remove other DB env variables and it should work fine.
I faced the same problem and this solved it perfectly.
DB_DATABASE=sqlite
Remove the others.

Creating sub-folders in Caching, Cakephp

I have a Cache folder in tmp.
I want a sub-folder in the cache folder, to store cache files of a particular page ONLY in that folder.
What should I do?
Find bootstrap.php in your app/config
Replace the following lines
Cache::config('users_cache', array( // users_cache is the custom cache log name
'engine' => File,
'prefix' => 'cake_',
'path' => CACHE . 'users' . DS, // added sub-directory in users in tmp/cache
'serialize' => true,
'duration' => '+360 minutes'
));
Now, create a directory in your app/tmp/cache/users
Then use it as the following format
Cache::write('enter_name', $users,'users_cache');
For best reference read the following article Cache::write

CakePHP cache permissions issues

This week I have moved a CakePHP application on the server so that it is served from C:\path\current\ where current is a symlink to C:\path\versions[date]. Previously the app was in C:\inetpub\wwwroot.
Thus each time I deploy changes, I make a new version of the app and the deploy script updates the symlink. In order to avoid having to re-create the temp dir each time, I've moved the temp dir to C:\path\app_tmp\ - the deploy script drops a symlink at app\tmp pointing to this temp dir.
The server is Windows Server 2008 R2 and the web server is IIS7. C:\path\app_tmp\ has full permissions (Everyone has Full Control).
Since making the change to the location of the app and the tmp dir, users are reporting sporadic instances of warnings appearing at the top of the page. The app is in debug=0 but these do not appear in the error log.
Examples:
Warning:
unlink(C:\path\app_tmp\cache\models\prefix_cake_model_default_app_modelname):
Permission denied in
C:\path\versions[date]\www\lib\Cake\Cache\Engine\FileEngine.php on
line 254
Warning:
SplFileInfo::openFile(C:\path\versions[date]\www\app\tmp\cache\models\prefix_cake_model_default_app_modelname):
failed to open stream: Permission denied in
C:\path\versions[date]\www\lib\Cake\Cache\Engine\FileEngine.php on
line 313
(actual paths/model names obfuscated)
Here is what I have in core.php:
$engine = 'File';
$duration = '+999 days';
if (Configure::read('debug') >= 1) {
$duration = '+10 seconds';
}
if (!isset($_SERVER['HTTP_HOST'])) {
$prefix = 'cmd_';
}
else {
$prefix = $_SERVER['HTTP_HOST'] . '_';
}
Cache::config('_cake_core_', array(
'engine' => $engine,
'prefix' => $prefix . 'cake_core_',
'path' => CACHE . 'persistent' . DS,
'serialize' => ($engine === 'File'),
'duration' => $duration,
'mask' => 0666
));
Cache::config('_cake_model_', array(
'engine' => $engine,
'prefix' => $prefix . 'cake_model_',
'path' => CACHE . 'models' . DS,
'serialize' => ($engine === 'File'),
'duration' => $duration,
'mask' => 0666
));
I have this in bootstrap.php:
Cache::config('default', array('engine' => 'File'));
Any suggestions? I have a feeling that perhaps the permissions aren't being inherited properly from the app\tmp symlink to the actual tmp dir, but on the other hand the error logs seem to write correctly and these errors are only sporadic.
One idea i had was to switch to using Wincache but then I can't find any information on how I clear the model cache when I've got a database change to deploy (currently I can just clear the model cache with a grunt task).
I haven't been able to resolve this while sticking to using the default file caching. I've switched the application to using Wincache. To clear model cache when a database change is made, I've written a short script to execute:
Cache::clear('_cake_model_');
This has to be done in the browser because CLI uses a different cache from IIS, but I've made it "gruntable" by using grunt-shell and just executing: start http://script/location/clear_cache

CakePHP + NGINX + Memcache

I am trying to use Memcache on NGINX for CakePHP (2.4.7) but when I update the core.php & bootstrap.php to do this I am then thrown the following exception:
Fatal error: Uncaught exception 'CacheException' with message 'Cache engine _cake_core_ is not properly configured
I have tried to search if any other configuration is required but can't see anything. Any help would be appreciated
Thanks,
First of all you need be sure that your Memcached configured and working properly.
Check memcached port (11211 if default settings) / process etc... for example memcached -u www-data -vv.
Then if you using memcached default configurations you should change core.php configurations like following:
Uncomment section about memcached. After it it's should looks like this:
Cache::config('default', array(
'engine' => 'Memcache', //[required]
'duration' => 1800, //[optional]
'probability' => 100, //[optional]
'prefix' => Inflector::slug(APP_DIR) . '_',
'servers' => array(
'127.0.0.1:11211'),
'persistent' => true,
'compress' => false));
Now change $engine = 'File'; to $engine = 'Memcache';
Use caching for example in controller you need write data with key => value, then access that data with key. Example:
Cache::write($key, $value);
Cache::read($key);
That's all.
Hope it's help you.

An internal error has occured

I'm Vaijanath. I'm using cakephp installed on lamp. Now i have created a blog application, but when i run this on localhost/cakephpproject/cakephp/ it is showing an error:
"An Internal Error Has Occured".
And i had changed the "routes.php" in "/app/Config/routes.php" from
"Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));"
to
"Router::connect('/', array('controller' => 'posts', 'action' => 'index'));"
This is an internal error and i'm not able to solve it. Could you please help me in this?
Is your project in your user's public_html dir ?
If so, you must update the three .htaccess files, located in <projectBase>/, <projectBase>/app/ and <projectBase>/app/webroot, and add the following code after each RewriteEngine on statement :
RewriteBase /~<yourUserName>/<projectBase>/
Hope that helped.
In your CakePHP app in the ‘config’ folder change the following setting in the ‘core.php’ file
Configure::write(‘debug’, 0);
Change the ’0′ value to a ’2′ and CakePHP will print all debug errors.
It will display all the errors.. and when the functions works then again change it back to 0.

Resources