2013年8月28日水曜日

パフォーマンス監視 New Relic



パフォーマンス監視 New RelicについてのURLメモ

カジュアルに鯖を監視するNewRelic をAmazon Linux に導入 - aws memo
http://understeer.hatenablog.com/entry/2012/10/10/140709

Rails - パフォーマンス監視サービスのNew Relicが超便利な件 - Qiita [キータ]
http://qiita.com/y_uuki/items/dd4fce78b1bc32b64600

New Relicでアプリケーションのパフォーマンス測定 | Developers.IO
http://dev.classmethod.jp/tool/newrelic/

Rails + New Relic ならレスポンスタイム等のパフォーマンス監視が簡単にできるよ - 彼女からは、おいちゃんと呼ばれています
http://d.hatena.ne.jp/inouetakuya/20121216/1355656214

プロセス監視 God



プロセス監視 GodについてのURLメモ

スクリプトをDaemonizeする。god ツール.しかもプロセス監視・再起動付き - それマグで!
http://takuya-1st.hatenablog.jp/entry/20110918/1316365681

プロセス監視ツール God で delayed job を監視しよう。 - モーリスのシステム開発日記
http://d.hatena.ne.jp/maurice38/20111128/1322469725

GOD Ruby製プロセス監視フレームワーク
http://www.bootstrap-english.net/translations/god

Ruby製のプロセス監視ツールGodについてのメモ。
http://maeshima.hateblo.jp/entry/20100825/1282743494

Ruby - godのevent監視:monitがやらないけどGodがすること - Qiita [キータ]
http://qiita.com/sawanoboly@github/items/0c9202d447ec67402a0f

あーありがち - Godで初めてのプロセス監視
http://aligach.net/diary/20090817.html

プロセス監視ツール「God」 | Fusic Developers' Weblog
http://blog.fusic.co.jp/archives/235

2013年8月26日月曜日

WordPress Similar Postsのパッチ 同じタイトルのポストが複数出なくなります。

*** similar-posts.php.org Mon Aug 26 13:50:00 2013
--- similar-posts.php Mon Aug 26 13:52:13 2013
***************
*** 140,146 ****
     if ($check_age) $where[] = where_check_age($options['age']['direction'], $options['age']['length'], $options['age']['duration']);
     if ($check_custom) $where[] = where_check_custom($options['custom']['key'], $options['custom']['op'], $options['custom']['value']);
     $sql .= "WHERE ".implode(' AND ', $where);
!    if ($check_custom) $sql .= " GROUP BY $wpdb->posts.ID"; 
     $sql .= " ORDER BY score DESC LIMIT $limit";
     //echo $sql;
     $results = $wpdb->get_results($sql);
--- 140,148 ----
     if ($check_age) $where[] = where_check_age($options['age']['direction'], $options['age']['length'], $options['age']['duration']);
     if ($check_custom) $where[] = where_check_custom($options['custom']['key'], $options['custom']['op'], $options['custom']['value']);
     $sql .= "WHERE ".implode(' AND ', $where);
!    if ($check_custom) $sql .= " GROUP BY $wpdb->posts.ID";
!    // delete duplicate rows
!    $sql .= " GROUP BY title"; 
     $sql .= " ORDER BY score DESC LIMIT $limit";
     //echo $sql;
     $results = $wpdb->get_results($sql);
similar-posts.php.patch
WordPressのプラグインである、Similar Postsのパッチを書いてみました。
Similar Postsのバージョンは、2.6.2.0です。
同じタイトルのポストが複数出なくなります。
普通にblogを書いているぶんには、同じタイトルのポストとかは、あまりしないと思いますが、情報を自動で集めたりしている場合には役に立つかもです。

2013年8月23日金曜日

PHP5.4 + MySQL5.5 + APCをインストール (おもに友人のWordPressサーバのセットアップメモです。)

 (おもに友人のWordPressサーバのセットアップメモです。)

