忘備録 > サーバー構築6

自宅サーバー構築6:Ubuntu 24.04LTS server

BXNUC10I5FNH.jpg
Linuxによるサーバー構築メモ6

 Intel® NUC キット BOXNUC7CJYH / Ubuntu 20.04LTS の大阪サーバーが OS サポートが切れるので
 Intel® NUC キット BXNUC10I5FNHに 2TB の M.2 SSDを搭載して、Ubuntu 24.04LTS をインストールし後継機とする。

※ 最終更新:2025/06/09 

Ubuntu 24.04 LTS インストール

  1. Ubuntu 24.04 LTSのサーバー版インストールイメージ、「ライブインストーラー版」を使ってインストールする
    モジュール名:ubuntu-24.04.2-live-server-amd64.iso
  2. 「rufus-3.8.exe」を使って USBメモリーに isoイメージを作成(GPT フォーマット)
  3. USB メモリーから起動しインストールする

ストレージ容量拡張

 Ubuntu インストール後、論理パーティションをストレージいっぱいまで拡張する

  1. 2TB のストレージを使っているが、98GBのパーティションしか作られていない
    $ df -h
    Filesystem                         Size  Used Avail Use% Mounted on
    tmpfs                              6.3G  1.6M  6.3G   1% /run
    efivarfs                           192K   66K  122K  36% /sys/firmware/efi/efivars
    /dev/mapper/ubuntu--vg-ubuntu--lv   98G   11G   83G  12% /
    tmpfs                               32G     0   32G   0% /dev/shm
    tmpfs                              5.0M     0  5.0M   0% /run/lock
    /dev/nvme0n1p2                     2.0G  101M  1.7G   6% /boot
    /dev/nvme0n1p1                     1.1G  6.2M  1.1G   1% /boot/efi
    tmpfs                              6.3G   12K  6.3G   1% /run/user/1000
  2. 物理パーティションは 1.9TB あるが、論理パーティションは 100GB
    $ lsblk
    NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
    nvme0n1                   259:0    0  1.9T  0 disk 
    ├─nvme0n1p1               259:1    0    1G  0 part /boot/efi
    ├─nvme0n1p2               259:2    0    2G  0 part /boot
    └─nvme0n1p3               259:3    0  1.9T  0 part 
      └─ubuntu--vg-ubuntu--lv 252:0    0  100G  0 lvm  /
  3. lvextend -l +100%FREE で、最大まで拡張する
    $ sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
      Size of logical volume ubuntu-vg/ubuntu-lv changed from 100.00 GiB (25600 extents) to 1.86 TiB (487597 extents).
      Logical volume ubuntu-vg/ubuntu-lv successfully resized.
  4. まだファイルシステムには反映されていない
    $ df -h
    Filesystem                         Size  Used Avail Use% Mounted on
    tmpfs                              6.3G  1.6M  6.3G   1% /run
    efivarfs                           192K   66K  122K  36% /sys/firmware/efi/efivars
    /dev/mapper/ubuntu--vg-ubuntu--lv   98G   11G   83G  12% /
    tmpfs                               32G     0   32G   0% /dev/shm
    tmpfs                              5.0M     0  5.0M   0% /run/lock
    /dev/nvme0n1p2                     2.0G  101M  1.7G   6% /boot
    /dev/nvme0n1p1                     1.1G  6.2M  1.1G   1% /boot/efi
    tmpfs                              6.3G   12K  6.3G   1% /run/user/1000
  5. resize2fs する
    $ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
    resize2fs 1.47.0 (5-Feb-2023)
    Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
    old_desc_blocks = 13, new_desc_blocks = 239
    The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 499299328 (4k) blocks long.
  6. 拡張された結果
    $ df -h
    Filesystem                         Size  Used Avail Use% Mounted on
    tmpfs                              6.3G  1.6M  6.3G   1% /run
    efivarfs                           192K   66K  122K  36% /sys/firmware/efi/efivars
    /dev/mapper/ubuntu--vg-ubuntu--lv  1.9T   11G  1.8T   1% /
    tmpfs                               32G     0   32G   0% /dev/shm
    tmpfs                              5.0M     0  5.0M   0% /run/lock
    /dev/nvme0n1p2                     2.0G  101M  1.7G   6% /boot
    /dev/nvme0n1p1                     1.1G  6.2M  1.1G   1% /boot/efi
    tmpfs                              6.3G   12K  6.3G   1% /run/user/1000

