本ドメインで使っているDDNSサービスのieserverに繋がらなくて「どうした?」って心配していたんだけど、何事もなかったように元に戻った。
SERVERの障害だったらしい。このアドレスは使えたがもう一つ運用しているがこちらが使えなかったが元に戻ってよかった。
AWS,UE4,PHPなどの備忘録
AWSの備忘録
本ドメインで使っているDDNSサービスのieserverに繋がらなくて「どうした?」って心配していたんだけど、何事もなかったように元に戻った。
SERVERの障害だったらしい。このアドレスは使えたがもう一つ運用しているがこちらが使えなかったが元に戻ってよかった。
第四回ではテーブル名とカラム名配列をサーバーに送って受け取る処理の解説をしましたが、今回は受け取ったテーブル名とカラム名配列で実際にデーターベースにアクセスして読み込んでみようかと思います。
【必要な事項】
①テーブル名とカラム名配列を受け取ったらデーターベースにSELECT命令を出す
②テーブルにカラム名が存在していたら読み込んで表示
③テーブルにカラム名が存在していない場合はエラー
以上になるかと思います。
//POSTされたデータが選ばれたテーブル名とカラムの配列だったら }else if(isset($_POST['post_tablename']) && isset($_POST['post_columname'])){ //で、テーブル名をゲットしとく $get_table=$_POST['post_tablename']; //まず、post_columnameの配列分の変数を作っておく $get_columns=$_POST['post_columname']; //配列が複数だった場合 if(count($get_columns)>1){ $get_column=implode(",",$_POST['post_columname']);//配列を,で区切って文字列にする }else{//配列が一つだった場合 $get_column=$get_columns[0]; } //じゃ、テーブルからカラムをSELECTする global $wpdb;//インスタンスを呼んでログイン $reading=$wpdb->get_results("SELECT $get_column FROM $get_table"); if(!$reading){ echo <<<TABLE <h1>SQL COMMAND : SELECT {$get_column} FROM {$get_table}</h1> <h2 style="color:red;">POSTしたTABLEのカラムは存在していません!</h2> <hr width='98%'> 不正なテーブル情報がPOSTされています。 TABLE; }else{ echo <<<TABLE <h1>SQL COMMAND : SELECT {$get_column} FROM {$get_table}</h1> POSTしたTABLEのカラムの内容を表示しています。 <hr width='98%'> <table border='1' cellpadding='5' bordercolor='#ffffff' width='98%'> <tr> TABLE; //こっちはカラムの名前を表示 for($r=0;$r<count($get_columns);$r++){ //変数を文字に変換しとかないとダメなもよう $name=strval($get_columns[$r]); echo "<td width='10%' bgcolor='#EEcccc'>".$name."</td>"; } echo "</tr>"; //OBUJECTなのでまず、$readingの配列数だけ繰り返す for($i=0;$i<count($reading);$i++){ echo "<tr>"; //次にカラム数だけ繰り返してカラムを抜き出す for($c=0;$c<count($get_columns);$c++){ //変数を文字に変換しとかないとダメなもよう $name=strval($get_columns[$c]); echo "<td width='10%' bgcolor='#eeeeee'>".$reading[$i]->$name."</td>"; } echo "</tr>"; } echo "</table>"; }
POSTされたカラム名が2つ以上の場合は配列を,で区切って文字に変換しています。1つしかない場合は最初の配列を入れています。また、カラムの変数は文字に変換しないとそのままではOBUJECTから抜き出せませんので注意です。
これでSELECT {$get_column} FROM {$get_table}のSQLコマンドを送信できます。$reading=$wpdb->get_results(“SELECT $get_column FROM $get_table”);してみましょう。
ちゃんと読み込めましたね。これでデータベースの内容を把握することができるようになりました。データベースの中身を覗いてみましょう。
【コード全般】
<?php /* Plugin Name: Show WordPress DataBase Plugin URI: https://wspri.dip.jp/?p=155 Description: wordpress.*データベースを読んで一覧を表示するプラグイン Author: jack.Amano Version: 0.0 Author URI:https://wspri.dip.jp */ //オブジェクトを作成 new showtables; //クラスを作る class showtables{ //コンストラクタ function __construct(){ add_action('admin_menu', array($this, 'add_admin'));//管理者メニューに追加する準備 } //メニューを追加する関数 function add_admin(){ if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)=="ja"){ $menuname="DB一覧表示"; }else{ $menuname="DB list display"; } //level_8から10が管理者 add_menu_page($nenuname,$menuname,'level_9', __FILE__, array($this,'show_DB')); } //DBを表示する関数 function show_DB(){ //non post if(isset($_POST['table_name']) && isset($_POST['colum_name'])){ if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)=="ja"){ $massage02="テーブルのカラムが送信されないとカラムの内容を読み込めません。ここに表示されているテーブル名のカラムか確認してから[Read Table & Columns]ボタンを押してください。"; }else{ $massage02="If the table column is not sent, the contents of the column can not be read. Confirm that the table name column is displayed here, and then click the [Read Table & Columns] button."; } $table_mono=$_POST['table_name']; $colum_array=$_POST['colum_name']; echo <<<FORM <h1>Show WordPress Table & Columns</h1> {$massage02} <hr width="98%"> <form method="post" action="" > <h2>TABLE NAME:<input type="text" name="post_tablename" value="{$table_mono}" style='font-size:20px;font-weight:900;'></h2> FORM; for ($i=0;$i<count($colum_array);$i++){ $c=$i+1; echo "<h4>» COLUMN{$c}:<input type='text' name='post_columname[]' value='{$colum_array[$i]}'></h4>"; } echo <<<FORM <input type="submit" value="Read Table & Columns"> </form> FORM; $this->readDB();//選びなおせるようにしますね //POSTされたデータが選ばれたテーブル名とカラムの配列だったら }else if(isset($_POST['post_tablename']) && isset($_POST['post_columname'])){ if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)=="ja"){ $errormsg1="POSTしたTABLEのカラムは存在していません!"; $errormsg2="不正なテーブル情報がPOSTされています。"; $massage03="POSTしたTABLEのカラムの内容を表示しています。"; }else{ $errormsg1="The column of TABLE that has been posted does not exist!"; $errormsg2="Incorrect table information has been POSTed."; $massage03="Displays the contents of the posted TABLE column."; } //で、テーブル名をゲットしとく $get_table=$_POST['post_tablename']; //まず、post_columnameの配列分の変数を作っておく $get_columns=$_POST['post_columname']; //配列が複数だった場合 if(count($get_columns)>1){ $get_column=implode(",",$_POST['post_columname']);//配列を,で区切って文字列にする }else{//配列が一つだった場合 $get_column=$get_columns[0]; } //じゃ、テーブルからカラムをSELECTする global $wpdb;//インスタンスを呼んでログイン $reading=$wpdb->get_results("SELECT $get_column FROM $get_table"); if(!$reading){ echo <<<TABLE <h1>SQL COMMAND : SELECT {$get_column} FROM {$get_table}</h1> <h2 style="color:red;">{$errormsg1}</h2> <hr width='98%'> {$errormsg2} TABLE; }else{ echo <<<TABLE <h1>SQL COMMAND : SELECT {$get_column} FROM {$get_table}</h1> {$massage03} <hr width='98%'> <table border='1' cellpadding='5' bordercolor='#ffffff' width='98%'> <tr> TABLE; //こっちはカラムの名前を表示 for($r=0;$r<count($get_columns);$r++){ //変数を文字に変換しとかないとダメなもよう $name=strval($get_columns[$r]); echo "<td width='10%' bgcolor='#EEcccc'>".$name."</td>"; } echo "</tr>"; //OBUJECTなのでまず、$readingの配列数だけ繰り返す for($i=0;$i<count($reading);$i++){ echo "<tr>"; //次にカラム数だけ繰り返してカラムを抜き出す for($c=0;$c<count($get_columns);$c++){ //変数を文字に変換しとかないとダメなもよう $name=strval($get_columns[$c]); echo "<td width='10%' bgcolor='#eeeeee'>".$reading[$i]->$name."</td>"; } echo "</tr>"; } echo "</table>"; } }else{ $this->readDB();//このクラス内の関数を呼びます } } private function readDB(){ if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)=="ja"){ $massage01="テーブル選択のラジオボタンをオンにして、そのテーブルのカラムを選択します。確認できたら[Set Form]ボタンを押してください。<br>テーブルまたはカラム名だけ送信することはできません。ラジオボタンがオフになっているテーブルのカラムを送信するとエラーになります。"; }else{ $massage01="Select the table selection radio button and select the columns for that table. After confirmation, please press the [Set Form] button. <br>You can not send only table or column names. It is an error to send a table column where the radio button is off."; } global $wpdb;//インスタンスを呼んでログイン $tables=$wpdb->get_results("SHOW TABLES FROM wordpress "); echo <<<TITLE <h1>Show WordPress All Tables</h1> {$massage01} <hr width="98%"> <form method="post" name="form1" action="" > TITLE; for ($i=0;$i<count($tables);$i++){//カウントアップしながら配列を分解 $tablename[$i]=$tables[$i]->Tables_in_wordpress; echo <<<ORITATAMI <div onclick="obj=document.getElementById('menu{$i}').style; obj.display=(obj.display=='none')?'block':'none';"> <h2><input type="radio" name="table_name" value="{$tablename[$i]}">{$tablename[$i]}</h2> </div> <div id="menu{$i}" style="display:none;clear:both;"> ORITATAMI; $colum_search = $wpdb->get_results("SHOW COLUMNS FROM $tablename[$i] "); for($c=0;$c<count($colum_search);$c++){ echo <<<ORITATAMI <div> <h3> » <input type="checkbox" name="colum_name[]" value="{$colum_search[$c]->Field}">{$colum_search[$c]->Field}</h3></div> ORITATAMI; } echo "</div>"; } echo "<input type='submit' value='Set Form'></form>"; } }
こんな感じです。日本語環境でない場合は英語になります。このプラグインは更にCSV出力機能などを付けてWordPressプラグインとして配布しますので(そのうち)使ってみてくださいね。
このプラグインを使ってみる。(日本語設定でない場合は英語表示)
ダウンロードしたzipファイルをプラグインページからインストールして有効化するとあなたのwordpressのMysqlデータベース一覧が見え、カラムの内容が所得できます。
Amazonからいかなるメールが突然来ました。
Greetings from Amazon Web Services, This e-mail confirms that your latest billing statement, for the account ending in ****6128, is available on the AWS web site. Your account will be charged the following: Total: $32.95 You can see a complete break down of all charges on the Billing & Cost Management page located here: https://console.aws.amazon.com/billing/home#/bill? statementTimePeriod=1538352000 To protect your privacy, we can only communicate account information to the e-mail address on file for your account. Thank you for using Amazon Web Services. Sincerely, Amazon Web Services To learn more about managing your AWS costs with Cost Allocation and Tagging, visit http://docs.aws.amazon.com/ awsaccountbilling/latest/ aboutv2/cost-alloc-tags.html This message was produced and distributed by Amazon Web Services, Inc. and affiliates, 410 Terry Ave. North, Seattle, WA 98109-5210. AWS will not be bound by, and specifically objects to, any term, condition or other provision which is different from or in addition to the provisions of the AWS Customer Agreement or AWS Enterprise Agreement between AWS and you (whether or not it would materially alter such AWS Customer Agreement or AWS Enterprise Agreement) and which is submitted in any order, receipt, acceptance, confirmation, correspondence or otherwise, unless AWS specifically agrees to such provision in a written instrument signed by AWS. The AWS Customer Agreement was updated on May 14, 2018. You can see more information about these changes at https://aws.amazon.com/ agreement/recent-changes/.
なに!32ドル92セント払え!何のこっちゃ!と思い、詳細を見てみると「GameLift」で32.92利用になっとる!ゲーム のサンプルで無料の筈が何故!
それの課金が!やられた!って感じでググってみたら案の定他のひとも悲惨な方がいらっしゃった。
あわてて僕もフリートを削除しました。
まあ、無料をうたうんだったらサンプルのダウンロードと通信も無料にしろよ!
高い勉強代になっちゃった!気をつけよう。
サポートに無料が課金されている旨を伝えたところ返金をする旨の連絡をいただきました。サンプルをビルドすると無料ではなく有料になる旨を記載すべきと進言しましたがどうなるかな?
少なくともサンプルをステップにしたがって進めると無料ではなくなるのでそれは明記すべきだと思います。
インストールしたパッケージのアンインストールは簡単
パッケージのみアンインストール
apt-get remove パッケージ名
設定ファイルごとアンインストール
apt-get purge パッケージ名
AWSで運用しているとWordPressのプラグインがインストール・アップデートできないって現象に見舞われるんですが、これはどうやら所有権の問題のようなのでその対処法を記載
まず、wp-config.phpに以下を追記。
define('FS_METHOD','direct');
これはFTPでエラーというのを防ぐためのもの。
続いてプラグインインストール・アップデート時のみwordpressディレクトリの所有権をrootに変更する。
chown -R root:root /home/wp
終わったら元に戻します。
chown -R user:user /home/wp
戻しておかないとWINSCPなどでアクセスできなくなっちゃいます。
AWSのubuntuを無料のSSL化ました。
でアクセスできます。
設定は簡単です。まず、rootでSSHにログインしてコンソールに
apt -y install certbot
と入力します。これでClientのcertbotがインストールされます。
次に証明書を発行しましょう。
certbot certonly --webroot -w /var/www/html -d wspri.dip.jp
で、DdocumentRootとアクセスしたいドメイン名を指定します。すると問い合わせ可能なメアドを聞いてきますので入力します。
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator webroot, Installer None Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel):
で、あとは利用許諾をするかしないかを選択して終わりです。が、おまけに財団に協力するかとか聞いてきます。
------------------------------------------------------------------------------- Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v01.api.letsencrypt.org/directory ------------------------------------------------------------------------------- (A)gree/(C)ancel: A ------------------------------------------------------------------------------- Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about EFF and our work to encrypt the web, protect its users and defend digital rights. ------------------------------------------------------------------------------- (Y)es/(N)o: Y
An unexpected error occurred:
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Please see the logfiles in /var/log/letsencrypt for more details.
IMPORTANT NOTES:
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
僕は一度失敗したのでもう一回やり直しました。
あとは/etc/letsencrypt/live/ドメイン/の中にcert.pem、chain.pem、fullchain.pem、privkey.pemがありますので通常の手順で設定してApache2の再起動で終わりです。
vi /etc/apache2/sites-available/default-ssl.conf SSLCertificateFile /etc/letsencrypt/live/wspri.dip.jp/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/wspri.dip.jp/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/wspri.dip.jp/chain.pem
尚証明書の更新は
certbot renew
のようです。
ログインしてインスタンスを作るまでは簡単な筈ですね。
まあ、僕は使っているモバイルノートがubuntuなのでubuntuにしています。アマゾンLinuxはアバウトでいいんですがセキュリティがちょっと心配なのでubuntuにしているのも理由のひとつです。
インスタンスが稼動するとこんな感じで「インスタンスの状態」がRunninngになります。
ただ、これだけではAWSネットワーク内にインスタンスがあるだけでwwwに公開されていません。自宅サーバの公開手順ではルータの設定でportをwww上に公開したようにAWSもportを公開する必要があります。
左側メニューの一番上の「EC2ダッシュボード」をクリックするとこのようなページが表示されます。
で、ここでは「6個のセキュリティグループ」をクリックして解放するportを追加します。
WSPRIはメール(受信)、WEB(ssl)、SSHを開放しています。
開放するportは「セキュリティグループの作成」で作成します。
開放するプロコトルを選択して「セキュリティグループ名」を適当につけて「説明」を入れます。
で、作成すると左メニューの「インスタンス」の<アクション<ネットワーキング<セキュリティグループの変更<で開放するportが選択できます。
これでApache2なりNginxなりPopServerなり公開できます。
でも、SMTPなどメールの送信にはAWSは使わない方がいいかも?なぜならほとんどが迷惑メール扱いにされちゃいます。
WSPRIでは送信にはGmailを使ってます。
サンダーバードなどの場合はそのままではsmtp.gmail.comにログインできないのでGmail設定の以下を変更します。(Googleアカウントでログインした上で)
送受信のテストをしてみましょう。送受信できるはず。
ちなみにApache2の設定もpostfix+Devecotの設定も特殊な方法はしていません。
またWSPRIはIeserverのDDNSを利用してます。
IPの設定はCrontabで以下を設定しています。(30分おきにIPをIeserverに送信しています)
10,40 * * * * /usr/bin/wget -O - 'https://ieserver.net/cgi-bin/dip.cgi?username=wspri&domain=dip.jp&password=*****&updatehost=1' > /dev/null
AWSではrootの初期状態ではパスワードの設定がないため必要な場合は以下を参考に設定します。
Apache2、php、Mysql、wordpressその他のインストールと設定は以下を参照します。
↑Linuxだとよく使うエディタの使い方
↑Unixのコマンドもよく使うんだよね