m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

nginx+(cakephp、fuelphp、zend framework2、symfony2)の簡易設定

とりあえず、ウェルカム画面だすだけ

nginxでサイトを公開する準備するのでとりあえず、他のフレームワークにも触っておく

共通対応

参照用PCにhostsを定義
192.168.51.129  dev1.example.com dev2.example.com dev3.example.com dev4.example.com dev5.example.com
設定ファイルを編集
[root@localhost webroot]# vim /root/.phpenv/versions/5.5.5/etc/php.ini
----
[Date]
date.timezone = Asia/Tokyo
----
composerをダウンロード
[root@localhost ~]# curl -s https://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /root/composer.phar
Use it: php composer.phar
[root@localhost ~]#



cakephp

cakephpのファイルをダウンロードしてサーバへコピー
[root@localhost ~]# ls -la /tmp/cakephp-2.3.10.zip
-rw-r--r-- 1 root root 2070915 11月  4 10:57 2013 /tmp/cakephp-2.3.10.zip
[root@localhost ~]#
解凍
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# unzip cakephp-2.3.10.zip
作業ディレクトリを作成して解凍したディレクトリを移動
[root@localhost tmp]# mkdir -p /var/www/html/dev2.example.com/public
[root@localhost tmp]# mv cakephp-2.3.10 /var/www/html/dev2.example.com/public/cakephp
[root@localhost tmp]#
ログのディレクトリを作成
[root@localhost public]# mkdir -p /var/log/nginx/dev2.example.com
[root@localhost public]#
nginxのファイルを作成
[root@localhost public]# vim /etc/nginx/conf.d/dev2.example.com.conf
----
server {
    # ポート、サーバネーム
    listen       80;
    server_name  dev2.example.com;

    # アクセスログ、エラーログ
    access_log  /var/log/nginx/dev2.example.com/access.log  main;
    error_log   /var/log/nginx/dev2.example.com/error.log;

    # ドキュメントルート
    root   /var/www/html/dev2.example.com/public/cakephp/app/webroot;

    # indexファイル
    index  index.php;

    # locationの設定
    location / {
        try_files $uri $uri/ /index.php?$uri&$args;
    }

    # phpの処理
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # アクセスを制限する
    location ~ (\.htaccess|\.git|\.svn) {
        deny  all;
    }
    
    # 文字コード
    charset utf-8;
    
}
----
ディレクトリの書き込み権限を設定し、nginxとphp-fpmを再起動
[root@localhost webroot]# chown nginx.nginx /var/www/html/dev2.example.com/public/cakephp -R
[root@localhost webroot]# service  nginx restart
[root@localhost webroot]# service  php-fpm restart
確認

f:id:m_shige1979:20131104150519j:plain







fuelphp

fuelphpのファイルをダウンロードしてサーバへコピー
[root@localhost ~]# ls -la /tmp/fuelphp-1.6.1.zip
-rw-r--r-- 1 root root 2002921 10月 12 09:01 2013 /tmp/fuelphp-1.7.zip
[root@localhost ~]#
解凍
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# unzip fuelphp-1.7.zip
作業ディレクトリを作成してディレクトリを移動
[root@localhost tmp]# mkdir -p /var/www/html/dev3.example.com/public
[root@localhost tmp]# mv fuelphp-1.7 /var/www/html/dev3.example.com/public/fuelphp
[root@localhost tmp]# 
ログのディレクトリを作成
[root@localhost public]# mkdir -p /var/log/nginx/dev3.example.com
[root@localhost public]# 
nginxファイルを作成
[root@localhost public]# vim /etc/nginx/conf.d/dev3.example.com.conf
----
server {
    # ポート、サーバネーム
    listen       80;
    server_name  dev3.example.com;

    # アクセスログ、エラーログ
    access_log  /var/log/nginx/dev3.example.com/access.log  main;
    error_log   /var/log/nginx/dev3.example.com/error.log;

    # ドキュメントルート
    root   /var/www/html/dev3.example.com/public/fuelphp/public;

    # indexファイル
    index  index.php;

    # locationの設定
    location / {
        try_files $uri /index.php?$uri&$args;
    }
    
    location ~* \favicon.ico$ {
      access_log off;
      expires 1d;
      add_header Cache-Control public;
    }

    location /doc/ {
        alias /var/www/html/dev3.example.com/public/fuelphp/docs/;
        autoindex on;
        allow 127.0.0.1;
        allow ::1;
        deny all;
    }

    # phpの処理
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # アクセスを制限する
    location ~ (\.htaccess|\.git|\.svn) {
        deny  all;
    }
    
    # 文字コード
    charset utf-8;
    
}
----
php composer.phar updateを実行
[root@localhost public]# cd /var/www/html/dev3.example.com/public/fuelphp
[root@localhost fuelphp]# php composer.phar update
書き込み権限を設定
[root@localhost webroot]# chown nginx.nginx /var/www/html/dev3.example.com/public/fuelphp -R
再起動
[root@localhost webroot]# service  nginx restart
[root@localhost webroot]# service  php-fpm restart
確認

