03 January 2013

Raspberry PiをWebサーバに(Apache2, MySQL, PHP, Perlのインストール)

Raspberry PiをWebサーバとして使う場合のインストール作業。

■ パッケージのインストール

Apache2 webサーバをインストール。apt-getによれば約5MBytesディスク領域が消費されるそうです。


$ sudo apt-get install apache2

PHP5をインストール。apt-getによれば約16MBytesディスク領域が消費されるそうです。


$ sudo apt-get install php5

データベース MySQL, sqlite をインストール。apt-getによれば約91MBytesディスク領域が消費されるそうです。


$ sudo apt-get install mysql-server
$ sudo apt-get install sqlite3

php5, perl用モジュールをインストール


$ sudo apt-get install php5-mysql php5-sqlite php5-gd
$ sudo apt-get install perlmagick

■ Apacheサービスの自動起動を確認

/etc/inittabに設定されているデフォルトのランレベル”id:2:initdefault:”に対応したinitディレクトリ内にapache2が存在していることを確認


$ ls /etc/rc2.d/
K01lightdm S01ifplugd S02apache2 S03ntp S05rmnologin
K05nfs-common S01motd S03cron S03pulseaudio
K05rpcbind S01rsyslog S03dbus S03ssh
README S01sudo S03dphys-swapfile S05plymouth
S01bootlogs S01triggerhappy S03mysql S05rc.local

■ Apacheの設定

Apache公式マニュアル「Apache コア機能」を参考にしながら設定を確認していく。

apache2.confには特に追加・変更するようなところはない。青に着色した所を念の為確認しておく。

/etc/apache2/apache2.conf

# Global configuration

#ServerRoot "/etc/apache2"
LockFile ${APACHE_LOCK_DIR}/accept.lock
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On ← セッション保持の有効化
MaxKeepAliveRequests 100
KeepAliveTimeout 5
# These need to be set in /etc/apache2/envvars
User ${APACHE_RUN_USER} ← Apache専用のユーザ
Group ${APACHE_RUN_GROUP}

AccessFileName .htaccess
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy all
</Files>

DefaultType None
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn

# Include module configuration:
Include mods-enabled/*.load
Include mods-enabled/*.conf
# Include list of ports to listen on and which to use for name based vhosts
Include ports.conf

# Include generic snippets of statements
Include conf.d/

# Include the virtual host configurations:
Include sites-enabled/ ← 公開するディレクトリやポート、各種設定をこのディレクトリ以下の設定ファイルから読み込む

Apacheが実行されるユーザ名を確認しておく

/etc/apache2/envvars

export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_PID_FILE=/var/run/apache2$SUFFIX.pid
export APACHE_RUN_DIR=/var/run/apache2$SUFFIX

export APACHE_LOG_DIR=/var/log/apache2$SUFFIX

## The locale used by some modules like mod_dav
export LANG=C
## Uncomment the following line to use the system default locale instead:
#. /etc/default/locale
export LANG

apache2.confの”Include sites-enabled/”設定により読み込まれるvirtual host configurations を確認。インストール直後は、ディレクトリ内に000-defaultというファイルが1個あるだけだ。

/etc/apache2/sites-enabled/000-default

<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
# Options Indexes FollowSymLinks MultiViews
↓ Includes (SSI有効), ExecCGI (CGIスクリプト有効), Indexes (指定されたファイルがない場合ファイル一覧を表示), FollowSymLinks (ファイルのシンボリックリンクを有効化)
Options Includes ExecCGI FollowSymLinks ← 変更後
# AllowOverride None

AllowOverride All ← 各ディレクトリの.htaccess機能を全て有効化
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ ← この機能を使わないならコメントアウトする
<Directory "/usr/lib/cgi-bin"> ← この機能を使わないならコメントアウトする
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

apache2.confの”Include conf.d/”設定により読み込まれるvirtual host configurations を確認。インストール直後は、ディレクトリ内にcharsetというファイルが1個あるだけだ。

/etc/apache2/sites-enabled/000-default

#AddDefaultCharset UTF-8 ← 文字コードは文書中でそれぞれ指定するから、コメントアウトのままでよい

デフォルトで検索されるファイル名の設定

/etc/apache2/mods-enabled/dir.conf

<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

cgi, ssiが有効となる拡張子の指定

/etc/apache2/mods-enabled/mime.conf

# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
AddHandler cgi-script .cgi ← この行のコメントアウトを外し有効化する

# Filters allow you to process content before it is sent to the client.
#
# To parse .shtml files for server-side includes (SSI):
# (You will also need to add "Includes" to the "Options" directive.)
#
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml

apache2サービス起動時に" Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName "というエラーが出るので、次のファイルを作成する。

/etc/apache2/conf.d/name

ServerName localhost


■ /var/www ディレクトリのアクセス制限変更

ユーザ pi がftpやsshでファイルアップロード出来るように、 /var/www ディレクトリのアクセス権限を変更する。
(wwwディレクトリのユーザ:グループをwww-dataに変更し、piユーザをwww-dataグループに含める 変更をする)


$ ls -la /var
〜 中略 〜
drwxr-xr-x 2 root root 4096 1月 3 09:45 www/

$ sudo chmod g+w /var/www

$ sudo chown www-data:www-data /var/www

$ ls -la /var
〜 中略 〜
drwxrwxr-x 2 www-data www-data 4096 1月 3 09:45 www/


■ MySQLの設定

デフォルトの文字コードがutf8ではないので、設定し直す。


$ mysql

mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

latin1になっている所を修正するために、/etc/mysql/my.cfgを編集。

/etc/apache2/mods-enabled/mime.conf

[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
skip-character-set-client-handshake ← この行を追加
character-set-server=utf8 ← この行を追加

[mysqldump]
quick
quote-names
max_allowed_packet = 16M
default-character-set=utf8 ← この行を追加

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition
default-character-set=utf8 ← この行を追加

MySQLサービスを再起動( sudo service mysql restart )して文字コードが変更されたか確認しておく。


$ mysql

mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)