LoginSignup
2
0

More than 3 years have passed since last update.

CentOS8にNginx+SSL化してdaemonにリバースプロキシで動かす

Last updated at Posted at 2020-12-02

ひさしぶりに素のCentOS8.2にnginxで環境設定する機会があったので流れまとめました。
やりたいことは、デーモンソフトウェアを起動しそのポートにnginxでリバースプロキシ(ssl)でアクセスするというものです。

Nginx

素のOSなのでWebサーバーがありません。
Webサーバーはnginxを使いたかったので、まずはnginxをインストールします。

Nginxリポジトリを設定する

nginxのリポジトリを指定します。

vi /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

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

リポジトリパッケージの確認とインストール

リポジトリの情報に間違いがないか確認してください。

$ yum info nginx

問題なければインストールします。

$ yum -y install nginx

インストールできたらバージョンを確認してみましょう。

$ nginx -v
nginx version: nginx/1.18.0

Nginxのサービスを自動化

サーバーが起動したら自動的にnginxを起動するように設定しておきます。
(設定していないと、サーバーが終了する度に再度nginxを起動させなくてはならなくなります)

$ systemctl enable nginx

いよいよnginxを起動します。

$ systemctl start nginx

nginx起動後、ドメインやIPアドレスを叩いてブラウザに「Welcome to nginx!」が表示されれば成功です。
ちなみにデフォルトのnginxのドキュメントルートは以下のとおりです。

/usr/share/nginx/html/index.html

もしうまく動かなったら以下を参考に。

nginx に繋がらない場合の確認方法・調査方法とコマンド解説

【Nginx】portが占領されてサーバーが起動できないときの対処法

ポートを握っているプロセスを見たい時

ファイアーウォール

firewalld

CentOS7から導入された管理コマンド「firewall-cmd」を使っていきます。
まずは現状のステータスを確認しましょう。

$ firewall-cmd --state

ステータスがrunningになっていなければ、ファイアウォールを起動します。
ついでに自動起動も設定しておきます。

$ systemctl enable firewalld
$ systemctl status firewalld

もしすでにmaskされていた場合でファイアーウォールが起動しないケースでは、unmaskしたあとに再度起動してみてください。

$ systemctl unmask firewalld 
$ systemctl status firewalld

デフォルトゾーンの確認

firewalldにはゾーンという考え方があります。
いくつかのルールをまとめた雛形が予め用意されているのですが、デフォルトで割り当てられているゾーンを確認しましょう。

$ firewall-cmd --get-default-zone
public

デフォルトでは「public」ゾーンが割当られています。
servicesをみるとsshなどが設定されているので、sshのポートがあいていることなります。

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

ポートの追加

http(80)やhttps(443)も開通しておきましょう。
publicゾーンに以下を追加します。

$ firewall-cmd --add-service=http --zone=public --permanent
$ firewall-cmd --add-service=https --zone=public --permanent

設定変更したあとは、リロードしておきます。

$ firewall-cmd --reload

その他、連続したポートを開けたい場合の例です。

$ firewall-cmd --add-port=100-1000/udp --zone=public --permanent

デーモンソフトウェアのインストールと起動

今回は、あるデーモンソフトウェアを使いました。
デーモンソフトウェアのインストールは以下で行いました。

$ cd /usr/local/bin

今回のデーモンソフトウェアは5000番ポートで起動するようになっています。
後にnginxの設定で5000番ポートにリバースプロキシすることで、nginxを前段においてデーモンプロセスにアクセスすることができます。

ユニットファイルを作れば、ソフトウエアのOSの起動合わせて自動起動(systemctl enable設定)などができるようになりますのでやってみてください。
https://tex2e.github.io/blog/linux/create-my-systemd-service

SSL化

DNSを割り当てたあと、ssl化したい場合の手順です。
snapインストール > certbotインストール&設定 > nginxの設定ファイルの更新の流れで設定します。

snapのインストール

certbotインストール用のツールを以下の手順にしたがってインストールします。

certbotのインストール

snapを使ってcertbotをインストールしてください。
証明書がインストールされます。

nginxの設定

nginxの設定ファイルを修正します。

設定方法はこちら。
https://qiita.com/morrr/items/7c97f0d2e46f7a8ec967

$ vim /etc/nginx/conf.d/default.conf

デーモンソフトウェアは5000番で動いているので、https://test.example.comに対して5000番ポートにリバースプロキシしてやることで、nginx経由でデーモンソフトウェアを動かすことができます。

server {

        listen 443 ssl default_server;

        index index.html index.htm;

        server_name test.example.com;

        location / {
                proxy_pass http://127.0.0.1:5000;
        }
}

confファイルの確認

$ nginx -t

エラーが起きてなければnginxを再起動します。

$ systemctl restart nginx

ドメインにアクセスしてデーモンソフトウェアが処理されることを確認します。

番外編(selinuxの無効化)

selinuxが有効になっていると、サービスが動作しない原因になることがあるので、基本的には無効化しておきます。

$ getenforce
Enforcing
vim /etc/selinux/config
config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

disabledに設定することでoffにできます。

SELINUX=disabled

サーバーの再起動。

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