PHPのログ出力

環境

CentOS release 6.7
Apache/2.2.15
PHP 5.4.45

 

php.iniの設定

php.ini
error_reporting = E_ALL | E_STRICT
display_errors on
error_log = /var/log/php/error.log
date.timezone = Asia/Tokyo

httpd.confの設定

httpd.conf
ErrorLog logs/error_log

ログファイル作成

#mkdir /var/log/php
#touch /var/log/php/error.log
#chmod 777 /var/log/php

動作確認

sample.php
error_log('hoge');

PHP Fatal error: Call to a member function prepare() on a non-object

データベースの接続がうまくいってないのが原因です。

試しで下の接続方法で接続してみた。

$link = pg_connect("host=localhost dbname=db user=usr password=pass");

FATAL: Ident authentication failed for use 'hoge_user' が出ました。

postgresqlがうまく動いているかを確認してみるのがいいと思います。

再起動してみる

#su - postgres

-bash-4.1$pg_ctl start -w

あれれ

pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2016-02-14 14:00:00 GMT [4244]LOG: could not bind IPv4 socket: Address already in use
2016-02-14 14:00:00 GMT [4244]HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2016-02-14 14:00:00 GMT [4244]LOG: could not bind IPv6 socket: Address already in use
2016-02-14 14:00:00 GMT [4244]HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2016-02-14 14:00:00 GMT [4244]WARNING: could not create listen socket for "*"
2016-02-14 14:00:00 GMT [4244]FATAL: could not create any TCP/IP sockets
.... stopped waiting
pg_ctl: could not start server
Examine the log output

起動失敗

-bash-4.1$ lsof -i:5432

-bash-4.1$ kill PID

再度起動(よっうまくいった)

接続みてみたら治った。よかった

AngularJSでPOST

$_POST['hoge']や$this->request->data['hoge']で受け取れない!!調べた結果下記の様になら取れる

postdata = file_get_contents("php://input");

request=json_decode(postada);

 AngularJS APIでPOSTのやり方(JS側ね)

<!doctype html>
<html land="ja" ng-app="myApp">
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>

//app
var app = angular.module('myApp',[]);

//controller
app.controller('myCtrl',function($scope,$http){

    $http.post(url,postdatas,{

    headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},

    })
    .success(function(response){

        //response
        $scope.name = response.name;
        $scope.email = response.email;

    });

});
</script>
</head>
<body>
<div ng-controller="myCtrl">
{{name}}<br>
{{email}}
</div>
</body>
</script>

PHP側受け取る

<?php

    $name = "default";
    $email = "default@test.com";

    $postdata = file_get_contents("php://input");

    $request = json_decode($postdata);
header("Content-type: application/json"); echo json_encode($res);

Composerでプラグインをインストールする時のエラー

composer update

Your requirements could not be resolved to an installable set of packages.

Problem 1
- cakephp/cakephp 2.6.9 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- cakephp/cakephp 2.6.8 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- cakephp/cakephp 2.6.7 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- cakephp/cakephp 2.6.6 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- cakephp/cakephp 2.6.5 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- cakephp/cakephp 2.6.4 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- cakephp/cakephp 2.6.3 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- cakephp/cakephp 2.6.2 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- cakephp/cakephp 2.6.12 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- cakephp/cakephp 2.6.11 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- cakephp/cakephp 2.6.10 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- cakephp/cakephp 2.6.1 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- cakephp/cakephp 2.6.0 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- cakephp/cakephp 2.6.8 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
- Installation request for cakephp/cakephp 2.6.* -> satisfiable by cakephp/cakephp[2.6.0, 2.6.1, 2.6.10, 2.6.11, 2.6.12, 2.6.2, 2.6.3, 2.6.4, 2.6.5, 2.6.6, 2.6.7, 2.6.8, 2.6.9].

 

 mcryptがないようと怒られる

$sudo apt-get install php5-mcrypt

再度試せばOK

WordPress構築「Vagrant+PHP+Mysql+Apache」

Vagrant作成

Vagrantfile作成

vagrant init Box名

Vagrantfileが存在するディレクトリでvagrant upを実行

②PHP5.6インストール

Centos6.0用

# For CentOS 6.5, 6.4, ...

% sudo rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm

% sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

Centos7.0用

# For CentOS 7.0

$ sudo rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

% sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

yum listパケージを確認する

% yum list --enablerepo=remi --enablerepo=remi-php56 | grep php

インストール

sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

sudo yum  --enablerepo=remi --enablerepo=remi-php56 install  php php-mbstring php-mysql 

*色々コマンドインストール

sudo yum install vim-enhanced git zsh dstat sysstat ccze s3cmd wget

 

MySQLインストール

rpmを追加する

yum install http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm

インストール

yum install mysql mysql-devel mysql-server mysql-utilities

設定ファイル

# /etc/my.cnf

[mysqld]
character-set-server = utf8
skip-character-set-client-handshake

[mysql]
default-character-set = utf8

MySQL の起動、自動起動設定

$ sudo service mysqld start
$ sudo chkconfig mysqld on

セキュリティ強化

$ sudo mysql_secure_installation

Enter current password for root (enter for none): Enter
Set root password?(rootパスワード設定する?) [Y/n] Y
Remove anonymous users?(匿名ユーザー削除する?) [Y/n] Y
Disallow root login remotely?(リモートからのrootログインを拒否する?) [Y/n] Y
Remove test database and access to it?(テストDB削除する?) [Y/n] Y
Reload privilege tables now?(設定した内容を反映するか?) [Y/n] Y

データベース作成

データベースを「wordpress」ユーザー名を「wpadmin」パスワードを「password」で作成します。

