Deploying an SSD Magento 2 Cloud Server in 55 seconds

·

·

If you already meet all requirements such as PHP 5.5/5.6 (or 7) and MySQL 5.6, installing Magento 2 on your local machine with composer should take between 15 and 30 minutes, depending on your Internet connection. With Digital Ocean you can one-click install and deploy Magento to an SSD cloud server in 55 seconds, which made me curious and so I set up a Magento 2 demo store (link might only work for a limited time), that our clients could also use to test drive Magento 2 both in the frontend and backend.

Creating a Magento Droplet

I chose the most popular $20/month plan with 2GB RAM, 2 CPUs, 40 GB SSD disk and 3TB of transfer. 2 GB RAM is not too much you might think, especially knowing that Magento 2 might need even more resources than Magento 1, but the backend navigation was pretty slick right from the beginning, the login only takes 2 seconds. After enabling all Magento caches I was surprised how fast also the frontend was. And even if you had to double those resources every small merchant would still be able to afford that kind of hosting, wouldn’t he?

I selected an Ubuntu image, because I mostly worked with Debian and Ubuntu distributions in the past, and picked the nearest datacenter to Switzerland which is located in Frankfurt/Germany. I added my SSH public key and within 55 seconds (or less?) the server instance was available and an empty Magento 2 store could be directly accessed via IP address.

Installing Magento Sample Data

Installing sample data using Composer is well documented, so it was easy to find and execute the following commands:

cd /var/www/html/magento/
php bin/magento sampledata:deploy
php bin/magento setup:upgrade

When I went back to the frontend to check if sample data would show up, I was greeted with

An error has happened during application run. See exception log for details.

instead. A quick look at /var/www/html/magento/var/log/exception.log revealed a permission problem

'Magento\Framework\Exception\LocalizedException' with message 'Can't create directory /var/www/html/magento/var/generation/Magento/Framework/App/Response/Http/.' in /var/www/html/magento/vendor/magento/framework/Code/Generator.php:103

which could be fixed by resetting file and directory permissions to the group magento and user www-data like so

chown -R magento:www-data /var/www/html/magento/var/

Running the Magento CLI as root might not be such a good idea after all, but if you remember to adjust permissions afterwards you should be fine.

Magento 2 Modes

As Magento 2 is running in default mode after installation I wanted to switch to production mode to make it even faster. The Magento 2 documentation also has a nice overview of the different modes which is included at the end of this posting. My first attempt in enabling production mode with

php bin/magento deploy:mode:set production

ended in a Server Error 500 both in the frontend and backend. I didn’t have the time to look deeper into this, but once I have, I will update this post with the solution. For the time being even in default or developer mode, this demo store is pretty fast. I hope this little introduction was helpful to anyone out there.

Have fun with Magento 2!

Magento Developer Mode

You should run the Magento software in developer mode when you’re extending or customizing it.

In developer mode:

  • Static view files are not cached; they are written to the Magento pub/static directory every time they’re called
  • Uncaught exceptions display in the browser
  • System logging in var/report is verbose
  • An exception is thrown in the error handler, rather than being logged
  • An exception is thrown when an event subscriber cannot be invoked

For more information, see Set the Magento mode.

Magento Default Mode

As its name implies, default mode is how the Magento software operates if no other mode is specified. Default mode enables you to deploy the Magento application on a single server without changing any settings. However, default mode is not optimized for production.

To deploy the Magento application on more than one server or to optimize it for production, change to one of the other modes.

In default mode:

  • Errors are logged to the file reports at server, and never shown to a user
  • Static view files are cached
  • Default mode is not optimized for a production environment, primarily because of the adverse performance impact of static files being cached rather than materialized. In other words, creating static files and caching them has a greater performance impact than generating them using the static files creation tool.

For more information, see Set the Magento mode.

Magento Production Mode

You should run the Magento software in production mode when it’s deployed to a production server. After optimizing the server environment (database, web server, and so on), you should run the static view files deployment tool to write static view files to the Magento pub/static directory.

This improves performance because static files don’t go through the fallback mechanism; instead, URLs for static files are created as needed.

In production mode:

  • Static view files are not materialized, and URLs for them are composed on the fly without going through the fallback mechanism. (Static view files are served from cache only.)
  • Errors are logged to the file system and are never displayed to the user

(Picture of Frankfurt Skyline and Eiserner Steg courtesy of Andre Douque)