ラベル PHP の投稿を表示しています。 すべての投稿を表示
ラベル PHP の投稿を表示しています。 すべての投稿を表示

2011年8月7日日曜日

[LAMP][CentOS]さくらVPSセットアップ

■概要
さくらVPSにLAMP環境を構築します。

■基本情報
OS   : CentOS 5(2011/8時点の最小構成)
ディスク: 20GB
メモリ : 512MB

■パッケージ確認
Apache、MySQL、PHPそれぞれのインストールパッケージを確認します。
# yum list | grep httpd
httpd.x86_64 2.2.3-45.el5.centos.1 updates
httpd-devel.i386 2.2.3-45.el5.centos.1 updates
httpd-devel.x86_64 2.2.3-45.el5.centos.1 updates
httpd-manual.x86_64 2.2.3-45.el5.centos.1 updates
system-config-httpd.noarch 5:1.3.3.3-1.el5 base

# yum list | grep mysql
apr-util-mysql.x86_64 1.2.7-11.el5_5.2 base
bytefx-data-mysql.x86_64 1.2.4-2.el5.centos extras
freeradius-mysql.x86_64 1.1.3-1.6.el5 base
freeradius2-mysql.x86_64 2.1.7-7.el5 base
libdbi-dbd-mysql.x86_64 0.8.1a-1.2.2 base
mod_auth_mysql.x86_64 1:3.0.0-3.2.el5_3 base
mysql.i386 5.0.77-4.el5_6.6 updates
mysql.x86_64 5.0.77-4.el5_6.6 updates
mysql-bench.x86_64 5.0.77-4.el5_6.6 updates
mysql-connector-odbc.x86_64 3.51.26r1127-1.el5 base
mysql-devel.i386 5.0.77-4.el5_6.6 updates
mysql-devel.x86_64 5.0.77-4.el5_6.6 updates
mysql-server.x86_64 5.0.77-4.el5_6.6 updates
mysql-test.x86_64 5.0.77-4.el5_6.6 updates
pdns-backend-mysql.x86_64 2.9.21-4.el5.centos extras
php-mysql.x86_64 5.1.6-27.el5_5.3 base
php-pear-MDB2-Driver-mysql.noarch 1.4.1-3.el5.centos extras
php53-mysql.x86_64 5.3.3-1.el5_6.1 updates
qt4-mysql.x86_64 4.2.1-1 base
rsyslog-mysql.x86_64 3.22.1-3.el5_6.1 updates

# yum list | grep php
php.x86_64 5.1.6-27.el5_5.3 base
php-bcmath.x86_64 5.1.6-27.el5_5.3 base
php-cli.x86_64 5.1.6-27.el5_5.3 base
php-common.x86_64 5.1.6-27.el5_5.3 base
php-dba.x86_64 5.1.6-27.el5_5.3 base
php-dbase.x86_64 5.1.6-15.el5.centos.1 extras
php-devel.x86_64 5.1.6-27.el5_5.3 base
php-gd.x86_64 5.1.6-27.el5_5.3 base
php-imap.x86_64 5.1.6-27.el5_5.3 base
php-ldap.x86_64 5.1.6-27.el5_5.3 base
php-mbstring.x86_64 5.1.6-27.el5_5.3 base
php-mcrypt.x86_64 5.1.6-15.el5.centos.1 extras
php-mhash.x86_64 5.1.6-15.el5.centos.1 extras
php-mssql.x86_64 5.1.6-15.el5.centos.1 extras
php-mysql.x86_64 5.1.6-27.el5_5.3 base
php-ncurses.x86_64 5.1.6-27.el5_5.3 base
php-odbc.x86_64 5.1.6-27.el5_5.3 base
php-pdo.x86_64 5.1.6-27.el5_5.3 base
php-pear.noarch 1:1.4.9-6.el5 base
php-pear-Auth-SASL.noarch 1.0.2-4.el5.centos extras
php-pear-DB.noarch 1.7.13-1.el5.centos extras
php-pear-Date.noarch 1.4.7-2.el5.centos extras
php-pear-File.noarch 1.2.2-1.el5.centos extras
php-pear-HTTP-Request.noarch 1.4.2-1.el5.centos extras
php-pear-Log.noarch 1.9.13-1.el5.centos extras
php-pear-MDB2.noarch 2.4.1-2.el5.centos extras
php-pear-MDB2-Driver-mysql.noarch 1.4.1-3.el5.centos extras
php-pear-Mail.noarch 1.1.14-1.el5.centos extras
php-pear-Mail-Mime.noarch 1.4.0-1.el5.centos extras
php-pear-Net-SMTP.noarch 1.2.10-1.el5.centos extras
php-pear-Net-Sieve.noarch 1.1.5-2.el5.centos extras
php-pear-Net-Socket.noarch 1.0.8-1.el5.centos extras
php-pear-Net-URL.noarch 1.0.15-1.el5.centos extras
php-pecl-Fileinfo.x86_64 1.0.4-3.el5.centos extras
php-pecl-memcache.x86_64 2.2.3-1.el5_2 extras
php-pgsql.x86_64 5.1.6-27.el5_5.3 base
php-readline.x86_64 5.1.6-15.el5.centos.1 extras
php-snmp.x86_64 5.1.6-27.el5_5.3 base
php-soap.x86_64 5.1.6-27.el5_5.3 base
php-tidy.x86_64 5.1.6-15.el5.centos.1 extras
php-xml.x86_64 5.1.6-27.el5_5.3 base
php-xmlrpc.x86_64 5.1.6-27.el5_5.3 base
php53.x86_64 5.3.3-1.el5_6.1 updates
php53-bcmath.x86_64 5.3.3-1.el5_6.1 updates
php53-cli.x86_64 5.3.3-1.el5_6.1 updates
php53-common.x86_64 5.3.3-1.el5_6.1 updates
php53-dba.x86_64 5.3.3-1.el5_6.1 updates
php53-devel.x86_64 5.3.3-1.el5_6.1 updates
php53-gd.x86_64 5.3.3-1.el5_6.1 updates
php53-imap.x86_64 5.3.3-1.el5_6.1 updates
php53-intl.x86_64 5.3.3-1.el5_6.1 updates
php53-ldap.x86_64 5.3.3-1.el5_6.1 updates
php53-mbstring.x86_64 5.3.3-1.el5_6.1 updates
php53-mysql.x86_64 5.3.3-1.el5_6.1 updates
php53-odbc.x86_64 5.3.3-1.el5_6.1 updates
php53-pdo.x86_64 5.3.3-1.el5_6.1 updates
php53-pgsql.x86_64 5.3.3-1.el5_6.1 updates
php53-process.x86_64 5.3.3-1.el5_6.1 updates
php53-pspell.x86_64 5.3.3-1.el5_6.1 updates
php53-snmp.x86_64 5.3.3-1.el5_6.1 updates
php53-soap.x86_64 5.3.3-1.el5_6.1 updates
php53-xml.x86_64 5.3.3-1.el5_6.1 updates
php53-xmlrpc.x86_64 5.3.3-1.el5_6.1 updates

