Passed
Branch 1.0.x (7d3940)
by Koldo
02:45
created

FileStructure::getNginxConf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 36
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 39
ccs 2
cts 2
cp 1
crap 1
rs 9.344
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Installer\Template\Docker;
6
7
use Antidot\Installer\Template\CommonFileStructure;
8
9
class FileStructure extends CommonFileStructure
10
{
11
    private const FILES = [
12
        'getDockerCompose' => 'docker-compose.yml',
13
        'getPhpDockerfile' => 'docker/php/Dockerfile',
14
        'getRedisIni' => 'docker/php/conf.d/redis.ini',
15
        'getNginxDockerfile' => 'docker/nginx/Dockerfile',
16
        'getNginxDefaultConf' => 'docker/nginx/default.conf',
17
        'getNginxConf' => 'docker/nginx/nginx.conf',
18
    ];
19
20
    private const DIRECTORIES = [
21
        'docker/php/conf.d',
22
        'docker/nginx',
23
    ];
24
25 2
    public function create(string $installationPath): void
26
    {
27 2
        $this->verifyInstallationPath($installationPath);
28 2
        $this->createDirectories($installationPath, self::DIRECTORIES);
29 2
        $this->createFiles($installationPath, self::FILES);
30 2
    }
31
32 2
    public static function getDockerCompose(): string
33
    {
34
        $dockerComposeContents = <<<YAML
35 2
version: "3"
36
37
volumes:
38
  redis:
39
  app:
40
41
services:
42
43
  nginx:
44
    build:
45
      context: ./docker/nginx/
46
      args:
47
        - UID=1000
48
    ports:
49
      - 80:80
50
      - 443:443
51
    links:
52
      - redis
53
      - php
54
    depends_on:
55
      - php
56
    volumes:
57
      - redis:/var/lib/redis
58
      - app:/opt/app
59
60
  php:
61
    build:
62
      context: ./docker/php/
63
      args:
64
        - UID=1000
65
    expose:
66
      - 9000
67
    links:
68
      - redis
69
    volumes:
70
      - redis:/var/lib/redis
71
      - ./:/opt/app
72
    working_dir: /opt
73
74
  redis:
75
    image: redis:latest
76
    volumes:
77
      - redis:/var/lib/redis
78
79
YAML;
80
81 2
        return $dockerComposeContents;
82
    }
83
84 2
    public static function getPhpDockerfile(): string
85
    {
86
        $phpDockerfileContents = <<<'EOT'
87 2
FROM php:7.4.9-fpm
88
89
ARG UID=1000
90
91
RUN apt-get update && apt-get install -y git zip zlib1g-dev libicu-dev g++ libxml2-dev \
92
autoconf pkg-config libssh-dev libonig-dev
93
94
RUN docker-php-ext-install pdo_mysql bcmath iconv pcntl mbstring intl calendar sockets
95
96
RUN pecl install -o -f redis \
97
&& rm -rf /tmp/pear \
98
&& docker-php-ext-enable redis 
99
100
COPY ./conf.d/redis.ini $PHP_INI_DIR/conf.d/redis.ini
101
102
RUN usermod -u ${UID} www-data
103
104
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
105
106
RUN composer global require hirak/prestissimo
107
108
EOT;
109
110 2
        return $phpDockerfileContents;
111
    }
112
113 2
    public static function getRedisIni(): string
114
    {
115
        $redisIniContents = <<<INI
116 2
session.save_handler=redis
117
session.save_path="tcp://redis/"
118
119
INI;
120
121 2
        return $redisIniContents;
122
    }
123
124 2
    public static function getNginxDockerfile(): string
125
    {
126
        $nginxDockerfileContents = <<<'EOT'
127 2
FROM nginx:latest
128
129
ARG UID=1000
130
131
RUN apt update && apt install openssl -y
132
RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048  -keyout /etc/nginx/nginx.key -out /etc/nginx/nginx.crt \
133
-subj "/C=GB/ST=London/L=London/O=Global Security/OU=IT Department/CN=example.com"
134
135
COPY ./nginx.conf /etc/nginx/nginx.conf
136
COPY ./default.conf /etc/nginx/conf.d/default.conf
137
138
RUN usermod -u ${UID} www-data
139
140
EOT;
141
142 2
        return $nginxDockerfileContents;
143
    }
144
145 2
    public static function getNginxDefaultConf(): string
146
    {
147
        $nginxDefaultConfContents = <<<'EOT'
148 2
upstream backend {
149
    least_conn;
150
    server  php:9000;
151
}
152
153
upstream app_server {
154
    least_conn;
155
    server 127.0.0.1:80;
156
}
157
158
proxy_cache_path /tmp/cache keys_zone=cache:10m levels=1:2 inactive=600s max_size=100m;
159
160
server {
161
    listen 80 default_server;
162
    root /opt/app/public;
163
    index index.php;
164
165
    charset utf-8;
166
167
    location / {
168
        try_files $uri $uri/ /index.php?$query_string;
169
    }
170
171
    access_log off;
172
    error_log  off;
173
174
    sendfile           off;
175
176
    client_max_body_size 100m;
177
178
    proxy_cache cache;
179
    proxy_cache_valid 200 1s;
180
181
    location ~ \.php$ {
182
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
183
        fastcgi_pass backend;
184
        fastcgi_index index.php;
185
        include fastcgi_params;
186
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
187
        fastcgi_intercept_errors off;
188
        fastcgi_buffer_size 16k;
189
        fastcgi_buffers 4 16k;
190
    }
191
192
    location ~ /\.ht {
193
        deny all;
194
    }
195
}
196
197
server {
198
    listen                     443 ssl http2;
199
200
    ssl                        on;
201
    ssl_protocols              TLSv1 TLSv1.1 TLSv1.2;
202
    ssl_certificate            nginx.crt;
203
    ssl_certificate_key        nginx.key;
204
205
    location / {
206
        proxy_pass          http://app_server;
207
        proxy_set_header    Host      $host;
208
        proxy_set_header    X-Real-IP $remote_addr;
209
        proxy_set_header    X-HTTPS   'True';
210
    }
211
}
212
213
EOT;
214
215 2
        return $nginxDefaultConfContents;
216
    }
217
218 2
    public static function getNginxConf(): string
219
    {
220
        $nginxConfContents = <<<'EOT'
221
222
user  nginx;
223
worker_processes  8;
224
225
error_log  /var/log/nginx/error.log warn;
226
pid        /var/run/nginx.pid;
227
228
229
events {
230
    worker_connections  2048;
231
}
232
233
234
http {
235
    include       /etc/nginx/mime.types;
236
    default_type  application/octet-stream;
237
238
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
239
                      '$status $body_bytes_sent "$http_referer" '
240
                      '"$http_user_agent" "$http_x_forwarded_for"';
241
242
    access_log  /var/log/nginx/access.log  main;
243
244
    sendfile        on;
245
    #tcp_nopush     on;
246
247
    keepalive_timeout  65;
248
249
    #gzip  on;
250
251
    include /etc/nginx/conf.d/*.conf;
252
}
253
254
EOT;
255
256 2
        return $nginxConfContents;
257
    }
258
}
259