nginxの導入

ちょっとフライングしてHTTPサーバの設定を行います。今回は、nginxの導入を行います。

CentOSの初期状態ではnginxに関するパッケージは存在しないようです。
試しに"yum"コマンドでnginxのパッケージを確認してみます。


$ yum search nginx
読み込んだプラグイン:fastestmirror
Determining fastest mirrors
 * base: ftp-srv2.kddilabs.jp
 * extras: ftp-srv2.kddilabs.jp
 * updates: ftp-srv2.kddilabs.jp
============================== N/S matched: nginx ==============================
pcp-pmda-nginx.x86_64 : Performance Co-Pilot (PCP) metrics for the Nginx
                      : Webserver

  Name and summary matches only, use "search all" for everything.

レポジトリの追加

nginxの公式サイトを参考にレポジトリファイルを追加します。
今回は、安定しているメインラインのレポジトリを追加します。

まずは事前準備から


$ sudo yum install -y yum-utils

先ほどのページからメインラインのレポジトリを追加する情報をコピーします。


$ sudo vi /etc/yum.repos.d/nginx.repo

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

メインラインのnginxパッケージを使用する場合は、次のコマンドを実行します。


$ sudo yum-config-manager --enable nginx-mainline

いよいよ、nginxの導入です。


$ sudo yum install -y nginx

まずは現在のnginxのステータスを確認します。


$ sudo systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: http://nginx.org/en/docs/

まぁインストールしただけなので、動作していないわけですが...
nginxを起動して、再度ステータスを確認します。


$ sudo systemctl start nginx
$ sudo systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 木 2022-04-07 10:53:06 JST; 4s ago
     Docs: http://nginx.org/en/docs/
  Process: 9001 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 9002 (nginx)
   CGroup: /system.slice/nginx.service
           tq9002 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.c...
           mq9003 nginx: worker process

 4月 07 10:53:06 localhost.localdomain systemd[1]: Starting nginx - high per...
 4月 07 10:53:06 localhost.localdomain systemd[1]: Can't open PID file /var/...
 4月 07 10:53:06 localhost.localdomain systemd[1]: Started nginx - high perf...
Hint: Some lines were ellipsized, use -l to show in full.

is-enabledで自動起動するようになっているか確認し、自動起動を設定します。


$ sudo systemctl is-enabled nginx
disabled
$ sudo systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
$ sudo systemctl is-enabled nginx
enabled

ファイアウォールの設定

CentOSではデフォルトでファイアウォールが有効になっています。
まずは、ファイアウォールのサービスの確認


$ sudo systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 木 2022-04-07 09:56:46 JST; 1h 4min ago
     Docs: man:firewalld(1)
 Main PID: 695 (firewalld)
   CGroup: /system.slice/firewalld.service
           mq695 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

 4月 07 09:56:44 localhost.localdomain systemd[1]: Starting firewalld - dyna...
 4月 07 09:56:46 localhost.localdomain systemd[1]: Started firewalld - dynam...
 4月 07 09:56:46 localhost.localdomain firewalld[695]: WARNING: AllowZoneDri...
Hint: Some lines were ellipsized, use -l to show in full.

今度は、ファイアウォールの設定を確認します。


$ sudo firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: dhcpv6-client ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

まずは、httpを有効にしておきます。


$ sudo firewall-cmd --add-service=http --permanent
success
$ sudo firewall-cmd --reload
success
$ sudo firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: dhcpv6-client http ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

まずは、nginxの起動が確認できました。 nginx_80