Choco Soft

Nadesiko DE RDB

Choco Soft header image 2

Nadesiko3サーバーの作り方その2

2021年06月15日 · コメント(0) · Nadesiko3, Nginx, Server

このページを印刷します。 このページを印刷します。

【20210731修正】
「SAKURA VPS」でOSインストール時にNGINXを指定して入れたら大失敗だったので(LETSENCRYPTを使わなければ良いかも知れませんが。)、単体でCentosStream8を入れました。

まずは既存パッケージのアップデートを実施(ちょっと時間がかかります。)
>yum update

NGINXをYUMで入れます。
>yum install nginx
>systemctl start nginx
>systemctl enable nginx

ブラウザでドメインを入力して、こんな画面が出ればOKです。

nginx

Version 1.1.20が入りました。さて、インストール先はどうかな。。。(ドキドキ

/usr/sbin/nginx(実行ファイル)
/etc/nginx/nginx.conf(設定ファイル)

ワンダフルです!!!!(THEデフォルト配置)

早速設定ファイルを確認します。
>cat /etc/nginx/nginx.conf

ドキュメントルートは「/usr/share/nginx/html」でした。
まさに教科書に載っているかのような配置です。

そしてお手本の様な配置だからこそ出来る必殺のコマンドで「certbot」を入れます。
まずは下準備です。「certbot」を入れる為に「snapd」を導入します。

>yum install epel-release
>yum install snapd
>systemctl enable –now snapd.socket(ここはハイフンが2つ入ってますので注意!)
>ln -s /var/lib/snapd/snap /snap
>snap install –classic certbot(ここはハイフンが2つ入ってますので注意!)
>ln -s /snap/bin/certbot /usr/bin/certbot

時々こんなエラーメッセージが出ます。
too early for operation, device not yet seeded or device model not acknowledged
この場合はしばらく待って、もう一度同じコマンドを実行してください。

ちゃんとインストール完了するとこんな感じに表示されます。
[root@ik1-xxx-xxxxx ~]# snap install –classic certbot(ここはハイフンが2つ入ってますので注意!)
2021-07-31T19:16:31+09:00 INFO Waiting for automatic snapd restart…
Warning: /var/lib/snapd/snap/bin was not found in your $PATH. If you’ve not restarted your
session since you installed snapd, try doing that. Please see
https://forum.snapcraft.io/t/9469 for more details.

注1)ここで一旦、止めてNGINXの設定ファイルにドメインを記載します。
>vi /etc/nginx/nginx.conf

39行目あたりの「SERVER_NAME」です。これがブランクになっているので取得したドメインをここに記載して保存します。

無事入ったら、次はドメインの設定です。(HTTPS設定)
>certbot –nginx(ここはハイフンが2つ入ってますので注意!)

[root@ik1-44-33333 /]# certbot –nginx(ここはハイフンが2つ入ってますので注意!)
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices)
(Enter ‘c’ to cancel): xxxxxxxx@gmail.com
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server. Do you agree?
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
(Y)es/(N)o: Y

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let’s Encrypt project and the non-profit organization that
develops Certbot? We’d like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
(Y)es/(N)o: Y
Account registered.
No names were found in your configuration files. Please enter in your domain
name(s) (comma and/or space separated) (Enter ‘c’ to cancel): nako.tokyo
Requesting a certificate for nako.tokyo
Performing the following challenges:
http-01 challenge for nako.tokyo
Waiting for verification…
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/nginx.conf
Redirecting all traffic on port 80 to ssl in /etc/nginx/nginx.conf

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Could not automatically find a matching server block for nako.tokyo.Set the `server_name` directive to use the Nginx installer

エラーです、エラーです!
「SERVER_NAME」をセットしろと叫んでます。
注1)を実施していると上記のメッセージは表示されません。

でも、実際のインストールには成功してました。

>vi /etc/nginx/nginx.conf

server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;

これは怒られる訳ですね。今回の場合ですがIPアドレスかドメイン名を入れる必要があります。

証明書ファイルは、この辺にあるようです。
– Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/nako.tokyo/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/nako.tokyo/privkey.pem

最終的に動作した設定ファイルを載せておきます。