アップデート

  1. update
    $ sudo apt -y update
    Hit:1 http://jp.archive.ubuntu.com/ubuntu noble InRelease                                                          
    Hit:2 http://jp.archive.ubuntu.com/ubuntu noble-updates InRelease
    Hit:3 http://jp.archive.ubuntu.com/ubuntu noble-backports InRelease
    Hit:4 http://security.ubuntu.com/ubuntu noble-security InRelease
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    69 packages can be upgraded. Run 'apt list --upgradable' to see them.
  2. upgrade
    $ sudo apt -y upgrade
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    Calculating upgrade... Done
        :

net-tools をインストール

  1. ubuntu20.04LTS インストール完了、アップデート後「net-tools」を入れておく。
    $ sudo apt install net-tools
  2. インストールの確認
    mizutu@ubuntu-osaka3:~$ ifconfig
    eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.3.89  netmask 255.255.255.0  broadcast 192.168.3.255
            inet6 2001:a453:6346:100:1e69:7aff:fea1:48f1  prefixlen 64  scopeid 0x0<global>
            inet6 fe80::1e69:7aff:fea1:48f1  prefixlen 64  scopeid 0x20<link>
        :

ufw(ファイアウォール)設定

  1. 現在の状況
    $ sudo ufw status
    Status: inactive
  2. 必要なポートを開く
    $ sudo ufw allow 22
    Rules updated
    Rules updated (v6)
    $ sudo ufw allow 80
    Rules updated
    Rules updated (v6)
    $ sudo ufw allow 443
    Rules updated
    Rules updated (v6)
    $ sudo ufw allow 50022
    Rules updated
    Rules updated (v6)
  3. ファイアウォールを設定する
    $ sudo ufw enable
    Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
    Firewall is active and enabled on system startup
  4. 設定の確認
    $ sudo ufw status
    Status: active
    
    To                         Action      From
    --                         ------      ----
    22                         ALLOW       Anywhere                  
    80                         ALLOW       Anywhere                  
    443                        ALLOW       Anywhere                  
    50022                      ALLOW       Anywhere                  
    22 (v6)                    ALLOW       Anywhere (v6)             
    80 (v6)                    ALLOW       Anywhere (v6)             
    443 (v6)                   ALLOW       Anywhere (v6)             
    50022 (v6)                 ALLOW       Anywhere (v6)

日時設定(timesyncd)

  1. オリジナルの設定を保存
    $ sudo cp /etc/systemd/timesyncd.conf /etc/systemd/timesyncd.conf.org
    $ ls -l /etc/systemd
        :
    -rw-r--r--  1 root root 1003 Oct 17  2024 timesyncd.conf
    -rw-r--r--  1 root root 1003 May 29 00:39 timesyncd.conf.org
        :
  2. NTP を設定する
    $ sudo vi /etc/systemd/timesyncd.conf
        :
    [Time]
    #NTP=
    NTP=ntp.nict.jp 0.jp.pool.ntp.org 1.jp.pool.ntp.org 2.jp.pool.ntp.org 3.jp.pool.ntp.org
        :
  3. 設定と確認
    $ sudo systemctl restart systemd-timesyncd
    $ sudo timedatectl status
                   Local time: Thu 2025-05-29 09:45:50 JST
               Universal time: Thu 2025-05-29 00:45:50 UTC
                     RTC time: Thu 2025-05-29 00:45:50
                    Time zone: Asia/Tokyo (JST, +0900)
    System clock synchronized: yes
                  NTP service: active
              RTC in local TZ: no

SSHサーバー

Ubuntu24.04では、インストール時にサーバーをインストールできる

  1. SSHサーバーが起動しているかの確認
    $ ps -ax | grep ssh
       1520 ?        Ss     0:00 sshd: mizutu [priv]
       1575 ?        S      0:00 sshd: mizutu@pts/0
      16018 ?        Ss     0:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
      16653 pts/0    S+     0:00 grep --color=auto ssh

sambaサーバー

  1. samba インストール
    $ sudo apt install samba
  2. samba 設定
    $ sudo vi /etc/samba/smb.conf
    
    [gloval]
    workgroup = WORKGROUP
    
    [share]
    path = /var/samba/fshare
    read only = no
    browseable = yes
  3. 共有フォルダ作成
    $ sudo mkdir -p /var/samba/fshare
    $ sudo chmod 777 /var/samba/fshare
    $ sudo pdbedit -a ユーザー名
  4. samba 再起動
    $ sudo systemctl restart smbd nmbd
  5. samba 確認
    $ sudo smbstatus -d
    
    Samba version 4.19.5-Ubuntu
    PID     Username     Group        Machine                                   Protocol Version  Encryption           Signing              
    ----------------------------------------------------------------------------------------------------------------------------------------
        :

LAMPサーバ (Linux Apache MySQL PHP)

  1. apache2
    $ sudo apt -y install apache2
  2. apache2
    $ sudo apt -y install php-fpm
    $ sudo apt -y install php-dev
    $ sudo apt -y install php-mysql
    $ sudo apt -y install php-mbstring
    $ sudo apt -y install php-gd
    $ sudo apt -y install php-curl
    $ sudo apt -y install php-intl
    $ sudo apt -y install php-zip
  3. MySQL(8.0.36)
    $ sudo apt -y install mysql-server
  4. インストールの確認
    $ apachectl -v
    Server version: Apache/2.4.58 (Ubuntu)
    Server built:   2025-04-03T14:36:49
    
    $ mysql --version
    mysql  Ver 8.0.42-0ubuntu0.24.04.1 for Linux on x86_64 ((Ubuntu))
    
    $ php --version
    PHP 8.3.6 (cli) (built: Mar 19 2025 10:08:38) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.3.6, Copyright (c) Zend Technologies
        with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies
  5. Webブラウザで実行
     http://サーバーIP/

Webブラウザ設定

Apache2

  1. ログインしているアカウントで作成・変更できるようにパーミッションを変更。
    $ sudo chgrp -R グループ名 /var/www
    $ sudo chmod g+w /var/www
    $ sudo chmod g+w /var/www/html
  2. index.phpの作成
    $ cd /var/www/html
    $ vi index.php
    
    <?php
    phpinfo();
    ?>
  3. index.phpが作成されているか確認
    $ cat index.php
  4. ブラウザで確認
     http://(LinuxのIPアドレス)/index.php

404,401エラーのリダイレクトを可能にする

  1. オリジナル設定ファイルの保存
    $ sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.org
  2. 設定ファイルの変更
    $ sudo vi /etc/apache2/apache2.conf
    
    <Directory />
            Options FollowSymLinks
    #       AllowOverride None
            AllowOverride All
            Require all denied
    </Directory>
       :
    
    <Directory /var/www/>
    #       Options Indexes FollowSymLinks
    #       AllowOverride None
            Options FollowSymLinks
            AllowOverride All
            Require all granted
    </Directory>
  3. Apache2 再起動
    $ sudo /etc/init.d/apache2 restart

PHP8 設定

  1. パッケージをインストール
    $ sudo apt -y install libapache2-mod-php
  2. php.iniを変更
    $ sudo vi /etc/php/7.4/apache2/php.ini
    
    extension_dir = "./"
    extension=mbstring
    
    post_max_size = 32M
    upload_max_filesize = 32M
    allow_url_fopen = Off
    
    [Date]
    ; Defines the default timezone used by the date functions
    ; https://php.net/date.timezone
    date.timezone = "Asia/Tokyo"
  3. aoache2再起動
    $ sudo service apache2 restart
  4. ブラウザで確認
    http://(LinuxのIPアドレス)/index.php

MySQL設定

事前準備

MySQL の設定

  1. MySQLサーバにログイン
    $ sudo mysql -u root
  2. 新しくユーザを作成
    mysql> CREATE USER 'ユーザ名'@'localhost' IDENTIFIED BY 'パスワード';
  3. 作成したユーザに権限を付与 (以前の版からの構文変更に注意)
    mysql> GRANT ALL ON *.* TO 'ユーザ名'@'localhost';
    mysql> FLUSH PRIVILEGES;
  4. DBの文字コードは「UTF8」になっている
    mysql> status
    --------------
    mysql  Ver 8.0.42-0ubuntu0.24.04.1 for Linux on x86_64 ((Ubuntu))
    
    Connection id:          8
    Current database: 
    Current user:           root@localhost
    SSL:                    Not in use
    Current pager:          stdout
    Using outfile:          ''
    Using delimiter:        ;
    Server version:         8.0.42-0ubuntu0.24.04.1 (Ubuntu)
    Protocol version:       10
    Connection:             Localhost via UNIX socket
    Server characterset:    utf8mb4
    Db     characterset:    utf8mb4
    Client characterset:    utf8mb4
    Conn.  characterset:    utf8mb4
    UNIX socket:            /var/run/mysqld/mysqld.sock
    Binary data as:         Hexadecimal
    Uptime:                 34 min 58 sec
    
    Threads: 2  Questions: 8  Slow queries: 0  Opens: 149  Flush tables: 3  Open tables: 68  Queries per second avg: 0.003
    --------------
    
    mysql> select user,host from mysql.user;
    +------------------+-----------+
    | user             | host      |
    +------------------+-----------+
    | debian-sys-maint | localhost |
    | mizutu           | localhost |
    | mysql.infoschema | localhost |
    | mysql.session    | localhost |
    | mysql.sys        | localhost |
    | root             | localhost |
    +------------------+-----------+
    6 rows in set (0.00 sec)
    
    mysql> exit

phpmyadmin (MySQLをWebから管理)

  1. phpmyadmin インストール
    $ sudo apt install phpmyadmin
    Webサーバの種類を聞かれるので「apache2」を選択。
    dbconfig-commonで設定しますか?と聞かれるので「yes」を選択。続いてMySQLサーバ上の「phpmyadmin」ユーザのパスワードを聞かれるので2回入力。
  2. MySQLサーバー上でのphpmyadminユーザーの権限確認
    $ sudo mysql -u root
        :
    mysql> SHOW GRANTS FOR 'phpmyadmin'@'localhost';
    +--------------------------------------------------------------------+
    | Grants for phpmyadmin@localhost                                    |
    +--------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'phpmyadmin'@'localhost'                     |
    | GRANT ALL PRIVILEGES ON `phpmyadmin`.* TO 'phpmyadmin'@'localhost' |
    +--------------------------------------------------------------------+
    2 rows in set (0.00 sec)
  3. phpmyadminユーザーに管理や権限付加
    mysql> GRANT ALL ON *.* TO 'phpmyadmin'@'localhost' WITH GRANT OPTION;
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> SHOW GRANTS FOR 'phpmyadmin'@'localhost';
    +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Grants for phpmyadmin@localhost                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
    +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `phpmyadmin`@`localhost` WITH GRANT OPTION                                                                                                                                                                                                                    |
    | GRANT APPLICATION_PASSWORD_ADMIN,AUDIT_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,CLONE_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,FLUSH_OPTIMIZER_COSTS,FLUSH_STATUS,FLUSH_TABLES,FLUSH_USER_RESOURCES,GROUP_REPLICATION_ADMIN,INNODB_REDO_LOG_ARCHIVE,INNODB_REDO_LOG_ENABLE,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_APPLIER,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SHOW_ROUTINE,SYSTEM_USER,SYSTEM_VARIABLES_ADMIN,TABLE_ENCRYPTION_ADMIN,XA_RECOVER_ADMIN ON *.* TO `phpmyadmin`@`localhost` WITH GRANT OPTION |
    | GRANT ALL PRIVILEGES ON `phpmyadmin`.* TO `phpmyadmin`@`localhost`                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
    +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    3 rows in set (0.00 sec)
  4. Webブラウザで実行
     http://サーバーIP/phpmyadmin
     phpmyadminのIDとパッケージをインストール時に入力したパスワードでログイン。

webmin (Webブラウザでの管理)

PHP ダウングレード(8.3 → 7.4)

  1. 「puliwiki 1.5.2」が PHP8.3 に対応できないようなので、PHP7.4 にダウングレードする
    参考:Ubuntu 20.04にPHPを複数インストールして切り替える方法(PPA・apt)
       PHPのバージョンをダウングレードする [PHP8.0->PHP7.4]
  2. リポジトリを追加する
    $ sudo add-apt-repository ppa:ondrej/php 
    PPA publishes dbgsym, you may need to include 'main/debug' component
    Repository: 'Types: deb
        :
    Press [ENTER] to continue or Ctrl-c to cancel.  ← 'Enter' を押す
        :
    $ sudo apt update
  3. PHPをインストール
    $ sudo apt install php7.4 php7.4-cli php7.4-fpm php7.4-mysql php7.4-xml php7.4-mbstring php7.4-curl php7.4-zip
    ・その他のモジュールを調べる場合
    $ sudo apt search php7.4-*
  4. インストール済みのモジュール確認
    $ ls /usr/bin | grep -i php
    dh_phpcomposer
    dh_phppear
    php
    php7.4
    php8.3
    phpabtpl
    php-config
    php-config8.3
    php-config.default
    phpize
    phpize8.3
    phpize.default
  5. PHP を切り替える
    $ sudo update-alternatives --config php
    There are 2 choices for the alternative php (providing /usr/bin/php).
    
      Selection    Path             Priority   Status
    ------------------------------------------------------------
    * 0            /usr/bin/php8.3   83        auto mode    ← 今のバージョン
      1            /usr/bin/php7.4   74        manual mode  ← 選択するバージョン
      2            /usr/bin/php8.3   83        manual mode
    
    Press <enter> to keep the current choice[*], or type selection number: 1
    update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in manual mode
  6. PHP を有効にするモジュールを入れる
    $ sudo apt -y install libapache2-mod-php7.4
    $ sudo /etc/init.d/apache2 restart
    $ php -v
    PHP 7.4.33 (cli) (built: May  9 2025 06:45:02) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
        with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies
  7. PHP7.4 設定をする
    $ sudo cp /etc/php/7.4/apache2/php.ini /etc/php/7.4/apache2/php.ini.org
    $ sudo vi /etc/php/7.4/apache2/php.ini
    
    extension_dir = "./"
    extension=mbstring
    
    post_max_size = 32M
    upload_max_filesize = 32M
    allow_url_fopen = Off
  8. aoache2再起動
    $ sudo service apache2 restart
  9. ブラウザで確認
    http://(LinuxのIPアドレス)/index.php

