Saturday

PrestaShop vs Magento which is better for e-commerce


The most widespread shopping platform. There is no need to say anything more. You all know this shopping cart. It’s Magento! 
One of the most user-friendly and easy to operate shopping carts. It’s PrestaShop! So, let’s get ready to rumble!!!
Who is where
It is an open source and free to download shopping cart. Magento community is extremely big and includes around 375 000 of people. More than 100 000 of Internet shops are based on Magento. The platform is available in 60 languages and this number continues to grow! Incredibly, but it seems that at the moment Magento is one of the best options and it definitely wins the first round.

Don’t forget that PrestaShop is also an open source platform. And it is available in more than 40 languages. At the moment more than 50 000 of Web stores are using this platform. This number is constantly growing from year to year with amazing speed. And what is even more important all users agree that PrestaShop has far more user-friendly and flexible interface. If we take into consideration that PrestaShop is almost six years younger than Magento you will understand that it is incredible result.

Which are best part of them
  • Both platforms have a wide selection of product management tools that include: product, order history and customers management systems.
  • Both carts support a great number of payment gateways that include: PayPal, Google Checkout, Authorized.Net and other.
  • A variety of available PrestaShop modules makes it very simple in development and administration. However, some of these modules are quite expensive.
  • PrestaShop is very simple in installation, but the number of basic features is considerably less when compare it to Magento.
  • PrestaShop software is much smaller in MBs and it has lower system requirements. However, Magento offers better out-of-the-box feature set.
  • Magento is more useful for big scale Internet stores while PrestaShop will better suit small and middle sized companies.
  • Magento offers better SEO features. However, PrestaShop has emphasized its search capabilities since its launch.
  • Magento definitely has bigger user community. While PrestaShop provides customers with an official support. Users also share their experience and advices on the PrestaShop Forum.
  • Magento store needs a very good hosting environment. It means that you will have to pay extra money for a good server.
Verdict
PrestaShop and Magento showed that they have both advantages and disadvantages. Presta shopping cart is much easier in installation and operating. It will perfectly handle with your Internet store if it is small or middle sized. However, the number of product items is almost unlimited. Which means that if you have enough skills and experience you can also manage a big Web store based on this platform. PrestaShop can become a good solution for a beginner to start-up his business. Nevertheless, experienced user will also find PrestaShop shopping cart very useful. At the same time Magento can be a very good solution for a big Internet store with a very wide range of items. New updates that are available for Magento can help you make your business successful.

Users decision
Both platforms are standing on their feet with their heads proudly raised. It is up to you to decide which of them is better for your business requirements.

Some more technical comparison

There seems to be growing anger with magento as the community is slowly and painfully discovering that varien, the company behind it isn't so much interested in championing a reliable open source product, than to create a community that would require extensive commercial support.
In the 2 days that I've been researching magento, the amount of negative feedbacks that I've heard and the code quality is more than enough to steer me away. After having a look through the source, I believe the intended basic architectural principles were decent, but the execution is wrong. Many PHP programmers see OOP and call it good software. I'm reasonably well versed with the Zend Framework and am generally reluctant to refer to any code as bloat, but in this case I have few other choice of word. Magento seems to uses some Zend libraries, but the relationship doesn't warrant good quality code. Don't take my word for it, here's some evidence. I decided to follow a request to see what happens during a call to one of the action controllers, so I tried to trace calls made within the Mage_Adminhtml_Customer_GroupController::indexAction method, starting with a call to $this->loadLayout(). Pretty self explanatory right, we're loading a layout. Lets see where it takes us. The following illustrates a single thread in that one call, where each line represents a call to a new method from the preceding method:
    Mage_Adminhtml_Customer_GroupController::indexAction
        Mage_Adminhtml_Controller_Action::loadLayout
            Mage_Core_Controller_Varien_Action::loadLayout
                Mage_Core_Controller_Varien_Action::addActionLayoutHandles
                    Mage_Core_Controller_Varien_Action::getLayout
                        Mage::getSingleton                               
                            Mage::getModel
                                Mage::getConfig
                                    Mage::registry
I repeat, we're following a single thread from within one call to $this->loadLayout(). In real time each call to a method would likely initiate multiple threads as they fork and each action would likely have multiple method calls. Is anyone still surprised as to why magento would be so slow? Just because it's OOP, doesn't necessarily mean this is good. This code looks like something meant to be compiled (therefore reduced), but PHP is (for the most part) a "just-in-time" technology, you should refrain from doing this sort of things.

Tuesday

How to: Dealer Locator extension for Magento


