Jump toUpdate content

Deploying Magento Open Source on a Scaleway Dedibox

Reviewed on 02 July 2020 • Published on 17 June 2020
  • compute
  • dedicated
  • server
  • magento
  • e-commerce
  • onlineshop

Magento is one of the most popular e-commerce systems available, with more than 100.000 online stores based on the platform. It is written in PHP and uses the Zend Framework. Database-wise it uses the MySQL or MariaDB relational database system.

The software is currently owned by Adobe, and is available in a free, community-driven, open-source version which is distributed under the Open Software License (OSL) v3.0, an Open Source Initiative approved license. A commercial version of the software is also available and provides advanced support and additional features.

In this tutorial you will learn how to install the Open Source version of Magento on a Scaleway Dedibox dedicated server running on Ubuntu Focal Fossa (20.04 LTS). To accomplish this mission we will deploy a LAMP stack (Ubuntu Linux, Apache2, MariaDB, and PHP) to install Magento 2 Open Source Edition on your Dedibox.

Requirements:

Installing your Dedibox

  1. Select your Dedibox for server from the server list and click the Install button:

  2. Click Server Distributions on the operating systems selection:

  3. Select Ubuntu 20.04_LTS-server in the list of available operating system versions. Then click Install Ubuntu.

  4. Choose the disk partitioning for your server and validate the configuration. In our example we use a large /data partiton which will be used to store the MariaDB databases as well as the Magento application and related files:

  5. Configure the server’s hostname, user and user password. If you have uploaded one or several SSH keys you choose the ones having access to the server. Configure the alert options and click Next Step.

  6. A summary of your configuration displays. Confirm it to launch the installation of your Dedibox dedicated server. The installation can take up to one hour, you will be redirected to your server’s status page once it has completed.

Preparing the server

  1. Connect to your Dedibox server using the regular user, created during the installation:

    ssh user@your_server_ip
  2. Update the apt package manager and upgrade the software installed on the server to ensure your system is up to date:

    sudo apt update && sudo apt upgrade -y
  3. Magento relies on a webserver to deliver content. Install the Apache2 web server using the apt package manager with the following command:

    sudo apt install apache2
  4. Disable directory listing in the Apache2 configuration to avoid the content of your web directories from being publically listed:

    sudo sed -i "s/Options Indexes FollowSymLinks/Options FollowSymLinks/" /etc/apache2/apache2.conf
  5. Restart the Apache2 web server to activate the updated configuration:

    sudo systemctl restart apache2.service
  6. Then install the required PHP packages with the apt package manager:

    sudo apt install -y php7.4-common php7.4-curl php7.4-fpm php7.4-mysql php7.4-opcache php7.4-xml php7.4-gd php7.4-dev php7.4-intl php7.4-mbstring php7.4-bcmath php7.4-json php7.4-soap php7.4-zip libapache2-mod-php7.4 unzip

Setting up MariaDB

  1. Install the MariaDB database server and client using the apt package manager. MariaDB is an open-source fork of the MySQL relational database system and provides compatibility with all MySQL commands. Magento has been tested to comply with all MariaDB feaures.

    sudo apt install mariadb-server mariadb-client
  2. Initialize the MariaDB database server by running the following command:

    sudo mysql_secure_installation

    You will be prompted to answer the following questions during installation:

    • Enter current password for root (enter for none): Press enter to continue, as the default MariaDB root password is empty.
    • Set root password? [Y/n]: Type Y and press enter to continue.
    • New password: Enter a secret password of your choice and press enter to continue.
    • Re-enter new password: Repeat your password and press enter to confirm.
    • Remove anonymous users? [Y/n]: Type Y, then press enter
    • Disallow root login remotely? [Y/n]: Type Y and confirm by pressing enter.
    • Remove test database and access to it? [Y/n]: Type Y and press enter to continue.
    • Reload privilege table now? [Y/n]: Confirm by typing Y, then press enter.
  3. Now that the database engine is configured, log into MariaDB using the root user and enter the password set in the previous step when prompted:

    sudo mysql
  4. Create new database for magento by running the following command in the MariaDB shell:

    CREATE DATABASE magento;
  5. Create a user for magento. Replace your_secret_password with a password for the user to authenticate:

    CREATE USER 'magento_db'@'localhost' IDENTIFIED BY 'your_secret_password';
  6. Grant the newly created user all permissons on the magento database:

    GRANT ALL ON magento.* TO 'magento_db'@'localhost' IDENTIFIED BY 'your_secret_password' WITH GRANT OPTION;
  7. Save the changes and quit the MariaDB shell:

    FLUSH PRIVILEGES;EXIT;

