Monday, February 3, 2020

PHP-FPM, Apache virtual host from different mounts

I was stuck in scenario where I had to inter-operate both Ubuntu and Windows.

Most of my PHP projects were in Ubuntu and some few in Windows. I found it difficult because if you want to switch to the other project, then you have to switch the OS.

To mitigate this, I created a folder which is a common one accessible from both OS. From Windows its a D drive and all I could do was edit some apache config and it loads the projects from this web root.

But in case of Ubuntu, there was the apache already with default /var/www
It did not like moving out of the document root to a different point that too along with php-fpm.
I badly needed this as I was running different projects based on 5.6. 7.1 and 7.2.

After hitting Google a large number of times, finally managed to get this work.

In order to help myself and anyone who needs this future, this is a little note.

  • Setup virtual host in apache as similar to other ones
  • Change the apache default user and group to your system user and group:
  • Change the php-fpm user and group similar to apache:
    • /etc/php/7.1/fpm/pool.d/www.conf
    • /etc/php/7.1/fpm/php-fpm.conf
    • systemctl reload php7.1-fpm.service
  • And finally the virtual host example:
<VirtualHost *:80>
        ServerAdmin webmaster@kauripizzeria.test
        ServerName kauripizzeria.test
        ServerAlias kauripizzeria.test
        #DocumentRoot /var/www/html/raas
        DocumentRoot /media/bubi/SHARED/workspace/raas/kauri-pizzeria/public
        ErrorLog /media/bubi/SHARED/workspace/raas/error.log
        CustomLog /media/bubi/SHARED/workspace/raas/access.log combined

        <FilesMatch \.php$>
            # Apache 2.4.10+ can proxy to unix socket
            SetHandler "proxy:unix:/var/run/php/php7.1-fpm.sock|fcgi://localhost"
        </FilesMatch>

        <Proxy fcgi://kauripizzeria.test>
            ProxySet connectiontimeout=5 timeout=240
        </Proxy>

        <directory "/media/bubi/SHARED/workspace/raas/kauri-pizzeria/public">
        #<directory "/var/www/html/appexert/sekure/crm">
            DirectoryIndex index.php index.html
            Options Indexes FollowSymLinks Includes ExecCGI
            AllowOverride All
            Allow from All
            Require all granted
        </directory>
</VirtualHost>

Sunday, May 3, 2015

Python, Django Initial Setup

There are plenty of resources available on the web for installing and configuring Python and Django.

This one is small set of commands for installation on what i have experienced while installing

1. Install Python as per your need - Either from Python website or PPM distribution of your OS if it provides the one. (I prefer package manager, since it gives you stable release that best works with your OS version)
2. Install Django - As said above for #Python
3. Database - Python by defaut comes with SQLite, but i prefer MySQL for its robust features

After all these, i stuck up all getting together - When you try to run server by issuing something like this python manage.py runserver and get any errors, try these:
4. sudo apt-get install libmysqlclient-dev
5. sudo apt-get install python-dev
6. sudo easy_install mysql-python

Now you can start server and access this to find default page http://127.0.0.1:8000/

Thursday, March 27, 2014

Wednesday, March 19, 2014

Removing an empty div using Jquery

$('.selector:empty').remove();

Another Way

$.expr[':'].blank = function(obj){ return obj.innerHTML.trim().length === 0; }; $('.selector:empty').remove();

Friday, February 7, 2014

Capitalize First character in a Javascript String

var capitalized = capitalizeMe.substr(0, 1).toUpperCase() + capitalizeMe.substr(1);

var capitalized = capitalizeMe[0].toUpperCase() + capitalizeMe.substring(1);

var capitalized = capitalizeMe.charAt(0).toUpperCase() + capitalizeMe.substring(1);

var capitalized = capitalizeMe.slice(0,1).toUpperCase() + capitalizeMe.slice(1, capitalizeMe.length);

var capitalized = capitalizeMe.substring(0, 1).toUpperCase() + capitalizeMe.substring(1);

Custom image for facebook share - Open in popup

<a class="facebook" target="_blank" onclick="return !window.open(this.href, 'Facebook', 'width=640,height=300')" href="http://www.facebook.com/sharer/sharer.php?u=YOUR_URL">Facebook</a>

Thursday, February 6, 2014

JQuery URL parser function

// Parse URL Queries Method
(function($){
    $.getQuery = function( query ) {
        query = query.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var expr = "[\\?&]"+query+"=([^&#]*)";
        var regex = new RegExp( expr );
        var results = regex.exec( window.location.href );
        if( results !== null ) {
            return results[1];
            return decodeURIComponent(results[1].replace(/\+/g, " "));
        } else {
            return false;
        }
    };
})(jQuery);

