Apache listing directory – I swear I have index file!

OK, the ultimate wisdom is you will put your index file on the webserver accessible directory and your website would be visible – right? Well, not all the time.
Apache might decide to list your files even though you put your index.php in the directory you are expecting..
The thing lies on the default configuration of your server. If the default http.conf file has the directory setting to list then it will list even in the virtual hosts.
The fix Add your DirectoryIndex

So typical .htaccess or your setting in http.conf file would look like

...you other configs
   DirectoryIndex index.php //or whatever you want it to start from 
....your other configs


, , , ,

Leave a comment

Vim not syntax highlighting in sudo/root huh..

So I had to deal with the server issue and I ssh into it. Then opened the file looked at it and wanted to change the content, I had to be on root and I noticed the syntax highlighting was off..
Here are the steps I took to check what went wrong.
Basically when I am sudo-ing the user has changed a bit. So the configuration per that user is the area of focus.
Also, there could be multiple installations and vanilla versions for the vim as well, so check which one the sudo is using

sudo which vim

I was able to locate the right vim the sudo was using and I applied that one to fix the issue

, , ,

Leave a comment

:insert not working for pasting on Vim editor

I will not explain how useful vim is, if you a developer on the *nix systems, then that is number one tool to tuck into your toolbox.

Now you want to paste huge text into vim and it looks all wacky, and the insert mode is not handling it.. here is what you would do..

Open your vim with

vi filename.ext

then do

:set paste

This will allow your vim to be in the paste mode and you can dump all you have without any problem then change the mode using esc and save it as :w

Leave a comment

Access MySQL from Host being on Vagrant

For this all you would need is tunneling/port forwarding from vagrant to mysql.

Being on vagrant machine, figure out what the host ip is, usually netstat would tell you that.
The default being and assuming you have 3306 port on host machine do the following

ssh username@ -L 3306: -N

The above would forward the 3306 [ the one on the left ] to the hosts port. Mind you, you have to use to access your vagrant machine in place of localhost which might not work.

, , ,

Leave a comment

Setting new Vagrant machine for PHP-Mysql development

As a developer you would know how stressful it would be to setup a machine. Even once you figure this out and set your machine working, you might be surprised by how it would get wacky easily when you update OS for specific packages. This is specially true mostly for *nix boxes.
Also this holds true for the new developer joining the team. Someone has to help him and all the time something might now work b/c of newer version of OS or something else.

The solution? Vagrant
Vagrant is relatively the new kid in this area. In short, it is like having a computer in your computer that is preconfigured with all what you need so that you can just start coding without having the hustle of installing 40+ packages.

Lets have PHP+MySQL+Memcached+.. setup using Vagrant

1. First thing first Get the provider
I am assuming the dev machine in this tutorial so the provider would be VirtualMachine
Get your share from Here

2. Install vagrant from Here

Go through the above links, download them and install them.. I am sure you rock star on this one :)

3. Start the provision. The advanced way to have provision would be either through the puppet or chef files.
But, for the php there is awesome site for simulating this. Go to Here and invest sometime there..
For now, just follow the wizard there until you get the box, we will replace the config file anyway.

