├── .docker ├── api │ ├── Dockerfile │ ├── conf │ │ ├── docker.conf │ │ └── php.ini │ └── entrypoint.sh ├── db │ ├── Dockerfile │ └── my.cnf ├── front │ └── Dockerfile └── nginx │ ├── Dockerfile │ └── default.conf ├── .gitignore ├── Makefile ├── README.md └── docker-compose.yml /.docker/api/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:7.4-fpm-alpine 2 | 3 | ENV TZ Asia/Tokyo 4 | ENV COMPOSER_ALLOW_SUPERUSER 1 5 | 6 | # install Library 7 | # ベースイメージのOSがalpineの場合は xdebug を使うために autoconf gcc g++ make が必要 8 | RUN apk update && \ 9 | apk add --no-cache --virtual \ 10 | .php-builds \ 11 | oniguruma-dev \ 12 | git \ 13 | zip \ 14 | unzip \ 15 | vim \ 16 | autoconf gcc g++ make 17 | 18 | # add php-extention-module & xdebug 19 | RUN docker-php-ext-install mbstring pdo_mysql bcmath && \ 20 | docker-php-ext-enable mbstring && \ 21 | pecl install xdebug && \ 22 | docker-php-ext-enable xdebug 23 | 24 | # config file COPY 25 | COPY conf/php.ini /usr/local/etc/php/php.ini 26 | COPY conf/docker.conf /usr/local/etc/php-fpm.d/docker.conf 27 | 28 | # install Composer 29 | COPY --from=composer:2.0 /usr/bin/composer /usr/bin/composer 30 | 31 | COPY entrypoint.sh / 32 | RUN chmod +x /entrypoint.sh 33 | # ENTRYPOINT ["docker-entrypoint.sh"] 34 | ENTRYPOINT ["/entrypoint.sh"] 35 | 36 | WORKDIR /app 37 | 38 | CMD ["/bin/ash"] -------------------------------------------------------------------------------- /.docker/api/conf/docker.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | error_log = /proc/self/fd/2 3 | 4 | [www] 5 | ; if we send this to /proc/self/fd/1, it never appears 6 | access.log = /proc/self/fd/1 7 | 8 | clear_env = no 9 | 10 | ; Ensure worker stdout and stderr are sent to the main error log. 11 | catch_workers_output = yes 12 | -------------------------------------------------------------------------------- /.docker/api/conf/php.ini: -------------------------------------------------------------------------------- 1 | [PHP] 2 | engine = On 3 | short_open_tag = Off 4 | precision = 14 5 | output_buffering = 4096 6 | zlib.output_compression = Off 7 | implicit_flush = Off 8 | unserialize_callback_func = 9 | serialize_precision = -1 10 | disable_functions = 11 | disable_classes = 12 | zend.enable_gc = On 13 | expose_php = Off 14 | 15 | max_execution_time = 30 16 | 17 | max_input_time = 60 18 | memory_limit = 512M 19 | 20 | error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT 21 | display_errors = Off 22 | display_startup_errors = Off 23 | 24 | log_errors = On 25 | log_errors_max_len = 1024 26 | error_log = php://stderr 27 | 28 | ignore_repeated_errors = Off 29 | ignore_repeated_source = Off 30 | 31 | report_memleaks = On 32 | track_errors = Off 33 | html_errors = On 34 | 35 | variables_order = "GPCS" 36 | request_order = "GP" 37 | 38 | register_argc_argv = Off 39 | auto_globals_jit = On 40 | 41 | post_max_size = 128M 42 | auto_prepend_file = 43 | auto_append_file = 44 | 45 | default_mimetype = "text/html" 46 | default_charset = "UTF-8" 47 | 48 | doc_root = 49 | user_dir = 50 | enable_dl = Off 51 | 52 | file_uploads = On 53 | 54 | upload_max_filesize = 128M 55 | max_file_uploads = 20 56 | 57 | allow_url_fopen = On 58 | allow_url_include = Off 59 | 60 | default_socket_timeout = 60 61 | 62 | [CLI Server] 63 | cli_server.color = On 64 | 65 | [Date] 66 | date.timezone = Asia/Tokyo 67 | 68 | [sqlite3] 69 | sqlite3.defensive = 1 70 | 71 | [Pcre] 72 | 73 | [Pdo] 74 | 75 | [Pdo_mysql] 76 | pdo_mysql.cache_size = 2000 77 | pdo_mysql.default_socket= 78 | 79 | [Phar] 80 | 81 | [mail function] 82 | SMTP = localhost 83 | smtp_port = 25 84 | mail.add_x_header = Off 85 | ;mail.log = 86 | ;mail.log = syslog 87 | 88 | [SQL] 89 | sql.safe_mode = Off 90 | 91 | [ODBC] 92 | odbc.allow_persistent = On 93 | odbc.check_persistent = On 94 | odbc.max_persistent = -1 95 | odbc.max_links = -1 96 | odbc.defaultlrl = 4096 97 | odbc.defaultbinmode = 1 98 | 99 | [Interbase] 100 | ibase.allow_persistent = 1 101 | ibase.max_persistent = -1 102 | ibase.max_links = -1 103 | ibase.timestampformat = "%Y-%m-%d %H:%M:%S" 104 | ibase.dateformat = "%Y-%m-%d" 105 | ibase.timeformat = "%H:%M:%S" 106 | 107 | [MySQLi] 108 | mysqli.max_persistent = -1 109 | mysqli.allow_persistent = On 110 | mysqli.max_links = -1 111 | mysqli.cache_size = 2000 112 | mysqli.default_port = 3306 113 | mysqli.default_socket = 114 | mysqli.default_host = 115 | mysqli.default_user = 116 | mysqli.default_pw = 117 | mysqli.reconnect = Off 118 | 119 | [mysqlnd] 120 | mysqlnd.collect_statistics = On 121 | mysqlnd.collect_memory_statistics = Off 122 | 123 | [OCI8] 124 | 125 | [PostgreSQL] 126 | pgsql.allow_persistent = On 127 | pgsql.auto_reset_persistent = Off 128 | pgsql.max_persistent = -1 129 | pgsql.max_links = -1 130 | pgsql.ignore_notice = 0 131 | pgsql.log_notice = 0 132 | 133 | [bcmath] 134 | bcmath.scale = 0 135 | 136 | [browscap] 137 | 138 | [Session] 139 | session.save_handler = files 140 | session.use_strict_mode = 0 141 | session.use_cookies = 1 142 | session.use_only_cookies = 1 143 | session.name = PHPSESSID 144 | session.auto_start = 0 145 | session.cookie_lifetime = 0 146 | session.cookie_path = / 147 | session.cookie_domain = 148 | session.cookie_httponly = 149 | session.serialize_handler = php 150 | session.gc_probability = 1 151 | session.gc_divisor = 1000 152 | session.gc_maxlifetime = 1440 153 | session.referer_check = 154 | session.cache_limiter = nocache 155 | session.cache_expire = 180 156 | session.use_trans_sid = 0 157 | session.sid_length = 26 158 | session.trans_sid_tags = "a=href,area=href,frame=src,form=" 159 | session.sid_bits_per_character = 5 160 | 161 | [Assertion] 162 | zend.assertions = -1 163 | 164 | [COM] 165 | 166 | [mbstring] 167 | mbstring.language = Japanese 168 | mbstring.encoding_translation = Off 169 | mbstring.detect_order = auto 170 | 171 | [gd] 172 | 173 | [exif] 174 | 175 | [Tidy] 176 | tidy.clean_output = Off 177 | 178 | [soap] 179 | soap.wsdl_cache_enabled=1 180 | soap.wsdl_cache_dir="/tmp" 181 | soap.wsdl_cache_ttl=86400 182 | soap.wsdl_cache_limit = 5 183 | 184 | [sysvshm] 185 | 186 | [ldap] 187 | ldap.max_links = -1 188 | 189 | [mcrypt] 190 | 191 | [dba] 192 | 193 | [opcache] 194 | 195 | [curl] 196 | 197 | [openssl] 198 | 199 | [xdebug] 200 | xdebug.mode=debug 201 | xdebug.client_host=host.docker.internal 202 | xdebug.client_port=9003 203 | xdebug.start_with_request=yes 204 | xdebug.log=/tmp/xdebug.log 205 | xdebug.log_level=0 -------------------------------------------------------------------------------- /.docker/api/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cd /app 4 | 5 | # .envが存在しなければ、.env.localからコピー 6 | if [ ! -e ".env" ]; then 7 | cp .env.example .env 8 | fi 9 | 10 | # PHP-FPM 起動 11 | exec php-fpm -------------------------------------------------------------------------------- /.docker/db/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM --platform=linux/x86_64 mysql:8.0 2 | 3 | ENV TZ=UTC 4 | 5 | COPY my.cnf /etc/my.cnf -------------------------------------------------------------------------------- /.docker/db/my.cnf: -------------------------------------------------------------------------------- 1 | [mysqld] 2 | user=mysql 3 | character_set_server = utf8mb4 4 | collation_server = utf8mb4_0900_ai_ci 5 | 6 | # timezone 7 | default-time-zone = SYSTEM 8 | log_timestamps = SYSTEM 9 | 10 | # Error Log 11 | log-error = mysql-error.log 12 | 13 | # Slow Query Log 14 | slow_query_log = 1 15 | slow_query_log_file = mysql-slow.log 16 | long_query_time = 1.0 17 | log_queries_not_using_indexes = 0 18 | 19 | # General Log 20 | general_log = 1 21 | general_log_file = mysql-general.log 22 | 23 | [mysql] 24 | default-character-set = utf8mb4 25 | 26 | [client] 27 | default-character-set = utf8mb4 -------------------------------------------------------------------------------- /.docker/front/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:14.17-alpine 2 | 3 | RUN apk update 4 | 5 | ENV TZ Asia/Tokyo 6 | ENV PATH $HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH 7 | 8 | WORKDIR /app -------------------------------------------------------------------------------- /.docker/nginx/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:1.19-alpine 2 | 3 | ENV TZ Asia/Tokyo 4 | 5 | COPY default.conf /etc/nginx/conf.d/default.conf 6 | -------------------------------------------------------------------------------- /.docker/nginx/default.conf: -------------------------------------------------------------------------------- 1 | server { 2 | # ポートを設定 3 | ## default_serverを指定すると想定外のアクセスが来てもこのserverブロックで処理する 4 | listen 80 default_server; 5 | 6 | # 仮想サーバの名前 7 | server_name localhost; 8 | 9 | # ログの出力 10 | access_log /var/log/nginx/access.log; 11 | error_log /var/log/nginx/error.log; 12 | 13 | # HTTPの持続的な接続時間 14 | keepalive_timeout 120; 15 | 16 | charset utf-8; 17 | 18 | # レスポンスヘッダーに追加 19 | ## ブラウザーに HTTP の代わりに HTTPS を用いて通信を行うよう指示 20 | add_header Strict-Transport-Security "max-age=15768000" always; 21 | ## ユーザーエージェントごとにキャッシュを分ける 22 | add_header Vary "User-Agent"; 23 | ## クリックジャッキング対策 24 | add_header X-Frame-Options "SAMEORIGIN"; 25 | ## XSS対策 26 | add_header X-XSS-Protection "1; mode=block"; 27 | ## IEで発生するコンテンツタイプSniffing対策 28 | add_header X-Content-Type-Options "nosniff"; 29 | 30 | # リクエストのルートディレクトリ 31 | root /app/public; 32 | 33 | # インデックスファイルの設定 34 | index index.php index.html; 35 | 36 | location / { 37 | try_files $uri $uri/ /index.php?$query_string; 38 | } 39 | 40 | location = /favicon.ico { access_log off; log_not_found off; } 41 | location = /robots.txt { access_log off; log_not_found off; } 42 | 43 | error_page 404 /index.php; 44 | 45 | location ~ \.php$ { 46 | # ファイルが見つからなければ404を返す 47 | try_files $uri =404; 48 | 49 | # $fastcgi_script_name, $fastcgi_path_info 変数に入れる値を正規表現で指定 50 | fastcgi_split_path_info ^(.+\.php)(/.+)$; 51 | 52 | # FastCGIサーバーのアドレスを指定 53 | fastcgi_pass api:9000; 54 | 55 | # インデックスのファイル名を指定 56 | fastcgi_index index.php; 57 | 58 | # FastCGIの設定ファイルを読み込む 59 | include fastcgi_params; 60 | 61 | # FastCGIの設定を追加 62 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 63 | fastcgi_param PATH_INFO $fastcgi_path_info; 64 | } 65 | 66 | # エラー時に表示するURIを指定 67 | error_page 404 /404.html; 68 | error_page 500 502 503 504 /500.html; 69 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /front/ 2 | /api/ -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | init: 2 | docker-compose up -d --build 3 | 4 | up: 5 | docker-compose up -d 6 | 7 | down: 8 | docker-compose down 9 | 10 | laravel 11 | docker-compose exec api composer create-project laravel/laravel . 12 | 13 | next 14 | docker-compose exec front yarn create next-app --typescript . 15 | 16 | dev 17 | docker-compose exec front yarn dev -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Next.js × Laravel の環境構築 2 | 3 | ## 前提 4 | 5 | - M1Mac対応 6 | - Intel製チップMacの場合は`.docker/db/Dockerfile`を以下の通り修正 7 | - Windowsでの動作確認は行っていない 8 | 9 | ```diff 10 | - FROM --platform=linux/x86_64 mysql:8.0 11 | + FROM mysql:8.0 12 | 13 | ENV TZ=UTC 14 | 15 | COPY my.cnf /etc/my.cnf 16 | ``` 17 | 18 | ## コンテナ起動 19 | 20 | ```sh 21 | docker-compose up -d --build 22 | ``` 23 | 24 | ## Laravelインストール 25 | 26 | ```sh 27 | docker-compose exec api composer create-project laravel/laravel . 28 | ``` 29 | 30 | `api`ディレクトリ内にLaravelがインストールされる 31 | 32 | `localhost:80`にアクセスするとLaravelのウェルカムページが表示される 33 | 34 | ## Next.jsインストール 35 | 36 | ```sh 37 | docker-compose exec front yarn create next-app --typescript . 38 | 39 | # 開発用サーバー起動 40 | docker-compose exec front yarn dev 41 | ``` 42 | 43 | `front`ディレクトリ内にNext.jsがインストールされる 44 | 45 | `localhost:3000`にアクセスするとNext.jsのウェルカムページが表示される 46 | 47 | 開発用サーバーの停止は`control + c` 48 | 49 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.8" 2 | 3 | services: 4 | front: 5 | build: 6 | context: ./.docker/front 7 | dockerfile: Dockerfile 8 | ports: 9 | - 3000:3000 10 | volumes: 11 | - ./front:/app:cached 12 | stdin_open: true 13 | tty: true 14 | depends_on: 15 | - nginx 16 | nginx: 17 | container_name: nginx 18 | build: 19 | context: ./.docker/nginx 20 | dockerfile: Dockerfile 21 | ports: 22 | - 80:80 23 | volumes: 24 | - ./api:/app:cached 25 | tty: true 26 | depends_on: 27 | - api 28 | api: 29 | build: 30 | context: ./.docker/api 31 | dockerfile: Dockerfile 32 | volumes: 33 | - ./api:/app:cached 34 | tty: true 35 | environment: 36 | - APP_ENV=development 37 | depends_on: 38 | - db 39 | db: 40 | build: 41 | context: ./.docker/db 42 | dockerfile: Dockerfile 43 | ports: 44 | - 3306:3306 45 | environment: 46 | MYSQL_DATABASE: spa_app 47 | MYSQL_USER: spa_app 48 | MYSQL_PASSWORD: password 49 | MYSQL_ROOT_PASSWORD: password 50 | TZ: "Asia/Tokyo" 51 | volumes: 52 | - db-volume:/var/lib/mysql:cached 53 | 54 | volumes: 55 | db-volume: --------------------------------------------------------------------------------