$ mysql -u root -p
> CREATE DATABASE `wordpress` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
> CREATE USER `wpadmin`@`localhost` IDENTIFIED BY 'password';
> GRANT ALL ON `wordpress`.* TO `wpadmin`@`localhost`;

Apache のインストール

$ sudo yum -y install httpd

設定ファイル

# /etc/httpd/conf/httpd.conf

# ユーザーとグループの設定です
User apache
Group apache

# Web サーバーにアクセスしたクライアントに返す情報です。
# レスポンスヘッダには「Server: Apache」という名前しか返されません。
ServerTokens Prod

# エラーメッセージ出力時にフッタを表示しないようにします。
ServerSignature Off
Apache の起動、自動起動設定
$ sudo service httpd start
$ sudo chkconfig httpd on

WordPress のインストール

https://ja.wordpress.org/ から最新版をダウンロードします。

$ cd /var/www
$ wget https://ja.wordpress.org/latest-ja.tar.gz
$ sudo tar zxfv latest-ja.tar.gz
$ sudo rm -rf  latest-ja.tar.gz
$ sudo chown -R apache:apache wordpress

設定ファイル

$ cd /var/www/wordpress
$ sudo cp wp-config-sample.php wp-config.php
# wp-config.php

// データベース情報を設定します。
define('DB_NAME', 'wordpress');
define('DB_USER', 'wpadmin');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');

// 「put your unique phrase here」を 適当な文字列に置き換えます。
define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

// プレフィックスは変更しておいた方がいいです。
$table_prefix  = 'wp_';

動作確認

http://[wordpress.confで設定したドメイン] をブラウザで開きます。

Basic 認証の設定

管理画面にBasic 認証を設定します。

鍵の生成

ユーザー名を「wordpress」 パスワードを「password」にします。

$ sudo htpasswd -cb /var/www/wordpress/.htpasswd wordpress password
# /var/www/wordpress/.htaccess

<Files "wp-login.php">
  AuthType Basic
  AuthName "Please enter your name and password"
  AuthUserFile /var/www/wordpress/.htpasswd
  Require valid-user
</Files>

設定を反映

$ sudo apachectl configtest
$ sudo service httpd graceful

シンボリックリンク

sudo ln -s /vagrant/hoge /var/www/html

 

* .htaccessを/html の直下に作成

権限を755にする

アクセル確認

http://VagrantFileのIP 設定値/wordpress/wp-admin/install.php

データベスとテーブルサイズの確認

全てのデータベースのサイズを確認する

# MB単位
select table_schema, sum(data_length+index_length) /1024 /1024 as MB from information_schema.tables  group by table_schema order by sum(data_length+index_length) desc;

+--------------------------+---------------+
| table_schema             | MB                  |
+--------------------------+---------------+
| database1                 | 5579.89978790| 
| database2                 | 3658.48437786| 
+--------------------------+---------------+


# GB単位
select table_schema, sum(data_length+index_length) /1024 /1024/1024 as GB from information_schema.tables  group by table_schema order by sum(data_length+index_length) desc;

+--------------------------+----------------+
| table_schema             | GB                |
+--------------------------+----------------+
| database1                  | 5.449120886624|  
| database2                  | 3.572738650255| 
+--------------------------+----------------+



特定のデータベースのサイズを確認する

use database1;
select table_schema, sum(data_length+index_length) /1024 /1024 as MB from information_schema.tables where table_schema = database();

もしくは
select table_schema, sum(data_length+index_length) /1024 /1024 as MB from information_schema.tables where table_schema = 'databaseName';

+-----------------+---------------+
| table_schema | MB            |
+-----------------+---------------+
| database1      | 5579.89978790| 
+-----------------+---------------+



テーブル単位でサイズを確認する

use databaseName;
select  
table_name, engine, table_rows as tbl_rows, avg_row_length as rlen,  
floor((data_length+index_length)/1024/1024) as allMB,  #総容量
floor((data_length)/1024/1024) as dMB,  #データ容量
floor((index_length)/1024/1024) as iMB   #インデックス容量
from information_schema.tables  
where table_schema=database()  
order by (data_length+index_length) desc; 

+------------------------------+--------+----------+------+-------+------+------+
| table_name                | engine     | tbl_rows  | rlen | allMB | dMB | iMB |
+------------------------------+--------+----------+------+-------+------+------+
| table1                 | MyISAM   |   156382     | 3738 |   570 |  557 |   12 | 
| table2                 | MyISAM   |   185280     | 1624 |   311 |  287 |   24 | 
| table3                 | MyISAM   |   208823     |  394  |   103 |   78  |   24 | 
+------------------------------+--------+----------+------+-------+------+------+

 

ストアドプロシージャに登録する

いちいち入力するのは面倒なので、ストアドプロシージャに登録しておくと便利です。

mysql> delimiter //
mysql> create procedure show_table_size()
mysql> begin
mysql> select  
mysql> table_name, engine, table_rows as tbl_rows, avg_row_length as rlen,  
mysql> floor((data_length+index_length)/1024/1024) as allMB, 
mysql> floor((data_length)/1024/1024) as dMB,  
mysql> floor((index_length)/1024/1024) as iMB 
mysql> from information_schema.tables  
mysql> where table_schema=database()  
mysql> order by (data_length+index_length) desc; 
mysql> end
mysql> //
mysql> delimiter ;

これで登録完了です。

使うときは、

mysql> call show_table_size;

これで簡単に使えますね。

 

ストアドプロシージャの一覧を見るには以下のコマンドで。

mysql> show procedure status;

必要なくなったプローシージャは以下のコマンドで削除ができます。

mysql> drop procedure show_table_size;