ミドルウェアは、最新のものを使うならば、PHP5.5 + MySQL5.6 + Zend OPcacheなのですが、 『簡単に・素早くセットアップ出来る』 ということを優先して、このバージョンを選んでいます。

環境はCentOS 6です。

MySQL5.5のインストール

既存のmysqlがあった場合アンインストール。
# yum remove mysql-libs mysql-server mysqlclient15 mysql-devel perl-DBD-mysql mysql

OSが64bitの場合です。
# wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm 
# wget http://ftp.jaist.ac.jp/pub/Linux/Fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
# rpm -Uvh epel-release-6-8.noarch.rpm
# rpm -Uvh remi-release-6.rpm 
# yum --enablerepo=remi install mysql.x86_64 mysql-server.x86_64 mysql-devel.x86_64
# /etc/init.d/mysqld start

パスワードを設定します。 new-passwordと、hostnameのところは便宜変えてください。
# /usr/bin/mysqladmin -u root password 'new-password'
# /usr/bin/mysqladmin -u root -h hostname password 'new-password'

ログインできることを確認。
# /usr/bin/mysql -uroot -pnew-password

my.cnf調整
innodb_buffer_pool_sizeは使用可能メモリの60%を推奨です。
PHPアプリなどと同居する場合は便宜調整してください。
# cp /usr/share/mysql/my-huge.cnf /etc/my.cnf
# emacs /etc/my.cnf
[client]
default-character-set = utf8

[mysqld]
log-bin=mysql-bin
binlog_format=mixed  
max_connections = 1024
character-set-server = utf8
table_open_cache = 4000
table_definition_cache = 400
key_buffer_size = 16M       
read_buffer_size = 256K     
read_rnd_buffer_size = 512K 
join_buffer_size = 256K
max_binlog_size = 128M
expire_logs_days = 7
server_id = 101 # サーバ毎に違う値を入れる
slow_query_log = 1
slow_query_log_file = /var/lib/mysql/mysqld-slow.log
long_query_time = 5
innodb_buffer_pool_size = 5G # 使用可能メモリの60%を推奨
innodb_additional_mem_pool_size = 16M
innodb_autoextend_increment = 64M
innodb_log_file_size = 128M
innodb_log_buffer_size = 16M
innodb_flush_method = O_DIRECT
innodb_thread_concurrency = 8
innodb_commit_concurrency = 10
innodb_lock_wait_timeout = 3600
innodb_file_format = Barracuda
innodb_file_per_table
skip-name-resolve
performance_schema = 0 # サーバのメモリが少ないときにはオフにしておく

MySQL5.5の再起動。
# /etc/init.d/mysqld stop
# cd /var/lib/mysql
# rm ib_logfile0 ib_logfile1
# /etc/init.d/mysqld start

ログを確認。
# cat /var/log/mysqld.log 

ログインして、データベースを作っておきます。
PHP5.4のインストール

iusレポジトリを使います。 一緒にhttpdもインストールします。
# rpm -Uvh  http://dl.iuscommunity.org/pub/ius/stable/CentOS/6/x86_64/ius-release-1.0-11.ius.centos6.noarch.rpm
# rpm -Uvh  http://dl.iuscommunity.org/pub/ius/stable/CentOS/6/x86_64/epel-release-6-5.noarch.rpm
# yum install php54-cli php54-pdo php54-mysql php54-common php54 php54-fpm php54-devel php54-mbstring php54-mcrypt php54-pear php54-gd httpd httpd-devel