Apache2.2系、MySQL5.0系、PHP5.1系がインストール可能です。

■Apache
# yum -y install httpd
# httpd -v
Server version: Apache/2.2.3
Server built: May 4 2011 06:51:15

■MySQL

今回、MySQLは5.5系、PHPは5.3系をインストールしたい為、サードパーティのリポジトリを指定してインストールします。

リポジトリの登録
# rpm -ivh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
# yum --enablerepo=remi install mysql mysql-server

MySQLサーバ起動
# /etc/rc.d/init.d/mysqld start
MySQL Daemon failed to start.
Starting mysqld: [FAILED]

失敗する。ログを確認してみる。
# cat /var/log/mysqld.log
110807 20:03:29 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
110807 20:03:29 [ERROR] Error message file '/usr/share/mysql/english/errmsg.sys' had only 480 error messages,
but it should contain at least 712 error messages.
Check that the above file is the right version for this program!
110807 20:03:29 [Note] Plugin 'FEDERATED' is disabled.
/usr/libexec/mysqld: Unknown error 1146
110807 20:03:29 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
110807 20:03:29 InnoDB: The InnoDB memory heap is disabled
110807 20:03:29 InnoDB: Mutexes and rw_locks use GCC atomic builtins
110807 20:03:29 InnoDB: Compressed tables use zlib 1.2.3
110807 20:03:29 InnoDB: Using Linux native AIO
110807 20:03:29 InnoDB: Initializing buffer pool, size = 128.0M
110807 20:03:29 InnoDB: Completed initialization of buffer pool
InnoDB: The first specified data file ./ibdata1 did not exist:
InnoDB: a new database to be created!
110807 20:03:29 InnoDB: Setting file ./ibdata1 size to 10 MB
InnoDB: Database physically writes the file full: wait...
110807 20:03:30 InnoDB: Log file ./ib_logfile0 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile0 size to 5 MB
InnoDB: Database physically writes the file full: wait...
110807 20:03:30 InnoDB: Log file ./ib_logfile1 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile1 size to 5 MB
InnoDB: Database physically writes the file full: wait...
InnoDB: Doublewrite buffer not found: creating new
InnoDB: Doublewrite buffer created
InnoDB: 127 rollback segment(s) active.
InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
110807 20:03:31 InnoDB: Waiting for the background threads to start
110807 20:03:32 InnoDB: 1.1.8 started; log sequence number 0
110807 20:03:32 [ERROR] Aborting

110807 20:03:32 InnoDB: Starting shutdown...
110807 20:03:33 InnoDB: Shutdown completed; log sequence number 1595675
110807 20:03:33 [Note]
110807 20:03:33 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

