====== Install Magento 2 on Simple Hosting ====== You can create e-commerce sites with Magento 2 on Simple Hosting instance types PHP 5.6 / MySQL 5.6 (Percona) and PHP 7 / MySQL 5.6 (Percona). This article offers a detailed guide on how to install and configure Magento Community Edition 2 in two ways (SFTP and SSH, or Composer, git and SSH) and supports you in the configuration of advanced settings such as Varnish Cache, background tasks, backups, using the Magento CLI and optimizing PHP. ===== Prerequisites ===== * A Gandi account (free) * A [[https://www.gandi.net/hosting/simple?language=php&db=mysql|PHP / MySQL instance]] (size M or higher recommended) * PHP 5.6 / MySQL 5.6 (Percona) or * PHP 7 / MySQL 5.6 (Percona) * A VHOST (Web address) already created for the domain of your site (for example, store.example.com) ([[:simple:vhost|see guide]]) * [[https://www.magentocommerce.com/products/customer/account/create/ |A Magento account]] (free) * [[https://marketplace.magento.com/customer/accessKeys/list/ |A Magneto "AccessKey"]] (free) * A database and a user / MySQL already created for your Magento website ([[:simple:mysql|see guide]]) ===== Method 1: copy the files via SFTP ===== ==== Download Magento 2 to your computer ==== Download the latest version of Magento 2 from the official website, choosing the format "tar.gz". You will be asked to register for free before you can download the file. https://www.magentocommerce.com/download You should now have a ".tar.gz" file on your computer. For example on Windows, ''C:\Users\Me\Downloads\Magento-CE-2.0.7-2016-05-24-12-15-40.tar.gz''. Or, on Linux ''~/Downloads/Magento-CE-2.0.7-2016-05-24-12-15-40.tar.gz''. ==== Access your instance via sFTP ==== Obtain the SFTP address from the control panel of your instance in your Gandi account. The address will look like : 123456789@sftp.dc2.gpaas.net Connect with your favorite SFTP client by entering the password of your body, or using your SSH key to identify you. You can read more about [[:simple:sftp | sFTP in our User guide for Simple Hosting]]. ==== Copy the files to your instance ==== Once connected, access inside the directory ''htdocs'' the VHOST corresponding to the domain chosen for Magento. We will always use ''store.example.com'' in the examples. For example: ''vhosts/store.example.com/htdocs''. Next, copy the file ''Magento-CE-2.0.7-2016-05-24-12-15-40.tar.gz'' to your instance with your SFTP client. You can then connect via the SSH console to unzip the file directly on the instance, as described below. Alternatively, you could unzip the file locally and upload the resulting files and folders, but it can take several hours due to the large number of files to copy (if you select this option, do not copy the "dev" directory to save you several extra hours). In exchange, you will not have to log in via SSH to unzip the file. ==== Decompress the file on your instance ==== Get the connection details to the SSH console of your body since its administration page on Gandi's website. Visit our guide on the [[:simple:console | SSH console for Simple Hosting]] for more information. me@laptop $ ssh 12345678@console.dc0.gpaas.net 12345678@console.dc0.gpaas.net's password: Asking for console, please wait Connected Grabbing terminal Ok If you have installed the [[https://github.com/Gandi/gandi.cli | Gandi CLI]], you can use the following command : $ gandi paas console {instance name} Once connected to the instance, change to the ''htdocs'' directory of the VHOST where you uploaded the file with Magento Community Edition 2. Decompress the file with the command ''tar'', and then delete the file. $ cd /srv/data/web/vhosts/store.example.com/htdocs $ tar xzf Magento-CE-2.0.7-2016-05-24-12-15-40.tar.gz $ rm Magento-CE-2.0.7-2016-05-24-12-15-40.tar.gz Decompression can take several minutes. Once complete, Magento 2 will be installed on your instance and you can switch to the configuration step described in the last section of this article. ===== Method 2: use Composer, git and SSH ===== To use [[:tutorials:simple:php:composer | Composer on Simple Hosting]], you manage the deployment with git+SSH. **We recommend that you install [[https://github.com/Gandi/gandi.cli | Gandi CLI]]** for your convenience: the Gandi CLI commands are very simple to use. Make sure you have the following PHP modules installed in order to perform the local installation of Magento (your git tree should include the file ''composer.lock'' resulting from a local installation to start the installation on the instance): # PHP5.6 php5 php5-gd php5-mcrypt php5-curl php5-intl php5-xsl # PHP7 php7.0 php7.0-gd php7.0-mcrypt php7.0-curl php7.0-intl php7.0-xsl If you have not yet installed Composer, you can [[:tutorials:simple:php:composer|follow our guide to learn how to install and use it]]. ==== Clone the VHOST ==== You can create the local directory and configure the git repository of the proceedings automatically by cloning the repository that was created automatically when creating the VHOST. $ gandi paas clone magento2 --vhost store.example.com The directory ''store.example.com'' is automatically created. Go ahead and change to this directory. cd store.example.com If you're not using the CLI tool, you can setup git repository using the following commands. $ mkdir store.example.com $ cd store.example.com/ $ git init $ git remote add gandi ssh+git://{login}@git.{datacenter_location}.gpaas.net/store.example.com.git $ mkdir htdocs ==== Install Magento with Composer ==== Use Composer to create a new Magento project within the ''htdocs'' directory. $ php composer.phar create-project --repository-url=https://repo.magento.com/ magento/project-community-edition htdocs You wil be prompted to authenticate with the "public key" (username) and the "private key" (password) obtained when creating your Access Key on the Magento site. ==== Deploy to your instance ==== Update your git tree and push your changes to your instance. $ git add . $ git commit -m 'Install Magento 2' $ git push gandi master $ gandi deploy If you're not using the Gandi CLI tool, substitute the 'gandi deploy' command with the following. $ ssh {login}@git.{datacenter_location}.gpaas.net 'deploy store.example.com.git' Once the process is complete, you can complete the configuration of Magento directly in your browser. ===== Complete the installation of Magento 2 from your browser ===== Once the files placed on your instance, you can access your domain from your browser to complete the installation of Magento 2. For example: http://store.example.com ==== Disable ''always_populate_raw_post_data'' on PHP 5.6 ==== On PHP 5.6 instances, you need to disable the PHP option ''always_populate_raw_post_data'' by assigning the value ''-1'' (by default it is set to '' 0 ''). This option is deprecated as of PHP 5.6 and no longer exists in PHP 7. Please consult the official documentation for more information. A file ''php-custom.ini'' is available on your body and you can change it from a web interface, via SFTP or SSH console. Please see our [[simple:php:phpini#comment_modifier_son_phpini | guide on modifying php.ini]] for more info. Via the SSH console, simply run the following command and then wait a few minutes for the Apache process to restart automatically. hosting-user@magento2:~$ echo "always_populate_raw_post_data=-1" >> /srv/data/etc/php/php-custom.ini You can also manually restart your instance from its administration interface on Gandi's website. Alternatively, with Gandi CLI, you can issue the following command ''$ gandi paas restart {instance name}''. Here is how the file ''php-custom.ini'' should look like with the option ''always_populate_raw_post_data'' deactivated, as required by Magento 2 : ; You can not override all parameters. ; We keep the possibility to filter other parameters. ; Use this file with caution. ; ; Vous ne pouvez pas remplacer tous les paramètres. ; Nous gardons la possibilité de filtrer d'autres paramètres. ; Utilisez ce fichier avec précaution. always_populate_raw_post_data=-1 Now your PHP 5.6 instance is ready for Magento 2, as will be confirmed during the environment test performed during the installation process. ==== Database configuration ==== You will be asked to insert the username and the database you want to use. If you do not yet created, you can do so now by following our [[:simple:mysql | database management guide]]. ==== Admin user configuration ==== You can also create the administrator of the store by entering a username, an email and a password. Make sure to use a strong password ! ===== Advanced options offered on Simple Hosting ===== Simple Hosting has a series of features that blend with Magento to get a quick and robust site easily: HTTPS support, background tasks with Anacron, the Varnish Cache and automatic snapshots. ==== HTTPS ==== You can enable HTTPS for Mangeto website if you have an instance of M or greater size. You can activate these options during installation at any time from the admin interface. To enable SSL on your Magento 2 site VHOST, simply add a certificate obtained at Gandi or another provider. You can learn more in our guide to[[:simple:ssl | Using SSL on Simple Hosting]]. With HTTPS, your users will be protected and your site will have better visibility. ==== PHP options (php.ini) ==== Magento 2 provides an example of php.ini file (''php.ini.sample'') which recommends some PHP settings for production use. We recommend that you edit the ''php-custom.ini'' file on your instance and add the following values : session.save_path="/srv/data/var/php/www" display_errors=Off The first option will be useful for the execution of certain Magento CLI commands. Then next option will conceal errors that potentially show sensitive information. You can edit the file ''php-custom.ini'' via the admin interface, SFTP or SSH console. Read our guide on php.ini modification for more information. In the SSH console, for example, you can use the following commands: : $ echo 'session.save_path="/srv/data/var/php/www"' >> /srv/data/etc/php/php-custom.ini $ echo 'display_errors=Off' >> /srv/data/etc/php/php-custom.ini ==== Using the Magento CLI ==== Magento 2 includes a CLI (Command Line Interface) that you can use via [[::en::simple::ssh|the SSH console of your instance]]. The Magento 2 CLI allows you to perform many maintenance tasks, like re-indexing the cache, run cron jobs, manage and restore backups, etc. Connect to your instance via SSH, navigate to your site and use the following command to list all available tasks: $ cd /srv/data/web/vhosts/store.example.com/htdocs $ php bin/magento list Certain commands may require specific PHP settings. For example, this is the case for the command ''setup:backup'', which is used to generate backups on demand. This command needs to use a writable path for the backup process. Since this is not a common practice in many PHP applications, you must set the option ''session.save_path'' manually. Otherwise, you would get a permission error. If you changed the ''php-custom.ini'' file as recommended in the section "PHP Options (php.ini)", you can use the following command when running CLI commands :: $ php -c "/srv/data/etc/php/php-custom.ini" bin/magento setup:backup --db To set an option temporarily on the fly, use ''php -d "option=value''. For example : $ php -d "session.save_path=/srv/data/var/php/www" bin/magento setup:backup --db You can also add the Magento 2''bin'' directory to your PATH and access the CLI using the command ''magento'' when you are connected to the SSH console. $ chmod +x /srv/data/web/vhosts/store.example.com/htdocs/bin/magento $ echo 'export PATH=$PATH:/srv/data/web/vhosts/store.example.com/htdocs/bin' >> /srv/data/etc/bash/bashrc $ source /srv/data/etc/bash/bashrc $ magento list Alternatively, or in addition, you can use the default custom PHP options with an ''Alias​''. For example : $ ini_path=/srv/data/etc/php/php-custom.ini $ cli_path=/srv/data/web/vhosts/store.example.com/htdocs/bin/magento $ echo "alias mage='php -c $ini_path $cli_path'" >> /srv/data/etc/bash/bashrc $ source /srv/data/etc/bash/bashrc $ mage list This technique also works with the ''-d'' flag described above. ==== Cron and background tasks with Anacron ==== You can run scripts in the background to keep Magento 2 updated: rebuild database indexes, send emails, automatically download updates... In the admin interface of your Magento site, go to "Stores > Configuration > Advanced > System > Cron Scheduled Tasks". We recommend starting with the following configuration : Cron configuration for group: index Generate Schedules Every: 60 Schedule Ahead for: 60 Missed if Not Run Within: 180 History Cleanup Every: 600 Success History Lifetime: 600 Failure History Lifetime: 600 Use Separate Process: Yes Cron configuration for group: default Generate Schedules Every: 60 Schedule Ahead for: 60 Missed if Not Run Within: 180 History Cleanup Every: 600 Success History Lifetime: 600 Failure History Lifetime: 600 Use Separate Process: No Next, configure [[::simple::anacron|Anacron]] to program the automatic execution of these tasks every hour. Via the SSH console, modify the file ''/srv/data/etc/cron/anacrontab'' (with vi, vim or nano) and add the following information (replace all references to "store.example.com" with the correct VHOST name) : MAGE_WWW=/srv/data/web/vhosts/store.example.com/htdocs PHP_OPT="-d session.save_path=/srv/data/var/php/www" # si vous avez modifié le fichier php-custom.ini, utilisez plutôt la ligne ci-dessous # PHP_OPT="-c /srv/data/etc/php/php-custom.ini" @hourly 0 mage.cron php $PHP_OPT $MAGE_WWW/bin/magento cron:run >> $MAGE_WWW/var/log/magento.cron.log @hourly 0 mage.update.cron php $PHP_OPT $MAGE_WWW/update/cron.php >> $MAGE_WWW/var/log/update.cron.log @hourly 0 mage.setup.cron php $PHP_OPT $MAGE_WWW/bin/magento setup:cron:run >> $MAGE_WWW/var/log/setup.cron.log With these definitions, Anacron will perform its routine tasks every hour, the shortest time interval supported by Anacron. You can perform this configuration via SFTP or SSH console. Visit our [[:simple:anacron|Anacron User Guide]] for more info. ==== Cron and background tasks with external provider ==== To perform background tasks more frequently, you can use an external service that calls the file ''pub/cron.php'' from the outside. If you opt for this method, we recommend that you secure access to this file. The secure access to ''cron.php'' from the web, store a strong password for the user ''hosting-user''. For this, use the tool ''htpasswd'' and use, for example, the following path ''/srv/data/home/.magento.cron.auth'' to store the file : $ htpasswd -c /srv/data/home/.magento.cron.auth hosting-user > Password: > Confirm password: Then, edit the file ''/srv/data/web/vhosts/store.example.com/htdocs/pub/.htaccess''. Locate and delete the following lines : ## Deny access to cron.php order allow,deny deny from all And replace them with the following instructions : AuthType Basic AuthName "Cron Authentication" AuthUserFile /srv/data/home/.magento.cron.auth Require valid-user You can now access http://store.example.com/pub/cron.php and authenticate with the username and password you created earlier. With curl, for example : $ curl --user hosting-user:{mot-de-passe} http://store.example.com/pub/cron.php?group=default ==== Varnish Cache ==== Magento 2 recommends using Varnish caching software for use in production. The Simple Hosting platform offers a Varnish cache system adapted to the [[:simple:cache|Web Accelerator]], that can be used natively with Magento 2. In the admin interface of your Magento website, navigate to "Stores > Configuration > Advanced > System > Full Page Cache". Select "Varnish Caching" in the field "Caching Application", and set the time period (TTL) for the cache, in secondes. Magento uses 86400 seconds (24 hours) by default. Save your changes, and now your Magento 2 site will no longer use the CPU, RAM, and storage of your instance to manage the cache system, but instead the Web Accelerator. In fact, it is already loaded by default, but your shop does not know about the Varnish cache yet and generated its own local cache, for nothing. You can consult the Web Accelerator cache system documentation to learn more. Also note that you can "purge" (empty) the cache from your instance control panel. ==== Snapshots and automatic backups ==== Magento 2 offers several options to make automatic backups of your site. Simple Hosting offers [[:simple:snapshots | automatic snapshots]] to safeguard your files (provided that you have subscribed to this option) and allows you to restore your instance to a previous state, with the exeption of the database. With the snapshot feature enabled, you just need to activate the automatic backup of databases in Magento for all of your sites to be effectively backed up. To enable automatic backups of the database, go to the admin interface of your Magento website and navigate to "Stores > Configuration > Advanced > System > Scheduled Backup Settings". In this section, select "Yes" to "Enable Scheduled Backup" and then "Database" for "Backup Type". Adjust the other preferences if necessary (the default options can be sufficient), then save. suffisantes), puis sauvegardez. If you ever need to use your backups, you can [[en:simple:snapshot|restore a Snapshot]] from the management interface of your instance. Then, recreate your database and user using the same data as before, and then re-import the database with the Magento 2 CLI. Connect to your instance via SSH console, identify the last backup of the database and make the "rollback" to restore the contents of the database : $ cd /srv/data/web/vhosts/store.example.com/htdocs $ php bin/magento info:backups:list $ php bin/magento setup:rollback -d "1440611839_db.tgz" A section dedicated to the use of the Magento 2 CLI is available above.