takaya030の備忘録

PHP、Laravel、Docker などの話がメインです

Lumen の Welcome Page を作る

nginx + php-fpm で Lumen を動かす記事の続編です。

Lumen 5.2 で Welcome Page が無くなっていた

最新版の Lumen をインストールして実行してみたらバージョン番号のテキストが表示されました。あの白い画面の Welcome Page が無いとチョット寂しいので Lumen 5.2 で作ってみました。
f:id:takaya030:20160505005324p:plain

ソースコード

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/ にアクセスして下の画像のように表示されるか確認。
f:id:takaya030:20160627001224p:plain

参考サイト

takaya030.hatenablog.com