Skip to content

Commit

Permalink
HW1: Docker&Homestead
Browse files Browse the repository at this point in the history
  • Loading branch information
Natallia Kazarynava committed Sep 2, 2023
1 parent f5c02a7 commit 69601bd
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PROJECT_NAME=otus_2
MYSQL_DATABASE=otus_2_db
MYSQL_USER=mysql_user
MYSQL_PASSWORD=erterterte
MYSQL_ROOT_PASSWORD=root
MYSQL_HOST=db
27 changes: 27 additions & 0 deletions Homestead/Homestead.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
memory: 2048
cpus: 2
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
- ~/.ssh/id_rsa

folders:
- map: ~/Stady/OTUS/Lesson1/PHP2021/Homestead
to: /home/vagrant/code

sites:
- map: application.local
to: /home/vagrant/code/public

databases:
- homestead

features:
- mariadb: true

ports:
- send: 8090
to: 80
3 changes: 3 additions & 0 deletions Homestead/public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
echo "Hello, Otus!";
phpinfo();
18 changes: 18 additions & 0 deletions code/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

$redis = new Redis();
$redis->connect('redis', 6379);

var_dump($redis->ping());

$memcached = new Memcached();
$memcached->addServer('memcaced', 11211);

var_dump($memcached->getVersion());

try {
$cncStr = 'mysql:host=' . $_ENV['MYSQL_HOST'] . ';dbname=' . $_ENV['MYSQL_DATABASE'];
$dbh = new \PDO($cncStr, $_ENV['MYSQL_USER'], $_ENV['MYSQL_PASSWORD']);
} catch (PDOException $exception) {
echo $exception->getMessage();
}
88 changes: 88 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
version: '3.9'

services:
app:
container_name: ${PROJECT_NAME}_app
build:
context: ./fpm
dockerfile: Dockerfile
image: ${PROJECT_NAME}_php_fpm
environment:
MYSQL_HOST: ${MYSQL_HOST}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
PROJECT_NAME: ${PROJECT_NAME}
ports:
- "9009:9000"
volumes:
- ./code:/data/mysite.local
- sock:/sock
networks:
- app-network

webserver:
container_name: ${PROJECT_NAME}_webserver
build:
context: ./nginx
dockerfile: Dockerfile
image: ${PROJECT_NAME}_nginx
ports:
- "8088:80"
- "443:443"
volumes:
- ./code:/data/mysite.local
- sock:/sock
networks:
- app-network
depends_on:
- app

memcaced:
container_name: ${PROJECT_NAME}_memcached
image: memcached:latest
ports:
- "11211:11211"
restart: always
networks:
- app-network
depends_on:
- app

redis:
image: redis:latest
container_name: redis
restart: always
ports:
- "6379:6379"
networks:
- app-network
depends_on:
- app

db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
networks:
- app-network
volumes:
- ./datadb:/var/lib/mysql
depends_on:
- app

phpMyAdmin:
image: phpmyadmin
environment:
PMA_ARBITRARY: 1
ports:
- "8880:80"
networks:
- app-network

networks:
app-network:
driver: bridge

volumes:
sock:
40 changes: 40 additions & 0 deletions fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
FROM php:8-fpm

RUN apt-get update && apt-get install -y \
curl \
wget \
git \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libonig-dev \
libzip-dev \
libmcrypt-dev \
libmemcached-dev \
zlib1g-dev \
libssl-dev \
&& pecl install mcrypt-1.0.6 \
&& docker-php-ext-enable mcrypt \
&& docker-php-ext-install -j$(nproc) iconv mbstring mysqli pdo_mysql zip \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd \
&& pecl install memcached-3.2.0 \
&& docker-php-ext-enable memcached \
&& pecl install redis \
&& docker-php-ext-enable redis

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# For restrict access to unix socket define same users
# in fpm and nginx containers and configure php-fpm to run processes under this user
RUN addgroup --gid 3000 --system safeapp
RUN adduser --uid 3000 --system --disabled-login --disabled-password --gid 3000 safeuser

COPY ./config/zz-docker.conf /usr/local/etc/php-fpm.d/zz-docker.conf

# performs mkdir and cd implicitly
WORKDIR /data

# create unnamed volume in folder like /var/lib/docker/volumes/f670...49f0/_data on host machine
VOLUME /data

CMD ["php-fpm"]
8 changes: 8 additions & 0 deletions fpm/config/zz-docker.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[global]
daemonize = no

[www]
listen = /sock/docker.sock
listen.owner = safeuser
listen.group = safeapp
listen.mode = 0666
14 changes: 14 additions & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM nginx:latest

COPY ./hosts/mysite.local.conf /etc/nginx/conf.d/mysite.local.conf

RUN addgroup --gid 3000 --system safeapp
RUN adduser --uid 3000 --system --disabled-login --disabled-password --gid 3000 safeuser

WORKDIR /data

VOLUME /data

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
36 changes: 36 additions & 0 deletions nginx/hosts/mysite.local.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
server {
# указываем 80 порт для соединения
listen 80;
# нужно указать, какому доменному имени принадлежит наш конфиг
server_name mysite.local;

# задаём корневую директорию
root /data/mysite.local;

# стартовый файл
index index.php index.html;

# при обращении к статическим файлам логи не нужны, равно как и обращение к fpm
# http://mysite.local/static/some.png
location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

# помним про единую точку доступа
# все запросы заворачиваются в корневую директорию root на index.php
location / {
try_files $uri $uri/ /index.php?$query_string;
}

# и наконец правило обращения к php-fpm
location ~* .php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
# fastcgi_pass app:9000;
fastcgi_pass unix:/sock/docker.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

0 comments on commit 69601bd

Please sign in to comment.