0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Raspberry PiにNginxをインストールする

Last updated at Posted at 2025-12-16

本記事は、めんどい太郎の Advent Calendar 2025 17日目の記事です。

はじめに

今回はRaspberry PiにNginxをインストールします。

環境

  • Raspberry Pi 4 Model B (8GB)
  • OS: Raspberry Pi OS Lite (64-bit)

Nginxのインストール

といっても公式ドキュメント通りにやっていくだけです。

まずは必要なパッケージのインストールをします。

sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring

次はNginxの公開鍵を登録します。

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

次は公開鍵の確認、と行きたいところでしたが...

$ gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
gpg: Fatal: /home/tarotaro/.gnupg: directory does not exist!
pub   rsa4096 2024-05-29 [SC]

なんか表示されない。

まぁいっか(よくない)

お次はリポジトリの登録をします。

今回はstableを登録します。

echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
https://nginx.org/packages/debian `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

最後に、Nginx公式が提供しているリポジトリを優先します。

echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
    | sudo tee /etc/apt/preferences.d/99nginx

これで準備完了です。

最後にインストールします。

sudo apt update
sudo apt install nginx

これでインストールは完了です。

ついでに.htaccessにアクセスできないようにする設定も有効化しておきます。

Nginxの設定ファイルは/etc/nginx/conf.d/にあります。

最後の方にこんなのがあると思いますので、下のコメントアウトを外します。

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

これで.htaccessにアクセスできなくなります。

本当はPHPのインストールまでやりたかったのですが、なぜかうまくいきませんでした。

前にやったことあるのでできるはずですが...どうやってやったっけな...

Nginxでルーティング

今回はあるドメインに対してきた通信を別ポートに振り分ける設定を紹介します。

(書くことなかった)

test.conf
server {
    listen       80;
    server_name  tarou.com;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        proxy_pass      http://localhost:5000;
    }
}

これで、tarou.comに足しての通信をlocalhost:500に振り分けることができます。

終わりに

クソ記事ですまんかった

PHPも動かせるようにしたかったのですが、なんかうまくいかなかったのでこんな感じになりました。

本当に申し訳ないです....

私はApacheよりもNginx派なので、今回はRaspberry PiにNginxを入れてみました。

皆さんもぜひNginxを使ってみてください。

それでは!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?