We deploy a bunch of Nginx + Php-Fpm + MySQL with magento2 on board and lay out the containers in Docker

Good afternoon!

More and more often when I knock on various development companies as a DevOps engineer, I get approximately the same test tasks. They differ from each other by versions of PHP or projects which need to be started.

But in general, they run into one bunch: Nginx \ Appache, SQL (there are a lot of variations, it all depends on the preferences of the customer), PHP, and it is desirable that this be decomposed into containers.
Therefore, I decided to tell with an example how to raise all this without much effort.

Perhaps this will help someone understand, with a simple example, what's what. I will not describe what Docker is, because articles on the subject of a wagon and a small cart.

In this article, we will prepare a small structure:

  • As a web server, Nginx will be used with minimal setup to start the project.
  • As SQL will use MySQL.
  • PHP7.0-fpm version with small add-ons to run our project.
  • As a project that we will deploy - we take magento2.

First, install Docker.

It all depends on the system on which you want to work, in terms of cross-platform, docker is pleasantly surprising (the same configuration file allows you to build and run containers on any * nix, Win, iOS system).

For Linux (for example, in CentOS)

Install:

# yum install docker

We turn on and start the service:

# systemctl enable docker.service
# systemctl start docker.service

For our structure to be created by one team, we need docker-compose.

First, we’ll put the necessary components for it:

# yum install epel-release
# yum install -y python-pip

Next, install docker-compose and update python:

# yum install docker-compose

