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

[HW-1] Create local environment #24

Open
wants to merge 2 commits into
base: AAristov/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
15 changes: 15 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
APP_NAME=
APP_PORT=

DB_HOST=
DB_PORT=
DB_NAME=
DB_USER=
DB_PASSWORD=

REDIS_HOST=
REDIS_PORT=
REDIS_PASSWORD=

MEMCACHED_HOST=
MEMCACHED_PORT=
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
.DS_Store
.AppleDouble
.LSOverride
.env
postgres/data
Binary file added app/img.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions app/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

echo "<h1>Application " . getenv('APP_NAME') . " - " . date('Y') . "</h1>";
echo "<img src='img.jpg'>";

$redis = new Redis();
if ($redis->connect(getenv('REDIS_HOST'))) {
echo "<p>Redis is connected!</p>";
} else {
echo "<p>Redis is not connected!</p>";
}


$m = new Memcached();
if ($m->addServer(getenv('MEMCACHED_HOST'), getenv('MEMCACHED_PORT'))) {
echo "<p>Memcached is connected!</p>";
} else {
echo "<p>Memcached is not connected!</p>";
}

$dbconn = pg_connect(
"host=" . getenv('DB_HOST') . " " .
"dbname=" . getenv('DB_NAME') . " " .
"user=" . getenv('DB_USER') . " " .
"password=" . getenv('DB_PASSWORD')
);
if (!$dbconn) {
echo "<p>DB is not connected!</p>";
} else {
pg_close($dbconn);
echo "<p>DB is connected!</p>";
}
66 changes: 66 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
version: '3.8'

networks:
app_network:
name: ${APP_NAME}_network

services:

nginx:
image: nginx:latest
container_name: ${APP_NAME}_nginx
ports:
- "${APP_PORT}:80"
volumes:
- ./app:/var/www/app
- ./nginx/app.conf:/etc/nginx/conf.d/app.conf
networks:
- app_network

php:
build:
context: .
dockerfile: ./php/Dockerfile
container_name: ${APP_NAME}_php
env_file: .env
ports:
- "9000:9000"
volumes:
- ./app:/var/www/app
networks:
- app_network

postgres:
image: postgres:latest
container_name: ${APP_NAME}_postgres
ports:
- "${DB_PORT}:5432"
restart: on-failure
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
volumes:
- ./postgres/data:/var/lib/postgresql/data:rw
networks:
- app_network

redis:
image: redis:latest
container_name: ${APP_NAME}_redis
restart: always
ports:
- "${REDIS_PORT}:6379"
environment:
REDIS_PASSWORD: ${REDIS_PASSWORD}
networks:
- app_network

memcached:
image: memcached:latest
container_name: ${APP_NAME}_memcached
ports:
- "${MEMCACHED_PORT}:11211"
restart: always
networks:
- app_network
68 changes: 68 additions & 0 deletions nginx/app.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
upstream _php {
server unix:/sock/docker.sock;
}

server {
listen 80;
index index.php;
server_name application.local;
root /var/www/app;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;

location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}

# optionally disable falling back to PHP script for the asset directories;
# nginx will return a 404 error when files are not found instead of passing the
# request to Symfony (improves performance but Symfony's 404 page is not displayed)
# location /bundles {
# try_files $uri =404;
# }

location ~ ^/index\.php(/|$) {
# when using PHP-FPM as a unix socket
fastcgi_pass php:9000;

# when PHP-FPM is configured to use TCP
# fastcgi_pass 127.0.0.1:9000;

fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;

# optionally set the value of the environment variables used in the application
# fastcgi_param APP_ENV prod;
# fastcgi_param APP_SECRET <app-secret-id>;
# fastcgi_param DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name";

# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
# Caveat: When PHP-FPM is hosted on a different machine from nginx
# $realpath_root may not resolve as you expect! In this case try using
# $document_root instead.
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;

fastcgi_buffer_size 128K;
fastcgi_buffers 4 256K;
#fastcgi_busy_buffers_size: 256K;

# Prevents URIs that include the front controller. This will 404:
# http://example.com/index.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}

# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
}
16 changes: 16 additions & 0 deletions php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM php:8.2-fpm

WORKDIR /var/www/app

RUN apt-get update && apt-get install -y zlib1g-dev libmemcached-dev g++ git libicu-dev zip libzip-dev libpq-dev \
&& docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
&& docker-php-ext-install intl opcache pdo pdo_pgsql pgsql \
&& pecl install apcu \
&& docker-php-ext-enable apcu \
&& docker-php-ext-configure zip \
&& docker-php-ext-install zip \
&& pecl install redis \
&& docker-php-ext-enable redis \
&& pecl install memcached \
&& docker-php-ext-enable memcached \
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Loading