📜 ⬆️ ⬇️

We expand the bundle of Nginx + Php-Fpm + MySQL with magento2 on board and decompose it in containers in the Docker

Good day!

Increasingly, knocking on various developer companies as an DevOps engineer, I get about the same test items. They differ from each other in PHP versions or projects that need to be run.

But in general, they run into one bundle, this is Nginx \ Appache, SQL (there are many 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 by example how to raise all this effortlessly.
')
Perhaps it will help someone to understand, with a simple example, what's what. I will not describe what a Docker is, because articles on this topic car and small truck.

In this article, we will prepare a small structure:


First, install Docker.

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

For Linux (for example on CentOS)

Install:

# yum install docker 

We turn on and start the service:

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

In order to create our structure with one team, we need docker-compose.

To begin with, we 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).

We enable the Hyper-V component in Turning Windows Features on or Off.

Download the installer from the Docker's site and install it. Also, in addition, you can put the GUI (Kitematis) for visual display.

Let's start creating the environment:

To begin with, we will create a folder for this project and go to it:

 # mkdir /mage (     ) # cd /mage 

Next, we build the folder structure in this way:

 # mkdir MySQL Nginx PHP 

Create a clear environment for nginx:

 # cd Nginx # mkdir core html Logs www 

MySQL - databases will be stored in this folder. Convenient to back up and transfer.
Nginx - logs, configuration file and our project will be stored here.
PHP - here we add Dockerfile with settings and php.ini.
at 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 not, you can install it using:

 # yum install nano 

And we 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 names, create alias for the long path where magento2 lies, write what logs are needed and specify where they should be stored, specify the folder where magento2 lives (in this case our alias $ MAGE_ROOT) .

In the second block, write the parameters fastcgi.

The third block is needed to solve the mapping problem, the project showed a blank page. From the documentation I read that magento2 requires rewriting. (on other projects there were no such problems).

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

 # cd /mage/Nginx/www # mkdir magento2 

Download from the magento2 site
and extract from the archive to the folder / mage / Nginx / www / magento2

With the settings for Nginx, we are finished.

Now let's do PHP:

Let's start with the Dockerfile

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

We collect 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 those 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 configure php.ini:

 # nano php.ini 

This is taken from php.ini.sample that Magento2 developers themselves offer. There is nothing supernatural I did not add 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 over.

Next, create a docker-compose file that will bring us all into one convenient system.

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

Here we will write out 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 don’t want to watch the containers work, you can add -d)

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

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

Opening the browser and typing localhost there you should see an invitation to install Magento2.

If all the same something did not work out, then perhaps this list of solutions can help you:

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


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

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

3) If, after installing Magento2 itself (in the browser), you do not draw 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 the 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 a PHP container (php-fpm) and type

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

He has to recount and check everything. And after that, everything should be displayed correctly.

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

4.1 in Linux, you must disable the protection policy

Shutdown before reboot

 # setenforce Permissive 

on or off forever

 # nano /etc/selinux/config 

we change the line

 "SELINUX=disabled" 

4.2 In windows, in the docker settings, select shared drivers and select the disk on which you have the project. After restarting Docker, the problem will go away.

Good luck in your endeavors, gentlemen!

Link to the finished assembly here .

Source: https://habr.com/ru/post/349704/


All Articles