httpdのログを圧縮・90日間保存するように設定しておく。
compressと、rotate 90を追記。 notifemptyを削除。
# emacs /etc/logrotate.d/httpd
/var/log/httpd/*log {
    missingok
    rotate 90
    compress
    sharedscripts
    delaycompress
    postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}

APCのインストール 

peclコマンドでインストールできます。
# pecl install APC

extensionの設定と、メモリの設定をします。
# emacs /etc/php.ini
extension=apc.so
apc.shm_size=64M

httpdもしくはphp-fpmの再起動で有効になります。
# /etc/init.d/httpd restart 

確認します。
# php -i | grep apc

APCの管理画面 apcのキャッシュを可視化できるようにしておくと良いでしょう。 phpスクリプトができているのでコピーします。 ※便宜アクセス制限をかけてください。
# mkdir -p /var/www/apc
# cp /usr/share/pear/apc.php /var/www/apc

apc.phpの以下の行を調整すると、グラフが見えるようになり、より使えるようになります。
defaults('ADMIN_PASSWORD','password');          // Admin Password - CHANGE THIS TO ENABLE!!!

httpdの設定例
# vi /etc/httpd/conf.d/002_apc.conf

Listen 8080

  ServerName ****************
  DocumentRoot /var/www/apc
  
    Options All
    Options -Indexes
    AllowOverride All
    Order allow,deny
    allow from all

    # BASIC Auth
    AllowOverride AuthConfig
    AuthType Basic
    AuthName Basic
    AuthUserFile /var/www/apc/.htpasswd
    Require valid-user
  


nginxの設定例
server {
  listen 5130;
  server_name xxxxxxxxxxxxxxxxxxxxxxxx;

  auth_basic "basic authentication";
  auth_basic_user_file "/var/www/apc_control_panel/.htpasswd";

  server_name _;
  root /var/www/apc_control_panel;
  index index.php index.html;
  charset utf-8;

  ## access log settings
  access_log  /var/log/nginx/fastcgi_log combined;

  location / {
    index index.php;
  }

  ## location /
  location ~ \.php$ {
      fastcgi_pass unix:/tmp/php.socket;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
      fastcgi_pass_header "X-Accel-Redirect";
      fastcgi_pass_header "X-Accel-Expires";
  }
}


iptablesの設定

とりあえず、ssh用の22番ポートと、HTTP用の80番ポートを開放します。 それ以外を遮断します。
# vi /etc/sysconfig/iptables
*filter
:INPUT   ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT  ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# SSH
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22    -j ACCEPT

# HTTP
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80    -j ACCEPT

# REJECT others
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited

COMMIT
# /etc/init.d/iptables restart


httpd.confの設定

アクセス過多でサーバがハングアップした状態にならないように、プロセスの数が一定なるように調整してあげます。 数値はメモリ量を見つつ、いい感じに変更してください。
# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
# vi /etc/httpd/conf/httpd.conf

     
StartServers       8     
MinSpareServers    5     
MaxSpareServers   20     
ServerLimit      256     
MaxClients       256     
MaxRequestsPerChild  4000
      
StartServers      50      
MinSpareServers   50      
MaxSpareServers   50      
ServerLimit       50      
MaxClients        50      
MaxRequestsPerChild  4000 
      
NameVirtualHostを有効にします。 今回は、80番ポートだけです。SSLも使う場合は443も設定しましょう。
# NameVirtualHost *:80  
↓
NameVirtualHost *:80  
WordPress用の設定
# vi /etc/httpd/conf.d/001_wp.conf 


  ServerName ssd1.xxxxxx.jp
  DocumentRoot /var/www/wordpress-ssd1
  
    Options All
    Options -Indexes
    AllowOverride All
    Order allow,deny
    allow from all
  



  ServerName ssd2.xxxxxx.jp
  DocumentRoot /var/www/wordpress-ssd2
  
    Options All
    Options -Indexes
    AllowOverride All
    Order allow,deny
    allow from all
  

ディレクトリを作成して、そこにWordPressを設置します。
mdkir -p /var/www/wordpress-ssd1
mdkir -p /var/www/wordpress-ssd2

↓
WordPress設置

↓
ファイルの権限を変更
# cd /var/www/
# chown -R apache:apache wordpress-ssd1
# chown -R apache:apache wordpress-ssd2
httpdをスタートして、ブラウザでアクセスしてみます。
# /etc/init.d/httpd restart


以上。
簡単ですね!