Things to Consider when application hosting on AppFog

I faced many issues when hosted our company website on AppFog.

Problems:

  • No CRON, service to schedule automated tasks
  • PHP's mail() function is disabled
  • Server load balancer IP instead of User's IP
  • All Logged in user's session outs automatically
  • Yii Assets Manager don't publishes every thing correctly, Some time for same files Apache/Yii reports 404
  • Database Username and Password changes automatically.

Your should consider these things to get up and running application without lack...

No CRON, service to schedule automated tasks

They don't provides CRON service to schedule automated back-end tasks.

Don't worry, it doesn't mean you won't. There are some third party service like IronWorker etc which supports CRON like tasks. Or you may write your own in Ruby or Python language. Read here: Time for Cron in PHP Fog.

But the bad thing is a PHP developer should know either Ruby or Python for Task scheduling service. Or write a PHP programme which executes lines of code continuously into a infinite while loop. Which wake-up after duration of sleep(10).

e.g:

while(true) {
    // Write for scheduled task, like sending email service
    // or calculate article relevancy score
    sleep(10); // Sleep to 10 seconds after every execution
}
PHP's mail() function is disabled

WTF? Really, then how will send emails. Hmmm, it's a really big issue. If your web application needs some email shooting then use other library like PHP-Mailer and connect through SMTP toother services like GMail and send emails. Or you may use MailGun. Read more here about sending emails through MailGun.

Get User IP Address

Fix: Server load balancer IP instead of User's IP

$user_ip_all = $user_ip = Yii::app()->getRequest()->getUserHostAddress();

// GETTING USER IP ADDRESS IF HOSTED ON AWS:AppFog Application sitting
// behind load balancer
// Getting User's all IP address because sometimes it comes like:
// 192.168.1.65, 182.74.58.28, 127.0.0.1
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])
        //
        && ($user_ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])) !== NULL
        //
        && count($user_ip) >= 2
) {
    // Removing Last one: 127.0.0.1
    $keys = array_keys($user_ip);
    unset($user_ip[end($keys)]);

    // Again joining to string except 127.0.0.1
    $user_ip_all = implode(',', $user_ip);
    //  Getting just the first one
    $user_ip = (string) $user_ip[0];
} elseif (is_array($user_ip)) {
    $user_ip = $user_ip[0];
} elseif (is_string($user_ip) && strpos($user_ip, ',') > 0) {
    $user_ip = explode(',', $user_ip);
    $user_ip = $user_ip[0];
}

Keep User sessions persist after application updates

Fix: All Logged in user's session outs automatically

Set a application id to config in file config/main.php. e.g:

return array(
    'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
    'name' => 'People Matters',
    'id' => 'people_matters_media_pvt_ltd',  // <== YOU SHOULD ADD THIS
    // ...
);

This was suggested by AppFog support team

Set Appropriate permission on assets directory

Well this is more complicated. Till now I haven't found any better solution because AppFog doesn't provides SSH access to server. So, we can't fix it nor debug in better way.

For now I've just fixed for jquery.yiiactiveform.js. If server reports 404 for this file then some client side validation don't happens.

Find your jquery.yiiactiveform.js file inside /assets/* directory and copy it to /assets and add below script to template at POS_END where ever you have CActiveForm widget:

<script type="text/javascript">
    ;(function(d,s){
        if(typeof $.fn.yiiactiveform==='function')return;
        var j=d.createElement(s);j.type='text/javascript';
        j.src="<?php echo Yii::app()->homeUrl; ?>/assets/js/jquery.yiiactiveform.js";
        var p=d.getElementsByTagName('script')[0].parentNode.insertAfter(j, p);
    })(document, 'script');
</script>
Take care of Database Triggers

Database Username and Password changes automatically in a while. It's not necessary but some times it happens. In my case it got changed and server started giving error. Because I had created some Triggers for tables. Actually MySQL creates a definer for Triggers and Procedures. And that trigger can only executes when the definer(user) has logged in or you should give permissions to all user.

So, in that case you should re-create triggers and procedures again.

Error was reported to me:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1449 The user specified as a definer ('xxx------xxx'@'%') does not exist. The SQL statement executed was: UPDATE `user` SET ...
All above fix/patch I've discovered during developing:  
[https://www.peoplematters.in](https://www.peoplematters.in)
1 0
1 follower
Viewed: 7 899 times
Version: 1.1
Category: Tips
Written by: VINAY Kr. SHARMA
Last updated by: VINAY Kr. SHARMA
Created on: Feb 24, 2015
Last updated: 9 years ago
Update Article

Revisions

View all history