f:id:m_shige1979:20131104151036j:plain







zend framework2

作業ディレクトリを作成
[root@localhost ~]# mkdir -p /var/www/html/dev4.example.com/public
[root@localhost ~]# 
ログのディレクトリを作成
[root@localhost ~]# mkdir -p /var/log/nginx/dev4.example.com
[root@localhost ~]#
Zend Framework2のセットアップ
[root@localhost ~]# php composer.phar create-project --repository-url="http://packages.zendframework.com" zendframework/skeleton-application:dev-master /var/www/html/dev4.example.com/public/zf2
Installing zendframework/skeleton-application (dev-master dc0dc6e0ec8ae6ccfe12bd15f7f66f32077244bd)
  - Installing zendframework/skeleton-application (dev-master master)
    Cloning master

Created project in /var/www/html/dev4.example.com/public/zf2
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing zendframework/zendframework (2.2.5)
    Loading from cache

zendframework/zendframework suggests installing doctrine/annotations (Doctrine Annotations >=1.0 for annotation features)
zendframework/zendframework suggests installing ircmaxell/random-lib (Fallback random byte generator for Zend\Math\Rand if OpenSSL/Mcrypt extensions are unavailable)
zendframework/zendframework suggests installing ocramius/proxy-manager (ProxyManager to handle lazy initialization of services)
zendframework/zendframework suggests installing zendframework/zendpdf (ZendPdf for creating PDF representations of barcodes)
zendframework/zendframework suggests installing zendframework/zendservice-recaptcha (ZendService\ReCaptcha for rendering ReCaptchas in Zend\Captcha and/or Zend\Form)
Writing lock file
Generating autoload files
Do you want to remove the existing VCS (.git, .svn..) history? [Y,n]?
[root@localhost ~]#
nginxファイルを作成
[root@localhost public]# vim /etc/nginx/conf.d/dev4.example.com.conf
----
server {
    # ポート、サーバネーム
    listen       80;
    server_name  dev4.example.com;

    # アクセスログ、エラーログ
    access_log  /var/log/nginx/dev4.example.com/access.log  main;
    error_log   /var/log/nginx/dev4.example.com/error.log;

    # ドキュメントルート
    root   /var/www/html/dev4.example.com/public/zf2/public;

    # indexファイル
    index  index.php;

    # locationの設定
    location / {
      if (!-e $request_filename) {
        rewrite ^.*$ /index.php last;
      }
    }
    
    location ~* \favicon.ico$ {
      access_log off;
      expires 1d;
      add_header Cache-Control public;
    }

    # phpの処理
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_param  APPLICATION_ENV develop;
    }

    # アクセスを制限する
    location ~ (\.htaccess|\.git|\.svn) {
        deny  all;
    }
    
    # 文字コード
    charset utf-8;
    
}
----
書き込み権限を設定
[root@localhost webroot]# chown nginx.nginx /var/www/html/dev4.example.com/public/zf2 -R
[root@localhost webroot]# service  nginx restart
[root@localhost webroot]# service  php-fpm restart
確認

f:id:m_shige1979:20131104151551j:plain







symfony2

作業ディレクトリの作成
[root@localhost ~]# mkdir -p /var/www/html/dev5.example.com/public
[root@localhost ~]# 
ログのディレクトリを作成
[root@localhost ~]# mkdir -p /var/log/nginx/dev5.example.com
[root@localhost ~]#
symfony2のスケルトンをセットアップ
[root@localhost ~]# php composer.phar create-project symfony/framework-standard-edition /var/www/html/dev5.example.com/public/symfony 2.3.6
Installing symfony/framework-standard-edition (v2.3.6)
  - Installing symfony/framework-standard-edition (v2.3.6)
    Loading from cache