Setting up PHP

As Magento has specific requirements for PHP, it is recommended to modify the PHP configuration file.

  1. Open the file in a text editor, for example nano:

    sudo nano /etc/php/7.4/apache2/php.ini
  2. Modify parameters as indicated below:

    file_uploads = Onallow_url_fopen = Onshort_open_tag = Onmemory_limit = 4096Mcgi.fix_pathinfo = 0opcache.save_comments = 1upload_max_filesize = 150Mmax_execution_time = 1800realpath_cache_size=10Mrealpath_cache_ttl=7200date.timezone = Europe/Paris
  3. Install curl and git which are required in a later step during the installation of Magento:

sudo apt install -y curl git

Moving MySQL Databases to the /data partition

As our server uses a large /data partition we will move the MariaDB databases to this partition to separate Magento application data from system files.

  1. Create a new directory for MySQL on the /data partition:

    mkdir /data/mysql
  2. Set the ownership of the new directory to match the existing one:

    chown --reference=/var/lib/mysql /data/mysql
  3. Set the permissions on the new directory to match the existing one:

    chmod --reference=/var/lib/mysql /data/mysql
  4. Stop the MySQL service before copying files to the new directory:

    service mysql stop
  5. Copy all files from the MySQL data directory to the new one while retaining file permissions (-p)

    cp -rp /var/lib/mysql/* /data/mysql

    Open the file /etc/mysql/mariadb.conf.d/50-server.cnf in a text editor and modify the line datadir as shown in the following example:

    ## * Basic Settings#user            = mysqlpid-file        = /var/run/mysqld/mysqld.pidsocket          = /var/run/mysqld/mysqld.sockport            = 3306basedir         = /usr#datadir         = /var/lib/mysqldatadir         = /data/mysqltmpdir          = /tmplc-messages-dir = /usr/share/mysqlskip-external-locking
  6. Restart the MariaDB service:

    service mysql start

Downloading and installing Elasticsearch

Starting with Version 2.4 Magento uses Elasticsearch as the catalog search engine. Install it by following these steps:

  1. Start by importing the Elasticsearch PGP key:

    wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
  2. Elasticserach uses a secure HTTPS transaction for its repositories. If not installed yet, install the apt-transport-https package use the apt package manager:

    sudo apt-get install apt-transport-https
  3. Create a new sources file for the Elasticsearch repository:

    echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-7.x.list
  4. Update the apt package manager and install Elasticsearch using it:

    sudo apt-get update && sudo apt-get install elasticsearch
  5. Restart the Elasticsearch service to make sure it is up and running:

    sudo service elasticsearch restart

Downloading and installing Magento Open Source

Magento will be installed using composer. Magento supports Composer version 1.x and we will use the version 1.10.17 in this tutorial.

Note:

A free Magento account is required to download Magento Open Source. You can generate Magento Access Key before downloading the application.

  1. Install the application with the following command.

    curl -sS https://getcomposer.org/installer | sudo php -- --version=1.10.17 --install-dir=/usr/local/bin --filename=composer
  2. Create and change into a new directory for Magento.

    sudo mkdir -p /data/www/magento && chown -R www-data:www-data /data/www/ && cd /data/www
  3. Install Magento Open Source using PHP composer:

    Note:

    This step may take some time. This is normal behavior and you can wait for its completion. During the execution of the command you may see some messages about the cache, but you can safely ignore them.

    sudo -u www-data composer create-project --repository=https://repo.magento.com/ magento/project-community-edition /data/www/magento

    When prompted enter the Public Key of your Magento token as username and the Private Key as password:

        Authentication required (repo.magento.com):      Username: <MAGENTO_PUBLIC_KEY>      Password: <MAGENTO_PRIVATE_KEY>
  4. Set the file permissions for Magento:

    cd /data/www/magentofind var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +chown -R :www-data . # Ubuntuchmod u+x bin/magento
  5. Run the Magento setup from the command line by using the following command. Set the values for --base-url to your domain name and --db-password to your magento_db MariaDB user password. Also set the parameters for --admin-email and --admin-password for your admin account.

    sudo -u www-data bin/magento setup:install \--base-url=https://my-ecommerce-site.com/ \--db-host=localhost \--db-name=magento \--db-user=magento_db \--db-password=<secret_password> \--admin-firstname=admin \--admin-lastname=admin \--admin-email=admin@admin.com \--admin-user=admin \--admin-password=admin123 \--language=en_US \--currency=EUR \--timezone=Europe/Paris \--use-rewrites=1 \--elasticsearch-host=127.0.0.1 \--elasticsearch-port=9200
  6. Once the installation is complete, the admin url displays. For security reasons a random string is generated on each installation to hide the admin interface:

    [Progress: 1270 / 1270][SUCCESS]: Magento installation complete.[SUCCESS]: Magento Admin URI: /admin_sefio2Nothing to import.
    Note:

    Take a note of the Magento Admin URI, as you will need it later to connect to your admin interface from http://<your_magento_domain>/<admin_uri>.

Configuring Apache

  1. Open a new configuration file for Magento in a text editor of your choice, in our example we use nano:

    sudo nano /etc/apache2/sites-available/magento.conf
  2. Copy the following configuration in the file. Replace the domain name my-ecommerce-site.com with your own before saving the file and quitting the text editor:

    <VirtualHost *:80>    ServerAdmin admin@my-ecommerce-site.com    DocumentRoot /data/www/magento/pub/    ServerName my-ecommerce-site.com    ServerAlias www.my-ecommerce-site.com
        <Directory /data/www/magento/pub/>        Options Indexes FollowSymLinks MultiViews        AllowOverride all        Require all granted    </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log    CustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>
  3. Magento uses the apache module mod_rewrite to generate URLs. Make sure it is enabled before enabling the new site:

    a2enmod proxy_fcgi setenvifa2enconf php7.4-fpmsudo a2enmod rewritesudo a2ensite magento.conf
  4. Reload the Apache web server to activate the new configuration:

    systemctl reload apache2

    In a default Magento configuration, the web root is configured to /data/www/magento. Whithin the magento folder are several other folders, such as /pub/, /var, and other files and folders. The Magento web application is served from /data/www/magento/pub. To avoid a security vulunerability as these files are accessible from a web browser, we need to reconfigure the Magento env.php file and set the new web root for the application.

  5. Open the file /data/www/magento/app/etc/env.php in a text editor and add the following lines at the end of the file.

    'directories' => [    'document_root_is_pub' => true]

    Once edited your file should look like this example:

        'downloadable_domains' => [        'my-ecommerce-site.com'    ],    'install' => [        'date' => 'Thu, 09 Nov 2020 15:37:49 +0000'    ],    'directories' => [        'document_root_is_pub' => true    ]];
  6. Go to the Magento installation directory (/data/www/magento) and run the following command to activate the new configuration.

    bin/magento deploy:mode:set productionbin/magento cache:flush

Setting the noexec-flag on the /data partition

The following step is optional. Please note that you won’t be able to run the command line interface of Magento if this option is activated. The noexec flag on a partition has the effect that programs can no longer be executed on it. So, if someone can place a file there because of a security hole, he can not execute it. We will set the noexec flag on the /data partition so as to create an additional layer of security on the system.

Important: Be careful when editing the fstab file. Incorrect changes to this file can cause great damage to the system!

  1. Open the file /etc/fstab in a text editor of your choice.

  2. Search for the line relating to the /data partiton of the line and add the noexec flag to it:

    # /data was on /dev/md2 during installationUUID=524400dd-b164-48a1-bb1f-724108f320e3 /data           ext4    defaults,noexec        0       2
  3. Save the file and exit the text editor.

  4. Restart your system to activate the new configuration:

    sudo reboot

Securing the connection using Let’s Encrypt

By default plain HTTP connections are not encrypted and data is transmitted in clear text over the Internet. To secure the connection between your users and the e-commerce site, a TLS/SSL certificate is highly recommended. We will therefore request a free TLS certifiticate from Let’s Encrypt. We use certbot to obtain and renew the certificate automatically for us.

  1. Install certbot using the apt package manager:

    sudo apt-get install certbot python3-certbot-apachesudo certbot --apache
  2. Follow the steps prompted by Certbot to obtain your certificate and reconfigure the Apache2 web server.

  3. You can now check your storefront by pointing your web browser to your domain name to check everything is working:

Magento Admin interface

The Magento admin interface is available on a customized URL, which was displayed during the installation of your shop. Type it into your browser and log in with the admin credentials configured during the inital setup of Magento.

The admin interface lets you manage your customers and products, and also customize the pages of your shop. For more information about it, refer to the official Magento documentation

You have successfully installed Magento Open Source on a Dedibox dedicated server and secured the connection between your users and your e-commerce site using a TLS certificate. You can now continue to customize your online shop and add articles to start selling. For more information, refer to the official user guides.