├── .gitignore ├── LICENSE ├── README.md ├── step1-nginx-static-server ├── data │ └── html │ │ ├── img.jpg │ │ ├── index.html │ │ ├── main.css │ │ └── main.js ├── docker-compose.yml └── web │ └── default.conf ├── step2-php ├── data │ └── html │ │ ├── img.jpg │ │ ├── index.php │ │ ├── main.css │ │ └── main.js ├── docker-compose.yml └── web │ └── default.conf ├── step3-connect-mysql ├── .env ├── app │ └── Dockerfile ├── data │ └── html │ │ ├── img.jpg │ │ ├── index.php │ │ ├── main.css │ │ └── main.js ├── db │ └── initial.sql ├── docker-compose.yml └── web │ └── default.conf └── step4-enable-http2 ├── .env ├── app └── Dockerfile ├── data └── html │ ├── img.jpg │ ├── index.php │ ├── main.css │ └── main.js ├── db └── initial.sql ├── docker-compose.yml └── web ├── Dockerfile └── default.conf /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled files 2 | *.tfstate 3 | *.tfstate.backup 4 | 5 | # Module directory 6 | .terraform/ 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 YAMADA Naoki 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | nginx-http2-php-mysql 2 | === 3 | 4 | 『[docker-compose による NGINX + HTTP/2 + PHP-FPM7 + MySQL 環境の構築方法](https://tech.recruit-mp.co.jp/infrastructure/post-12795/)』で紹介しているサンプルコードです。 5 | 6 | - Step.1) NGINX だけのシンプルな静的 web サーバを構築 7 | - Step.2) PHP 環境を構築 8 | - Step.3) PHP と MySQL を接続 9 | - Step.4) HTTP/2 に対応させる 10 | -------------------------------------------------------------------------------- /step1-nginx-static-server/data/html/img.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wakamsha/nginx-http2-php-mysql/1c3a685db1295598cc7e1d85e891d036f69645c3/step1-nginx-static-server/data/html/img.jpg -------------------------------------------------------------------------------- /step1-nginx-static-server/data/html/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Step.1 nginx Static Server 6 | 7 | 8 | 9 |
10 |

nginx Static Server

