さくらVPS【SSL】でCakePHP

さくらVPSでスタートアップスクリプトを使用して簡単に構築したSSL環境にCakePHP3.7をインストールする

◎事前準備

◎Composer のインストール

  • さくらVPSにsshでユーザーログインし、「composer」をインストールしました。
    $ curl -sS https://getcomposer.org/installer | php
    
  • パスが通るように「/usr/local/bin/」以下に「composer.phar」を移動しました。
    $ su
    Password:
    # mv composer.phar /usr/local/bin/composer
    
  • 動作確認を兼ねて、composerをアップデートしました。
    # composer self-update
    You are already using composer version 1.8.6 (stable channel).
    

◎Cakephp3.7 のインストール

  • Cakephp3.7 のインストール用に「/var/www/cake」フォルダを作成しました。
    # cd /var/www/ 
    # mkdir cake
    
  • 「/var/www/cake」フォルダに移動してcakephp3を「/var/www/cake」以下にインストールしました。
    # cd cake
    # composer create-project --prefer-dist cakephp/app ./
    
  • 上手くインストール出来たかどうかのテストを兼ねて、テスト用サーバーを起動してみました。
    # bin/cake server
    
    Welcome to CakePHP v3.7.8 Console
    ---------------------------------------------------------------
    App : src
    Path: /var/www/cake/src/
    DocumentRoot: /var/www/cake/webroot
    Ini Path:
    ---------------------------------------------------------------
    built-in server is running in http://localhost:8765/
    You can exit with `CTRL-C`
    

◎Nginxの設定

  • 「/etc/nginx/conf.d/」以下にある「default.conf」と「https.conf」の記載を下記のように変更しました。
    # cd /etc/nginx/conf.d/
    # nano default.conf (または https.conf)
    
    1. 「default.conf」と「https.conf」の「location /{…}」「location ~ \.php$ {…}」他(「location = /50x.html{…})にあるrootの記述を「/var/www/cake/webroot」に変更しました。
      location / {
          # root /usr/share/nginx/html;
          # root /var/www/html;
          root   /var/www/cake/webroot;
          index  index.html index.htm;
      }
      
    2. 「default.conf」と「https.conf」の「location /{…}」に「try_files $uri $uri/ /index.php?$args;」という記述を追記しました。(/controller/view?key=value を正しくハンドルできるようにするため)
      location / {
          # root /usr/share/nginx/html;
          # root /var/www/html;
          root   /var/www/cake/webroot;
          index  index.html index.htm;
        try_files $uri $uri/ /index.php?$args;
      }
      
  • nginxを再起動しました。
    # systemctl restart nginx.service
    
  • 「https://(サーバーのURL)/」にアクセスして、スタートページが表示されることを確認しました。

◎データベースの設定

  • スタートページのDatabaseの項目が「CakePHP is NOT able to connect to the database.」となっていたので、phpMyAdminでデータベース「cake」を照合順序「utf8_general_ci」で作成しました。
  • エディタ(nano)で「config/app.php」に記載のデータベース設定を変更しました。
    # cd /var/www/cake/config
    # nano app.php
    
    • 「’username’」「’password’」「’database’」を作成したデータベースの内容に変更しました。
      /*
      * 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' => 'non_standard_port_number',
      'username' => 'my_app',
      'password' => 'secret',
      'database' => 'my_app',
      
  • 「https://(サーバーのURL)/」にアクセスして、スタートページのDatabaseの項目が「CakePHP is able to connect to the database.」に変わったことを確認して、CakePHP3.7のインストールを完了しました。
<= さくらVPSでphpMyAdmin |
スポンサーリンク
ohhappyをフォローする
Sakura.Oh!Happy.JP