LoginSignup
62
53

More than 5 years have passed since last update.

春だし CakePHP 3.0をComposer経由でCentOS6.5にインストール してみた

Posted at

春ですね。
今回はVagrant上に構成しました。firewallの設定などは割愛します。
vagrant環境は以下の通りです。
OS:CentOS6.5
PHP:5.6.7
MySQL:5.1.73

PHPをインストールする

CakePHP3系から、PHPのバージョンが 5.4.16以上が必要になっています。デフォルトのレポジトリだとPHP5.3.3になってしまうので他のレポジトリからインストールしなければなりません。

  1. まず、remiのレポジトリを追加します。

    console
    $ sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
    
  2. PHPと必要なモジュールをインストールします。

    console
    $ sudo yum install --enablerepo=remi --enablerepo=remi-php56 php php-intl php-mbstring php-mysqlnd
    
    console
    $ php -v
    PHP 5.6.7 (cli) (built: Mar 19 2015 15:09:09)
    Copyright (c) 1997-2015 The PHP Group
    Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    

    :thumbsup:

Composerをインストールする

  1. 現在の作業ディレクトリにインストールします。

    console
    $ curl -sS https://getcomposer.org/installer | php
    
  2. 今回はグローバルにcomposerをインストールしますので、すでにPATHが通っているディレクトリにcomposerを移動させます。

    console
    $ sudo mv composer.phar /usr/local/bin/composer
    
    console
    $ composer
        ______
      / ____/___  ____ ___  ____  ____  ________  _____
     / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
    / /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
    \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                        /_/
    Composer version 1.0-dev (f10c71475167a4661225b14560ca0a400d730829)
    
    

    :thumbsup:

MySQLをインストール

  1. MySQLは5.1.10以上ということなので普通にyumでインストールします。

    console
    $ sudo yum install mysql-server
    
  2. とりあえず、起動

    console
    $ sudo service mysqld start
    Starting mysqld:                       [  OK  ]
    
  3. バージョンを確認

    console
    $ mysql -u root
    mysql>
    mysql> \s
    --------------
    mysql  Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
    
    Server version:     5.1.73 Source distribution
    
    console
    mysql> quit
    Bye
    

    :thumbsup:

CakePHP3を取得

  1. composerでインストール

    console
    $ composer create-project --prefer-dist cakephp/app [appName]
    Installing cakephp/app (3.0.1)
      - Installing cakephp/app (3.0.1)
        Downloading: 100%
    
    Created project in test
    Loading composer repositories with package information
    Installing dependencies (including require-dev)
      - Installing aura/installer-default (1.0.0)
        Downloading: 100%
    
      - Installing cakephp/plugin-installer (0.0.11)
        Downloading: 100%
     .
     .
     .
     .
    
    Created `config/app.php` file
    Set Folder Permissions ? (Default to Y) [Y,n]?
    

    なかなか時間がかかった。
    俺がPermission設定しちゃうけどどうする?ってComposerが聞いてくるのでとりあえず、デフォルトがyesなので何も入力せずEnter

    console
    Permissions set on /home/vagrant/test/tmp/cache
    Permissions set on /home/vagrant/test/tmp/cache/models
    Permissions set on /home/vagrant/test/tmp/cache/persistent
    Permissions set on /home/vagrant/test/tmp/cache/views
    Permissions set on /home/vagrant/test/tmp/sessions
    Permissions set on /home/vagrant/test/tmp/tests
    Permissions set on /home/vagrant/test/tmp
    Permissions set on /home/vagrant/test/logs
    Updated Security.salt value in config/app.php
    

    でインストール終了。Security.saltの設定は後回し:thumbsup:

とりあえず、動くか確認

  1. ビルトインサーバーがあるらしいので使ってみる。

    console
    $ cd [appName]
    $ bin/cake server
    
    Welcome to CakePHP v3.0.1 Console
    ---------------------------------------------------------------
    App : src
    Path: /home/vagrant/test/src/
    DocumentRoot: /home/vagrant/test/webroot
    ---------------------------------------------------------------
    built-in server is running in http://localhost:8765/
    You can exit with `CTRL-C`
    
    

    おお、動いた
    この状態で http://192.168.33.10:8765/ にアクセスする。
    (仮想マシン上に構成していない場合は http://localhost:8765/ で構いません。これ以降に出てくる192.168.33.10もすべてlocalhostに置き換えてください)

    Chrome 「 このウェブページにアクセスできません。

    みさわ

  2. 原因確認

    1. Vagrant上でcurlしてみる。

      console
      $ curl http://localhost:8765/
      <!DOCTYPE html>
      <html>
          <head>
          .
          .
          .
          .
          以下略
      

      動いてるけどアクセスできない。

    2. Cakeサーバーを起動した状態でssしてみる。

      console
      $ ss -lnput | grep 8765
      tcp    LISTEN     0      128                  ::1:8765                 :::*      users:(("php",10467,10))
      

      localAddress:Portが::1:8765になってる。これか。

    3. cake serverのオプションで起動してみる。

      console
      $ bin/cake server -h
      
      Welcome to CakePHP v3.0.1 Console
      ---------------------------------------------------------------
      App : src
      Path: /home/vagrant/test/src/
      DocumentRoot: /home/vagrant/test/webroot/
      ---------------------------------------------------------------
      PHP Built-in Server for CakePHP
      [WARN] Do not use this at the production environment
      
      Usage:
      cake server [-h] [-v] [-q] [-H] [-p] [-d]
      
      Options:
      
      --help, -h           Display this help.
      --verbose, -v        Enable verbose output.
      --quiet, -q          Enable quiet output.
      --host, -H           ServerHost
      --port, -p           ListenPort
      --document_root, -d  DocumentRoot
      
      

      -Hでホストが指定できるようだ。

      console
      $ bin/cake server -H 0.0.0.0
      
      Welcome to CakePHP v3.0.1 Console
      ---------------------------------------------------------------
      App : src
      Path: /home/vagrant/test/src/
      DocumentRoot: /home/vagrant/test/webroot
      ---------------------------------------------------------------
      built-in server is running in http://0.0.0.0:8765/
      You can exit with `CTRL-C`
      
      

      こんどこそ http://192.168.33.10:8765/

      Imgur

      やったね

Apacheを動かす

  1. 関連モジュールとしてインストールされる記憶があったけど、インストールされていないならインストールする。

    console
    $ sudo yum install httpd
    
    console
    $ sudo service httpd start
    Starting httpd: httpd: apr_sockaddr_info_get() failed for vagrant-centos65.vagrantup.com
    httpd: Could not reliably determine the servers fully qualified domain name, using 127.0.0.1 for ServerName
                                            [  OK  ]
    

    起動した
    ServerName変えろよって怒られる。

  2. 確認
    http://192.168.33.10 アクセス

    Imgur

    おーけー
    で、こうやって開いたらCakeのページが出るようにしたい

  3. Cakeをドキュメントルートに移動する

    console
    $ cd ..
    $ sudo mv [appName] /var/www/html/
    $ cd /var/www/html/
    $ ls
    [appName]
    
  4. ドキュメントルートを書き換える

    console
    $ sudo sed -i 's_DocumentRoot \"/var/www/html\"_DocumentRoot \"/var/www/html/[appName]\"_' /etc/httpd/conf/httpd.conf
    

    [appName]は各自書き換えてください

  5. Apache起動するたびに怒られるのでServerNameも書き換える

    console
    sudo sed -i 's/#ServerName www.example.com:80/ServerName 127.0.0.1/' /etc/httpd/conf/httpd.conf
    
  6. .htaccessも許可しとく

    console
    $ sudo sed -i '/<Directory \"\/var\/www\/html\">/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/httpd/conf/httpd.conf
    
  7. Apache再起動

    console
    $ sudo service httpd restart
    Stopping httpd:                                        [  OK  ]
    Starting httpd:                                        [  OK  ]
    

    今度は何も怒られない

  8. 確認

    もう一度アクセス http://192.168.33.10

    Imgur

    めちゃエラー出てる
    どうやらキャッシュが書き込めない感じのPermissionDenied

  9. [appName]のOwnerとGroupをapacheにする

    console
    $ sudo chown -R apache:apache [appName]
    
  10. 確認
    http://192.168.33.10 アクセス

    Imgur

    さきほどと同じ画面が出れば成功です:thumbsup:

MySQLに接続する

  1. [appName]/config/app.phpを編集する

    console
    $ sudo vi [appName]/config/app.php
    
    app.phpの210行目あたりから
    'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            /**
             * CakePHP will use the default DB port based on the driver selected
             * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
             * the following line and set the port accordingly
             */
            //'port' => 'nonstandard_port_number',
            'username' => 'root',
            'password' => '',
            'database' => 'test',
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
    

    こんな感じに書き換える。
    基本的にはusernameとpasswordとdatabaseしか書き換えなくてよい
    *各自の環境に合わせてください。

  2. 確認

    Imgur

    CakePHP is able to connect to the database.
    つながりました:thumbsup:

最後に

とりあえず、インストールしてApacheで動作したので今回は満足です。
ありがとうございました。

62
53
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
62
53