In this article I will try to explain how to build a “Dealer Locator” extension for Magento. This article is all about extension concepts, structure and planing and not the code itself.
First we will start with scoping the feature. What functionality should it have? In our example, let’s imagine a client that wants a dealer locator to have “Search by nearest dealer based on ZIP/Postcode”, “Search by State”, “Search by dealer company name”. Search results should be displayed primarily on Google Map, plus it would be nice to have a table listing for those disliking the map visual style.
If we extend this a bit further, we soon come to realisation that “dealers” themselves should be a customers in our Magento system. Justification for this lies in a fact that dealers should be able to do a regular purchases on our Magento store like any other customer just with special prices applied to products. Since customer in Magento has to have a customer group assigned to it, and products special pricing can be applied for each customer group. Thus all we need to do in order to “convert” dealers to customers is to create a customer group “Dealer” or even use “Wholesale” for these types of customers. Then on each product set a special price for that customer group, that is if the dealers will have special prices.
Now, since dealers are just customers in our system then customer address(es) can be used for dealer locations. And this realization is the basis of our “Dealer locator” extension. Now that we have the general picture set, lets make a wireframe of the “Dealer locator” functionality.

Once the wireframe is approved by the client we can start with the preparations for development. Now, if we look back at the original requirement: “Search by nearest dealer based on ZIP/Postcode”, “Search by State”, “Search by dealer company name” we can easily see that “Search by nearest dealer based on ZIP/Postcode” part will be the most challenging one. “Search by State”, “Search by dealer company name” are straightforward as this is the direct property of each customer address. “Search by nearest dealer based on ZIP/Postcode” is something we will need to use certain calculations that involve geocode data.
General approach we will take towards “Search by nearest dealer based on ZIP/Postcode”:
- We will create an install script, for example app/code/community/Inchoo/Dealerlocator/sql/inchoo_dealerlocator_setup/install-1.0.0.0.php that adds two additional attributes to the customer address entity: inchoo_geo_latitude, inchoo_geo_longitude
- We will observe “customer_address_save_before” event and trigger a Inchoo_Dealerlocator_Model_Observer->injectLatLongIntoAddress() method once the event is fired. Note, injectLatLongIntoAddress is freely taken/chosen method name
- Within injectLatLongIntoAddress() we will execute a code that “contacts” the Google Maps API maps.google.com/maps/geo sending it the customer address information (namely street, postcode, city, country) then parses the response into the inchoo_geo_latitude, inchoo_geo_longitude properties of an address. Since we are observing customer_address_save_before event this data is then simply saved to customer address
- Once we have geo latitude and longitude data saved on each address we can now query the address collection for this information
- We still lack one important ingredient in this “Search by nearest dealer based on ZIP/Postcode” request, thats the “nearest”. We need to come up with a way to calculate the distance between two geo coordinates. There is a nice MySQL code snippet for this http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe#MySQL. All we need to do is to transform to Magento collection :)
- Finally, the same way we did a geo mapping in the “customer_address_save_before” event for each address save, we will do a live geo mapping of the provided “referencing ZIP/Postcode”, the one that is provided on the frontend input text field. This way we have two geo location coordinates between which we calculate the distance: the referencing ZIP/Postcode geo coordinates vs. specific customer address geo coordinates
- Now we should have all pre-requirements for building the “Search by nearest dealer based on ZIP/Postcode”, we have latitude and longitude, we have distance.
I’m gonna be kind enough to provide you with my example of code for injectLatLongIntoAddress():


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$coordinates = array();$saveCoordinatesToAddress = true;$lineAddress = $address->getStreet1(). ', '.$address->getPostcode().' '.$address->getCity().', '.$address->getCountry();$client = new Zend_Http_Client();$client->setUri('http://'.self::GOOGLE_MAPS_HOST.'/maps/geo');$client->setMethod(Zend_Http_Client::GET);$client->setParameterGet('output', 'json');$client->setParameterGet('key', $this->getGoogleMapsApiKey());$client->setParameterGet('q', $lineAddress);$response = $client->request();if ($response->isSuccessful() && $response->getStatus() == 200) {    $_response = json_decode($response->getBody());    $_coordinates = @$_response->Placemark[0]->Point->coordinates;    if (is_array($_coordinates) && count($_coordinates) >= 2) {        $coordinates = array_slice($_coordinates, 0, 2);        if ($saveCoordinatesToAddress) {            try {                $address->setInchooGeoLongitude($coordinates[0]);                $address->setInchooGeoLatitude($coordinates[1]);                $address->save();            } catch (Exception $e) {                Mage::logException($e);            }        }    }}
Please note the $saveCoordinatesToAddress variable. If we pack the code above into a separate method we can reuse it for live geo mapping of the provided “referencing ZIP/Postcode” as well.
And here is the Magento customer collection object with included addresses and “distance” value calculation:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public function getNearbyDealersLocations($radius, $centerLat, $centerLng)
{
    $customerGroup = Mage::getModel('customer/group');
    $customerGroup->load('Dealer', 'customer_group_code');
    if (!$customerGroup->getId()) {
        throw new Exception($this->__('Unable to load the customer group.'));
    }
    $collection = Mage::getResourceModel('customer/customer_collection')
                        ->addNameToSelect()
                        ->addAttributeToFilter('group_id', array('eq'=>$customerGroup->getId()))
                        ->joinAttribute('billing_company', 'customer_address/company', 'default_billing', null, 'left')
                        ->joinAttribute('inchoo_geo_latitude', 'customer_address/inchoo_geo_latitude', 'default_billing', null, 'left')
                        ->joinAttribute('inchoo_geo_longitude', 'customer_address/inchoo_geo_longitude', 'default_billing', null, 'left')
                        ->addAttributeToFilter('inchoo_geo_latitude', array('notnull'=>true))
                        ->addAttributeToFilter('inchoo_geo_longitude', array('notnull'=>true))
                        ->addExpressionAttributeToSelect('distance', sprintf("(3959 * acos(cos(radians('%s')) * cos(radians(at_inchoo_geo_latitude.value)) * cos(radians(at_inchoo_geo_longitude.value) - radians('%s')) + sin(radians('%s')) * sin( radians(at_inchoo_geo_latitude.value))))",   $centerLat, $centerLng, $centerLat, $radius), array('entity_id'));
    if ($radius !== 0) {
        $collection->getSelect()->having('distance < ?', $radius);
    }
    $collection->getSelect()->order('distance ' . Varien_Db_Select::SQL_ASC);
    //echo (string)$collection->getSelect(); exit;
    return $collection;
}