11 | 12 |
13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /step1-nginx-static-server/data/html/main.css: -------------------------------------------------------------------------------- 1 | html { 2 | height: 100%; 3 | background-image: linear-gradient(white, #fafafa); 4 | } 5 | 6 | body { 7 | margin: 0; 8 | } 9 | 10 | .container { 11 | text-align: center; 12 | padding: 64px 0; 13 | } 14 | .title { 15 | color: #ead3a4; 16 | font-weight: 600; 17 | font-family: serif; 18 | margin: 0 0 24px; 19 | } 20 | .thumbnail { 21 | filter: brightness(.9); 22 | transition: filter .5s; 23 | display: inline-block; 24 | } 25 | .thumbnail:hover { 26 | filter: brightness(1.2); 27 | transition: filter .4s cubic-bezier(0, 2.5, 0.2, 2.5); 28 | } 29 | -------------------------------------------------------------------------------- /step1-nginx-static-server/data/html/main.js: -------------------------------------------------------------------------------- 1 | console.log(`this. page is ${document.title}`); 2 | 3 | // alert('hello world!'); 4 | -------------------------------------------------------------------------------- /step1-nginx-static-server/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | web: 4 | image: nginx:1.13.5-alpine 5 | ports: 6 | - "80:80" 7 | volumes: 8 | - ./web/default.conf:/etc/nginx/conf.d/default.conf 9 | - ./data/html:/var/www/html 10 | -------------------------------------------------------------------------------- /step1-nginx-static-server/web/default.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | server_name _; 4 | 5 | root /var/www/html; 6 | index index.html index.htm; 7 | 8 | access_log /var/log/nginx/access.log; 9 | error_log /var/log/nginx/error.log; 10 | } 11 | -------------------------------------------------------------------------------- /step2-php/data/html/img.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wakamsha/nginx-http2-php-mysql/1c3a685db1295598cc7e1d85e891d036f69645c3/step2-php/data/html/img.jpg -------------------------------------------------------------------------------- /step2-php/data/html/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Step.2 nginx + PHP+FPM Server 6 | 7 | 8 | 9 |
10 |

nginx + PHP+FPM Server

11 | 12 |
13 | 14 | 15 | 16 | 2 | 3 | 4 | 5 | Step.3 nginx + PHP + MySQL 6 | 7 | 8 | 9 |
10 |

nginx + PHP + MySQL

11 | 12 | 13 | query($sql); 25 | 26 | $sql = "SELECT * FROM hoges ORDER BY hoge_id desc limit 1"; 27 | 28 | $result = $mysql->query($sql)->fetch_row(); 29 | 30 | echo '
';
31 |         var_dump($result);
32 |         echo '
'; 33 | 34 | mysqli_close($mysql); 35 | 36 | phpinfo(); 37 | ?> 38 |
39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /step3-connect-mysql/data/html/main.css: -------------------------------------------------------------------------------- 1 | html { 2 | height: 100%; 3 | background-image: linear-gradient(white, #fafafa); 4 | } 5 | 6 | body { 7 | margin: 0; 8 | } 9 | 10 | .container { 11 | padding: 64px 0; 12 | max-width: 100%; 13 | width: 960px; 14 | margin: auto; 15 | } 16 | .title { 17 | color: #ead3a4; 18 | font-weight: 600; 19 | font-family: serif; 20 | margin: 0 0 24px; 21 | text-align: center; 22 | } 23 | .thumbnail { 24 | filter: brightness(.9); 25 | transition: filter .5s; 26 | display: block; 27 | width: 350px; 28 | margin: auto; 29 | float: none; 30 | } 31 | .thumbnail:hover { 32 | filter: brightness(1.2); 33 | transition: filter .4s cubic-bezier(0, 2.5, 0.2, 2.5); 34 | } 35 | 36 | .log { 37 | background: #eee; 38 | padding: 16px; 39 | margin: 24px 0; 40 | } 41 | -------------------------------------------------------------------------------- /step3-connect-mysql/data/html/main.js: -------------------------------------------------------------------------------- 1 | console.log(`this. page is ${document.title}`); 2 | 3 | // alert('hello world!'); 4 | -------------------------------------------------------------------------------- /step3-connect-mysql/db/initial.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `hoges` ( 2 | `hoge_id` int(20) unsigned NOT NULL AUTO_INCREMENT, 3 | `created_at` datetime NOT NULL, 4 | PRIMARY KEY (`hoge_id`) 5 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; 6 | -------------------------------------------------------------------------------- /step3-connect-mysql/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | web: 4 | image: nginx:1.13.5-alpine 5 | ports: 6 | - "80:80" 7 | depends_on: 8 | - app 9 | volumes: 10 | - ./web/default.conf:/etc/nginx/conf.d/default.conf 11 | - ./data/html:/var/www/html 12 | 13 | app: 14 | build: ./app 15 | env_file: .env 16 | environment: 17 | DATABASE_HOST: db 18 | depends_on: 19 | - db 20 | volumes: 21 | - ./data/html:/var/www/html 22 | 23 | db: 24 | image: mysql:5.7.19 25 | env_file: .env 26 | ports: 27 | - "3306:3306" 28 | volumes: 29 | - db-data:/var/lib/mysql 30 | - ./db/initial.sql:/docker-entrypoint-initdb.d/initial.sql 31 | 32 | volumes: 33 | db-data: 34 | -------------------------------------------------------------------------------- /step3-connect-mysql/web/default.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | server_name _; 4 | 5 | root /var/www/html; 6 | index index.php; 7 | 8 | access_log /var/log/nginx/access.log; 9 | error_log /var/log/nginx/error.log; 10 | 11 | location / { 12 | try_files $uri $uri/ /index.php$is_args$args; 13 | } 14 | 15 | location ~ \.php$ { 16 | fastcgi_split_path_info ^(.+\.php)(\.+)$; 17 | fastcgi_pass app:9000; 18 | fastcgi_index index.php; 19 | include fastcgi_params; 20 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 21 | fastcgi_param PATH_INFO $fastcgi_path_info; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /step4-enable-http2/.env: -------------------------------------------------------------------------------- 1 | MYSQL_RANDOM_ROOT_PASSWORD=yes 2 | MYSQL_DATABASE=step4 3 | MYSQL_USER=db_user 4 | MYSQL_PASSWORD=password 5 | -------------------------------------------------------------------------------- /step4-enable-http2/app/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:7.1.9-fpm-alpine 2 | RUN docker-php-ext-install pdo_mysql mysqli mbstring 3 | -------------------------------------------------------------------------------- /step4-enable-http2/data/html/img.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wakamsha/nginx-http2-php-mysql/1c3a685db1295598cc7e1d85e891d036f69645c3/step4-enable-http2/data/html/img.jpg -------------------------------------------------------------------------------- /step4-enable-http2/data/html/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Step.4 Enable HTTP/2 6 | 7 | 8 | 9 |
10 |

Enable HTTP/2

11 | 12 | 13 | query($sql); 25 | 26 | $sql = "SELECT * FROM hoges ORDER BY hoge_id desc limit 1"; 27 | 28 | $result = $mysql->query($sql)->fetch_row(); 29 | 30 | echo '
';
31 |         var_dump($result);
32 |         echo '
'; 33 | 34 | mysqli_close($mysql); 35 | 36 | phpinfo(); 37 | ?> 38 |
39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /step4-enable-http2/data/html/main.css: -------------------------------------------------------------------------------- 1 | html { 2 | height: 100%; 3 | background-image: linear-gradient(white, #fafafa); 4 | } 5 | 6 | body { 7 | margin: 0; 8 | } 9 | 10 | .container { 11 | padding: 64px 0; 12 | max-width: 100%; 13 | width: 960px; 14 | margin: auto; 15 | } 16 | .title { 17 | color: #ead3a4; 18 | font-weight: 600; 19 | font-family: serif; 20 | margin: 0 0 24px; 21 | text-align: center; 22 | } 23 | .thumbnail { 24 | filter: brightness(.9); 25 | transition: filter .5s; 26 | display: block; 27 | width: 350px; 28 | margin: auto; 29 | float: none; 30 | } 31 | .thumbnail:hover { 32 | filter: brightness(1.2); 33 | transition: filter .4s cubic-bezier(0, 2.5, 0.2, 2.5); 34 | } 35 | 36 | .log { 37 | background: #eee; 38 | padding: 16px; 39 | margin: 24px 0; 40 | } 41 | -------------------------------------------------------------------------------- /step4-enable-http2/data/html/main.js: -------------------------------------------------------------------------------- 1 | console.log(`this. page is ${document.title}`); 2 | 3 | // alert('hello world!'); 4 | -------------------------------------------------------------------------------- /step4-enable-http2/db/initial.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `hoges` ( 2 | `hoge_id` int(20) unsigned NOT NULL AUTO_INCREMENT, 3 | `created_at` datetime NOT NULL, 4 | PRIMARY KEY (`hoge_id`) 5 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; 6 | -------------------------------------------------------------------------------- /step4-enable-http2/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | web: 4 | build: ./web 5 | ports: 6 | - "80:80" 7 | - "443:443" 8 | depends_on: 9 | - app 10 | volumes: 11 | - ./web/default.conf:/etc/nginx/conf.d/default.conf 12 | - ./data/html:/var/www/html 13 | 14 | app: 15 | build: ./app 16 | env_file: .env 17 | environment: 18 | DATABASE_HOST: db 19 | depends_on: 20 | - db 21 | volumes: 22 | - ./data/html:/var/www/html 23 | 24 | db: 25 | image: mysql:5.7.19 26 | env_file: .env 27 | ports: 28 | - "3306:3306" 29 | volumes: 30 | - db-data:/var/lib/mysql 31 | - ./db/initial.sql:/docker-entrypoint-initdb.d/initial.sql 32 | 33 | volumes: 34 | db-data: 35 | -------------------------------------------------------------------------------- /step4-enable-http2/web/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:1.13.5-alpine 2 | 3 | # ツールをインストール 4 | RUN apk --update add openssl 5 | 6 | # ルートディレクトリを作成 7 | RUN mkdir -p /var/www/html 8 | 9 | # 自己証明書を発行 10 | RUN openssl genrsa 2048 > server.key \ 11 | && openssl req -new -key server.key -subj "/C=JP/ST=Tokyo/L=Chuo-ku/O=RMP Inc./OU=web/CN=localhost" > server.csr \ 12 | && openssl x509 -in server.csr -days 3650 -req -signkey server.key > server.crt \ 13 | && cp server.crt /etc/nginx/server.crt \ 14 | && cp server.key /etc/nginx/server.key \ 15 | && chmod 755 -R /var/www/html \ 16 | && chmod 400 /etc/nginx/server.key 17 | -------------------------------------------------------------------------------- /step4-enable-http2/web/default.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | server_name _; 4 | return 301 https://$host$request_uri; 5 | } 6 | 7 | server { 8 | listen 443 ssl http2; 9 | server_name _; 10 | 11 | root /var/www/html; 12 | index index.php; 13 | 14 | access_log /var/log/nginx/access.log; 15 | error_log /var/log/nginx/error.log; 16 | 17 | location / { 18 | try_files $uri $uri/ /index.php$is_args$args; 19 | } 20 | 21 | location ~ \.php$ { 22 | fastcgi_split_path_info ^(.+\.php)(\.+)$; 23 | fastcgi_pass app:9000; 24 | fastcgi_index index.php; 25 | include fastcgi_params; 26 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 27 | fastcgi_param PATH_INFO $fastcgi_path_info; 28 | } 29 | 30 | # SSL 暗号化 31 | ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 32 | ssl_certificate /etc/nginx/server.crt; 33 | ssl_certificate_key /etc/nginx/server.key; 34 | ssl_session_timeout 1d; 35 | ssl_session_cache shared:SSL:50m; 36 | } 37 | --------------------------------------------------------------------------------