Get WordPress up and running on OpenShift Online 2

最近打算把舊的 blog 從 lovein.tw 移出,選了 RedHat 的 SaaS 平臺 OpenShift Online 2。

重要的兩個 repository

OpenShift Cartridge for Nginx and PHP 7

pinodex/openshift-cartridge-nginx-php7

Custom cartridge for OpenShift providing MySQL 5.7.17

icflorescu/openshift-cartridge-mysql

Add Application ... -> Search by keyword or tag 輸入 openshift-cartridge-nginx-php7

完成之後,進入 application overview 畫面,選 Or, see the entire list of cartridges you can add

Install your own cartridge 中輸入 https://raw.githubusercontent.com/icflorescu/openshift-cartridge-mysql/master/metadata/manifest.yml


取得 MySQL Host IP and Port

1
2
~$ echo $OPENSHIFT_MYSQL_DB_HOST
~$ echo $OPENSHIFT_MYSQL_DB_PORT

取得 PHP IP

1
~$ echo $OPENSHIFT_PHP_IP

設定 MySQL

1
2
3
4
${OPENSHIFT_DATA_DIR}.mysql/bin/mysql --socket=${TMP}mysql.sock -u root
mysql> create database ls_wp;
mysql> GRANT ALL PRIVILEGES ON LS_WP.* TO 'ls_wp'@'**PHP IP**.%' IDENTIFIED BY '**PassWord**' WITH GRANT OPTION;
mysql> flush privileges;
1
2
3
4
5
~$ git clone **Source Code ssh url**
~$ cd **app_name**/www
~$ rm -rf ./*
~$ git add -A
~$ git commit -m "remove template file"
1
2
3
~$ wget -q -O - https://wordpress.org/latest.tar.gz | tar xz && mv ./wordpress/* ./ && rm -rf ./wordpress
~$ git add -A
~$ git commit -m "add wordpress-4.8-zh_TW source code"

如果需要上傳附件,需要修改 client_max_body_size 的大小

1
2
3
4
5
6
7
# Handle any other URI
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?$args;
}

client_max_body_size 15M;
1
2
~$ git add -A
~$ git commit -m "edit nginx setting"

需要修改 php.ini 設定的話

1
2
~$ cd config/php.d
~$ touch php.ini && vim php.ini

如果需要上傳附件,要修改 PHP upload_max_filesizepost_max_size 的大小(PHP7 預設為 2M),例如要將以前的 WordPress Import。

1
2
upload_max_filesize = 15M
post_max_size = 15M
1
2
~$ git add -A
~$ git commit -m "add php.ini"

完成之後就可以 git push 到 OpenShift 的 repository 上面。

1
~$ git push

接著打開 Application URL 就可以進行 WordPress 安裝。

比較需要注意的是 Database 的設定,位置請填寫下面指令的結果。

1
~$ echo $OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT

如果要開啟 WordPress FORCE_SSL_ADMIN

1
2
3
4
~$ vim app-root/repo/www/wp-config.php
define('FORCE_SSL_ADMIN', true);
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
$_SERVER['HTTPS'] = 'on';

Reference