Skip to content

Commit

Permalink
hw1: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-pogorelov committed Feb 4, 2024
1 parent df0d466 commit 620e483
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MYSQL_USER=
MYSQL_PASSWORD=
MYSQL_ROOT_PASSWORD=
REDIS_PASSWORD=
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
init: docker-down docker-build docker-up

docker-down:
docker compose down --remove-orphans

docker-build:
docker compose build

docker-up:
docker compose up -d
48 changes: 48 additions & 0 deletions code/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

Check failure on line 1 in code/index.php

View workflow job for this annotation

GitHub Actions / phpcs

End of line character is invalid; expected &quot;\n&quot; but found &quot;\r\n&quot;

Check failure on line 1 in code/index.php

View workflow job for this annotation

GitHub Actions / phpcs

End of line character is invalid; expected &quot;\n&quot; but found &quot;\r\n&quot;

echo "<pre>";

$redis = new Redis();
$redis->connect('redis', 6379);
$redis->auth(getenv("REDIS_PASSWORD"));

if ($redis->ping()) {
print_r('Redis is alive!!!!!!');
echo "<hr />";
}

$mc = new Memcached();
$mc->addServer("memcached", 11211);
$mc->set("foo", "Hello!");
$mc->set("bar", "Memcached...");
$data = [
$mc->get("foo"),
$mc->get("bar")
];

print_r($data);
echo "<hr />";

try {
$user = getenv("DB_USER");
$pass = getenv("DB_PASSWORD");
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO("mysql:host=mysql;dbname=mysite", $user, $pass, $opt);
$stmt = $pdo->query('SHOW DATABASES;');

while ($row = $stmt->fetch()) {
print_r($row);
}
} catch (\Throwable $e) {
print_r($e->getMessage());
}

echo "<hr />";

echo "</pre>";

phpinfo();
74 changes: 74 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
version: '3'

services:
nginx:
image: nginx:latest
container_name: mysite-nginx
depends_on:
- php-fpm
ports:
- "80:80"
volumes:
- ./code:/data/mysite.local
- ./nginx/hosts/mysite.local.conf:/etc/nginx/conf.d/mysite.local.conf
networks:
- app-network

php-fpm:
build:
context: ./fpm
dockerfile: Dockerfile
image: mysite/php-fpm
container_name: mysite-php-fpm
environment:
- REDIS_PASSWORD=${REDIS_PASSWORD}
- DB_USER=${MYSQL_USER}
- DB_PASSWORD=${MYSQL_PASSWORD}
depends_on:
- memcached
- redis
volumes:
- ./code:/data/mysite.local
networks:
- app-network

memcached:
image: memcached:latest
container_name: mysite-memcached
ports:
- '11211:11211'
networks:
- app-network

redis:
image: redis:latest
container_name: mysite-redis
environment:
- REDIS_PASSWORD=${REDIS_PASSWORD}
command: sh -c 'exec redis-server --requirepass "$REDIS_PASSWORD"'
ports:
- '6379:6379'
networks:
- app-network

mysql:
image: mysql:8.0
container_name: mysite-mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: mysite
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- mysite-dbdata:/var/lib/mysql
networks:
- app-network

networks:
app-network:
driver: bridge

volumes:
mysite-dbdata:
36 changes: 36 additions & 0 deletions fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
FROM php:8.2-fpm

# ставим необходимые для нормальной работы модули
RUN apt-get update \
&& apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libonig-dev \
libzip-dev \
libmemcached-dev \
zlib1g-dev \
libssl-dev \
libz-dev \
libmcrypt-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 redis \
&& docker-php-ext-enable redis

RUN yes '' | pecl install -f memcached-3.2.0 \
&& docker-php-ext-enable memcached

# Устанавливаем Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

COPY ./php.ini /usr/local/etc/php/conf.d/php-custom.ini

WORKDIR /data

VOLUME /data

CMD ["php-fpm"]
2 changes: 2 additions & 0 deletions fpm/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
session.save_handler = memcache
session.save_path = "tcp://memcache:11211"
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 php-fpm:9000;
#fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

0 comments on commit 620e483

Please sign in to comment.