1 # For more information on configuration, see:
2 # * Official English Documentation: http://nginx.org/en/docs/
3 # * Official Russian Documentation: http://nginx.org/ru/docs/
4
5 user nginx;
6 worker_processes auto;
7 error_log /var/log/nginx/error.log;
8 pid /run/nginx.pid;
9
10 # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
11 include /usr/share/nginx/modules/*.conf;
12
13 events {
14 worker_connections 1024;
15 }
16
17 http {
18 log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
19 ‘$status $body_bytes_sent “$http_referer” ‘
20 ‘”$http_user_agent” “$http_x_forwarded_for”‘;
21
22 access_log /var/log/nginx/access.log main;
23
24 sendfile on;
1 # For more information on configuration, see:
2 # * Official English Documentation: http://nginx.org/en/docs/
3 # * Official Russian Documentation: http://nginx.org/ru/docs/
4
5 user nginx;
6 worker_processes auto;
7 error_log /var/log/nginx/error.log;
8 pid /run/nginx.pid;
9
10 # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
11 include /usr/share/nginx/modules/*.conf;
12
13 events {
14 worker_connections 1024;
15 }
16
17 http {
18 log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
19 ‘$status $body_bytes_sent “$http_referer” ‘
20 ‘”$http_user_agent” “$http_x_forwarded_for”‘;
21
22 access_log /var/log/nginx/access.log main;
23
24 sendfile on;
25 tcp_nopush on;
26 tcp_nodelay on;
27 keepalive_timeout 65;
28 types_hash_max_size 2048;
29
30 include /etc/nginx/mime.types;
31 default_type application/octet-stream;
32
33 # Load modular configuration files from the /etc/nginx/conf.d directory.
34 # See http://nginx.org/en/docs/ngx_core_module.html#include
35 # for more information.
36 include /etc/nginx/conf.d/*.conf;
37
38 server {
39 server_name nako.tokyo;
40 root /usr/share/nginx/html;
41
42 # Load configuration files for the default server block.
43 include /etc/nginx/default.d/*.conf;
44
45 location / {
46 }
47
48 error_page 404 /404.html;
49 location = /40x.html {
50 }
51
52 error_page 500 502 503 504 /50x.html;
53 location = /50x.html {
54 }
55
56 listen [::]:443 ssl ipv6only=on; # managed by Certbot
57 listen 443 ssl; # managed by Certbot
58 ssl_certificate /etc/letsencrypt/live/nako.tokyo/fullchain.pem; # managed by Certbot
59 ssl_certificate_key /etc/letsencrypt/live/nako.tokyo/privkey.pem; # managed by Certbot
60 include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
61 ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
62
63 }
64
65 # Settings for a TLS enabled server.
66 #
67 # server {
68 # listen 443 ssl http2 default_server;
69 # listen [::]:443 ssl http2 default_server;
70 # server_name _;
71 # root /usr/share/nginx/html;
72 #
73 # ssl_certificate “/etc/pki/nginx/server.crt”;
74 # ssl_certificate_key “/etc/pki/nginx/private/server.key”;
75 # ssl_session_cache shared:SSL:1m;
76 # ssl_session_timeout 10m;
77 # ssl_ciphers PROFILE=SYSTEM;
78 # ssl_prefer_server_ciphers on;
79 #
80 # # Load configuration files for the default server block.
81 # include /etc/nginx/default.d/*.conf;
82 #
83 # location / {
84 # }
85 #
86 # error_page 404 /404.html;
87 # location = /40x.html {
88 # }
89 #
90 # error_page 500 502 503 504 /50x.html;
91 # location = /50x.html {
92 # }
93 # }
94
95
96
97 server {
98 if ($host = nako.tokyo) {
99 return 301 https://$host$request_uri;
100 } # managed by Certbot
101
102
103 listen 80 default_server;
104 listen [::]:80 default_server;
105 server_name nako.tokyo;
106 return 404; # managed by Certbot
107
108
109 }}
110

今までの苦労は何だったのだろう。。

LestsEncryptは常に80番で更新をかけますので、必ず80番ポートを開けておいてください。(Firewall,etc)

crontabで設定する時はフルパスを推奨します。
$ crontab -l
PATH=/usr/local/bin:/usr/sbin:/bin:/usr/bin

ここまでやっておきながら、衝撃の事実が判明しました。
Nodejsは、WEBサービスを自前で持っていました。

実際に世界中でプロダクトとして運用されている node.js サーバでは、前段にリバースプロキシとして nginx を置くことがほとんどのようです。(調べてもそんなのばっかりでした。。。)
node.js サーバは 8000 番ポートで起動しておき、nginx は 80 番ポートで動くように設定します。
ユーザーからくる HTTP リクエストはまず nginx で受け、nginx は node.js サーバにHTTPリクエストを渡すだけの役割として使います。
サーバは分ける必要がなく、同一のインスタンスに nginx と node.js アプリを起動しても良いようです。
nginx はアクセスログを出力、GZIP圧縮や、サーバのレスポンスタイムもログ出力可能なのでビジネスにはこれっぽい。

いよいよ、次回から「なでしこ3」の準備に入ります。

タグ : ···

コメント(0)

コメントはまだありません。

コメントする

*