(or # pip install docker-compose)

# yum upgrade python*

For Win systems (many consider this a perversion)

But if you decide, I strongly recommend that you do this on a version that supports Hyper-V (for example win10 Prof).

Turn on the Hyper-V component in Turn Windows features on or off.

Download the installer from the official site of the docker and install. Also, in addition, you can put a GUI (Kitematic) for visual display.

Let's start creating the environment:

First, create a folder for this project and go into it:

# mkdir /mage (папка может быть названа как угодно)
# cd /mage

Next, we build the folder structure in this way:

# mkdir MySQL Nginx PHP

Create a friendly environment for nginx:

# cd Nginx
# mkdir core html Logs www

MySQL - databases will be stored in this folder. It is convenient to backup and transfer.
Nginx - logs, a configuration file and our project will be stored here.
PHP - here we add the Dockerfile with the settings and php.ini.
in the root (in our case the / mage folder) will be the docker-compose.yml file.

Create a configuration file for Nginx:

# cd /mage/Nginx/core
# touch nginx.conf
# nano nginx.conf 

You can use any other editor. If it is not, you can install it with:

# yum install nano

And add the following to nginx.conf:

server {
    listen 80;
    index index.php index.html index.htm;
    server_name magento2.dev;
    set $MAGE_ROOT /var/www/magento2;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root $MAGE_ROOT;
location ~* \.php$ {
    try_files $uri $uri/ /index.php last;
    fastcgi_split_path_info  (.+?\.php)(/.*)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    }
location ~* .php/ { rewrite  (.*.php)/ $1 last; }
}

This is the minimum config for everything to start. In the first block we describe which port to listen to, list possible index pages, call it, create alias for the long path where magento2 lies, write what logs are needed and indicate where they should be stored, indicate the folder where magento2 lives (in this case, our alias $ MAGE_ROOT) .

In the second block we prescribe the fastcgi parameters.

The third block is needed to solve the display problem, a blank page was shown in the project. I read from the documentation that magento2 requires rewriting. (on other projects, such problems did not arise).

In the www folder, create a directory for our project:


# cd /mage/Nginx/www
# mkdir magento2

Download from of.sayta magento2
and extract the archive to a folder / mage / Nginx / www / magento2

the C settings for Nginx we finished.

Now let's get

started with PHP: Let's start with the Dockerfile

# cd /mage/PHP
# touch Dockerfile php.ini
# nano Dockerfile

We assemble ourselves:

FROM php:7.0-fpm
RUN apt-get update && apt-get install -y \
curl \
wget \
git \
libfreetype6-dev \
libjpeg62-turbo-dev \
libxslt-dev \
libicu-dev \
libmcrypt-dev \
libpng12-dev \
libxml2-dev \
&& docker-php-ext-install -j$(nproc) iconv mcrypt mbstring mysqli pdo_mysql zip \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
RUN docker-php-ext-configure intl
RUN docker-php-ext-install intl
RUN docker-php-ext-install xsl
RUN docker-php-ext-install soap
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
ADD php.ini /usr/local/etc/php/conf.d/40-custom.ini
WORKDIR /var/www/magento2
CMD ["php-fpm"]

This is necessary so that we can use the modules that are needed specifically for this project.

In this file, we told what should be installed in this image, and also indicated where the root directory will be located and where to copy the settings from php.ini

Now we will configure php.ini:

# nano php.ini

This is taken from php.ini.sample, which Magento2 developers themselves offer. I didn’t add anything supernatural to it.

memory_limit = 2G
always_populate_raw_post_data = -1
cgi.fix_pathinfo = 1
fastcgi_split_path_info = 1
max_execution_time = 18000
flag session.auto_start = off
zlib.output_compression = on
suhosin.session.cryptua = off
display_errors = Off

That's it, the PHP setup is done.

Next, we create a docker-compose file that will collect everything for us into one convenient system.


# cd /mage
# touch docker-compose.yml
# nano docker-compose.yml

Here we will write down what services and with what settings should start:

# Пропишем версию
version: '3.3'
# Перечислим сервисы
services:
    nginx:
# Пропишем какой образ мы хотим использовать
        image: nginx:latest
# Назовем свой контейнер по красивому
        container_name: nginx
# Проброс портов
        ports:
            - "80:80"
            - "443:443"
# Проброс папок
        volumes:
            - ./Nginx/core:/etc/nginx/conf.d
            - ./Nginx/www:/var/www/
            - ./Nginx/Logs:/var/log/nginx/
            - ./Nginx/html:/usr/share/nginx/html/
# Укажем зависимости
        links:
            - php
    mysql:
        image: mysql:latest
        ports:
            - "3306:3306"
        container_name: mysql
# Пропишем настройки, предлагаю использовать вместо mypassword более сложный пароль, он пренадлежит root
        environment:
            - MYSQL_ROOT_PASSWORD=mypassword
            - MYSQL_DATABASE=magento2
            - MYSQL_USER=magento2
            - MYSQL_PASSWORD=magento2
        volumes:
            - ./MySQL:/var/lib/mysql
    php:
# Билдим с помощью dockerfile указав директорию где он лежит
        build: ./PHP
        container_name: php-fpm
        volumes:
            - ./Nginx/www:/var/www
        links:
            - mysql
    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        container_name: phpmyadmin
        ports:
            - 8090:80
        links:
            - mysql:db

(or if you do not want to monitor the operation of containers, you can add -d)

And the lines will run across the screen, and you can easily pour yourself a mug of hot coffee while the machine works for you.

After installation, in your MySQL folder, a lot of files and folders will be created, one of which will be magento2, and 2 logs will appear in the Nginx / Logs folder.

Having opened the browser and typing localhost there, you should see a prompt to install Magento2.

If all the same, something didn’t work, then maybe this list of solutions can help you:

1) The version of the docker-compose file did not fit, then you need to correct “version: '3.3'”, you can see which one is right for you here


2) Everything started fine, but the browser opens a clean page, without a single error - the line in nginx.conf will help

"location ~* .php/ { rewrite  (.*.php)/ $1 last; }"

3) If after installing Magento2 itself (in the browser) you don’t see the frames and everything looks like a text version of the site, you need to do the following:

3.1 in SQL, I advise you to go through phpmyadmin localhost : 8090 login root password mypassword, select the base magento2 and enter sql query

insert into core_config_data (config_id, scope, scope_id, path, value) values (null, 'default', 0, 'dev/static/sign', 0)

3.2 connect to the container with PHP (php-fpm) and type

# php bin/magento cache:clean config
# php bin/magento setup:static-content:deploy

He must recount and verify everything. And after that, everything should be displayed correctly.

4) If Docker does not have write permissions to folders (it will tell if you typed) docker-compose up (without -d)

4.1 in Linux you need to disable the protection policy

Disable before rebooting

# setenforce Permissive

or shutdown forever

# nano /etc/selinux/config

change the line

"SELINUX=disabled" 

4.2 In windows you need to select shared drivers in docker settings and select the drive on which your project is located. After restarting Docker, the problem will go away.

Good luck in your endeavors, gentlemen!

Link to the finished assembly here .

Also popular now: