How-To Install Magento 2 on OpenShift

MAgento 2If you are wondering how to try and install Magento 2 on a free hosting provider, keep reading. This is how we managed to deploy Magento 2 to Openshift without spending a penny!

This guide assumes you know OpenShift and have some basic experience with it, otherwise head to www.openshift.com, create an account and do some reading and basic tests.

Note that at the end of this post you’ll have the link to a repository that is almost ready to spin up a Magento 2 instance, but if you want to be aware of the caveats we’ll recommend you to keep on reading.

To begin with, we have the following “out of the box” issues:

  • Magento 2 requires PHP 5.5+ and OpenShift currently has official cartridges of up to PHP 5.4
  • Magento 2 requires shit lot of space, and OpenShift free gears are limited to 1GB
  • Magento 2 requires MySQL 5.6+ and OpenShift currently supports up to MySQL 5.5 official cartridges

To solve the PHP version issue, we firstly tried a community cartridge that installs Nginx and PHP 5.5/5.6. The cartridge itself worked flawlessly, but unfortunately then we run out space while running composer install after deploying the Magento 2 repository. We searched for something else and tried to run Magento 2 in PHP 5.4 (it was a bad idea btw). Eventually, we had to come back to that cartridge, as it seemed to most straightforward way to go. There doesn’t seem to be much options out there to install PHP 5.4+ in OpenShift without manually downloading the source code and compiling it and that would be beyond the scope of our purpose.

Moving on to the next issue, the out of space problems: While deploying Magento 2 to OpenShift, you almost run out of space, and yet you have to run `composer install` to fetch all the dependencies, and there are quite a few. While running `composer install –no-dev`, it would fail at half way, showing an error: Disk quota usage exceeded. We tried clearing composer cached files to free up some space but still wasn’t enough.

After a lot of back and forth, we managed to install all the dependencies without running out of space, but at the price of deleting the rollback deployments ($OPENSHIFT_DEPLOYMENTS_DIR). Note that this is not recommended at all, as you are actually loosing many of the benefits of using OpenShift, but if you ever need to make some extra space, you can run the following command in the OpenShift instance (We actually added that command to the `post_deploy` action_hooks script (file .openshift/action_hooks/post_deploy of your app’s repository):

rm -fr $OPENSHIFT_DEPLOYMENTS_DIR/*/repo/

Now, we had enough space available to install the dependencies, but it is required to have an API KEY and an API SECRET to be able to fetch them from the Magento Repository. You can get your own key pair for free by following this guide (basically create an account in Magento, and click on Generate Keys inside the Developers section after logging in). In order to keep the keys outside the source code, we opted to set them as environment variables (API_KEY and API_SECRET) while starting the new app. You’ll see how to do this further down.

Last, but not least, the issue with the MySQL version. After some research we ended up with the conclusion that it will not be easy at all to install MySQL on OpenShift, not to mention that we were already struggling with the disk space available. That’s why we decided that, for the shake of the experiment, we would try to make Magento 2 run over MySQL 5.5. After all, it wouldn’t be that different, would it? It turns out it is actually quite different. We were expecting to get away with this by just amending the check for the MySQL version in the Magento code, but that wasn’t enough. While running the installer, it would crash while creating the tables due some incompatible definitions. It took a bit of hacking that we are not proud of, which basically allowed us to install it by doing the following changes:

  • Removing the checks for the MySQL version
  • Amending a few table definitions that were not compatible with MySQL5.5:
    • The first error was: “There can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause”. In order to fix it we basically got rid of the CURRENT_TIMESTAMP that was being added to the updated_at columns.
    • The second error was: “The used table type doesn’t support FULLTEXT indexes”. To fix it we got rid of the FULLTEXT indexes on the InnoDB tables.

After the above changes, we managed to install Magento 2 successfully. However, as a result of this, the database and the app’s behaviour might (and will) not be stable, mostly in regards of search functionality, but also there will be a few datetime columns that will not be updated accordingly after data updates. Hopefully, all of this will not be needed soon as OpenShift provides an official MySQL 5.6 cartridge.

The “good news” are that our repository automatically provides the database schema that we managed to generate (which is compatible with mysql 5.5), so the source code of Magento 2 is not modified at all, as the changes were only required for the installation stage.

After this, we had a few permissions issues, and we had to add the config.php and env.php files (generated by the installation) to the repository with a default config and the database credentials taken from the environment variables. As Magento 2 doesn’t use local.xml anymore, it was fairly easy to replace the hardcoded database credentials on the env.php file by getenv(‘OPENSHIFT_MYSQL_DB_USERNAME’), etc. and it just worked. This is very convenient as well because you don’t have to make any changes on this files for it to work out of the box in your app, as it will get the database credentials from OpenShift provided environment variables.

Ok, enough talking. We’ve put all this together in a repository for you, so now you can almost almost plug & play it by following the below steps.

How to spot a new OpenShift app with Magento 2

In order to start a new app, all you need to have is:

Then, open a termial and run the below commands:

# Create a new OpenShift app with NGINX as web server
# Replace "123" by your actual api key & secrets and $myapp with your preferred app name.
# Write down the "Git Remote" ssh url that will be shown once the command finishes.
rhc create-app $myapp http://cartreflect-claytondev.rhcloud.com/github/boekkooi/openshift-cartridge-nginx API_KEY=123 API_SECRET=123
# Install PHP 5.6 cartridge into the app
rhc cartridge add -a $myapp http://cartreflect-claytondev.rhcloud.com/github/boekkooi/openshift-cartridge-php
# Install mysql-5.5 cartridge into the app
rhc cartridge add -a $myapp mysql-5.5
# Clone our repository and push it to your app, we will take care of everything for you :)
git clone https://github.com/javilumbrales/magento2-openshift
cd magento2-openshift
# Remember to replace $myapp by your app's name and YOUR_GIT_REMOTE by your actual repository url, the one that you got when you created the app (ie. should be something like ssh://*******@magento2-mage2.rhcloud.com/~/git/yourappname.git/)
git remote add $myapp YOUR_GIT_REMOTE
git push $myapp master -f

This whole process will take a while, but you don’t have to do anything other than copy and paste the commands and wait. Hopefully, you’ll see the following success message at the end:

remote: Deployment completed with status: success

Done! You should now be able to browse your Magento 2 store on http://$myapp-$mynamespace.rhcloud.com/.

Probably the statics will be missing, and in order to fix that SSH into your app (`rhc ssh $myapp`) and run the below command to deploy the statics:

php $OPENSHIFT_REPO_DIR/public/bin/magento setup:static-content:deploy

Note that you might need to run it a few times, as, for some reason that we couldn’t figure out, sometimes it just crashes. Retrying a few times seems to do the trick though.

Eventually, you should see at the end of the execution the following message:

New version of deployed files: XXXXXXXX

And that’s it! Now your Magento 2 app should be completely up and running! The admin is available on http://$myapp-$mynamespace.rhcloud.com/admin123 with the credentials: admin / OpenShiftAdmin123.

Good luck!

Conclusions

It was a quite painful process, and it took several hacks to get Magento 2 running on OpenShift, but mostly due the outdated official cartridges for both PHP and MySQL. Hopefully, they’ll be updated soon and we should be able to make this process much easier and quicker.

After all, the app seems to perform very well in terms of speed, and we haven’t spend any time optimising any settings. So it certainly looks that OpenShift might potentially be a very interesting option to host a Magento 2 store. As per today, this is only on an experimental stage, to say the least, but it was a very challenging and interesting task to find our ways around the many limitations that we faced, and we finally did it! (well, kind of :)).

Troubleshooting

If you get this error while deploying the app:

remote: [Composer\Downloader\TransportException]
remote: The ‘https://repo.magento.com/archives/magento/composer/magento-composer-1.0.2.0.zip’ URL required authentication.
remote: You must be using the interactive console to authenticate

Make sure that your API_KEY and API_SECRET env variables are set with the proper values. You can re-set them with the following command:

rhc set-env API_KEY=ENTER_YOUR_API_KEY_HERE API_SECRET=ENTER_YOUR_API_SECRET_HERE -a $myapp

Demo

Finally, if you just want to have a look at the resulst, you can find here our Magento 2 app running on OpenShift: http://demo-mage2.rhcloud.com/

You can also login to the admin on http://demo-mage2.rhcloud.com/index.php/admin_1ep725/ (User: demo Pass: demo123)

Show me the code!

10 thoughts on “How-To Install Magento 2 on OpenShift

  1. exec. git remote add $myapp YOUR_GIT_REMOTE
    fatal: Not a git repository (or any parent up to mount point /var/lib/openshift)
    Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

    what can i do ?

    • You have to replace $myapp by your app’s name and YOUR_GIT_REMOTE by the Git Remote url that you’ll be provided when you run the first command (create the app). Should be something like this:
      git remote add magentoapp ssh://*******@magento2-mage2.rhcloud.com/~/git/magento2.git/

      Please, let me know if the above doesn’t work.

      • Thank you very much.
        It is well.
        I can down this statement…
        When the finally, the css style path is not correct.
        So, I check the path…
        I found, “..pub/static/frontend/Magento/…” do not have “/luma”.
        Only have “/blank”
        and …
        I don’t know how 2 change it.

        • Try to deploy the statics again (php $OPENSHIFT_REPO_DIR/public/bin/magento setup:static-content:deploy), I’ve noticed that it’s crashing due an out of memory in php, and have deployed a fix for it on my repo. If you create a new app from scratch it should work fine and deploy the statics as part of the deployment. By they way, I’m in about to create a second part of this post showing how to deploy Magento 2 on OpenShift with MySQL 5.7 instead of hacking with MySQL 5.5, stay tunned!

  2. Pingback: How-To Install Magento 2 on OpenShift - II • The Developer World Is Yours

  3. Hi Team,

    Thanks for your efforts to wrap everything in one solution. I see the comment dates from December 2015 till now there is no new way to install magento 2.0.5 over openshift ?

    I have gone through the steps many times faced lots of issues managed them all except one final issue dont know have to make it happen.

    in the last command: git push $myapp master -f

    I received the below error after restarting all the cartridges
    remote: install [–prefer-source] [–prefer-dist] [–dry-run] [–dev] [–no-dev] [–no-custom-installers] [–no-autoloader] [–no-scripts] [
    –no-progress] [-v|vv|vvv|–verbose] [-o|–optimize-autoloader] [-a|–classmap-authoritative] [–ignore-platform-reqs] [–] []…
    remote:
    remote: Autoload error: Vendor autoload is not found. Please run ‘composer install’ under application root directory.ln: creating symbolic l
    ink `/var/lib/openshift/9x9x9x9x9x99x9x9x/app-root/runtime/repo//public/pub/static/adminhtml/Magento/backend/en_GB’: No such file or
    directory
    remote: Autoload error: Vendor autoload is not found. Please run ‘composer install’ under application root directory.———————–

    I believe composer install command needs to be run over the magento 2.0.5 in the openshift servers but I`m not sure what command to use.

    hopefully you could help here.

    Thanks,
    MR

    • Hi MR,
      Haven’t tried recently so not sure if there’s an easy way, I’m afraid there won’t be unless they’ve upgraded PHP and MySQL out of the box cartridges. Anyway, theres an slightly more recent post where we provide a script that kind of automates the whole process, have you tried it?
      You are right, composer install needs to be run on the server as part of the install, but it should be running automatically as part of the deployment, so don’t need to do anything. Sometimes the instances can get messy after failed deployments, so perhaps kill it and spin up a new one. Try this, and also check the other post, and let us know if it works, else send us the whole output of the commands you are running and hopefully we’ll be able to help you.

    • I’m afraid there’s nothing much you can do, other than purchase extra storage, maybe try to delete some unused files, such as template files or an unused theme or something similar, but it will be very tricky to fit all the sample data with only 1GB of storage. With the free gear you cannot do much other than the proof of concept with a super slim installation and an almost empty database.

  4. Thanks for sharing this awesome article about installing Magento, I was having an issue while installing but your post helped me a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *