サーバー構築③Let’sEncryptの自動更新とサーバーファイル設定

https接続のために

SSL証明書の確認

SSL証明書をアップロードしたので正しいフォルダに入っているかを確認します。

SSL証明書を/etc/nginx/sslというディレクトリにコピーしたのでここでそこに入っているかを確認します。

bashCopy code

cd /etc/nginx/ssl/ ls -l

これで目的としていたディレクトリにダウンロードしたSSL証明書が保存されていることが確認できました。

~# cd /etc/nginx/ssl/

root@x:/etc/nginx/ssl# ls -l

合計 12

-rw-r–r– 1 root root 1769 11月 20 02:38 letsencrypt.crt

-rw-r–r– 1 root root 1704 11月 20 02:40 letsencrypt.key

-rw-r–r– 1 root root 3749 11月 20 02:39 letsencryptInt.crt

root@x:/etc/nginx/ssl#

WEBサーバーアプリケーションのnginxをここで再起動します。

bashCopy code

sudo service nginx restart

これでhttps接続ができるつもりだったので、ここでこのブログぺーじである”https://www.blog.rikimaru.xyz”が表示できるかブラウザで確認しますが、表示できません。(まだ、サーバーアプリケーションの設定ができていないので表示されるわけもないのですが。)

GPT君のススメでエラーログを確認します。

bashCopy code

sudo cat /var/log/nginx/error.log

2023/11/20 02:31:40 [notice] 21311#21311: signal process started

bashCopy code

sudo cat /var/log/nginx/access.log

(抜粋)

“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36”

どちらもエラーは見つかりませんでした。

ひとまず、次に移ります

Let’sEncryptのSSL証明書を自動更新するように設定する

Let’sEncryptの証明書は有効期限が3ヶ月

Let’sEncryptのSSL証明書は有効期限が3ヶ月しかありません。

手動でファイルを入れ替えて、サーバーの情報を手動で書き換えるということもだめではないのですが、結構手間ですよね。

というか、リマインダーでもしておかないと忘れる。

というか、リマインダーしていてもタイミングで後回しにしたりして、多分忘れる。

おっさんはそんな自信が満々です。

そのため、Let’sEncryptのSSL証明書を自動で更新されるように設定してみます。

crontabに自動更新をお願いする

bashCopy code

sudo crontab -e

エディタが開くので、以下の行を追加します。

bashCopy code

0 4 * * * /usr/bin/certbot renew --quiet

これで毎日4時にcertbotが証明書の更新を試みます。

うまく動いてくれますように。

GPT君の指示ではこれで証明書の取得は終了。

次はサーバーアプリケーションの設定です。

nginx設定

サーバーアプリケーションはapache(アパッチ)とnginx(エンジンエックス)が使われることが多いようです。それぞれ特徴があるようですが、わたしは、何故かGPT君がnginxをインストールするよ。と導いてくれて、当時はこんなことも知らなかったのでなんの疑問も持たずにnginxをインストールしました。

なので、nginxの設定を前提に進めていきます。

SSL証明が取れて、https接続が前提の設定です。

あまりないかとは思いますが、http接続でという方はポートが80になったり、
SSL証明書がいらなかったりしますので、カスタマイズしていただければと思います。

また、わたしはマルチドメイン(複数のページを作る)事を前提にサーバー構築をしているので
ディレクトリの作成の仕方などで少々違いがあります。

sites-availableの設定

/etc/nginx/sites-available/defaultというのがデフォルトのディレクトリとファイル名です。この中にサーバーの設定が書かれていますので、ここを編集します。

nanoエディターでsites-available/defaultを編集する

sudo nano /etc/nginx/sites-available/default

エディターを開いたらおおよそ下のように書かれているので、必用なところを編集します。

おおよそのところは#をつけて無効にする(コメントアウトする)か#をはずして実行データにすれるだけでした。

ドメインや、ディレクトリは内容を見ながら、ご自身の構成に変更する必用があります。

SSL証明書のところはご自身のディレクトリとファイル名に変更下ください。

https接続にする必用がない、もしくはするつもりのない方はそもそも証明書の欄はすべてコメントアウト(#をつける)にしてOKです。



You should look at the following URL's in order to grasp a solid understanding

of Nginx configuration files in order to fully unleash the power of Nginx.

https://www.nginx.com/resources/wiki/start/

https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/

https://wiki.debian.org/Nginx/DirectoryStructure

In most cases, administrators will remove this file from sites-enabled/ and

leave it as reference inside of sites-available where it will continue to be

updated by the nginx packaging team.

This file will automatically load configuration files provided by other

applications, such as Drupal or WordPress. These applications will be made

available underneath a path with that package name, such as /drupal8.

Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.

Default server configuration

server {
#listen 80 default_server;  #http接続の場合は#を外して、コメントアウトを削除
#listen [::]:80 default_server;

    # SSL configuration
    #
    listen 443 ssl default_server;  #httpで接続する場合は#をつけてコメントアウトする
    listen [::]:443 ssl default_server;  #httpで接続する場合は#をつけてコメントアウトする
    #
    # Note: You should disable gzip for SSL traffic.
    # See: <https://bugs.debian.org/773332>
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: <https://bugs.debian.org/765782>
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    server_name blog.rikimaru.xyz ;  #あなたのドメイン

    root /ワードプレスの場合ワードプレスをインストールしているディレクトリ/wordpress; #ルートディレクトリ(最初に読み込むページを設定)

    ssl_certificate /ssl証明書が置かれているディレクトリ/fullchain.pem;
    ssl_certificate_key /ssl証明書が置かれているディレクトリ/ssl証明書のファイル名.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    #ssl_ciphers ECDSA-AES128-GCM-SHA256:ECDHE-RSA-GCM256:ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GMC-SHA384:ECDSA-CHCHa20-POLY1305;EDCHE-RSA-CHACHA20-POLY1305:DHE-RSA-AE128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    #ssl_prefer_server_chiphers on;

    # Add index.php to the list if you are using PHP
    index index.php index.htmlindex.htm index.nginx-debian.html; #読むファイルの拡張子を設定

    #return 301 https://$host$request_uri;

    location / {

try_files $uri $uri/ /index.php$is_args$args;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
}

    # pass PHP scripts to FastCGI server
    #index index.html index.htm index.nginx-debian.html index.php;

    #
    location ~ \\.php$ {
            include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
    #       fastcgi_pass 127.0.0.1:9000;
    }

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

Virtual Host configuration for [example.com](http://example.com/)

nginx.confの設定

同様にnanoエディターで開きます。

sudo nano /etc/nginx/conf

正直どこをどう編集したかわからないですが、おおよそデフォルトだと思います。

ser www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
worker_connections 768;
# multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    types_hash_max_size 2048;
    client_max_body_size 20M;
    # server_tokens off;

    server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
        # Virtual Host Configs
        ##

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

        #server {
        #       listen 80;
        #       server_name blog.rikimaru.xyz;
        #       return 301 https://blog.rikimaru.xyz$request_uri;
        #}

#       server {
#               listen 443 ssl;
#               server_name blog.rikimaru.xyz;
#               return 301 https://$host$request_uri;


#       }
}
#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

さてさて、サーバー構築はもう一息ですよ。

コメントする