LoginSignup
20
27

More than 5 years have passed since last update.

RaspbianにnginxとPHP7.0を入れたいな

Last updated at Posted at 2016-02-14

Raspberry Pi を買いました

GPIOにいろいろつけて遊びたいのですけどその前にWebサーバーにしておきたかったので。

Raspbianのインストール

NOOBS 1.7.0 からインストールしたけど特に設定するような項目もなくすんなりインストール完了。
USBのWiFiアダプタまであっさり動いて拍子抜け。便利な世の中になったもんだ。
やったのは日本語化ぐらい。

nginxのインストール

apt-get install nginx

php7.0のインストール

こちらを参考にしながら。
https://www.symfony.fi/entry/install-php-7-on-raspbian-raspberry-pi
4. Install PHP 7 のところだけ、
apt-get install php7.0 php7.0-dev php7.0-fpm
にした。php7.0-opcache とかインストールしたらApacheとか入ってきてえらい目にあった。
必要になったら後から足せばいいでしょきっと。

nginxのVirtualDomainの設定とか

hoge.local のドメインでアクセスできるような設定にしたいので
/etc/nginx/sites-available/default をコピーして /etc/nginx/sites-available/hoge.local を作成して編集。

/etc/nginx/sites-available/hoge.local
default_serverを消した
-       listen 80 default_server;
-       listen [::]:80 default_server;
+       listen 80;

DocumentRootの変更(あらかじめ作っておいた)
-       root /var/www/html;
+       root /var/www/hoge;

index.phpの追加
-       index index.html index.htm;
+       index index.php index.html;

ServerName設定
-       server_name _;
+       server_name hoge.local;

PHPの設定
-       #location ~ \.php$ {
-       #       include snippets/fastcgi-php.conf;
-       #
-       #       # With php5-cgi alone:
-       #       fastcgi_pass 127.0.0.1:9000;
-       #       # With php5-fpm:
-       #       fastcgi_pass unix:/var/run/php5-fpm.sock;
-       #}
+       location ~ \.php$ {
+              include snippets/fastcgi-php.conf;
+       
+       #       # With php5-cgi alone:
+       #       fastcgi_pass 127.0.0.1:9000;
+       #       # With php5-fpm:
+               fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
+       #       fastcgi_index index.php;
+               fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
+               include       fastcgi_params;
+       }

fastcgi_pass はそれっぽい場所にそれっぽいファイルがあったので指定したら動いた。
fastcgi_index はsnippets/fastcgi-php.confで宣言されてしまっていて、ここを有効にするとduplicateなエラーが出るのでメモ代わりのコメントアウト。

編集完了したらsites-enabledにシンボリックリンクをはる。
はらないと認識してくれない。
ln -s /etc/nginx/sites-available/hoge.local /etc/nginx/sites-enabled/hoge.local

php7の設定とか

/etc/php/7.0/fpm/php-fpm.conf
- ;daemonize = yes
+ daemonize = yes
/etc/php/7.0/fpm/pool.d/www.conf
adduserでnginxユーザーを作っておく必要がある
- user = www-data
- group = www-data
+ user = nginx
+ group = nginx

socketの指定(?)
- listen = /run/php/php7.0-fpm.sock
+ listen = /var/run/php/php7.0-fpm.sock

ownerとgroupとpermission
- listen.owner = www-data
- listen.group = www-data
- ;listen.mode = 0660
+ listen.owner = nginx
+ listen.group = nginx
+ listen.mode = 0666

よそのディストリビューションだとyumしただけでユーザーを作ってくれるらしい。
raspbianでは作ってくれないみたいなのでnologinとかな奴を作る。
listen.modeは本当はちゃんとした設定がありそうなんだけども、デフォルトの0660だといつまでたってもpermissionのエラーになってしまったのでだめもとで666にしたら動いた。
とはいえセキュリティ的によろしくない気がしている(根拠なし)。

再起動

service nginx restart
service php7.0-fpm restart

これで /var/www/hoge に置いた index.php とか動くはず!

動かなかったらごめんなさい。なんか抜けてます。

自動起動の設定とかはあとでやる。

参考にさせていただきました

ありがとうございました。
http://qiita.com/nenokido2000/items/3cbb76dac2b9940f339e
http://qiita.com/monkick@github/items/610d1435d812beb853e2
http://qiita.com/MiyaseTakurou/items/923c28f7ac60b2ce761a
http://qiita.com/yh2020/items/d287712f8a91357ef05b
http://qiita.com/duke-gonorego/items/4c5bfe1042cc0f7c6680
http://qiita.com/onishik/items/810fd840530d4fb6970c
http://qiita.com/koni/items/05158e33b4c8aac07b59

20
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
27