メッセージファイル(/usr/share/mysql/english/errmsg.sys)の競合が発生しているようです。

該当ファイルの競合を確認
# rpm -q --whatprovides /usr/share/mysql/english/errmsg.sys
mysql-libs-5.5.15-1.el5.remi
mysql-5.0.77-4.el5_6.6

2つのバージョンが競合していることが分かりました。現状のmysqlをいったん削除します。
# yum -y remove mysql mysql-server mysql-libs

明示的にバージョン指定
# yum --enablerepo=remi install mysql-server.x86_64
# yum --enablerepo=remi install mysql-devel.x86_64

# mysql_install_db

# /etc/rc.d/init.d/mysqld start

因みにMySQL5.5系ではmy.cnfに以下の設定を記述すると起動しない。innodbがデフォルトエンジンだからかも。 
設定内容:skip_innodb

■PHP
# yum install -y --enablerepo=remi php
# php -v
PHP 5.3.6 (cli) (built: May 16 2011 19:18:05)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

以上です。

2010年3月15日月曜日

[PHP] 標準関数まとめ

PHP関数について、使用例をまとめていきます。

parse_url() — URL を解釈し、その構成要素を返す
$geturl = $_GET['url'];
$aParse = parse_url($geturl);

パース内容は以下の通り
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)

getenv() — 環境変数の値を取得する
$env = getenv('HTTP_REFERER');
$ip = getenv('REMOTE_ADDR');

※スーパーグローバル変数を使用する場合と等価
$ip = $_SERVER['REMOTE_ADDR'];

strpos() — 文字列が最初に現れる場所を見つける
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme); // 0

ereg_replace() — 正規表現にマッチした文字列を置き換える
$string_from = "original_string"; // 変更対象の文字列
$string_before = "~"; // 変更する箇所
$string_after = "~" // 変更する内容

$string_to = ereg_replace($string_before, $string_after, $string_from);

preg_replace() — はPerlの正規表現が使用できる。

配列内に要素があることを確認
in_array()

型チェック(整数値であるかを確認)
in_numeric()


以上です。


2010年3月10日水曜日

[PHP] キャッシュ手法まとめ

PHPでのキャッシュ手法をまとめる

1.中間コードキャッシュ
・APC
・eAccelerator
・XCache
・Zend Optimizer

APCが最も安定している
インストールコマンド
sudo aptitude install php-apc
sudo /etc/init.d/apache restart

管理画面apc.phpの導入
cd /usr/share/doc/php-apc/
sudo gzip -d apc.php.gz -c > /var/www/html/apc.php

GDのインストール
sudo aptitude install php5-gd
sudo /etc/init.d/apache restart

apache-benchコマンド
ab -n 500 -c 5 http://localhost/index.php
同時接続数5で500リクエストを送信


2.データキャッシュ
・ディスクキャッシュ
Cache_Liteの使用
インストール
sudo aptitude install php-pear
sudo pear install -Z Cache_Lite

・オンメモリキャッシュ

3.ページキャッシュ

2010年2月1日月曜日

[PHP] PEARのインストール

コマンドは以下のとおり

$go-pear.bat

Are you installing a system-wide PEAR or a local copy?
(system|local) [system] :

enter


以上です。

2008年8月21日木曜日

[Security]認証・認可処理のアンチパターン

WEBアプリケーションの認証処理でやってはいけないこと、その理由をまとめます。

Refererを認証・認可に使用しない
認証・認可にRefererを使用し、判定しているサイトがあるが、RefererはWEBブラウザから送信される情報で、容易に改ざん可能であるため、認証・認可に適さない。

認証・認可にDBを利用する場合、適切なエスケープを行うこと
認証・認可において、DBに保存しているユーザ情報を使用する場合、ユーザ入力値(ユーザIDやパスワード)は必ずエスケープすること。また、プリペアード・ステートメントを合わせて使用すること。

ユーザ名とパスワードが一致するレコードの有無で認証している場合、以下のようなロジックを実装することになる。(PHPの例)
$result = pg_query("SELECT * FROM user_table
                                WHERE user_name = '".$_POST['username']."
                                AND password = '".$_POST['username']."'");
if (pg_num_rows($result) === 1) {
    ログインOK
}

ここで、ユーザ名に「'"admin';--"」を入力すると次のようなSQLが発行され、認証に成功する。
SELECT * FROM user_table WHERE username = 'admin';
-- AND password = 'any_password' ← SQLの仕様上コメント扱いとなる。

また、入力値「"'OR 1=1;--"」も認証に成功する。
つまり、ユーザ名を知らなくても認証をバイパス可能となる脆弱性を埋め込むことになるわけです。

対策として、入力値を適切にエスケープし、シングルクォーテーションやダブルクォーテーションを無効化することが重要となる。

以上です。