项目作者: jcastellanos926

项目描述 :
Magento 2.3 Apache Docker Image
高级语言: VCL
项目地址: git://github.com/jcastellanos926/magento2-docker.git
创建时间: 2019-03-22T19:36:08Z
项目社区:https://github.com/jcastellanos926/magento2-docker

开源协议:

下载


Magento 2.3 Apache Docker Image

alt text
alt text
alt text

Table of Contents

Versions" class="reference-link">Versions

  • Docker: 18.09.3
  • Docker Compose: 1.23.0
  • Magento: 2.3.1
  • Apache: 2.4.25
  • Php: 7.2.14
  • Mariadb: 10.3.13
  • Xdebug: 2.6.1
  • Varnish: 4.1.10
  • Redis: 5.0.4
  • Elasticsearch: 6.7.2
  • System: Debian GNU/Linux 9.6 (stretch)

Install Docker on Ubuntu 18.04" class="reference-link">Install Docker on Ubuntu 18.04

Although we can install Docker and Docker Compose from the official Ubuntu repositories, they are several minor versions behind the latest release. So, we’ll install Docker following the official documentation page (https://docs.docker.com/install/linux/docker-ce/ubuntu/).

Installation steps of Docker CE:

  1. # Uninstall old versions
  2. $ sudo apt-get remove docker docker-engine docker.io containerd runc
  3. # Update the apt package index
  4. $ sudo apt-get update
  5. # Install packages to allow apt to use a repository over HTTPS:
  6. $ sudo apt-get install \
  7. apt-transport-https \
  8. ca-certificates \
  9. curl \
  10. gnupg-agent \
  11. software-properties-common
  12. # Add Docker’s official GPG key
  13. $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  14. # Use the following command to set up the stable repository.
  15. $ sudo add-apt-repository \
  16. "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  17. $(lsb_release -cs) \
  18. stable"
  19. # Update the apt package index.
  20. $ sudo apt-get update
  21. # Install the latest version of Docker CE and containerd
  22. $ sudo apt-get install docker-ce docker-ce-cli containerd.io

Verify the installation

Verify that Docker CE is installed correctly by running the hello-world image.

$ sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

Now Docker CE is installed and running.

[Optional] Configure Docker to start on boot

Enable the docker system service to start docker when the system boots:

$ sudo systemctl enable docker

To disable this behavior, use disable instead.

$ sudo systemctl disable docker

[Optional] Docker without sudo

By default, you need to use sudo to run Docker commands.

If you don’t want to use sudo when you use the docker command, create a Unix group called docker and add users to it.

Warning:
The docker group is root-equivalent; see Docker Daemon Attack Surface details and this blogpost on Why we don’t let non-root users run Docker in CentOS, Fedora, or RHEL.

To create the docker group and add your user:

1- Add the docker group if it doesn’t already exist

$ sudo groupadd docker

2- Add your user to the docker group.

$ sudo usermod -aG docker $USER

Other optional configuration steps: (https://docs.docker.com/install/linux/linux-postinstall/)

Install Docker Compose" class="reference-link">Install Docker Compose

In order to manage and execute docker-compose files we need to install the Docker Compose package.

The command below is slightly different than the one you’ll find on the Releases page. By using the -o flag to specify the output file first rather than redirecting the output, this syntax avoids running into a permission denied error caused when using sudo.

Go to https://docs.docker.com/release-notes/docker-compose/ and look for the last release of docker-compose.

We’ll check the current release and if necessary, update it in the command below:

  1. $ sudo curl -L https://github.com/docker/compose/releases/download/1.23.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

Next we’ll set the permissions:

sudo chmod +x /usr/local/bin/docker-compose

Then we’ll verify that the installation was successful by checking the version:

docker-compose --version

This will print out the version we installed:

  1. Output
  2. docker-compose version 1.23.0, build a133471

Creating a new Magento project

Set up Apache-php and Mysql containers" class="reference-link">Set up Apache-php and Mysql containers

  1. Create the following folder structure in our workspace folder.

    1. # Recommended path: ~/workspace/{project_name}/
    2. - data // mysql data, apache log files...
    3. |-- apache
    4. |-- elasticsearch
    5. |-- mysql
    6. |-- redis
    7. - docker // Our magento2 docker project
    8. - src // Magento 2 source code
    1. $ mkdir -p data/apache data/elasticsearch data/mysql data/redis docker src
  2. Clone the Docker project

    $ git clone git@github.com:jcastellanos926/magento2-docker.git docker

  3. Create the .env file from .env.sample and configure it.

    1. $ cp .env.sample .env
    2. $ gedit .env
  4. Go to the docker folder and execute:

    1. $ docker-compose build
    2. $ docker-compose up -d
  5. Install Magento 2 using composer.

    1. $ docker exec -it web bash
    2. $ composer create-project --repository=https://repo.magento.com/ magento/project-community-edition .
    3. $ composer install

    More Info

  6. Change apache user as the file owner of our files and set the right permissions following the official Magento documentation

    1. $ docker exec -it web bash
    2. $ chown -R www-data:www-data .
    3. $ find var generated vendor pub/static pub/media app/etc -type f -exec chmod u+w {} +
    4. $ find var generated vendor pub/static pub/media app/etc -type d -exec chmod u+w {} +
    5. $ chmod u+x bin/magento
  7. Edit the /etc/hosts file in your local machine to point your custom domain names to localhost

    1. # Magento 2 project
    2. 127.0.0.1 local-m2.com
    3. 127.0.0.1 db
    4. 127.0.0.1 redis
  8. Copy .htaccess and .gitignore and files from the magento2 github repository to your project.

    1. $ curl -O https://raw.githubusercontent.com/magento/magento2/2.3-develop/.htaccess
    2. $ curl -O https://raw.githubusercontent.com/magento/magento2/2.3-develop/.gitignore
    3. $ chown www-data:www-data .gitignore .htaccess
    4. $ chmod 644 .gitignore .htaccess

Installation via Magento Wizard" class="reference-link">Installation via Magento Wizard

  1. Open your browser and go to your configured localhost domain
    alt text

  2. Follow the installation wizard. Use the previously configured database credentials.
    alt text

Installation via Magento Script" class="reference-link">Installation via Magento Script

Alternatively, we can install Magento using an installation script.

Open the file web/bin/install and put your database and Admin User credentials.

Restart the Docker containers to be sure that your changes were applied successfully.

  1. $ docker-compose restart

To execute, run:

  1. $ docker exec -it web install

Sample data" class="reference-link">Sample data

Look for your repo.magento.com credentials in the auth.json file and run:

  1. $ docker exec -it web bash
  2. $ magento sampledata:deploy
  3. $ magento setup:upgrade
  4. $ magento cache:clean

Set up Magento 2 environment for existing project

Set up Apache-php and Mysql containers" class="reference-link">Set up Apache-php and Mysql containers

  1. Follow 1-4 Creating a new Magento steps

  2. Clone your Magento project into the src folder

    1. $ docker exec -it web bash
    2. $ git clone {your-project-repo} .
    3. $ composer install
  3. Change apache user as the file owner of our files and set the right permissions following the official Magento documentation

    1. $ docker exec -it web bash
    2. $ chown -R www-data:www-data .
    3. $ find var generated vendor pub/static pub/media app/etc -type f -exec chmod u+w {} +
    4. $ find var generated vendor pub/static pub/media app/etc -type d -exec chmod u+w {} +
    5. $ chmod u+x bin/magento
  4. Import the database of your project

    mysql -umagento -hdb -p magento < yourdatabase.sql

  5. Put the magento database credentials in your app/etc/env.php file. By default:

    1. 'db' => [
    2. 'table_prefix' => '',
    3. 'connection' => [
    4. 'default' => [
    5. 'host' => 'db',
    6. 'dbname' => 'magento',
    7. 'username' => 'magento',
    8. 'password' => 'magento',
    9. 'active' => '1'
    10. ]
    11. ]
    12. ]

Enable Magento Cache" class="reference-link">Enable Magento Cache

Add the following lines to app/etc/env.php

  1. 'cache_types' => [
  2. 'config' => 1,
  3. 'layout' => 1,
  4. 'block_html' => 1,
  5. 'collections' => 1,
  6. 'reflection' => 1,
  7. 'db_ddl' => 1,
  8. 'compiled_config' => 1,
  9. 'eav' => 1,
  10. 'customer_notification' => 1,
  11. 'config_integration' => 1,
  12. 'config_integration_api' => 1,
  13. 'full_page' => 1,
  14. 'config_webservice' => 1,
  15. 'translate' => 1,
  16. 'vertex' => 1
  17. ],

Execute:

  1. $ magento config:set --scope=default --scope-code=0 system/full_page_cache/caching_application 2

Configure Redis" class="reference-link">Configure Redis

The following commands will edit the app/etc/env.php file to enable the Redis cache.

Configure Redis default caching

  1. $ bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=redis --cache-backend-redis-db=0

Configure Redis page caching

  1. $ bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=redis --page-cache-redis-db=1

Configure Magento to use Redis for session storage

  1. $ bin/magento setup:config:set --session-save=redis --session-save-redis-host=redis --session-save-redis-log-level=3 --session-save-redis-db=2

Clean cache storage manually to apply changes

  1. $ rm -rf var/cache/*

Links and verification instructions:

Enable OPCACHE" class="reference-link">Enable OPCACHE

Remove the semicolons (;) from opcache.ini file and reload apache

service apache2 reload

Enable Varnish" class="reference-link">Enable Varnish

  1. Add the following to docker-compose.yml

    1. varnish:
    2. build:
    3. context: ./varnish/
    4. container_name: varnish
    5. depends_on:
    6. - web
    7. volumes:
    8. - ./varnish/default.vcl:/etc/varnish/default.vcl
    9. - ./varnish/varnish:/etc/default/varnish
    10. - ./varnish/supervisord.conf:/etc/supervisor/conf.d/supervisord.conf
    11. ports:
    12. - "80:80"
    13. - "6082:6082"
    14. networks:
    15. - magento2
  2. Change the port of the web docker service from 80:80 to 8080:8080

Sources:

ELASTICSEARCH" class="reference-link">ELASTICSEARCH

  1. In admin panel, go to Stores - Settings - Configuration - Catalog - Catalog Search

  2. Configure:

    • Search Engine: Elasticsearch 6.0+
    • Server Hostname: elasticsearch
      alt text
  3. Click on Test connection and Save Config.

  4. Reindex catalog data.

    1. $ magento indexer:reindex
    2. $ magento cache:clean
  5. Verify that Elasticsearch is working:

    1. $ docker exec -it elasticsearch bash
    2. $ curl http://localhost:9200/_cat/health?v&pretty

If correct, a message similar to the following will be displayed:

  1. epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks
  2. 1519701563 03:19:23 elasticsearch green 1 1 0 0 0 0 0 0

Sources:

XDEBUG FOR PHPSTORM (Optional)" class="reference-link">XDEBUG FOR PHPSTORM (Optional)

Enable or disable Xdebug

  1. # Enable
  2. $ docker exec -it web xdebug-enable
  3. # Disable
  4. $ docker exec -it web xdebug-disable

Links:

Enable profiler in Magento 2" class="reference-link">Enable profiler in Magento 2

To enable or disable the html profiler:

  1. # enable
  2. $ magento bin/magento dev:profiler:enable html
  3. # disable
  4. $ magento bin/magento dev:profiler:disable
  5. # refresh varnish cache
  6. $ docker restart varnish

To use the SQL profiler add the following code to the app/etc/env.php

  1. 'profiler' => [
  2. 'class' => '\Magento\Framework\DB\Profiler',
  3. 'enabled' => true,
  4. ],
  5. # For example:
  6. 'db' => [
  7. 'table_prefix' => 'mlx_',
  8. 'connection' => [
  9. 'default' => [
  10. 'host' => 'db',
  11. 'dbname' => 'magento',
  12. 'username' => 'magento',
  13. 'password' => 'magento',
  14. 'model' => 'mysql4',
  15. 'engine' => 'innodb',
  16. 'initStatements' => 'SET NAMES utf8;',
  17. 'active' => '1',
  18. 'profiler' => [
  19. 'class' => '\Magento\Framework\DB\Profiler',
  20. 'enabled' => true,
  21. ],
  22. ]
  23. ]
  24. ],

Paste the following code in the index.php after $bootstrap->run($app);

  1. /** @var \Magento\Framework\App\ResourceConnection $res */
  2. $res = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\App\ResourceConnection');
  3. /** @var Magento\Framework\DB\Profiler $profiler */
  4. $profiler = $res->getConnection('read')->getProfiler();
  5. echo "<table cellpadding='0' cellspacing='0' border='1'>";
  6. echo "<tr>";
  7. echo "<th>Time <br/>[Total Time: ".$profiler->getTotalElapsedSecs()." secs]</th>";
  8. echo "<th>SQL [Total: ".$profiler->getTotalNumQueries()." queries]</th>";
  9. echo "<th>Query Params</th>";
  10. echo "</tr>";
  11. foreach ($profiler->getQueryProfiles() as $query) {
  12. /** @var Zend_Db_Profiler_Query $query*/
  13. echo '<tr>';
  14. echo '<td>', number_format(1000 * $query->getElapsedSecs(), 2), 'ms', '</td>';
  15. echo '<td>', $query->getQuery(), '</td>';
  16. echo '<td>', json_encode($query->getQueryParams()), '</td>';
  17. echo '</tr>';
  18. }
  19. echo "</table>";

Source:

Flip the system into developer mode" class="reference-link">Flip the system into developer mode

  1. $ php bin/magento deploy:mode:set developer