4. Once you got your zip file unzip it wherever you want and replace the config file with the following

        box: puphpet/ubuntu1404-x64
        box_url: puphpet/ubuntu1404-x64
        hostname: 'vagrant-local'
        memory: '1024'
        cpus: '1'
        chosen_provider: virtualbox
                    host: '8640'
                    guest: '22'
                    host: '3307'
                    guest: '3306'
        post_up_message: ''
                    natdnshostresolver1: on
                numvcpus: 1
                cpus: 1
                manifests_path: puphpet/puppet
                manifest_file: site.pp
                module_path: puphpet/puppet/modules
                    - '--verbose'
                    - '--hiera_config /vagrant/puphpet/puppet/hiera.yaml'
                    - '--parser future'
                owner: www-data
                group: www-data
                source: ./
                target: /var/www
                sync_type: nfs
                        - '--verbose'
                        - '--archive'
                        - '-z'
                        - .vagrant/
                    auto: 'false'
            start: 10200
            stop: 10500
        host: null
        port: null
        private_key_path: null
        username: vagrant
        guest_port: null
        keep_alive: true
        forward_agent: false
        forward_x11: false
        shell: 'bash -l'
        host: detect
    install: '1'
        - build-essential
        - memcached
        - vim
        - curl
        - git-core
        - imagemagick
        - sendmail
        - libmemcached-dev
        - htop
        - unzip
        - mercurial  
    install: '1'
    groups: {  }
    users: {  }
    install: '1'
    jobs: {  }
    install: '1'
    rules: null
    install: '1'
        user: www-data
        group: www-data
        default_vhost: true
        manage_user: false
        manage_group: false
        sendfile: 0
        - rewrite
            servername: my-local-website.com
                - www.my-local-website.com
            docroot: /var/www/local-website
            port: '80'
                - 'APP_ENV dev'
                    provider: directory
                    path: /var/www/local-website
                        - Indexes
                        - FollowSymlinks
                        - MultiViews
                        - All
                        - all
                        - granted
                    custom_fragment: ''
            engine: php
            custom_fragment: ''
            ssl_cert: ''
            ssl_key: ''
            ssl_chain: ''
            ssl_certs_dir: ''
    mod_pagespeed: 0
    install: '0'
        default_vhost: 1
        proxy_buffer_size: 128k
        proxy_buffers: '4 256k'
    upstreams: {  }
            proxy: ''
            server_name: my-local-website.com
                - www.my-local-website.com
            www_root: /var/www/local-website
            listen_port: '80'
            location: \.php$
                - index.html
                - index.htm
                - index.php
                - 'APP_ENV dev'
            engine: php
            client_max_body_size: 1m
            ssl_cert: ''
            ssl_key: ''
    install: '1'
    version: '55'
    composer: '1'
    composer_home: ''
            - cli
            - intl
            - mcrypt
            - curl
            - memcached
            - sqlite
        pear: {  }
            - pecl_http
        display_errors: On
        error_reporting: 'E_ALL & ~E_STRICT'
        session.save_path: /var/lib/php/session
    timezone: America/Los_Angeles
    mod_php: 0
    install: '0'
    nightly: 0
    composer: '1'
    composer_home: ''
        port: '9000'
        display_errors: On
        error_reporting: '-1'
    timezone: null
    install: '1'
        xdebug.default_enable: '1'
        xdebug.remote_autostart: '0'
        xdebug.remote_connect_back: '1'
        xdebug.remote_enable: '1'
        xdebug.remote_handler: dbgp
        xdebug.remote_port: '9000'
    install: '0'
    install: '0'
    version: v0.17.1
    install: '0'
    version: 6.3.0
    install: '1'
            version: ''
    install: '1'
    npm_packages: {  }
    install: '1'
    packages: {  }
            version: ''
    install: '1'
    override_options: {  }
    root_password: '123'
    adminer: 0
                - ALL
            name: dbname
            host: localhost
            user: dbuser
            password: '123'
            sql_file: ''
    install: '0'
        root_password: '123'
        user_group: postgres
        encoding: UTF8
        version: '9.3'
    databases: {  }
    adminer: 0
    install: '0'
    override_options: {  }
    root_password: '123'
    adminer: 0
    databases: {  }
    version: '10.0'
    install: '1'
    adminer: 0
    databases: {  }
    install: '1'
        auth: 1
        port: '27017'
    databases: {  }
    install: '1'
        conf_port: '6379'
    install: '1'
        smtp_port: 1025
        http_port: '1080'
        mailcatcher_path: /usr/local/rvm/wrappers/default
        from_email_method: inline
    install: '0'
        listenport: '13000'
        maxjobsize: '65535'
        maxconnections: '1024'
        binlogdir: /var/lib/beanstalkd/binlog
        binlogfsync: null
        binlogsize: '10485760'
    beanstalk_console: 0
    binlogdir: /var/lib/beanstalkd/binlog
    install: '1'
        port: '5672'
    install: '1'
        version: 1.4.1
        java_install: true
    install: '1'
        version: 4.10.2
        port: '8984'
    install: '1'

** I have used 1G memory size for the guest. You can increase or decrease based on your physical memory accounting.

**Notice the usage of nfs here. In the case of windows OS, it might not work as expected so you might have to select samba or something to for the file synching part. I haven’t tried anything on windows for a while so don’t quote me on this.

As you might have guessed it, this is the heart of the provision. This will telling your little box what it should stuff to itself to be ready for PHP coding.
4. Now you have all the necessary stuff to get started. So far your machine has php, apache2 web server, mamcached and radis for your caching, mongodb incase you need it, git and mercurial for your versioning and more server goodies.
So if you want to add server based packages, go under the server and add your package. In this provision, ubuntu distro is used. There are many flavors out there. So, when you add the server package make sure the appropriate bundle is sited there to get away from surprises.

Being on the same directory as your config.yaml file, run

vagrant up

This one liner, will read the config file and populate your box with all the packages you listed with web server and and everything.

** If you see something strange like a bunch of red lines, please read it carefully and try to research it. You can also ask here and I will try to help.

Hopefully, it will all run nicely

5. Now you can say hi to your new machine by logging into it using

vagrant ssh

The above command will land you the virtual machine packed with all the software you have requested.. Feel free to move around, check apache, memacache and the like..

6. Now, if you notice, there is a part on the config.yaml file that mentions local-website
Lets talk a bit about it.
Vagrant will sync the folders between the host and the guest boxes. That is a great relief. You can work on any files from your favorite IDE on your host and all the changes would take effect on the guest machine – the trick is you are updating the same file. Thanks to nfs.
So, Vagrant will sync anything on the host directory that hosts the Vagrantfile to the guest sync folder in accordance to how you tell it. We have told it to match anything in the aforementioned folder to be syncd with /var/www inside the guest machine. So if you go to your /var/www folder inside your guest box, you will see the replica of the files and directories there.

Create a folder local-website inside the directory where you unzip the zip file, that is the one holding the Vagrantfile file.

mkdir local-website

And create an index file inside it

touch local-website/index.php

Inside the index.php just put the phpinfo()


7. Moment of truth!
We will try to access the web from our guest machine being on the host machine.
Before that, we have tell to our host machine what my-local-website.com mean. See how this is used as server name in the config.yaml file

Go to your host’s /etc/hosts file and insert my-local-website.com

sudo echo " my-local-website.com" >> /etc/hosts

What this does is, when you type my-local-website.com on your browser, before it is relayed to the outer DNS, it will check if it has to do something with it. In this case it will forward it to
If you lookup for this ip-address in the config.yaml file we created, you will see it under the network. This is the ip-assigned for the host in the little host-guest network vagrant provides.

Now you can go to your favorite browser and type my-local-website.com to see the phpinfo() of your guest machine’s php settings.

This will end up the journey. Now you have a fully working box that you can save it somewhere and reuse it anytime.

8 Bonus.
To shutdown the box use

vagrant halt

To destroy the box – this will, as the name implies remove everything

vagrant destroy

If you update the config file, you have to re-provision it by

vagrant provision

, , , , , ,

Leave a comment

A new entity was found through the relationship doctrine error

This is typical problem of doctrine with joined relations.
And, to be specific, this would happen when trying to save an entity with relationship and the one being joined is fetched from memcached or from other entity manager or you just populated it.

use the merge on EntityManager before you persist the object
Say you have an entity for pen and entity for color as well.. You want to save a new pen but you assigned a color from memcached object

$color = $this->memcacheGiveMeColor('blue');
$color = $em->merge($color)

Make sure you are using the object that is returned from merge!


, , ,

Leave a comment

Using unquoted json formatted result set in symfony2 twig

In the request->process->response world of the MVC infrastructure, Symfony2 fits just perfect. I was working on some data intensive site. All I want was to get the set of records from the mysql database and pass it to the front end through the controller. The front end wants the JSON format of the result set. The front end is getting the polished output of the twig
The problem
Front-end is not happy since it is getting quote-escaped version of JSON
The solution
Very very very simple. Rather than passing the json_encode(all_cars), where all_cars being the array of results coming from database or whatever it is, just pass the array it self

{{ all_cars | json_encode | raw }}

Yup, twig will not try to escape the quotes in this case since it is told to present raw.

Additional View
As you can see in this menu, both JSON and html views are together as combo. From design point of view this is not a good approach.
Better approach
You can bake the whole view right in the view and you might not want additional javascript logic on your view
Have specific API to return 100% JSON response for the request like

some domain dot com/all/cars

By hitting this from, say your ajax call, you will be provided with json formatted list of all cars. Since you are calling it from javascript, it will be directly coming to its home and no funny business would be there.
Then you will have another url call to load the page say:

some domain dot com/cars

where it will simply load the bare html format to the front-end and the front-end will know what to do with it. Of course, this would have two trips and even more so it would be suitable for 80-90% of ajaxy sites..


, , , , , ,

Leave a comment


Get every new post delivered to your Inbox.