2016年3月28日月曜日

AWS EC2 を使ってWordPressの実行環境を構築してみた(4)
(WordPress install)

1. WordPressのインストール
・WordPressの取得
$ sudo wget http://ja.wordpress.org/wordpress-4.3.1-ja.tar.gz

・WordPressの展開
$ sudo tar zxvf wordpress-4.3.1-ja.tar.gz

・DocumentRootへコピー
$ sudo cp -r wordpress /var/www/html/

・権限の変更
$ sudo chown -R apache:apache /var/www/html/wordpress
$ sudo service httpd start
2. 画面を確認

-- Build a WordPress execution environment using the EC2 --
(WordPress install)


1. WordPress install
・Acquisition of WordPress
$ sudo wget http://ja.wordpress.org/wordpress-4.3.1-ja.tar.gz

・WordPress Deployment
$ sudo tar zxvf wordpress-4.3.1-ja.tar.gz

・Copy to the documentRoot
$ sudo cp -r wordpress /var/www/html/

・Authority change
$ sudo chown -R apache:apache /var/www/html/wordpress
$ sudo service httpd start
2. Check the screen

AWS EC2 を使ってWordPressの実行環境を構築してみた(3)
(Apache install)

1. Apacheのインストール
$ sudo yum -y install httpd24 httpd24-devel
2. Apache 初期設定
・自動起動設定 [Automatic start setting]
$ sudo chkconfig httpd on


-- Build a WordPress execution environment using the EC2 --
(Apache install)


1. Apache install
$ sudo yum -y install httpd24 httpd24-devel
2. Apache Initial setting
・Automatic start setting
$ sudo chkconfig httpd on

AWS EC2 を使ってWordPressの実行環境を構築してみた(2)
(MySQL install)

1. MySQLのインストール
$ sudo yum -y install mysql55 mysql55-server mysql55-devel
2. MySQLの初期設定
・自動起動設定
$ sudo chkconfig mysqld on

・バックアップ取得
$ sudo cp -a /etc/my.cnf{,.bk}

・my.cnfの編集
$ sudo vi /etc/my.cnf
--------------------------------
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

character-set-server=utf8 ←この行を追加

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[mysql] ←この行を追加
default-character-set=utf8 ←この行を追加
--------------------------------

$ sudo service mysqld start
$ sudo mysql_secure_installation
3. DB設定
$ sudo mysql -u root -p

mysql> create database db_name;
mysql> grant all privileges on db_name.* to db_user@localhost identified by 'db_pw';
mysql> quit;


-- Build a WordPress execution environment using the EC2 --
(MySQL install)


1. MySQL install
$ sudo yum -y install mysql55 mysql55-server mysql55-devel
2. MYSQL Initial setting
・Automatic start setting
$ sudo chkconfig mysqld on

・create buckup
$ sudo cp -a /etc/my.cnf{,.bk}

・my.cnf edit
$ sudo vi /etc/my.cnf
--------------------------------
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

character-set-server=utf8 ← add line

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[mysql] ← add line
default-character-set=utf8 ← add line
--------------------------------

$ sudo service mysqld start
$ sudo mysql_secure_installation
3. DB setting
$ sudo mysql -u root -p

mysql> create database db_name;
mysql> grant all privileges on db_name.* to db_user@localhost identified by 'db_pw';
mysql> quit;

2016年3月10日木曜日

cakephp3 バリデーション(1)
-- validation of cakephp3 --

PHP Ver 5.6.27
CakePHP Ver 3.1.9


Model

・ここでは例としてModel/Table/userTable.phpというファイルを編集

