Dorokhov.codes

Yii2 Framework installation

Yii2 Framework installation

Installation of Yii2 Framework can be quite simple if we use Composer and Docker. I will show how to install Yii2 Framework so that it doesn’t take more than 5 minutes.

Initial files

To download all the necessary files for the framework we should use the next command for Composer:

composer create-project yiisoft/yii2-app-basic .

With Docker, it can be more simple because we don’t need to install Composer on the local machine:

docker run --rm \
    --interactive \
    --tty \
    --volume "$PWD:/app" \
    --user $(id -u):$(id -g) \
    composer \
    create-project yiisoft/yii2-app-basic .

Some developers may wonder why we don’t use --no-install option for Composer, since we don’t need any vendor packages for the local machine (we use Docker instead). There are two reasons. First, Yii2 Framework has scripts that have to be run after installation. And these scripts are inside the yii2`s vendor package. Second, we need these vendor packages for debugging purpose (for mapping from a Docker file system to a local’s one).

Yii2 has a development image on Docker Hub. So we can install the template using this image (it contains Composer).

docker run --rm \
    --interactive \
    --tty \
    --volume "$PWD:/app" \
    --user $(id -u):$(id -g) \
    yiisoftware/yii2-php:8.2-apache composer \
    create-project yiisoft/yii2-app-basic .

For example:

  • yiisoftware/yii2-php:8.2-apache - for development.
  • yiisoftware/yii2-php:8.2-apache-min - for production.

Some cleaning

I like doing some minimal cleaning after fetching the initial files:

rm -R \
commands \
config/test* \
mail \
tests \
vagrant \
web/index-test.php \
codeception.yml \
LICENSE.md \
requirements.php \
Vagrantfile \
yii.bat

Dockerfile

On the next step we have to create a Docker image with our application. As a basic image I will take yiisoftware/yii2-php:8.2-apache.

Let’s create a Dockerfile:

FROM yiisoftware/yii2-php:8.2-apache

RUN apt-get update && apt-get install -y

COPY . /app

RUN composer install

RUN mkdir runtime && chown www-data:www-data runtime
RUN mkdir web/assets && chown www-data:www-data web/assets

We changed an owner of the directories because the default user of Apache is www-data (we need permission to write to these folders).

And we shouldn’t forget to exclude unnecessary files via .dockerignore:

# Files:
.dockerignore
.git
.gitignore
.idea
Dockerfile
README.md
docker-compose.yml

# Folders:
runtime
vendor
web/assets

docker-compose.yml

And docker-compose.yml:

version: '2.4'
services:
  app:
    container_name: andrew.app
    build: .
    ports:
      - "81:80"

If we use db service, don’t forget to add dependency to the app service:

depends_on:
  - db

Adding MySQL service

version: '2.4'
services:
  db:
    container_name: andrew.db
    image: mysql:5.7.29
    restart: always
    environment:
      MYSQL_DATABASE: andrew
      MYSQL_USER: andrew
      MYSQL_PASSWORD: password
      MYSQL_RANDOM_ROOT_PASSWORD: 1
    volumes:
      - andrew:/var/lib/mysql
volumes:
  andrew:
    name: andrew

Adding phpMyAdmin service

version: '2.4'
services:
  phpmyadmin:
    container_name: andrew.phpmyadmin
    image: phpmyadmin
    restart: always
    ports:
      - 8080:80
    environment:
      - PMA_ARBITRARY=1

And, of course, we should add dependency on the database:

depends_on:
  - db

Setting up xdebug for PhpStorm

And the last step is setting up xdebug for convenient development process.

First, lets configure xdebug in the Docker image. If we are talking about yiisoftware/yii2-php:8.2-apache image, then the config file is located here: /usr/local/etc/php/conf.d/xdebug.ini

Our goal is to make it look like this:

zend_extension=xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.discover_client_host=true
xdebug.client_port=9005

To do it and override an existing file we can use the next commands for Dockerfile:

RUN echo "zend_extension=xdebug.so" > /usr/local/etc/php/conf.d/xdebug.ini && \
    echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/xdebug.ini && \
    echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/xdebug.ini && \
    echo "xxdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/xdebug.ini && \
    echo "xdebug.discover_client_host=true" >> /usr/local/etc/php/conf.d/xdebug.ini && \
    echo "xdebug.client_port=9005" >> /usr/local/etc/php/conf.d/xdebug.ini

Also, we should configure correct mapping for PhpStorm and correct client host port for xdebug.

Debug port

File -> Settings... -> PHP -> Debug. And add a new debug port (9005).

Mapping

File -> Settings... -> PHP -> Servers. And add root project folders for remote and local file systems.

Let’s do it in 5 minutes

Let’s install Yii2 Framework using Docker and Composer with xdubug in 5 minutes.

  1. Create a folder for out project and enter it (5 seconds):
mkdir andrew && cd $_
  1. Install the basic template (20 seconds):
docker run --rm \
    --interactive \
    --tty \
    --volume "$PWD:/app" \
    --user $(id -u):$(id -g) \
    yiisoftware/yii2-php:8.2-apache composer \
    create-project yiisoft/yii2-app-basic .
  1. Do some cleaning (3 seconds):
rm -R \
commands \
config/test* \
mail \
tests \
vagrant \
web/index-test.php \
codeception.yml \
LICENSE.md \
requirements.php \
Vagrantfile \
yii.bat
  1. Create Dockerfile, .dockerignore, docker-compose.yml (1 minute):
touch Dockerfile .dockerignore docker-compose.yml

Dockerfile:

FROM yiisoftware/yii2-php:8.2-apache

RUN apt-get update && apt-get install -y

COPY . /app

RUN composer install

RUN mkdir runtime && chown www-data:www-data runtime
RUN mkdir web/assets && chown www-data:www-data web/assets

RUN echo "zend_extension=xdebug.so" > /usr/local/etc/php/conf.d/xdebug.ini && \
    echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/xdebug.ini && \
    echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/xdebug.ini && \
    echo "xxdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/xdebug.ini && \
    echo "xdebug.discover_client_host=true" >> /usr/local/etc/php/conf.d/xdebug.ini && \
    echo "xdebug.client_port=9005" >> /usr/local/etc/php/conf.d/xdebug.ini

.dockerignore:

# Files:
.dockerignore
.git
.gitignore
.idea
Dockerfile
README.md
docker-compose.yml

# Folders:
runtime
vendor
web/assets

.docker-compose.yml:

version: '2.4'
services:
  app:
    container_name: andrew.app
    build: .
    ports:
      - "81:80"
    volumes:
      - .:/app
    depends_on:
      - db
  db:
    container_name: andrew.db
    image: mysql:5.7.29
    restart: always
    environment:
      MYSQL_DATABASE: andrew
      MYSQL_USER: andrew
      MYSQL_PASSWORD: password
      MYSQL_RANDOM_ROOT_PASSWORD: 1
    volumes:
      - andrew:/var/lib/mysql
  phpmyadmin:
    container_name: andrew.phpmyadmin
    image: phpmyadmin
    restart: always
    ports:
      - "8080:80"
    environment:
      - PMA_ARBITRARY=1
    depends_on:
      - db

volumes:
  andrew:
    name: andrew
  1. Set up a debug port and mapping (1 minute).

  2. Run docker-compose up (1 minute).