// Document load
$(function(){
    var test_query = $.getQuery('test');
    alert(test_query); // For the URL /?test=yes, the value would be "yes"
});
Reference:

http://www.kevinleary.net/jquery-parse-url/

Change URL parameter value of a variable in a Javascript Regular Expression (RegEx)

function changeParamByName(href, paramName, newVal) {
var tmpRegex = new RegExp("(" + paramName + "=)[a-z]+", 'ig');
return href.replace(tmpRegex, '$1'+newVal);
}
 
var href = changeParamByName("http://domain.com?var=thisIsOld&bca=something&b=2&c=3","bca","theNewValue");
console.log(href);



Reference:

http://blog.adrianlawley.com/jquery-change-url-parameter-value/

Changing the URL of parent

In some cases to make the parent window to load a different URL ( from IFRAME and pop etc.,), use

window.top.location.href = "http://www.site.com"; 
To just refresh the parent window use

parent.location.reload();

To get the parent URL as a string (in cases to modify parent URL), use this,

parentlocation = document.referrer;


Javascript function to get parent URL

This is usefull in case of finding parent URL from iframe or a popup etc.,

function getParentUrl() {
    var isInIframe = (parent !== window),
        parentUrl = null;

    if (isInIframe) {
        parentUrl = document.referrer;
    }

    return parentUrl;
}

Thursday, January 23, 2014

Setting up a global state variable Joomla

Sometimes it is necessary to maintain a state variable across the site.
For example, to check whether Jquery is already loaded in another custom built component or module.

Setting up a state variable

$app = JFactory::getApplication();
$app->setUserState( 'myvar', $myvarvalue );
$app->setUserState( 'com_yourcomponent.data', $yourvalue );

Retrieving a variable saved

$app = JFactory::getApplication();
$variable = $app->getUserStateFromRequest( "com_yourcomponent.data" );

Tuesday, January 21, 2014

List documents in Joomla media manager

By normal, Joomla media manager does not default display the documents manager.

In some cases we need to show them in media manager to link to document.


  1. In administrator/com_media/views/imagelist.php, add the following function
          # Test to list PDF files
function setDocument($index = 0)
{
if (isset($this->documents[$index]))
{
$this->_tmp_document = &$this->documents[$index];
}
else
{
$this->_tmp_document = new JObject;
}
}

Add this in in the apt place in display function
# Test to list PDF files
$documents = $this->get('documents');
$this->documents = &$documents;

      2.  In administrator/com_media/views/tmpl/default.php, add this snippet next to image block
     
       <?php for ($i = 0, $n = count($this->documents); $i < $n; $i++) :
$this->setDocument($i);
echo $this->loadTemplate('document');
endfor; ?>   

      3.  Create a file 'default_document.php' inside administrator/com_media/views/tmpl/, and add this code

       <?php
       defined('_JEXEC') or die;
        
        $params = new JRegistry;
        $dispatcher = JEventDispatcher::getInstance();
        $dispatcher->trigger('onContentBeforeDisplay', array('com_media.file', &$this->_tmp_document, &$params));
        ?>
        <li class="imgOutline thumbnail height-80 width-80 center">
        <a class="img-preview" href="javascript:ImageManager.populateFields('<?php echo $this->_tmp_document->path_relative; ?>')" title="<?php echo $this->_tmp_document->name; ?>" >
        <div class="height-50">
        <!--<?php echo JHtml::_('image', $this->baseURL . '/' . $this->_tmp_document->path_relative, JText::sprintf('COM_MEDIA_IMAGE_TITLE', $this->_tmp_document->title, JHtml::_('number.bytes', $this->_tmp_document->size)), array('width' => $this->_tmp_document->width_60, 'height' => $this->_tmp_document->height_60)); ?>-->
        <?php echo JHtml::_('image', $this->_tmp_document->icon_32, $this->_tmp_document->name, null, true, true) ? JHtml::_('image', $this->_tmp_document->icon_32, $this->_tmp_document->title, null, true) : JHtml::_('image', 'media/con_info.png', $this->_tmp_document->name, null, true); ?></a>
        </div>
        <div class="small">
        <?php echo JHtml::_('string.truncate', $this->_tmp_document->name, 10, false); ?>
        </div>
        </a>
        </li>
        <?php
        $dispatcher->trigger('onContentAfterDisplay', array('com_media.file', &$this->_tmp_document, &$params));
        ?>




Saturday, January 11, 2014

CakePHP base URL

To access base URL in controller use
Router::url('/', true);

To access base URL in view use
<?php echo $this->webroot; ?>

Thursday, December 12, 2013

Joomla load module anywhere

if (!class_exists( 'CarsModelCars' )){
   JLoader::import( 'cars', JPATH_BASE . DS . 'components' . DS . 'com_cars' . DS . 'models' );
}

Now create an object for that class