コンテンツ移行(pukiwikiサイトの場合)

  1. 旧サーバーでデータのバックアップ
    $ cd /var/www/html
    
    $ sudo tar cvzf aippr_20250529.tar.gz aippr
    $ sudo tar cvzf pukiwiki_20250529.tar.gz pukiwiki
    $ sudo tar cvzf wiki-boubi_20250529.tar.gz wiki-boubi
    $ sudo tar cvzf wiki-diy_20250529.tar.gz wiki-diy
    $ sudo tar cvzf wiki-base_20230624.tar.gz wiki-base
  2. 新サーバーで展開する
    $ cd /var/www/html
    
    $ sudo tar xvzf aippr_20250529.tar.gz
    $ sudo tar xvzf pukiwiki_20250529.tar.gz
    $ sudo tar xvzf wiki-boubi_20250529.tar.gz
    $ sudo tar xvzf wiki-diy_20250529.tar.gz
    $ sudo tar xvzf wiki-base_20230624.tar.gz

「固定IPアドレス」を設定する

 参考:IPアドレスの固定

  1. 現状を知る
    $ ls -l /etc/netplan
    total 4
    -rw------- 1 root root 63 May 29 08:52 50-cloud-init.yaml
    
    $ sudo cat /etc/netplan/50-cloud-init.yaml
    [sudo] password for mizutu: 
    network:
      version: 2
      ethernets:
        eno1:
          dhcp4: true
  2. 既にある設定ファイルを保存し新しいファイルを作成
    $ sudo cp /etc/netplan/50-cloud-init.yaml /etc/netplan/99-manual.yaml
    $ sudo mv /etc/netplan/50-cloud-init.yaml /etc/netplan/50-cloud-init.yaml.org
    $ sudo vi /etc/netplan/99-manual.yaml
    
    network:
      ethernets:
        eno1:
          dhcp4: false
          addresses:
          - 192.168.0.203/24 # IP address
          routes:
          - to: default
            via: 192.168.0.1 # Gateway
          nameservers:
            addresses:
            - 192.168.0.1 # DNS
          dhcp6: false
      version: 2
    ※以下のネットワーク設定の記述例。
     ネットワークインタフェース:eno1
     アドレス:192.168.0.203
     ネットマスク:255.255.255.0(ビット指定だと「/24」)
     ゲートウェイ:192.168.0.1
     DNS:192.168.0.1
    ※yamlファイル記述のインデントに注意すること。TABでなくスペースで数にも要注意

  3. 設定を適応する
    $ sudo netplan apply
  4. IPアドレスの確認
    $ ifconfig
    eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.3.203  netmask 255.255.255.0  broadcast 192.168.3.255
            inet6 2001:a453:6346:100:1e69:7aff:fea1:48f1  prefixlen 64  scopeid 0x0<global>
            inet6 fe80::1e69:7aff:fea1:48f1  prefixlen 64  scopeid 0x20<link>
            ether 1c:69:7a:a1:48:f1  txqueuelen 1000  (Ethernet)
            RX packets 22398  bytes 22918698 (22.9 MB)
            RX errors 0  dropped 4260  overruns 0  frame 0
            TX packets 5892  bytes 487594 (487.5 KB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
            device interrupt 16  memory 0x96300000-96320000  
    
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 1000  (Local Loopback)
            RX packets 118  bytes 11378 (11.3 KB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 118  bytes 11378 (11.3 KB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Let's EncryptのSSL/TSLサーバ証明書の取得と更新設定

let's encrypt 証明書 マシン移設

 参考:サーバー移転(引越し)でLet’s Encrypt 「SSL/TLSサーバ証明書」はどうする?

  1. 旧サーバーで保存(ホームディレクトリに)
    $ sudo tar cvzf letsencrypt.tar.gz /etc/letsencrypt        旧サーバーで保存
    tar: Removing leading `/' from member names
    /etc/letsencrypt/
    /etc/letsencrypt/archive/
    /etc/letsencrypt/archive/izutsu.aa0.netvolante.jp/
    /etc/letsencrypt/archive/izutsu.aa0.netvolante.jp/fullchain6.pem
    /etc/letsencrypt/archive/izutsu.aa0.netvolante.jp/privkey22.pem
        :
  2. 新サーバーに certbot をインストール
    $ sudo apt update
    $ sudo apt install certbot
  3. 新サーバーのルートディレクトリに「letsencrypt.tar.gz」を転送し解凍する
    $ cd /
    $ ls
    bin                boot   dev  home                lib    lib.usr-is-merged  media  opt   root  sbin                snap  swap.img  tmp  var
    bin.usr-is-merged  cdrom  etc  letsencrypt.tar.gz  lib64  lost+found         mnt    proc  run   sbin.usr-is-merged  srv   sys       usr
    
    $ sudo tar xvzf letsencrypt.tar.gz
    etc/letsencrypt/
    etc/letsencrypt/archive/
    etc/letsencrypt/archive/izutsu.aa0.netvolante.jp/
    etc/letsencrypt/archive/izutsu.aa0.netvolante.jp/fullchain6.pem
    etc/letsencrypt/archive/izutsu.aa0.netvolante.jp/privkey22.pem
        :
  4. 証明書のバックアップ
    $ sudo cp -r /etc/letsencrypt/ /etc/letsencrypt_cp/

Let's Encryptサーバ証明書の自動更新

  1. 自動更新のテスト
    $ sudo certbot renew --dry-run
    Saving debug log to /var/log/letsencrypt/letsencrypt.log
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Processing /etc/letsencrypt/renewal/izutsu.aa0.netvolante.jp.conf
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Simulating renewal of an existing certificate for izutsu.aa0.netvolante.jp
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Congratulations, all simulated renewals succeeded: 
      /etc/letsencrypt/live/izutsu.aa0.netvolante.jp/fullchain.pem (success)
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  2. Let's Encryptより発行されるサーバ証明書の有効期間は90日間なので自動更新を設定しておく
    crontab で毎日0:00に更新を実行するように設定
    $ sudo crontab -e
    no crontab for root - using an empty one
    
    Select an editor.  To change later, run 'select-editor'.
      1. /bin/nano        <---- easiest
      2. /usr/bin/vim.basic
      3. /usr/bin/vim.tiny
      4. /bin/ed
    
    Choose 1-4 [1]: 2
    
    エディタを選んで最下行に下記1行追加
    0 0 * * * certbot renew
    :wq で書き込み終了
    
    crontab: installing new crontab
    
    $ sudo service cron restart
    $ sudo ls -l /var/spool/cron/crontabs
    total 4
    -rw------- 1 root crontab 1114 Jun  6 06:00 root
  3. 後日、ログから実行結果を確認しておく
    $ sudo ls -ltr /var/log/letsencrypt
    total 40
    -rw-r--r-- 1 root root 38911 Jun  9 00:00 letsencrypt.log
    
    $ sudo cat /var/log/letsencrypt/letsencrypt.log
        :
    -----END CERTIFICATE-----
    
    2025-06-08 14:34:13,464:DEBUG:acme.client:Storing nonce: z1TVAgOyxItjqqaoijXQc42A2Kyj_QjyU3hVwLZSEjZ_UZ5EXcc
    2025-06-08 14:34:13,468:DEBUG:certbot._internal.renewal:Dry run: skipping updating lineage at /etc/letsencrypt/live/izutsu.aa0.netvolante.jp
    2025-06-08 14:34:13,479:DEBUG:certbot._internal.updater:Skipping updaters in dry-run mode.
    2025-06-08 14:34:13,479:DEBUG:certbot._internal.display.obj:Notifying user: 
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    2025-06-08 14:34:13,480:DEBUG:certbot._internal.display.obj:Notifying user: Congratulations, all simulated renewals succeeded: 
    2025-06-08 14:34:13,480:DEBUG:certbot._internal.display.obj:Notifying user:   /etc/letsencrypt/live/izutsu.aa0.netvolante.jp/fullchain.pem (success)
    2025-06-08 14:34:13,480:DEBUG:certbot._internal.display.obj:Notifying user: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    2025-06-08 14:34:13,480:DEBUG:certbot._internal.renewal:no renewal failures
    2025-06-08 20:04:13,638:DEBUG:certbot._internal.main:certbot version: 2.9.0
    2025-06-08 20:04:13,639:DEBUG:certbot._internal.main:Location of certbot entry point: /usr/bin/certbot
    2025-06-08 20:04:13,639:DEBUG:certbot._internal.main:Arguments: ['-q', '--no-random-sleep-on-renew']
    2025-06-08 20:04:13,639:DEBUG:certbot._internal.main:Discovered plugins: PluginsRegistry(PluginEntryPoint#manual,PluginEntryPoint#null,PluginEntryPoint#standalone,PluginEntryPoint#webroot)
    2025-06-08 20:04:13,644:DEBUG:certbot._internal.log:Root logging level set at 40
    2025-06-08 20:04:13,645:DEBUG:certbot._internal.display.obj:Notifying user: Processing /etc/letsencrypt/renewal/izutsu.aa0.netvolante.jp.conf
    2025-06-08 20:04:13,646:DEBUG:certbot._internal.plugins.selection:Requested authenticator None and installer None
    2025-06-08 20:04:13,664:INFO:certbot.ocsp:Cannot extract OCSP URI from /etc/letsencrypt/archive/izutsu.aa0.netvolante.jp/cert25.pem
    2025-06-08 20:04:13,667:DEBUG:certbot._internal.display.obj:Notifying user: Certificate not yet due for renewal
    2025-06-08 20:04:13,668:DEBUG:certbot._internal.plugins.selection:Requested authenticator webroot and installer None
    2025-06-08 20:04:13,668:DEBUG:certbot._internal.display.obj:Notifying user: 
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    2025-06-08 20:04:13,668:DEBUG:certbot._internal.display.obj:Notifying user: The following certificates are not due for renewal yet:
    2025-06-08 20:04:13,668:DEBUG:certbot._internal.display.obj:Notifying user:   /etc/letsencrypt/live/izutsu.aa0.netvolante.jp/fullchain.pem expires on 2025-08-16 (skipped)
    2025-06-08 20:04:13,668:DEBUG:certbot._internal.display.obj:Notifying user: No renewals were attempted.
    2025-06-08 20:04:13,668:DEBUG:certbot._internal.display.obj:Notifying user: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    2025-06-08 20:04:13,668:DEBUG:certbot._internal.renewal:no renewal failures
    2025-06-09 00:00:01,530:DEBUG:certbot._internal.main:certbot version: 2.9.0
    2025-06-09 00:00:01,531:DEBUG:certbot._internal.main:Location of certbot entry point: /usr/bin/certbot
    2025-06-09 00:00:01,531:DEBUG:certbot._internal.main:Arguments: []
    2025-06-09 00:00:01,531:DEBUG:certbot._internal.main:Discovered plugins: PluginsRegistry(PluginEntryPoint#manual,PluginEntryPoint#null,PluginEntryPoint#standalone,PluginEntryPoint#webroot)
    2025-06-09 00:00:01,535:DEBUG:certbot._internal.log:Root logging level set at 30
    2025-06-09 00:00:01,536:DEBUG:certbot._internal.display.obj:Notifying user: Processing /etc/letsencrypt/renewal/izutsu.aa0.netvolante.jp.conf
    2025-06-09 00:00:01,537:DEBUG:certbot._internal.plugins.selection:Requested authenticator None and installer None
    2025-06-09 00:00:01,551:INFO:certbot.ocsp:Cannot extract OCSP URI from /etc/letsencrypt/archive/izutsu.aa0.netvolante.jp/cert25.pem
    2025-06-09 00:00:01,554:DEBUG:certbot._internal.display.obj:Notifying user: Certificate not yet due for renewal
    2025-06-09 00:00:01,555:DEBUG:certbot._internal.plugins.selection:Requested authenticator webroot and installer None
    2025-06-09 00:00:01,555:DEBUG:certbot._internal.display.obj:Notifying user: 
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    2025-06-09 00:00:01,555:DEBUG:certbot._internal.display.obj:Notifying user: The following certificates are not due for renewal yet:
    2025-06-09 00:00:01,555:DEBUG:certbot._internal.display.obj:Notifying user:   /etc/letsencrypt/live/izutsu.aa0.netvolante.jp/fullchain.pem expires on 2025-08-16 (skipped)
    2025-06-09 00:00:01,555:DEBUG:certbot._internal.display.obj:Notifying user: No renewals were attempted.
    2025-06-09 00:00:01,555:DEBUG:certbot._internal.display.obj:Notifying user: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    2025-06-09 00:00:01,555:DEBUG:certbot._internal.renewal:no renewal failures

サイトSSL化

Apache2 の設定

  1. SSLに関するモジュールを有効化する
    $ sudo a2enmod ssl
  2. 設定ファイルを編集 (以下修正箇所)
    $ sudo vi /etc/apache2/sites-available/default-ssl.conf
    
     (管理者のメールアドレスへ修正)
     ServerAdmin = izutsum@venus.dti.ne.jp
    
     (ルートディレクトリとして公開するディレクトリのパスへ修正)
     DocumentRoot = /var/www/html
    
     (取得したサーバ証明書と公開鍵のパスに変更)
     SSLCertificateFile      /etc/letsencrypt/live/izutsu.aa0.netvolante.jp/cert.pem
    
     (取得した秘密鍵のパスに変更)
     SSLCertificateKeyFile   /etc/letsencrypt/live/izutsu.aa0.netvolante.jp/privkey.pem
    
     (コメント解除して取得した中間証明書のパスに変更)
     SSLCertificateChainFile /etc/letsencrypt/live/izutsu.aa0.netvolante.jp/chain.pem
  3. サイト設定を有効化する
    $ sudo a2ensite default-ssl
  4. Apache2 の再起動
    $ sudo systemctl restart apache2
  5. ブラウザより「https://izutsu.aa0.netvolante.jp」に接続。WEBページが表示されれば完了

HTTP接続に対してHTTPSへのリダイレクトをかける

  1. Apache2のRewriteEngine はオプションなので、有効にしないと動作しない

    ● 現在の状態を確認する
    $ sudo ls /etc/apache2/mods-enabled
    access_compat.load  authn_core.load  authz_user.load  deflate.load  filter.load       mpm_prefork.load  php7.4.load      setenvif.load       status.conf
    alias.conf          authn_file.load  autoindex.conf   dir.conf      mime.conf         negotiation.conf  reqtimeout.conf  socache_shmcb.load  status.load
    alias.load          authz_core.load  autoindex.load   dir.load      mime.load         negotiation.load  reqtimeout.load  ssl.conf
    auth_basic.load     authz_host.load  deflate.conf     env.load      mpm_prefork.conf  php7.4.conf       setenvif.conf    ssl.load
    ● mod_rewrite を有効にする
    $ sudo a2enmod rewrite
    Enabling module rewrite.
    To activate the new configuration, you need to run:
      systemctl restart apache2
    ● Apache2の再起動
    $ sudo systemctl restart apache2
    ● 有効化を確認する(rewrite.load を確認)
    $ sudo ls /etc/apache2/mods-enabled
    access_compat.load  authn_core.load  authz_user.load  deflate.load  filter.load       mpm_prefork.load  php7.4.load      setenvif.conf       ssl.load
    alias.conf          authn_file.load  autoindex.conf   dir.conf      mime.conf         negotiation.conf  reqtimeout.conf  setenvif.load       status.conf
    alias.load          authz_core.load  autoindex.load   dir.load      mime.load         negotiation.load  reqtimeout.load  socache_shmcb.load  status.load
    auth_basic.load     authz_host.load  deflate.conf     env.load      mpm_prefork.conf  php7.4.conf       rewrite.load     ssl.conf
  2. 設定ファイルに下記のように 3行を Virtualhost 内最下部に追記
    $ sudo vi /etc/apache2/sites-available/000-default.conf
    
    <VirtualHost *:80>
        :
        :
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    </VirtualHost>
  3. Apache2の再起動
    $ sudo systemctl restart apache2

(参考)LAN内から自宅公開Webサーバーにアクセス

  1. 「メモ帳」を「管理者として実行」する。
  2. c:\Windows\System32\drivers\etc にある hosts ファイルを開く。
  3. 192.168.3.203 XXXXXX.jp などを追加。
  4. これで、URLでLAN内からURLでアクセスできるようになる。

更新履歴

 

参考資料