This is IT

技術、日常

【Debian】Debianでnginxをインストールして、htmlの編集をした。

概要

タイトル通り。インストールの仕方とhtml編集方法 インストールは公式サイトをきちんと読んでやりましょう。 GPTで書いてそうな記事みたいのもありますが、公式に勝る説明はありません。基本的に全てコピペで行えるはずです。

❯ nginx -v
nginx version: nginx/1.22.1

最後に

sudo /etc/init.d/nginx start

でスタートするのを忘れずに。管理者権限でないと起動できませんでした。

インストールについて

公式サイト見て、やれるならいろいろな記事検索しないよ!ってことなので、もう少しだけ詳しく書きます。 僕はDebianなので、Debianを選択しますが、ちゃんと自分のOSを間違えずに選んであげましょう。

では、Debianを使って説明をしていきます。基本的には公式を和訳しただけレベルの説明。

# 下準備
sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring

# keyの取得(※\は改行のこと)
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

# 上のコマンドを打ったときの出力例
pub   rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
      573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid                      nginx signing key <signing-key@nginx.com>

# stable nginxのセットアップ(後述のmainline版のどちらかでセットアップする)
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/debian `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

# mainline nginxのセットアップ(stableかmainlineか)
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

# リポジトリのセットアップ
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

stableかmainlineか

上のセットアップで書いたけど、stable版とmainline版があるみたい。 stableは動作確認済みで、mainlineは最新だけどまだバグとか残ってるかも、といった感じらしい。 実稼働はstable版がおすすめとのこと

qiita.com

htmlの編集の仕方

nginxは、何もいじらないとデフォルトのhtmlで設定されています。

ここのhtmlをいじるのが、第一歩だと思いますが、「htmlどこで編集すんねん!!!!」で少々悩んだのでまとめておきます。 バージョンによって多少変わるかもしれませんが、大まかなやり方は一緒なはずです。

StableVersion nginx-1.22.1での環境になります。

1. 設定ファイルの確認

cat /etc/nginx/nginx.conf

で、設定ファイルの中身を出力しましょう。confは基本的に設定ファイルです。nginxに限らず。

❯ cat /etc/nginx/nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/sites-enabled/*;
    include /etc/nginx/conf.d/*.conf;
}

この中の include /etc/nginx/conf.d/*.conf; が注目ポイント。

これは、「/etc/nginx/conf.d配下にある、末尾にconfとついているファイルも設定として使う」という意味になります。 つまりnginxの設定は、1つのファイルだけでなく、他のディレクトリに格納してある設定ファイルも使っている、ということです。

/etc/nginx/conf.d/*.conf;を見に行きましょう。

2. /etc/nginx/conf.d/*.confを見てみる

❯ ls /etc/nginx/conf.d
default.conf

/etc/nginx/conf.dディレクトリ配下には、default.confという設定ファイルがありました。 ではこのファイルの中をさらに見ていきましょう。

3. default.confを見る

❯ cat /etc/nginx/conf.d/default.conf
server {
    listen       80 default_server;
    server_name  localhost;

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

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.html;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

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

#コメントアウトされている箇所なので無視するとして、

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.html;
    }

こんな記述があります。どうやらここら辺が怪しそうです。 特にrootというのは、いわゆるトップページを示すことが多いので、この設定を見るに、 「トップ(root)ページでは、/usr/share/nginx/htmlディレクトリにあるファイルを使っている」 ということでしょう

4. /usr/share/nginx/htmlを見てみる

❯ ls /usr/share/nginx/html
50x.html  index.html

HTMLが2つありますね。どちらを使えばよいでしょうか。

詳細は省きますが、index.htmlが本命のHTMLファイルです。 こちらの中身を変更することで、実際にブラウザの表示も変更することができます。