With the above in place you all that remains is to output the search results into the table or into the Google Map. There are plenty of example codes out there that show how to embed google maps with marker so I’m not going to cover this here. As mentioned at the beginning, the purpose of article is not to give a code ready solution but to give you an overview of the process that latter can then be given to developer so he can develop the feature.

courtsy: Branko Ajzele

Wednesday

Magento primary things


Q 1. What is Magento?

Ans. Magento is a feature-rich eCommerce platform built on open-source technology that provides online merchants with unprecedented flexibility and control over the look, content and functionality of their eCommerce store. Magentos intuitive administration interface features powerful marketing, search engine optimization and catalog-management tools to give merchants the power to create sites that are tailored to their unique business needs. Designed to be completely scalable and backed by Variens support network, Magento offers companies the ultimate eCommerce solution.


Q 2. What is the difference between Mage::getSingletone() andMage::getModel() in Magento

Ans. Mage::getSingletone() always finds for an existing object if not then create that a newobject but Mage::getModel() always creates a new object.


Q 3. Why Magento use EAV database model ?

Ans. In EAV database model, data are stored in different smaller tables rather than storing in asingle table.product name is stored in catalog_product_entity_varchar tableproduct id is stored in catalog_product_entity_int tableproduct price is stored in catalog_product_entity_decimal tableMagento Use EAV database model for easy upgrade and development as this model givesmore flexibility to play with data and attributes.


Q 4. How to upgrade to the latest version using Magento Connect?

Ans. Upgrading Magento to the latest version is a fairly simple task. Copy and Paste this key magento-core/Mage_All_Latest VIA Magento Connect where it states Paste extension key to install:. This will upgrade Magento to the newest version.


Q 5. Explain about the Modules of Magento?

Ans. Magento supports installation of modules through a web-based interface accessible through the administration area of a Magento installation. Modules are hosted on the Magento eCommerce website as a PEAR server. Any community member can upload a module through the website and is made available once confirmed by a member of the Magento team. Modules are installed by entering a module key, available on the module page, into the web based interface.

There are three categories of modules hosted on Magento Connect:

Core Modules

Community Modules

Commercial Modules

Core and Community modules can be installed via the administration area. Commercial module pages provide price information and a link to an external website.


Q 6. What technology used by Magento?

Ans. Magento uses PHP as a web server scripting language and the MySQL Database. The data model is based on the Entity-attribute-value model that stores data objects in tree structures, thus allowing a change to a data structure without changing the database definition.


Q 7. What is MVC structure in Magento?

Ans. The Model-View-Controller (MVC) architecture traces its

origins back to the Smalltalk Programming language and Xerox

Parc. Since then, there have been many systems that describe

their architecture as MVC. Each system is slightly

different, but all have the goal of separating data access,

business logic, and user-interface code from one another.


Q 8. What is benefit of namespace (package) in magento?

Ans. We can have more than one module with same name but they should be placed in different namespaces. All magento core modules are contained in mage namespace.

core/Mage/Catalog

and all custom modules are placed in

local/CustomModule


Q 9. How to include CMS block in template file(.phtml)?

Ans. Access block’s content from .phtml template file by :

echo $this->getLayout()->createBlock(‘cms/block’)->setBlockId(‘static_block_id’)->toHTML();


Q 10. How to add an external javascript/css file to Magento?

Ans.

css/yourstyle.css

or

skin_jsjs/ yourfile.js

skin_csscss/yourstyle. css


Q 11. What are handles in magento (layout)?

Ans. Handles are basically used for controlling the structure of the page like which block will be displayed and where. First level child elements of the node are called layout handles. Every page request can have several unique Handles. The handle is called for every page. handle for products belongs to virtual product type, PRODUCT_TYPE_simple is called for product details page of simple product type and PRODUCT_TYPE_virtual is called for the virtual product detail page and customer_logged_in handle is called only if customer is logged in. The muster_index_index handle is created by combining the frontName (muster), Action Controller (index), and Action Controller Action Method (index) into a single string and this handle will be called only when /zag/index/index url is accessed.


Q 12. What is in magento?

Ans. The routers tag allow us to decide frontname for each module. The tag is defined in config.xml file of module. For Namespace_MyModule frontname is moduleurl so the url will be like :

