nginx + php-fpm で Lumen を動かす記事ã®ç¶ç·¨ã§ãã
Lumen 5.2 㧠Welcome Page ãç¡ããªã£ã¦ãã
ææ°çã® Lumen ãã¤ã³ã¹ãã¼ã«ãã¦å®è¡ãã¦ã¿ãããã¼ã¸ã§ã³çªå·ã®ããã¹ãã表示ããã¾ããããã®ç½ãç»é¢ã® Welcome Page ãç¡ãã¨ãã§ããå¯ããã®ã§ Lumen 5.2 ã§ä½ã£ã¦ã¿ã¾ããã
ã½ã¼ã¹ã³ã¼ã
app/Http/Controllers/WelcomeController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class WelcomeController extends Controller { public function index( Request $request ) { $client_ip = $request->ip(); $server = $request->server(); // $_SERVER return view('welcome', ['server' => $server, 'client_ip' => $client_ip]); } }
app/Http/routes.php
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It is a breeze. Simply tell Lumen the URIs it should respond to | and give it the Closure to call when that URI is requested. | */ $app->get('/', [ 'uses' => 'WelcomeController@index', ]);
resources/views/welcome.blade.php
<!DOCTYPE html> <html> <head> <title>Lumen</title> <link href='//fonts.googleapis.com/css?family=Lato:100' rel='stylesheet' type='text/css'> <style> body { margin: 0; padding: 0; width: 100%; height: 100%; color: #B0BEC5; display: table; font-weight: 100; font-family: 'Lato'; } .container { text-align: center; display: table-cell; vertical-align: middle; } .content { text-align: center; display: inline-block; } .title { font-size: 96px; margin-bottom: 40px; } .quote { font-size: 24px; } .info { font-size: 48px; } </style> </head> <body> <div class="container"> <div class="content"> <div class="title">Lumen.</div> <div class="info">Hostname: {{ gethostname() }}</div> <div class="info">Server IP: {{ $server['SERVER_ADDR'] }}</div> <div class="info">Client IP: {{ $client_ip }}</div> @if ( array_key_exists('X-Forwarded-For', $server) ) <div class="info">X-Forwarded-For: {{ $server['X-Forwarded-For'] }}</div> @elseif ( array_key_exists('HTTP_X_FORWARDED_FOR', $server) ) <div class="info">HTTP_X_FORWARDED_FOR: {{ $server['HTTP_X_FORWARDED_FOR'] }}</div> @elseif ( array_key_exists('REMOTE_ADDR', $server) ) <div class="info">REMOTE_ADDR: {{ $server['REMOTE_ADDR'] }}</div> @endif </div> </div> </body> </html>
åä½ç¢ºèªã®ããã® Docker é¢é£ãã¡ã¤ã«
ãã£ã¬ã¯ããªæ§æ
lumen â docker-compose.yml â âânginx â Dockerfile â server.conf â ââphp7 Dockerfile k8s_lumen/
lumen/php7/k8s_lumen ã¯ä¸è¨ã³ãã³ãã§ã¤ã³ã¹ãã¼ã«ããã Lumen ã®å®è¡ç°å¢ã«ä¸è¨ã½ã¼ã¹ã³ã¼ããé©ç¨ãããã®ã«ãªãã¾ãã
$ composer create-project --prefer-dist laravel/lumen k8s_lumen
lumen/docker-compose.yml
nginx: build: ./nginx ports: - "80:80" links: - php7 php7: build: ./php7
lumen/nginx/Dockerfile
FROM nginx:latest MAINTAINER takaya030 ADD server.conf /etc/nginx/conf.d/server.conf
lumen/nginx/server.conf
server {
listen 80 default;
server_name _;
root /webapp/public;
index index.php index.html index.htm;
charset utf-8;
access_log off;
error_log off;
rewrite ^(.+)/$ $1;
location / {
# try_files $uri $uri/ /index.php$is_args$args;
try_files $uri /index.php?$query_string;
}
location ~ ^/index.php$ {
fastcgi_pass lumen_php7_1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
lumen/php7/Dockerfile
FROM php:7-fpm MAINTAINER takaya030 RUN apt-get update \ && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng12-dev libmcrypt-dev \ && docker-php-ext-install pdo_mysql mysqli mbstring gd iconv mcrypt RUN mkdir /webapp COPY k8s_lumen /webapp
ã¤ã¡ã¼ã¸ã®ãªãã«ã
$ cd lumen
$ docker-compose build
ã³ã³ããã®èµ·å
$ cd lumen $ docker-compose up -d
åä½ç¢ºèª
web ãã©ã¦ã¶ã§ http://192.168.99.100/ ã«ã¢ã¯ã»ã¹ãã¦ä¸ã®ç»åã®ããã«è¡¨ç¤ºãããã確èªã