magento2

Crazy Magento 2 core patches

Last year I took over a maintenance of the Magento 2 Commerce site. One of first tasks was adjusting the site to meet industry standards as it was a bit messy. The whole content of vendor/ directory was kept in the repository and there were about 30 patch files placed in the site root, without any information which were applied and in which order. Most of them looked as provided by M2 support. I needed to clean this up – to figure out which patches are applied, in which order they are applied and finally find a way allowing me to remove vendor/ from the repository and to be able to apply patches dynamically, during the deployment.

This was a challenging task – I needed to keep comparing the vendor/ contents with the version of this directory coming from clean Magento 2 site, and then keep applying patches until directories were the same. I was able to do this, it proved that about 25 of these patches were really applied.

Recently I wrote a blog post explaining how to deal with the second part of this challenge – check Rocket Web blog to see how to apply Magento 2 core patches.

How to prepare Magento 2 beta package for offline use

Magento 2 comes with a composer installer and all external dependencies including sample data are being installed using composer. However, I needed to have a simple way to install Magento 2 along with sample data in an offline environment, without using composer. I had a few reasons to do this – I wanted to have a fast way to install Magento 2 multiple times and I wanted to test command line installing for MageTesting.com purposes.

Main goals are:

  • avoid downloading more than 1 GB of data each time
  • let it work in offline mode
  • operate with smaller packages
  • simplify steps needed to install Magento 2

Cloning GIT repository and downloading dependencies resulted in downloading more than 1GB of data:

  • Magento 2 code cloned with packages downloaded using composer: 471.3 MB (194 MB after gzipping)
  • sample data media: 590.9 MB (zipped)
  • sample data code: 0.2 MB (zipped)

I decided to prepare a Magento 2 package which contains only code needed to run application and to prepare sample data package which could be installed just by copy pasting that into Magento 2. Recently I was playing with a sample data compression script provided by Vinai Kopp, and I made a fork which can compress Magento 2 sample data.

At the end I have the following packages:

  • Magento 2 code (26 MB, gzipped)
  • compressed sample data media (92MB, zipped)
  • sample data code: 0.2 MB (zipped)

I know there is a composer cache. I know I could use Vagrant/Docker or other virtualization, but still I wanted to avoid overcomplicating the process. If you see that use case useful, please find all needed steps described below.

Just keep in mind it is written for 0.42.0-beta1 release of Magento 2 and it is not a recommended way to install Magento 2.

Prepare Magento 2 package

1. Clone GIT repository

git clone git@github.com:magento/magento2.git

2. Install composer dependencies

composer.phar update

3. Remove huge directories not needed to run application

rm -rf dev/tests
rm -rf .git
rm -rf vendor/magento/zendframework1/documentation
rm -rf vendor/magento/zendframework1/tests
rm -rf vendor/magento/zendframework1/demos

4. Prepare package

tar czf magento2-0.42.0-beta1.tar.gz -C magento2/ .

Prepare Magento 2 sample data package

1. Downlod demo data

curl -O http://packages.magento.com/_packages/magento_sample-data-0.42.0-beta1.zip
curl -O http://packages.magento.com/_packages/magento_sample-data-media-0.42.0-beta1.zip

2. Compress demo data

compress-sample-data-magento2.sh magento_sample-data-media-0.42.0-beta1.zip

Install Magento 2 using created package

1. Prepare directory and unpack package:

mkdir magento2
tar xzf magento2-0.42.0-beta1.tar.gz -C magento2/

2. Set required permissions

cd magento2
chmod -R 777 var/
chmod -R 777 pub/media/
chmod -R 777 pub/static
chmod -R 777 app/etc/

3. Run Setup

php -f setup/index.php install 
    --base_url=http://local.magento2new.com/ 
    --backend_frontname=admin 
    --db_host=127.0.0.1 
    --db_name=mage2 
    --db_user=mage2 
    --db_pass=mage2 
    --admin_firstname=John 
    --admin_lastname=Doe 
    --admin_email=john@example.com 
    --admin_username=admin 
    --admin_password=admin 
    --language=en_US 
    --currency=USD 
    --timezone=Europe/Warsaw

Install sample data using package

1. Unpack media sample data

unzip -q -d pub/media/ compressed-magento_sample-data-media-0.42.0-beta1.zip

2. Unpack sample data code

mkdir dev/tools/Magento/Tools/SampleData
unzip -q -d dev/tools/Magento/Tools/SampleData magento_sample-data-0.42.0-beta1.zip

3. Install sample data

php -f dev/tools/Magento/Tools/SampleData/install.php -- --admin_username=admin

4. Make sure newly added files are writable:

chmod -R 777 pub/media/
chmod -R 777 pub/static

This one is a little dirty, but as far as I know composer doesn’t support installing local packages.

Let me know if you find this article useful and if you have any thoughts around that.