websiteurl.com/moduleurl/controllername/actionname

standard

Namespace_MyModule

moduleurl


Q 13. Which factors affect performance of magento?

Ans.

1. EAV structure of magento database, even for retrieving single entity the query becomes very complex .

2. Magento’s template system involves a lot of recursive rendering

3. Huge XML trees built up for layout configuration, application configuration settings

Q 14. How to improve magento performance?

Ans.

Enabled magento caching

MySQL Query caching

Enable Gzip Compression

Disable any unused modules

Disable the Magento log

Optimise your images

Combine external CSS/JS into one file

Enable Apache KeepAlives: Make sure your Apache configuration has KeepAlives enabled.


Q 15. How to get the Total Price of items currently in the Cart?

helper(‘checkout’)->formatPrice(Mage::getSingleton(‘checkout/cart’)->getQuote()->getGrandTotal()); ?>


Q 16. How to set different themes for logged in users?

if(Mage::getSingleton(‘customer/session’)->isLoggedIn()):

Mage::getDesign()->setPackageName(‘package_name’)->setTheme(‘themename’);

endif;


Q 17. How to create magento custom module?

Ans. Steps to create custom magento module:

Namespace : Zag

Module Name : Mymodule

1. Create directory Mymodule in app/code/local/Zag

2. Create Block, controllers, etc, Module directories. Create controller, block and module file as required.

3. Create module configuration file (app/code/local/Zag/Mymodule/etc/config.xml).

4. Create xml file (app/etc/modules/Zag_ Mymodule.xml)to enable/disable module and tell magento system from which code pool that module will be taken.


Q 18. How to set different themes for each store?

Ans. Go to : System>Designs

Then, add new design change or edit existing. You can select Store and Custom Design.


Q 19. How to make product’s custom attribute searchable in adavance search?

Ans. Go to : Catalog > Attribues > Manage Attribues

Edit the attribute and select “Yes” for Use in Advanced Search.


Q 20. How to fetch 5 bestsellers products programmatically?

Ans.

Mage::getResourceModel(‘reports/product_collection’)

->addOrderedQty()

->addAttributeToSelect(‘*’)

->setPage(1, 5)

->load();


 21-Explain Magento’s MVC architecture

First of all, what is MVC?

MVC stands for Model-View-Controller. Any application that separates it’s data access, business logicand user interface is called MVC. There can be two types of MVC: convention-based andconfiguration-based. Example, cakePHP is convention-based, i.e. you just need to follow the instructions of the core system to get your module ready in just few lines. Magento is configuration-based, i.e. you need to specify each and every thing to your module’s config file in order to get it work. Magento has Controller (for Routing), Block (for Business Logic), Model (for DB access, sql) and Template file (for Presentation i.e. View).

How Magento’s MVC works:

