Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My first homework #55

Open
wants to merge 2 commits into
base: AKiselev/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
POSTGRES_DB_NAME=otus
POSTGRES_PASSWORD=otus
POSTGRES_USER_NAME=otus
POSTGRES_HOST=postgres
POSTGRES_PORT=5432

REDIS_HOST=redis
REDIS_PORT=6379

MEMCACHED_HOST=memcached
MEMCACHED_PORT=11211

ROOT_DIR=/var/www/html
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
.DS_Store
.AppleDouble
.LSOverride
/www/vendor/
/docker/db/
.env
62 changes: 62 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
version: '3'

services:
nginx:
build:
context: docker/nginx
dockerfile: Dockerfile
image: myapp/nginx
container_name: nginx
ports:
- "${NGINX_PORT}:${NGINX_PORT}"
volumes:
- ./www:${ROOT_DIR}
networks:
- app-network
depends_on:
- app

app:
build:
context: docker/fpm
dockerfile: Dockerfile
image: myapp/php
container_name: app
env_file:
- .env
volumes:
- ./www:${ROOT_DIR}
depends_on:
- redis
- memcached
- postgres
networks:
- app-network
redis:
image: redis:latest
container_name: ${REDIS_HOST}
networks:
- app-network
memcached:
image: memcached:latest
container_name: ${MEMCACHED_HOST}
networks:
- app-network
postgres:
image: postgres:latest
restart: always
container_name: ${POSTGRES_HOST}
ports:
- "${POSTGRES_PORT}:${POSTGRES_PORT}"
environment:
POSTGRES_DB: ${POSTGRES_DB_NAME}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_USER: ${POSTGRES_USER_NAME}
volumes:
- ./docker/db/postgresql/data:/var/lib/postgresql/data
networks:
- app-network

networks:
app-network:
driver: bridge
23 changes: 23 additions & 0 deletions docker/fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM php:8.2-fpm

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

RUN apt-get update && apt-get install -y \
libpq-dev \
zlib1g-dev \
libzip-dev \
libmemcached-dev \
libssl-dev \
&& docker-php-ext-install -j$(nproc) \
mysqli \
pdo_mysql \
zip \
&& pecl install memcached \
&& docker-php-ext-enable memcached \
&& docker-php-ext-install pdo pdo_pgsql pgsql

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

WORKDIR /var/www/html

CMD composer install;php-fpm
9 changes: 9 additions & 0 deletions docker/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM nginx:latest

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

WORKDIR /var/www/html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
27 changes: 27 additions & 0 deletions docker/nginx/hosts/otus.local.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
server {
listen 80;

server_name otus.local;

root /var/www/html;

index index.php index.html;

location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~* .php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
10 changes: 10 additions & 0 deletions www/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"require": {
"predis/predis": "^1.1"
},
"autoload": {
"psr-4": {
"Otus\\": "src/"
}
}
}
85 changes: 85 additions & 0 deletions www/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions www/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

require __DIR__ . '/vendor/autoload.php';

use Predis\Client;

$redisHost = getenv('REDIS_HOST');
$redisPort = getenv('REDIS_PORT');

$redisClient = new Client(['host' => $redisHost, 'port' => $redisPort]);

try

Check failure on line 12 in www/index.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space(s) after TRY keyword; newline found
{
$redisClient->set('connect', 'Redis connected');

Check failure on line 14 in www/index.php

View workflow job for this annotation

GitHub Actions / phpcs

Spaces must be used to indent lines; tabs are not allowed
$redisValue = $redisClient->get('connect');

Check failure on line 15 in www/index.php

View workflow job for this annotation

GitHub Actions / phpcs

Spaces must be used to indent lines; tabs are not allowed
echo "<div style=\"color:red\">$redisValue</div></br>";

Check failure on line 16 in www/index.php

View workflow job for this annotation

GitHub Actions / phpcs

Spaces must be used to indent lines; tabs are not allowed
}

Check failure on line 17 in www/index.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space after closing brace; newline found
catch (Exception $e)

Check failure on line 18 in www/index.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space(s) after closing parenthesis; found newline
{
echo 'Error connect Redis: ' . $e->getMessage() . '</br>';

Check failure on line 20 in www/index.php

View workflow job for this annotation

GitHub Actions / phpcs

Spaces must be used to indent lines; tabs are not allowed
}

$memcachedHost = getenv('MEMCACHED_HOST');
$memcachedPort = getenv('MEMCACHED_PORT');
$memcached = new Memcached();
$memcached->addServer($memcachedHost, $memcachedPort);
$memcached->set('connect', 'Memcached connected');
$memcachedValue = $memcached->get('connect');

if ($memcachedValue)

Check failure on line 30 in www/index.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space(s) after closing parenthesis; found newline
{
echo "<div style=\"color:blue\">$memcachedValue</div></br>";

Check failure on line 32 in www/index.php

View workflow job for this annotation

GitHub Actions / phpcs

Spaces must be used to indent lines; tabs are not allowed
}

Check failure on line 33 in www/index.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space after closing brace; newline found
else
{
echo 'Error connect Memcached' . '</br>';
}

$postgresHost = getenv('POSTGRES_HOST');
$postgresDBName = getenv('POSTGRES_DB_NAME');
$postgresUserName = getenv('POSTGRES_USER_NAME');
$postgresPassword = getenv('POSTGRES_PASSWORD');
$postgresPort = getenv('POSTGRES_PORT');
$dsn = "pgsql:host=$postgresHost;port=$postgresPort;dbname=$postgresDBName;";

try
{
$pdo = new PDO("pgsql:host=$postgresHost;port=$postgresPort;dbname=$postgresDBName;", $postgresUserName, $postgresPassword);
echo "<div style=\"color:orange\">Postgres connected</div></br>";
}
catch (\PDOException $e)
{
echo "Could not connect to the database $postgresDBName:" . $e->getMessage();
}
Loading