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

DSmolyaninov/hw1 #96

Open
wants to merge 3 commits into
base: 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
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MYSQL_ROOT_PASSWORD=rootpassword
MYSQL_DATABASE=mydb
MYSQL_USER=user
MYSQL_PASSWORD=userpassword
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.DS_Store
.AppleDouble
.LSOverride
.env
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# PHP_2024
# HW1

https://otus.ru/lessons/razrabotchik-php/?utm_source=github&utm_medium=free&utm_campaign=otus
## Настройка переменных окружения

Скопировать .env.example в новый файл .env и настроить переменные окружения в нем:

```bash
cp .env.example .env
```

## Сборка и запуск контейнеров

```bash
docker-compose up -d --build
```

## Установка зависимостей

```bash
docker-compose run --rm php-fpm composer install -d /var/www/html
```
48 changes: 48 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
version: '3.8'

services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
- ./www:/var/www/html
depends_on:
- php-fpm

php-fpm:
build:
context: ./php-fpm
dockerfile: Dockerfile
volumes:
- ./www:/var/www/html
depends_on:
- redis
- memcached

redis:
image: redis:latest
ports:
- "6379:6379"

memcached:
image: memcached:latest
ports:
- "11211:11211"

db:
image: mysql:8.0
platform: linux/amd64 # Если на Mac с чипом M1/M2
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- db-data:/var/lib/mysql
ports:
- "3306:3306"

volumes:
db-data:
19 changes: 19 additions & 0 deletions nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
server {
listen 80;
server_name localhost;

root /var/www/html;
index index.php index.html index.htm;

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
28 changes: 28 additions & 0 deletions php-fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM php:8.2-fpm

# Установка зависимостей для расширений PHP и других пакетов
RUN apt-get update && apt-get install -y \
libmemcached-dev \
libz-dev \
libpq-dev \
libjpeg-dev \
libpng-dev \
libfreetype6-dev \
libssl-dev \
libmcrypt-dev \
libzip-dev \
zlib1g-dev \
unzip \
# Очищаем кеш apt-get для уменьшения размера образа
&& rm -rf /var/lib/apt/lists/*

# Конфигурируем и устанавливаем расширения PHP
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install pdo_mysql mysqli zip gd

# Установка расширения Memcached через PECL
RUN pecl install memcached \
&& docker-php-ext-enable memcached

# Установка Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
5 changes: 5 additions & 0 deletions www/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"predis/predis": "^1.1"
}
}
40 changes: 40 additions & 0 deletions www/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

require 'vendor/autoload.php';

echo "<h1>Тестирование Redis...</h1>";
try {
$redis = new Predis\Client([
'scheme' => 'tcp',
'host' => 'redis',
'port' => 6379,
]);

if ($redis->ping()) {
echo "Redis PONG!<br>";
}
} catch (Exception $e) {
echo "Соединение с Redis не удалось: " . $e->getMessage();
}

echo "<h1>Тестирование Memcached...</h1>";
try {
$mem = new Memcached();
$mem->addServer('memcached', 11211);
$mem->set('key', 'Hello, Memcached!');
if ($mem->get('key')) {
echo $mem->get('key');
}
} catch (Exception $e) {
echo "Соединение с Memcached не удалось: " . $e->getMessage();
}

echo "<h1>Тестирование MySQL...</h1>";

try {
$conn = new PDO("mysql:host=db;dbname=mydb", "user", "userpassword");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Успешное соединение с MySQL";
} catch (PDOException $e) {
echo "Соединение с MySQL не удалось: " . $e->getMessage();
}
Loading