M2M2: The server

Published on 15 Mar 2019 , edited on 22 Mar 2019

I was going to get my existing clean LEMP server install based on Ubuntu 14.04 LTS and update it. But I decided to get a clean slate and create a new image based on a clean Ubuntu 18.04 LTS version and start from scratch. What follows is more or less a direct copy of this article, but I like to run through it for myself and note down everything here as well.

So I spin up a new instance based on Ubuntu 18.04-LTS in Google Compute Cloud (not going indepth on that here). Then we logon via SSH and run these commands to update all and install the unzip and mysql tools;

Set up server and MySQL

sudo apt update && sudo apt upgrade
sudo apt install unzip
sudo apt install mysql-server mysql-client
sudo mysql_secure_installation

At this point we’ll create a database for our test;

sudo mysql
mysql > CREATE DATABASE m2test;
mysql > CREATE USER m2test;
mysql > GRANT ALL ON m2test.* TO 'm2test'@'localhost' IDENTIFIED BY 'your-strong-password-for-user';
mysql > exit;

Add system user and PHP

From my previous experiences working with Magento 2.0 just after it launched was that the user-management serverwise was a pain in the ass. So let’s set it up correctly from the get-go; we setup a user magento and add www-data user to the same group;

sudo useradd -m -U -r -d /opt/magento magento 
sudo usermod -a -G magento www-data
sudo chmod 750 /opt/magento

PHP7.2 is the new standard in Ubuntu 18.04-LTS but we use Nginx so we want the PHP-FPM version (too). These commands install them, and should output the status (which should be active);

sudo apt install php7.2-common php7.2-cli php7.2-fpm php7.2-opcache php7.2-gd php7.2-mysql php7.2-curl php7.2-intl php7.2-xsl php7.2-mbstring php7.2-zip php7.2-bcmath php7.2-soap

sudo systemctl status php7.2-fpm

And next we set the required PHP settings via this handy dandy snippet;

sudo sed -i "s/memory_limit = .*/memory_limit = 1024M/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 256M/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/;opcache.save_comments.*/opcache.save_comments = 1/" /etc/php/7.2/fpm/php.in

Then we create a PHP pool for the Magento user

sudo nano /etc/php/7.2/pool.d/magento.conf

user = magento
group = www-data
listen.owner = magento
listen.group = www-data
listen = /var/run/php/php7.2-fpm-magento.sock
pm = ondemand
pm.max_children = 50
pm.process_idle_timeout = 10s
pm.max_requests = 500
chdir = /

Then restart the PHP service and check if the socket is running correctly

sudo service php7.2-fpm restart

ls -al /var/run/php/php7.2-fpm-magento.sock

Setup Composer

My brief encounter with Magento 2.0 made me realize the huge potentional of tools such as composer and modman. So I “retro fitted” my Magento 1.9.x installations to sort-of fit the same workflow. Hopefully the M2 experience will be way smoother then that! Installing it is still the smoothest with this oneliner;

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Install Magento

This is where we get freaky. So far everything has been more or less the same with our 1.9 setup the past years, but now we get into M2 setup and best-practices, and it’s time to apply all I’ve learned. Starting with, having a magento user. We switch to it and then using composer we download the latest release to our (to be) server root. Running below commands will prompt for username/password; do not be led astray by this; it needs your public/private key combo you can generate in your Magento Marketplace profile.

sudo su - magento
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition /opt/magento/public_html

My biggest issue with the development workflow on Magento 1.9.x was the autoload feature of composer. I’m hoping this is no issue in M2 because it relies on it out of the box, rather then being shoved in later.

So far this is looking good; creating the project via composer this way automagically gets all php depencies so it’s far less hassle to pull in all seperate requirements by hand via ssh. It’s easier, more secure and less error-prone. Now that is what I call progress.

Setup Magento

Once composer is done autoloading everything, we can install magento with this command. Of course we fill it with our actual details;

php bin/magento setup:install --base-url=https://example.com/ \
--base-url-secure=https://example.com/ \
--admin-firstname="John" \
--admin-lastname="Doe" \
--admin-email="john@example.com" \
--admin-user="john" \
--admin-password="enter-your-strong-password" \
--db-name="magento" \
--db-host="localhost" \
--db-user="magento" \
--currency=EUR \
--timezone=America/Chicago \
--use-rewrites=1 \

And here I run in to the fact that Magento only will install on a FQDN with SSL. So check this and come back here, and run above command again

At this point you should see the complete installation progress rolling by, ending in a success message with a admin-url endpoint.

And then..

We have to configure Nginx to run Magento on our domain with proper SSL set up. This is a bare-bones version of what should work;

upstream fastcgi_backend {
  server   unix:/var/run/php/php7.2-fpm-magento.sock;

server {
    listen 80;
    server_name example.com www.example.com;

    include snippets/letsencrypt.conf;
    return 301 https://example.com$request_uri;
server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    set $MAGE_ROOT /opt/magento/public_html;
    set $MAGE_MODE developer; # or production

    access_log /var/log/nginx/example.com-access.log;
    error_log /var/log/nginx/example.com-error.log;

    include /opt/magento/public_html/nginx.conf.sample;

Next up restart nginx with sudo service nginx restart and you should be set. Next up; Migration!


Moving To Magento Two; A big move from multiple Magento 1.9.x systems to Magento 2.x. Documented every step chronologically (currently ongoing!).

All categories

All tags

All posts