LoginSignup
0
1

More than 3 years have passed since last update.

Raspberry Pi OSにNginx&PHPをインストールしてWebサーバーをたてる

Posted at

制限事項

webサービス用ではなくて、httpプロトコル関係の実験用なので、データベスサーバーのインストールはしていないです。
PHPは要らないかもだけど、一応。
LAN内の独立したサーバーを想定しています。なので、セキュリティとか考えてないです。

OS&HWバージョン確認

$ lsb_release -a #OSのバージョンを確認
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 10 (buster)
Release:    10
Codename:   buster
$ uname -a #カーネルのバージョンを確認
Linux capybara 5.4.79-v7+ #1373 SMP Mon Nov 23 13:22:33 GMT 2020 armv7l GNU/Linux
$ cat /proc/device-tree/model #ハードウェアのモデルを確認
Raspberry Pi 3 Model B Rev 1.2

Nginxのインストール

$ sudo apt update
$ sudo apt install nginx

ホスト名指定で /var/www/html/index.html が表示されるのを確認する。
Screenshot from 2020-12-13 21-03-47.png

Nginxの設定

fastcgi_params

NginxでPHPを動かす為の設定を追加

$ sudo echo 'fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;' | sudo tee -a /etc/nginx/fastcgi_params

tee-a オプション忘れると追記じゃなくて上書きになっちゃうから注意。

$ sudo nginx -t #構文チェック
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

PHPのインストール

$ apt list | grep PHP | grep fpm #インストールできるバージョンの確認
php-fpm/stable,now 2:7.3+69 all
php7.1-fpm/stable 7.1.20-1+b2 armhf
php7.2-fpm/stable 7.2.9-1+b2 armhf
php7.3-fpm/stable,now 7.3.19-1~deb10u1 armhf

上の例だと、php7.1 php7.2 php7.3 のいずれかをインストール可能なことが確認できる。php-fpmを指定すると、自動的に最新のphp7.3がインストールされる。

$ sudo apt-get install php-fpm #PHPをインストール
$ apt list | grep php | grep fpm #確認
php-fpm/stable,now 2:7.3+69 all [インストール済み]
php7.1-fpm/stable 7.1.20-1+b2 armhf
php7.2-fpm/stable 7.2.9-1+b2 armhf
php7.3-fpm/stable,now 7.3.19-1~deb10u1 armhf [インストール済み、自動]

NginxでPHPが走るようにする

sudo vim /etc/nginx/sites-available/default

修正前

# Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

修正後(index.phpを追加)

# Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

修正前

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #    include snippets/fastcgi-php.conf;
    #
    #    # With php-fpm (or other unix sockets):
    #    fastcgi_pass unix:/run/php/php7.3-fpm.sock;
    #    # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    }

修正後(コメントアウトを外す/include fastcgi_params;を追加)

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
    }

最終型

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}

sudo nginx -t #構文テスト

確認用のページ作成(index.php)

sudo vim /var/www/htmlindex.php を作成する。

<?php
phpinfo();

PHPページの表示テスト

$ sudo systemctl restart nginx

ブラウザでホスト名を指定してアクセス。index.html ではなく index.php が表示されたのを確認。
Screenshot from 2020-12-13 22-19-18.png

− 以上 −

0
1
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
0
1