Created project in /var/www/html/dev5.example.com/public/symfony
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing jdorn/sql-formatter (v1.2.9)
    Loading from cache

  - Installing psr/log (1.0.0)
    Loading from cache

  - Installing twig/twig (v1.14.2)
    Loading from cache

  - Installing doctrine/lexer (v1.0)
    Loading from cache

  - Installing doctrine/annotations (v1.1.2)
    Loading from cache

  - Installing doctrine/collections (v1.1)
    Loading from cache

  - Installing doctrine/cache (v1.3.0)
    Loading from cache

  - Installing doctrine/inflector (v1.0)
    Loading from cache

  - Installing doctrine/common (v2.4.1)
    Loading from cache

  - Installing symfony/symfony (v2.3.6)
    Loading from cache

  - Installing symfony/icu (v1.1.0)
    Loading from cache

  - Installing doctrine/dbal (2.3.4)
    Loading from cache

  - Installing doctrine/doctrine-bundle (v1.2.0)
    Loading from cache

  - Installing kriswallsmith/assetic (v1.1.2)
    Loading from cache

  - Installing symfony/assetic-bundle (v2.3.0)
    Loading from cache

  - Installing monolog/monolog (1.6.0)
    Loading from cache

  - Installing symfony/monolog-bundle (v2.3.0)
    Loading from cache

  - Installing incenteev/composer-parameter-handler (v2.0.0)
    Loading from cache

  - Installing doctrine/orm (2.3.4)
    Loading from cache

  - Installing twig/extensions (v1.0.1)
    Loading from cache

  - Installing swiftmailer/swiftmailer (v5.0.2)
    Loading from cache

  - Installing symfony/swiftmailer-bundle (v2.3.4)
    Loading from cache

  - Installing sensio/distribution-bundle (v2.3.4)
    Loading from cache

  - Installing sensio/framework-extra-bundle (v2.3.4)
    Loading from cache

  - Installing sensio/generator-bundle (v2.3.4)
    Loading from cache