1. When you enter the URL (something like http://mysite.com/frontname/controller/method/param1/value1/param2/value2), this URL is intercepted by one PHP file called index.php which instantiates Magento application

2. Magento application instantiates Front Controller object

3. Further, front controller instantiates Router objects (specified in module’s config.xml, global tag)

4. Now, Router is responsible to “match” the frontname which is in our URL

5. If “match” is found, it sees controller name and method name in the URL, which is finally called.

6. Now depending on what is written in action name (method name), it is executed. If any models are called in it, the controller method will instantiate that model and call the method in it which is requested.

7. Then the controller action (method) instantiate the Layout object, which calls Block specified for this action (method) name (Each controller action name have block and template file associated with it, which can be found at app/design/frontend or adminhtml/namespace/module/layout/module.xml file, name of layout file (module.xml) can be found in config.xml of that module, in layout updates tag).

8. Template file (.phtml) now calls the corresponding block for any method request. So, if you write $this->methodName in .phtml file, it will check “methodName” in the block file which is associated in module.xml file.

9. Block contains PHP logic. It references Models for any data from DB.

10. If either Block, Template file or Controller need to get/set some data from/to database, they can call Model directly like Mage::getModel(‘modulename/modelname’).

For diagramatic view: click here (courtsey: Alan Storm)


22 =How Magento ORM works?

ORM stands for Object Relational Mapping. It’s a programming technique used to convert different types of data to Objects and vice versa.

In Magento, ORM is shown as Model (based on Zend Framework’s Zend_Db_Adapter), which further breaks down to two types of Models.

- First is the “simple” i.e. Regular Models which is nothing but flat table or our regular table structure.

- Second Model is EAV (Entity Attribute Value), which is quite complicated and expensive to query.

All Magento Models interacting with database are inherited from Mage_Core_Model_Abstract class, which is further inherited from Varien_Object.

Difference between two Models is, Simple Model is inherited fromMage_Core_Model_Resource_Db_Abstract class,

while EAV is inherited from Mage_Eav_Model_Entity_Abstract.

For those who don’t know what EAV is, please read my 3rd answer below.

So, to end up this question,

when you want to get some data in Magento, you call it like this:

Mage::getModel('module/model')->load(1);

where 1 is the primary key id for some Regular/Simple table, while in EAV so many tables are joined to fetch just single row of data.


23. What is EAV in Magento?

EAV, stands for Entity Attribute Value, is a technique which allows you to add unlimited columns to your table virtually. Means, the fields which is represented in “column” way in a regular table, is represented in a “row” (records) way in EAV. In EAV, you have one table which holds all the “attribute” (table field names) data, and other tables which hold the “entity” (id or primary id) and value (value for that id) against each attribute.

In Magento, there is one table to hold attribute values called eav_attribute and 5-6 tables which holds entity and data in fully normalized form,

- eav_entity, eav_entity_int (for holding Integer values),

- eav_entity_varchar (for holding Varchar values),

- eav_entity_datetime (for holding Datetime values),

- eav_entity_decimal (for holding Decimal/float values),

- eav_entity_text (for holding text (mysql Text type) values).

EAV is expensive and should only be used when you are not sure about number of fields in a table which can vary in future. To just get one single record, Magento joins 4-5 tables to get data in EAV. But this doesn’t mean that EAV only has drawbacks. The main advantage of EAV is when you may want to add table field in future, when there are thousands or millions of records already present in your table. In regular table, if you add table field with these amount of data, it will screw up your table, as for each empty row also some bytes will be allocated as per data type you select. While in EAV, adding the table column will not affect the previously saved records (also the extra space will not get allocated!) and all the new records will seamlessly have data in these columns without any problem.


24. Difference between Mage::getSingleton() and Mage::getModel()

The difference between Mage:getSingleton() and Mage::getModel() is that the former one does not create an object if the object for same class is already created, while the later creates new objects every time for the class when it’s called.

Mage::getSingleton() uses the “singleton design pattern” of PHP. If the object is not created, it will create it.

Mage::getSingleton() is mostly used when you want to create an object once, modify it and later fetch from it. Popular example is session, you first create a session object, and then add/remove values from session across different pages, so that it retains your values (e.g. cart values, logged in customer details, etc.) and doesn’t create new session object losing your last changes.

Mage::getModel() is used when you want to have the fresh data from the database. Example is when you want to show records from database.

x

Major GIT commands with examples


Here you will find a list with the major commands, their short descriptions and exemplary usage. For a detailed description of all the GIT commands please visit 

Major GIT commands:
  • git config
  • Sets configuration values for your user name, email, gpg key, preferred diff algorithm, file formats and more.

    Example: git config --global user.name "My Name"
                     git config --global user.email "user@domain.com"

    cat ~/.gitconfig
    [user]
        name = My Name
        email = user@domain.com
  • git init
  • Initializes a git repository – creates the initial ‘.git’ directory in a new or in an existing project.

    Example: cd /home/user/my_new_git_folder/
    git init

  • git clone
  • Makes a Git repository copy from a remote source. Also adds the original location as a remote so you can fetch from it again and push to it if you have permissions.

    Example: git clone git@github.com:user/test.git
  • git add
  • Adds files changes in your working directory to your index.

    Example: git add .
  • git rm
  • Removes files from your index and your working directory so they will not be tracked.

    Example: git rm filename
  • git commit
  • Takes all of the changes written in the index, creates a new commit object pointing to it and sets the branch to point to that new commit.

    Examples: git commit -m ‘committing added changes’
    git commit -a -m ‘committing all changes, equals to git add and git commit’
  • git status
  • Shows you the status of files in the index versus the working directory. It will list out files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing).

    Example: git status
    # On branch master
    #
    # Initial commit
    #
    # Untracked files:
    #   (use "git add ..." to include in what will be committed)
    #
    #    README
    nothing added to commit but untracked files present (use "git add" to track)
  • git branch
  • Lists existing branches, including remote branches if ‘-a’ is provided. Creates a new branch if a branch name is provided.

    Example: git branch -a
    * master
      remotes/origin/master
  • git checkout
  • Checks out a different branch – switches branches by updating the index, working tree, and HEAD to reflect the chosen branch.

    Example: git checkout newbranch
  • git merge
  • Merges one or more branches into your current branch and automatically creates a new commit if there are no conflicts.

    Example: git merge newbranchversion
  • git reset
  • Resets your index and working directory to the state of your last commit.

    Example: git reset --hard HEAD
  • git stash
  • Temporarily saves changes that you don’t want to commit immediately. You can apply the changes later.

    Example: git stash
    Saved working directory and index state "WIP on master: 84f241e first commit"
    HEAD is now at 84f241e first commit
    (To restore them type "git stash apply")
  • git tag
  • Tags a specific commit with a simple, human readable handle that never moves.

    Example: git tag -a v1.0 -m 'this is version 1.0 tag'
  • git fetch
  • Fetches all the objects from the remote repository that are not present in the local one.

    Example: git fetch origin
  • git pull
  • Fetches the files from the remote repository and merges it with your local one. This command is equal to the git fetch and the git merge sequence.

    Example: git pull origin
  • git push
  • Pushes all the modified local objects to the remote repository and advances its branches.

    Example: git push origin master
  • git remote
  • Shows all the remote versions of your repository.

    Example: git remote
    origin
  • git log
  • Shows a listing of commits on a branch including the corresponding details.

    Example: git log
    commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe
    Author: User
    Date:   Mon May 3 09:24:05 2010 +0300

        first commit
  • git show
  • Shows information about a git object.

    Example: git show
    commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe
    Author: User
    Date:   Mon May 3 09:24:05 2010 +0300

        first commit

    diff --git a/README b/README
    new file mode 100644
    index 0000000..e69de29
  • git ls-tree
  • Shows a tree object, including the mode and the name of each item and the SHA-1 value of the blob or the tree that it points to.

    Example: git ls-tree master^{tree}
    100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    README
  • git cat-file
  • Used to view the type of an object through the SHA-1 value.

    Example: git cat-file -t e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
    blob
  • git grep
  • Lets you search through your trees of content for words and phrases.

    Example: git grep "www.siteground.com" -- *.php
  • git diff
  • Generates patch files or statistics of differences between paths or files in your git repository, or your index or your working directory.

    Example: git diff
  • gitk
  • Graphical Tcl/Tk based interface to a local Git repository.

    Example: gitk
    Gitk
  • git instaweb
  • Runs a web server with an interface into your local repository and automatically directs a web browser to it.

    Example: git instaweb --httpd=webrick
    git instaweb --stop
    Git Instaweb
  • git archive
  • Creates a tar or zip file including the contents of a single tree from your repository.

    Example: git archive --format=zip master^ README >file.zip
  • git gc
  • Garbage collector for your repository. Optimizes your repository. Should be run occasionally.

    Example: git gc
    Counting objects: 7, done.
    Delta compression using up to 2 threads.
    Compressing objects: 100% (5/5), done.
    Writing objects: 100% (7/7), done.
    Total 7 (delta 1), reused 0 (delta 0)
  • git fsck
  • Does an integrity check of the Git file system, identifying corrupted objects.

    Example: git fsck
  • git prune
  • Removes objects that are no longer pointed to by any object in any reachable branch.

    Example: git prune

    GIT Directory structure

    A .git directory has a structure similar to the following one:
    • branches
    • A deprecated way to store shorthands that specify URL to the git fetch, git pull and git push commands is to store a file in branches/ and give the name to the command in the place of the repository argument.
    • COMMIT_EDITMSG
    • This is the last commit message. It’s not actually used by Git at all, but it is for your reference after you have made a commit.

      user@user:/GIT/test# cat COMMIT_EDITMSG
      first commit
    • config
    • This is the main Git configuration file. It keeps specific Git options for your project, such as your remotes, push configurations, tracking branches and more. Your configuration will be loaded first from this file, then from a ~/.gitconfig file and then from an /etc/gitconfig file, if they exist.

      A exemplary content of this file is:

      user@user:/GIT/test# cat config
      [core]
          repositoryformatversion = 0
          filemode = false
          bare = false
          logallrefupdates = true
      [remote "origin"]
          url = git@github.com:user/test.git
          fetch = +refs/heads/*:refs/remotes/origin/*
    • description
    • If you’re using gitweb or invoking git instaweb, this will show when you have viewed your repository or the list of all versioned repositories.
    • gitweb
    • A folder with the GIT web scripts. They allow you to browse the git repository using a web browser.
    • HEAD
    • This file holds a reference to the branch you are currently on. This tells Git what to use as the parent of your next commit:

      user@user:/GIT/test# cat HEAD
      ref: refs/heads/master
    • hooks/
    • This directory contains shell scripts that are invoked after the corresponding Git commands. For example, after you run a commit, Git will try to execute the post-commit script.
    • index
    • The Git index is used as a staging area between your working directory and your repository. You can use the index to build up a set of changes that you want to commit together. When you create a commit, what is committed is what is currently in the index, not what is in your working directory. It is a binary file containing a sorted list of path names, each with permissions and the SHA-1 of a blob object. Its content can be listed through:

      user@user:/GIT/test# git ls-files --stage
      100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0    README
    • info/
    • Contains additional information about the repository.
    • logs/
    • Keeps records of changes made to refs.
    • objects/
    • In this directory the data of your Git objects is stored – all the contents of the files you have ever checked in, your commits, trees and tag objects.
      The files are stored by their SHA-1 values. The first two characters are for the subdirectory and the next 38 are the filename.

      For example, if the SHA-1 for a blob we’ve checked in is a576fac355dd17e39fd2671b010e36299f713b4d
      the path to the corresponding file is:
      [GIT_DIR]/objects/a5/76fac355dd17e39fd2671b010e36299f713b4d
    • ORIG_HEAD
    • This is the previous state of HEAD.
    • packed-refs
    • The file consists of packed heads and tags. It is useful for an efficient repository access.
    • refs/
    • This directory normally contains three subfolders – heads, remotes and tags. There you will find the corresponding local branches, remote branches and tags files.
    For example, if you create a production branch, the file .git/refs/heads/production will be created and will contain the SHA-1 of the latest branch commit.

    courtsy: SiteGround

Monday

Getting Started with Nginx and PHP-FPM


Nginx, a HTTP and reverse proxy server, known for its blazing speed in serving static files, including grand performance in terms of serving up FastCGI pages makes for a great coupling with the upcoming PHP-FPM sapi in PHP (It is currently in the 5.3 branch and previously was a patch) offer a great solution for finally getting rid of that old sloppy mod_php in Apache. Do you have the same issue where your apache instances have started to run too large? This might be the time to start to move forward.
A little background on why I have endeavored on this path. While running Apache at several jobs and institutions it simply became clear that under heavier traffic loads having all of the ridged custom components of Apache were starting to slow things down. To the point of large instances running sometimes up to 300MB in size. Having this size just to get a simple image or PHP page was just not necessary as well as proved to be a performance bottleneck.
While the first point of departure is to utilize a faster web server to proxy the existing one, it still does not stand to the point of keeping everything to a single task. This is where the FastCGI instances of PHP started to come in very useful as well as having something to monitor the processes aka PHP-FPM.

Overview

This posting assumes that you are not operating under a shared hosting account and that you have some dedicated hardware, a virtual machine or something in the cloud. Currently I have just switched from a dedicated server at The Planet to their new cloud offering which reduced my immediate costs substantially (149 to 99 per month for essentially the same thing but even better – my disk now operates off of a SAN. To you cloud junkies, you know disk speed / IO wait time is a pain in the a**. This simply mitigates these types of issues). Which has allowed me to take some time to move over to Nginx and PHP-FPM without having to have reconfigured my existing machine to what I wanted.
Further, you must be comfortable in running everything under a single user account, this is not something for a shared hosting supplier (while you can accomplish this, it is not why I am writing this post since the security of which, is just terrible). Simply put, if you have a shared hosting account switch to a VM (cloud hosting is a VM) or a dedicated server. This may not make some of you happy to hear but there are generally a large amount of issues with shared hosting – mostly related to security. But that is another rant…. Let’s get started.

PHP-FPM

PHP-FPM is a FastCGI process manager for PHP, say goodbye to the spawn-fcgi script from Lighttpd. The PHP-FPM has additional features that can make it appealing for any crowd. To name a few:
  • Emergency restart in case of accidental opcode cache destruction
  • Enhancements to FastCGI, such as fastcgi_finish_request() – a special function to finish request & flush all data while continuing to do something time-consuming
  • Ability to start workers with different uid/gid/chroot/environment and different php.ini (replaces safe_mode) – Covered in the PHP-FPM workers documentation.
  • Adaptive process spawning
PHP-FPM has been available for quite some time, however, it is new to the PHP 5.3 branch (to date, it has not been in a packaged release from PHP). There is a bit of configuration that you will need to do with PHP in order to get up and running with PHP-FPM. If you are afraid of compiling or not using your package managers builds, then you may want to stop reading now.

PHP 5.3 + PHP-FPM Installation

Installing PHP 5.3 and including PHP-FPM is simple. I tend to statically compile many of my extensions into PHP to make maintenance more simple with multiple machines. However, this is not a necessity. To get going follow these instructions (if you already have PHP and want to maintain some of your current configuration do a php -i from the command line and grab the configure statement – excluding apache related configuration (aka apxs). I’ve removed most of my configuration from this tutorial. You may find that you need additional librarys installed (just install them through your package manager).
#!/bin/bash
wget http://us2.php.net/get/php-5.3.2.tar.gz/from/www.php.net/mirror
tar -zxf php-5.3.2.tar.gz
cd php-5.3.2
svn export http://svn.php.net/repository/php/php-src/branches/PHP_5_3/sapi/fpm sapi/fpm
rm -rf autom4te.cache
rm -f configure
./buildconf --force
./configure \
--with-layout=GNU \
--with-libdir=lib64 \
--enable-fpm \
--with-gd \
--enable-mbstring \
--enable-pcntl \
--enable-soap \
--enable-sockets \
--enable-sqlite-utf8 \
--enable-zip \
--with-zlib \
--with-curl \
--with-pcre \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-gettext \
--with-mcrypt \
--with-mysql \
--with-mysqli \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-tidy \
--with-pear \
--with-xml \
--disable-debug
make && make install

Add Your Init Script

Download the script from: http://svn.php.net/repository/php/php-src/branches/PHP_5_3/sapi/fpm/init.d.php-fpm.in
Now that you have the script, modify it to contain the correct paths. I am not going to do this here since if you’re running your own server or virtual machine I feel you should know to do such things :) .

Update Your Configuration

The default may run fine for you, however, it is always best to take a peak at what users are running, your port numbers and ensuring that all of your items are setup the way you want them. Look for php-fpm.conf under /etc/php-fpm.conf or /usr/local/etc/php-fpm.conf. Then update this file to your liking. Once coompleted, start it up: /etc/init.d/php-fpm start – if you have issues, ensure that your variables are correct and that the configuration lines up with the init script (aka pid file for instance).

Nginx

Installation

Most of your distributions support nginx. You can install this through your package manager, however, I prefer to have the most recent versions (and running a redhat based distro that generally requires installing from source or creating a package – there is no .spec file so I took the easy way out) therefore, I decided to install from source which is a simple task but yet I customized it a bit to exclude a few things I did not need.
Steps that you will need to follow (to some degree):
  1. Download Nginx (we’ll use stable for now)
  2. Extract the Package
  3. Configure, Make and Make Install
Just as a note, I generally save my configuration in a shell script to allow for easy upgrading. You may want to do the same as it will allow you to quickly upgrade rather than attempting to figure out what you’re configure script was.
wget http://nginx.org/download/nginx-0.7.xx.tar.gz #not the real link, find current stable release on 7.x series)
tar -zxf nginx-0.7.xx.tar.gz
cd nginx-0.7.xx
./configure --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module
make && make install
So that was easy, or did you take the cheater way out and just install the package (which most are at 0.6)? Either way, this should still work for you.
Many tutorials (especially the one that I looked at beginning to convert to nginx simply rely on a single index.php – not that it is an issue but be wary about these tutorials since many of them exhibit issues related to security (one of which, is the first result in Google on a search for: nginx wordpress rewrite). I’ll say a little more once we get into the Site portion of the configuration.

The Main Configuration

The main configuration file is generally in /etc/nginx/nginx.conf or /usr/local/nginx/conf/nginx.conf. Here we want to setup a few of our variables to improve the server itself. There is a great guide from Slicehost on nginx defaults, you may want to get started there. Now my configuration is based quite a bit off of the Slicehost configuration as well as a few additional changes when we get more into the PHP section. For reference here is my configuration.
user  www-data;
worker_processes  4;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 128;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
    error_log   /var/log/nginx/error.log   debug;
 
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     off;
 
    keepalive_timeout  2;
 
    gzip  on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
 
    include /usr/local/nginx/sites-enabled/*;
}
One thing to note, is that due to some of my domain name sizes, I needed to increase the variable “server_names_hash_bucket_size” to 128 instead of the current default 32.

Setting up FastCGI Variables

These will be needed in order for your installation to work, as well as, setting up your environmental variables that get sent to PHP. You will notice most of these parameters.
; /etc/nginx/fastcgi_params
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param  PATH_INFO $fastcgi_script_name;
 
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
 
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
 
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
Now in that same file, you may want to also include some additional fastcgi configuration. These will likely need to be tuned for your environment and may take a little trial and error until you find the best fit.
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

Setting up your First Site

Let’s configure a simple default site that just processes PHP scripts. No real hard trouble here! Create a new file in the sites-enabled directory called default.conf. We are going to have it parse our /var/www/default directory (if you do not have one, create one and add an index.php file of some sort in there). Here is a quick and simple configuration to get started:
server {
        listen 80 default;
 
        root   /var/www/default/public;
        index index.php;
 
        location ~ \.php$ {
                include /usr/local/nginx/conf/fastcgi_params;
                fastcgi_index index.php;
                if (-f $request_filename) {
                    fastcgi_pass 127.0.0.1:9000;
                }
        }
}
Note the location ~ \.php$, this tells us that for every PHP file to push it into fastcgi, now if you parse additional extensions through PHP, for the sake of everything, please add extensions to the location! Otherwise your precious PHP files can be downloaded. Yes, it is a serious thing!

What Not to Do

This configuration is evil and I actually found it on some tutorial but here it is, in it’s fully glory.
        # if the request starts with our frontcontroller, pass it on to fastcgi
        location ~ ^/index.php
        {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME /var/www/default/pub$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                include /usr/local/nginx/conf/fastcgi_params;
        }
See how it only pushes things to index.php? Yes, this means that no other PHP files will be parsed. WTF mate? Yes, so please do not utilize a configuration such as this unless you know the repercussions.

Non-Default Sites aka Virtual Hosts

Virtual hosts are extremely easy in Nginx, all you need to define is the server name (the same goes with subdomains). You can basically copy out the default.conf into a specific site and add in a “server_name” variable to the “sever” section followed by removing “default” from the port. You can add in aliases by simply putting a space in between the servers. Here is an example (I also cache local files for a period of 30 days since they will likely not change):
server {
        listen 80;
        server_name digitalstruct.com www.digitalstruct.com;
 
        location / {
                root   /var/www/digitalstruct.com/public;
                index index.php;
 
        }
 
        # serve static files directly
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
            access_log        off;
            expires           30d;
        }
 
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /var/www/digitalstruct.com/public$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                include /usr/local/nginx/conf/fastcgi_params;
        }
}
That’s just about all you need for now with Nginx, if you run into issues the nginx wiki has quite a large amount of links to get you started in the right direction with rewriting those old nasty apache rewrite rules.

Conclusion

While this posting was not as detailed as I was hoping it to be, I hope that you can see some of the items that I was attempting to address, specifically the PHP portion in ensuring that you are covering yourself when it comes to ensuring that people are not downloading your configuration. Also if you are running multiple virtual hosts, it is a good idea to ensure that they are running under separate users.
courtsy:Mike Willbanks

Python Kafka

  Developing Microservices with Python, REST API, Nginx, and Kafka (End-to-End) Here's a step-by-step guide to developing microservices...