diff --git a/.env.example b/.env.example new file mode 100644 index 000000000..825b28b98 --- /dev/null +++ b/.env.example @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore index b28e598f1..04ee46f9f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ .DS_Store .AppleDouble .LSOverride +/www/vendor/ +/docker/db/ +.env \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 000000000..e58cccbe7 --- /dev/null +++ b/docker-compose.yaml @@ -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 \ No newline at end of file diff --git a/docker/fpm/Dockerfile b/docker/fpm/Dockerfile new file mode 100644 index 000000000..884e2bb4c --- /dev/null +++ b/docker/fpm/Dockerfile @@ -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 \ No newline at end of file diff --git a/docker/nginx/Dockerfile b/docker/nginx/Dockerfile new file mode 100644 index 000000000..59955c903 --- /dev/null +++ b/docker/nginx/Dockerfile @@ -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;"] \ No newline at end of file diff --git a/docker/nginx/hosts/otus.local.conf b/docker/nginx/hosts/otus.local.conf new file mode 100644 index 000000000..58cadce2f --- /dev/null +++ b/docker/nginx/hosts/otus.local.conf @@ -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; + } +} \ No newline at end of file diff --git a/www/composer.json b/www/composer.json new file mode 100644 index 000000000..33bcbb068 --- /dev/null +++ b/www/composer.json @@ -0,0 +1,10 @@ +{ + "require": { + "predis/predis": "^1.1" + }, + "autoload": { + "psr-4": { + "Otus\\": "src/" + } + } +} \ No newline at end of file diff --git a/www/composer.lock b/www/composer.lock new file mode 100644 index 000000000..c3e35472d --- /dev/null +++ b/www/composer.lock @@ -0,0 +1,85 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "80aa1bb66ab44973c6b98031709a4dbc", + "packages": [ + { + "name": "predis/predis", + "version": "v1.1.10", + "source": { + "type": "git", + "url": "https://github.com/predis/predis.git", + "reference": "a2fb02d738bedadcffdbb07efa3a5e7bd57f8d6e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/predis/predis/zipball/a2fb02d738bedadcffdbb07efa3a5e7bd57f8d6e", + "reference": "a2fb02d738bedadcffdbb07efa3a5e7bd57f8d6e", + "shasum": "" + }, + "require": { + "php": ">=5.3.9" + }, + "require-dev": { + "phpunit/phpunit": "~4.8" + }, + "suggest": { + "ext-curl": "Allows access to Webdis when paired with phpiredis", + "ext-phpiredis": "Allows faster serialization and deserialization of the Redis protocol" + }, + "type": "library", + "autoload": { + "psr-4": { + "Predis\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Daniele Alessandri", + "email": "suppakilla@gmail.com", + "homepage": "http://clorophilla.net", + "role": "Creator & Maintainer" + }, + { + "name": "Till Krüss", + "homepage": "https://till.im", + "role": "Maintainer" + } + ], + "description": "Flexible and feature-complete Redis client for PHP and HHVM", + "homepage": "http://github.com/predis/predis", + "keywords": [ + "nosql", + "predis", + "redis" + ], + "support": { + "issues": "https://github.com/predis/predis/issues", + "source": "https://github.com/predis/predis/tree/v1.1.10" + }, + "funding": [ + { + "url": "https://github.com/sponsors/tillkruss", + "type": "github" + } + ], + "time": "2022-01-05T17:46:08+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.6.0" +} diff --git a/www/index.php b/www/index.php new file mode 100644 index 000000000..e22808d03 --- /dev/null +++ b/www/index.php @@ -0,0 +1,54 @@ + $redisHost, 'port' => $redisPort]); + +try +{ + $redisClient->set('connect', 'Redis connected'); + $redisValue = $redisClient->get('connect'); + echo "
$redisValue

"; +} +catch (Exception $e) +{ + echo 'Error connect Redis: ' . $e->getMessage() . '
'; +} + +$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) +{ + echo "
$memcachedValue

"; +} +else +{ + echo 'Error connect Memcached' . '
'; +} + +$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 "
Postgres connected

"; +} +catch (\PDOException $e) +{ + echo "Could not connect to the database $postgresDBName:" . $e->getMessage(); +} \ No newline at end of file