記述例
public function validationDefault(Validator $validator)
{
    // Cakephp既存バリデーション
    $validator
        // lengthチェック
        ->add('user_name', [
            'length' => [
                'rule' => ['lengthBetween', 4, 10],
                'message' => 'Error Message'
            ]
        ]);


    // 独自バリデーション
    $validator
        ->add('mailaddress', [
            'unique' => [
                'rule' => [$this, 'uniqueMailAddress' ],
                'message' => 'Error Message'
        ]);

    return $validator;
}

public function uniqueMailAddress($value, $context)
{
    // 処理内容
}

2016年3月5日土曜日

cakephp3 でユーザー定義定数ファイルを作成する -- create a user-defined constants file of cakephp3 --

PHP Ver 5.6.27
CakePHP Ver 3.1.9



1. ユーザー定義定数を記載したファイルを作成する

・ここでは例としてconst.phpというファイルを作成
config/const.phpを作成
--------------------------------------
return [
  'Common' => [
    'Valid_User' => 0
  ]
];
--------------------------------------
配列で定義することに注意


2. ユーザー定義定数ファイルを読み込ませる

・ここでは例としてconst.phpというファイルを読み込ませる
config/app.php もしくは config/bootstrap.phpを編集
--------------------------------------
Configure::load('const', 'default'); ← この行を追記
--------------------------------------


3. ユーザー定義定数ファイルを利用する

--------------------------------------
use Cake\Core\Configure; ← この行を追記

Configure::read('Common.Valid_User');
--------------------------------------


1. create a user-defined constants file

・ example: Create a file called const.php
Create a const.php under config directory
--------------------------------------
return [
  'Common' => [
    'Valid_User' => 0
  ]
];
--------------------------------------
Note that defined by the array


2. To read the user-defined constants file

・ example: Read a file called const.php
edit app.php or bootstrap.php
--------------------------------------
Configure::load('const', 'default'); ← add this line
--------------------------------------


3. To use the user-defined constants

--------------------------------------
use Cake\Core\Configure; ← add this line

Configure::read('Common.Valid_User');
--------------------------------------

2016年3月3日木曜日

cakephp3 のデフォルト機能を利用して認証を行う
-- Perform authentication using cakephp3 default auth --

PHP Ver 5.6.27
CakePHP Ver 3.1.9


Cakephp3のAuth機能を利用して管理画面の認証を作ってみた。
意外と手こずったので書き留める。

made manage function using Auth of Cakephp3.
I was struggling. so write down this blog.


1. DBの設定 [DB setting]


・config/app.phpの編集 [app.php edit]
--------------------------------
'Datasources' => [
    'default' => [
        ~
        'username' => 'db_user',
        'password' => 'db_password',
        'database' => 'database_name',
        ~
--------------------------------

・src/Controller/xxxController.phpの編集 [xxxController.php edit]
--------------------------------
public function initialize()
    {
        parent::initialize();

        // auth config
        $this->loadComponent('Auth', [
                'authenticate' => [
                        'Form' => [ // 認証の種類 Form,Basic,Digestが使える。デフォルトはForm
                                'userModel' => 'xxx', // テーブル名
                                'fields' => [ // ユーザー名とパスワードに使うカラムの指定。省略した場合はusernameとpasswordになる
                                        'username' => 'xxxx',
                                        'password' => 'xxxx'],
                                'scope' => [ // その他の条件
                                        'del_flg' => 0
                                ]
                                ]
                        ],
                'loginRedirect' => [ // ログイン後に遷移するアクションを指定
                        'controller' => 'xxx',
                        'action' => 'index'
                ],
                'logoutRedirect' => [ // ログアウト後に遷移するアクションを指定
                        'controller' => 'xxx',
                        'action' => 'login'
                ],
                'loginAction' => [ // ログインしていない場合のアクションを指定
                        'controller' => 'xxx',
                        'action' => 'login'
                ],
                'storage' => 'Session',
                'authError' => 'Did you really think you are allowed to see that?',
                'unauthorizedRedirect' => $this->referer()
        ]);

        $this->Auth->sessionKey = 'Auth.Admin';
    }

    public function login($id = null)
    {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);                 $this->Flash->success(__('Admin Login Success'), [
                        'key' => 'auth'
                        ]);
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                $this->Flash->error(__('Username or password is incorrect'), [
                        'key' => 'auth'
                        ]);
            }
        }
    }

    public function logout()
    {
        $this->request->session()->destroy();
        $this->Flash->success(__('Admin Logout Success'));

        return $this->redirect($this->Auth->logout());
    }

    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);

        //ログイン無しで表示されるAction
        $this->Auth->allow(['login', 'logout']);
    }
--------------------------------

・src/Template/xxx/login.ctpの作成 [create login.ctp file]
--------------------------------
< ?php
    echo $this->Form->create();
    echo $this->Flash->render('auth');
    echo $this->Form->input('textbox', [
            'label' => 'MailAddress',
            'id' => 'mailaddress',
            'name' => 'mailaddress'
    ]);
    echo $this->Form->input('password', [
            'label' => 'Password',
            'id' => 'password',
            'name' => 'password'
    ]);
    echo $this->Form->button('Login');
    echo $this->form->end();
? >
--------------------------------

※ Entityに_setPasswordというメソッドを作り、以下のように記述すれば
DBに登録する際に暗号化してくれます。

Create a method called _setPassword to Entity.
with encryption when insert to DB.
--------------------------------
use Cake\Auth\DefaultPasswordHasher;

    protected function _setPassword($value) {
        $hasher = new DefaultPasswordHasher();
        return $hasher->hash($value);
    }
--------------------------------

※2 パスワードの長さに気を付けないとLengthが足りなくてもerrorも吐かずに
登録され、ずっと認証されないことになります。
これではまった・・・orz