Cache Permissions

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $10.00

Our app is a big 500 error because Symfony can't write to its cache directory.

This is an easy fix... well mostly. Let's start with the easy part: if we 777 the var/ directory, we should be good.

Add a new task at the end: "Fix var directory permissions":

---
- hosts: vb
... lines 3 - 9
tasks:
... lines 11 - 143
- name: Fix var directory permissions
... lines 145 - 163

To refer to the var/ directory, I'll create another variable: symfony_var_dir set to {{ symfony_root_dir }}/var:

---
- hosts: vb
vars:
... line 5
symfony_root_dir: /var/www/project
... line 7
symfony_var_dir: "{{ symfony_root_dir }}/var"
... lines 9 - 163

Back at the bottom, use the file module, set the path to the new variable, and state to directory:

---
- hosts: vb
... lines 3 - 9
tasks:
... lines 11 - 143
- name: Fix var directory permissions
file:
path: "{{ symfony_var_dir }}"
state: directory
... lines 148 - 163

That'll create the directory if it doesn't exist, but, it should. Then, they key part: mode: 0777 and recurse: yes:

---
- hosts: vb
... lines 3 - 9
tasks:
... lines 11 - 143
- name: Fix var directory permissions
file:
path: "{{ symfony_var_dir }}"
state: directory
mode: 0777
recurse: yes
... lines 150 - 163

Tip

If you're going to 777 your var/ directory, make sure that you've uncommented the umask calls in app.php, app_dev.php and bin/console:

26 lines web/app.php
<?php
... lines 2 - 4
// If you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#checking-symfony-application-configuration-and-setup
// for more information
umask(0000);
... lines 9 - 26

33 lines web/app_dev.php
<?php
... lines 2 - 5
// If you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#checking-symfony-application-configuration-and-setup
// for more information
umask(0000);
... lines 10 - 33

30 lines bin/console
#!/usr/bin/env php
... lines 3 - 7
// if you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
umask(0000);
... lines 11 - 30

You can see this in the finished code download.

Ok, run the playbook!

ansible-playbook ansible/playbook.yml -i ansible/hosts.ini

By the way, needing to re-run the entire playbook after a tiny change is annoying! We'll learn a trick in a minute to help with this.

Done! Ok, switch back to your browser and try it. Woohoo! A working Symfony project... not our project yet, but still, winning! We'll use our real project next.

Fixing Permissions... in a more Secure Way?

Setting the directory permissions to 777 is easy... and perfectly fine for a development machine. But if this were a production machine, well, 777 isn't ideal... though honestly, a lot of people do this.

What's better? In a few minutes, we'll add a task to clear and warm up Symfony's cache. On a production machine, after you've done that, you can set the var/cache permissions back to be non-writeable, so 555. In theory, that should just work! But in practice, you'll probably need to tweak a few other settings to use non-filesystem cache - like making annotations cache in APC.

But, that's more about deployment - which we'll save for a different course!

Leave a comment!

  • 2019-09-30 Victor Bocharsky

    Hey Damir,

    Yeah, it's easy to miss something, so sometimes re-watching the video might help ;)

    Or, maybe look at the finish/ directory of the downloaded course code that contains the finish code at the end of this course.

    I hope this helps!

    Cheers!

  • 2019-09-30 Victor Bocharsky

    Hey Junior,

    I'm sorry, but we do not provide support via WhatsApp, etc. And unfortunately we do not have a bandwidth for providing support in your personal projects, we can answer only questions related to the screencasts. If you have a question related to our screencasts, please, post the comment below the video where you have the problem.

    I hope this helps!

    Cheers!

  • 2019-09-22 Junior Ugwu

    hello i have a problem with a script im working on... and i really need your help with it.. my email is patorikky@gmail.com....just send me an email so that we can engage in a discussion from there....or you can send me a whatsapp text +2349025627930 it'll be easier for us to communicate there

  • 2019-09-20 Damir Vakhitov

    Hey Victor,
    Thank you for replay. It seems I need to go through all the lessons again. :)

  • 2019-09-20 Victor Bocharsky

    Hey Damir,

    Yeah, you had to do this, and we mention this somewhere in this course, so probably you just missed it. Glad it works finally!

    Cheers!

  • 2019-09-19 Damir Vakhitov

    Hey Victor,
    I don't know, what was the problem. I just added in file /etc/hosts of my host machine vagrant's IP with "mootube.l" domain name, and it's works. Will see is it works in future :). May be I will find problem later.
    Thanks.

  • 2019-09-19 Victor Bocharsky

    Hey Damir,

    I suppose you just need to use "mootube.l" instead of "www.mootube.l" - the easiest solution. So, literally, use the exact same domain in the browser URL as you use in your Ansible playbooks, as I understand it should be http://mootube.l for you (not http://www.mootube.l)

    Cheers!

  • 2019-09-18 Damir Vakhitov

    Hey Victor,
    I have one modul writing "mootube.l" server name. How can i write "www.mootue.l" ? If I copy and paste modul, I just rewrite all lines with "mootube.l".

  • 2019-09-18 Victor Bocharsky

    Hey Damir,

    Did you add www.mootube.l to your hosts? I mean that /etc/hosts file. Also, please, keep in mind that "www.mootube.l" and "mootube.l" are different hosts, and you should add them both to your /etc/hosts

    Cheers!

  • 2019-09-18 Damir Vakhitov

    Hello!
    Thanks for tutorial. It's great!
    But, on this chapter I stacked.
    Ansible works perfect, but my project does not work. In firefox browser I see
    next error: "We can’t connect to the server at www.mootube.l".
    May be I missed something?
    I have a Ubuntu distribution 18.04, and the same on vagrant.

  • 2017-05-30 Victor Bocharsky

    Hey Jian,

    Unfortunately, we don't have such course, but we talk about APC in this screencast: https://knpuniversity.com/s... . However, if you're on PHP >=5.5 - you probably want to use OPcache instead of APC.

    Cheers!

  • 2017-05-29 jian su

    Hi Guys: Which course talk about making annotations cache in APC?
    Thanks