ネットで nginx をラズパイを動かすための方法を探すといろいろヒットしますが、我が家の Model B+ にインストールされている、最新版の Raspbian に対応している日本語の記事が見つからなかったのでメモして置きます。
- ディストリビューションに関する情報は次の通り。
- まず、パッケージリストを最新にする
- ラズベリーパイ財団のホームページにある記事を見ながらインストール
- PHP も使いたかったので、php5-fpm というパッケージもインストール
- pi ユーザのホームディレクトリ下に public_html を置き、Apache の UserDir と同じように使う目的のフォルダを作成。
- PHP の動作確認のためのテストスクリプト作成
- Nginx で PHP を使えるようにするため、設定ファイルを編集。事前にオリジナルのバックアップを取得。
- 設定ファイルを次のように変更
- 設定ファイルを保存して、Nginx を再起動
- epiphany-browser で http://localhost/~pi/ をリクエスト。下記が表示されればOK。
pi@raspberrypi ~ $ uname -a Linux raspberrypi 3.12.28+ #709 PREEMPT Mon Sep 8 15:28:00 BST 2014 armv6l GNU/Linux pi@raspberrypi ~ $ ls /etc/*release /etc/os-release pi@raspberrypi ~ $ cat /etc/*release PRETTY_NAME="Raspbian GNU/Linux 7 (wheezy)" NAME="Raspbian GNU/Linux" VERSION_ID="7" VERSION="7 (wheezy)" ID=raspbian ID_LIKE=debian ANSI_COLOR="1;31" HOME_URL="http://www.raspbian.org/" SUPPORT_URL="http://www.raspbian.org/RaspbianForums" BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs" pi@raspberrypi ~ $
sudo apt-get update sudo apt-get upgrade
sudo apt-get install nginx
sudo apt-get install php5-fpm
mkdir ~/public_html
echo '<? echo phpinfo(); ?>' > ~/public_html/index.php
cd /etc/nginx/sites-available sudo cp default default.org
pi@raspberrypi /etc/nginx/sites-available $ diff -u default.org default
--- default.org 2014-11-23 10:27:38.034350896 +0900
+++ default 2014-11-23 10:28:42.393470847 +0900
@@ -22,7 +22,7 @@
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
root /usr/share/nginx/www;
- index index.html index.htm;
+ index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
@@ -77,6 +77,22 @@
#location ~ /\.ht {
# deny all;
#}
+ # Userdir - php
+ location ~ ^/~([^/]+)/(.+\.php)$ {
+ if (!-f /home/$1/public_html/$2) {
+ rewrite ^ 404;
+ }
+ alias /home/$1/public_html/$2;
+ fastcgi_pass unix:/var/run/php5-fpm.sock;
+ fastcgi_index index.php;
+ fastcgi_param SCRIPT_FILENAME $request_filename;
+ include fastcgi_params;
+ }
+ # Userdir - static
+ location ~ ^/~([^/]+)(/.*)?$ {
+ alias /home/$1/public_html$2;
+ autoindex on;
+ }
}
sudo service nginx restart