kriswallsmith/assetic suggests installing leafo/lessphp (Assetic provides the integration with the lessphp LESS compiler)
kriswallsmith/assetic suggests installing leafo/scssphp (Assetic provides the integration with the scssphp SCSS compiler)
kriswallsmith/assetic suggests installing ptachoire/cssembed (Assetic provides the integration with phpcssembed to embed data uris)
kriswallsmith/assetic suggests installing leafo/scssphp-compass (Assetic provides the integration with the SCSS compass plugin)
monolog/monolog suggests installing mlehner/gelf-php (Allow sending log messages to a GrayLog2 server)
monolog/monolog suggests installing raven/raven (Allow sending log messages to a Sentry server)
monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
Writing lock file
Generating autoload files
Creating the "app/config/parameters.yml" file.
Some parameters are missing. Please provide them.
database_driver (pdo_mysql):
database_host (127.0.0.1):
database_port (null):3306
database_name (symfony):
database_user (root):
database_password (null):password
mailer_transport (smtp):
mailer_host (127.0.0.1):
mailer_user (null):
mailer_password (null):
locale (en):ja
secret (ThisTokenIsNotSoSecretChangeIt):
Clearing the cache for the dev environment with debug true
Installing assets using the hard copy option
Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
Installing assets for Acme\DemoBundle into web/bundles/acmedemo
Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution
[root@localhost ~]#
nginxファイルを作成
[root@localhost public]# vim /etc/nginx/conf.d/dev5.example.com.conf
----
server {
    # ポート、サーバネーム
    listen       80;
    server_name  dev5.example.com;

    # アクセスログ、エラーログ
    access_log  /var/log/nginx/dev5.example.com/access.log  main;
    error_log   /var/log/nginx/dev5.example.com/error.log;

    # ドキュメントルート
    root   /var/www/html/dev5.example.com/public/symfony/web;

    # indexファイル
    index  index.php;

    # locationの設定
    location / {
        try_files $uri $uri/ /app_dev.php; 
    }
    
    location ~* \favicon.ico$ {
        access_log off;
        expires 1d;
        add_header Cache-Control public;
    }

    # phpの処理
    location ~ \.php$ {
        location ~ \..*/.*\.php$ {return 404;}
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  app_dev.php;               # Synfonyの Welcome ページ
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # アクセスを制限する
    location ~ (\.htaccess|\.git|\.svn) {
        deny  all;
    }
    
    # 文字コード
    charset utf-8;
    
}
----
権限を設定してチェック
[root@localhost web]# cd /var/www/html/dev5.example.com/public/symfony/app
[root@localhost app]# chmod 777 -R cache
[root@localhost app]# chmod 777 -R logs
[root@localhost app]# php check.php
********************************
*                              *
*  Symfony requirements check  *
*                              *
********************************

* Configuration file used by PHP: /root/.phpenv/versions/5.5.5/etc/php.ini

** ATTENTION **
*  The PHP CLI can use a different php.ini file
*  than the one used with your web server.
*  To be on the safe side, please also launch the requirements check
*  from your web server using the web/config.php script.

** Mandatory requirements **

 OK       PHP version must be at least 5.3.3 (5.5.5 installed)
 OK       PHP version must not be 5.3.16 as Symfony won't work properly with it
 OK       Vendor libraries must be installed
 OK       app/cache/ directory must be writable
 OK       app/logs/ directory must be writable
 OK       date.timezone setting must be set
 OK       Configured default timezone "Asia/Tokyo" must be supported by your installation of PHP
 OK       json_encode() must be available
 OK       session_start() must be available
 OK       ctype_alpha() must be available
 OK       token_get_all() must be available
 OK       simplexml_import_dom() must be available
 OK       detect_unicode must be disabled in php.ini
 OK       xdebug.show_exception_trace must be disabled in php.ini
 OK       xdebug.scream must be disabled in php.ini
 OK       PCRE extension must be available

** Optional recommendations **

 WARNING  xdebug.max_nesting_level should be above 100 in php.ini
          Set "xdebug.max_nesting_level" to e.g. "250" in php.ini* to stop Xdebug's infinite recursion protection erroneously throwing a fatal error in your project.

 OK       Requirements file should be up-to-date
 OK       You should use at least PHP 5.3.4 due to PHP bug #52083 in earlier versions
 OK       When using annotations you should have at least PHP 5.3.8 due to PHP bug #55156
 OK       You should not use PHP 5.4.0 due to the PHP bug #61453
 OK       When using the logout handler from the Symfony Security Component, you should have at least PHP 5.4.11 due to PHP bug #63379 (as a workaround, you can also set invalidate_session to false in the security logout handler configuration)
 OK       You should use PHP 5.3.18+ or PHP 5.4.8+ to always get nice error messages for fatal errors in the development environment due to PHP bug #61767/#60909
 OK       PCRE extension should be at least version 8.0 (8.32 installed)
 OK       PHP-XML module should be installed
 OK       mb_strlen() should be available
 OK       iconv() should be available
 OK       utf8_decode() should be available
 OK       posix_isatty() should be available
 OK       intl extension should be available
 OK       intl extension should be correctly configured
 OK       intl ICU version should be at least 4+
 OK       a PHP accelerator should be installed
 OK       short_open_tag should be disabled in php.ini
 OK       magic_quotes_gpc should be disabled in php.ini
 OK       register_globals should be disabled in php.ini
 OK       session.auto_start should be disabled in php.ini
 OK       PDO should be installed
 OK       PDO should have some drivers installed (currently available: mysql, sqlite)
[root@localhost app]#
書き込み権限を設定
[root@localhost webroot]# chown nginx.nginx /var/www/html/dev5.example.com/public/symfony -R
php.iniを編集
[root@localhost app]# vim /root/.phpenv/versions/5.5.5/etc/php.ini
----
;xdebug.profiler_enable=1
;xdebug.profiler_output_dir="/tmp/xdebug"
xdebug.auto_trace=1             ; enable tracing
xdebug.trace_format=0
;xdebug.show_mem_delta=0        ; memory difference
;xdebug.show_local_vars=1
xdebug.max_nesting_level=250
----

※最後尾に追加

ファイルを編集して許可するIPを設定し、別PCから参照可能にする
[root@localhost app]# vim /var/www/html/dev5.example.com/public/symfony/web/app_dev.php
----
if (isset($_SERVER['HTTP_CLIENT_IP'])
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
    || !in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1'))
) {
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}
----
↓
----
if (isset($_SERVER['HTTP_CLIENT_IP'])
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
    || !in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1', '192.168.51.1'))
) {
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}
----

[root@localhost app]# vim /var/www/html/dev5.example.com/public/symfony/web/config.php
----
if (!in_array(@$_SERVER['REMOTE_ADDR'], array(
    '127.0.0.1',
    '::1',
))) {
----
↓
----
if (!in_array(@$_SERVER['REMOTE_ADDR'], array(
    '127.0.0.1',
    '::1',
    '192.168.51.1',
))) {
----
再起動
[root@localhost app]# service  nginx restart
[root@localhost app]# service  php-fpm restart
確認

f:id:m_shige1979:20131104152257j:plain





まとめ

nginxでも一応それぞれの設定は可能な感じ以前HipHop for PHPやってみようとしたけどなんか縛りがややこしそうだった上にうまくインストールできないことでつまづきまくった。
今回はとりあえずここまで行った。

次はperlでMojoliciousと連携をやってみる