今日のテスト
さくらVPSにAlma Linux9 LAMPをインストール及び設定のメモ(2023/08/20)
→目次

先週、さくらVPSにAlma Linux9 LAMPスクリプトをインストールしました。さくらVPSのスケールは「2Core・1GBメモリ」です。 インストールが成功するまで試行錯誤を多く重ねました。将来再インストールする必要がある場合に備えて、インストール手順をメモしました。
インストールおよび設定の内容は下記の通りです。

・LAMPスタートアップスクリプトでのインストール
・SSH接続
・httpd設定/VirtualHost作成
・Let's EncryptのSSL証明書の導入
・FFmpegインストール
・yt-dlpインストール



1.OSインストール
1.1 事前準備
端末のPCクライアント側のWindows11で、sshキー(秘密鍵・公開鍵)ファイルを作成します。

id_rsa ファイル
id_rsa.pub ファイル

id_rsa.pubをさくらVPSコントロールパネルの【SSHキー管理】にて登録しておきます。

1.2 OS再インストール
VPSコントロールパネルの【OS再インストール】クリックします。オプションは下記ように選択します。
◎スタートアップスクリプト: LAMP
◎登録済みの公開鍵を使ってインストールする
◎SSHを利用したログインの際に、パスワードを利用しない
VPSコントロールパネルの【内容確認】クリックして【OS再インストール】をクリックします。
「完了しました」が表示されたら、各種設定を始めます。

1.3 OS再インストール結果の確認
almaログインして、Linux/Apache/Mysql/Phpのバージョンを確認します。
VPSコントロールパネルの【シリアルコンソール】で、almaログインします。
tk2-nnn-nnnnn login: alma
Password: xxxxxxxx
SAKURA internet [Virtual Private Server SERVICE]
[alma@tk2-nnn-nnnnn ~]$ 
◆スーパーユーザー(rootユーザー)」の権限が必要なコマンドを打ちやすいために、rootにパスワードを設定します。
[alma@tk2-nnn-nnnnn ~]$ sudo passwd root
Changing password for user root.
New password: xxxxxxxx
Retype new password: xxxxxxxx
passwd: all authentication tokens updated successfully.
[alma@tk2-nnn-nnnnn ~]$ 
◆rootに切り替えてみます。
[alma@tk2-nnn-nnnnn ~]$ su
Password: xxxxxxxx
[root@tk2-nnn-nnnnn alma]# 
◆Linuxディストリビューションの種類やバージョンを確認します。
[root@tk2-nnn-nnnnn etc]# cat /etc/redhat-release
AlmaLinux release 9.2 (Turquoise Kodkod)
[root@tk2-nnn-nnnnn etc]# 
◆Apacheバージョン確認
[alma@tk2-nnn-nnnnn ~]$ httpd -v
Server version: Apache/2.4.53 (AlmaLinux)
Server built:   Apr 28 2023 00:00:00
[alma@tk2-nnn-nnnnn ~]$ 
◆mysqlバージョン確認
[alma@tk2-124-62201 ~]$ mysql --version
mysql  Ver 15.1 Distrib 10.5.16-MariaDB, for Linux (x86_64) using  EditLine wrapper
[alma@tk2-nnn-nnnnn ~]$
◆phpバージョン確認
[alma@tk2-124-62201 ~]$ php -v
PHP 8.0.27 (cli) (built: Jan  3 2023 16:17:26) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.27, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.27, Copyright (c), by Zend Technologies
[alma@tk2-nnn-nnnnn ~]$ 


2.SSH接続
2.1 /home/alma/.ssh/authorized_keysファイルが存在するかを確認します。
[alma@tk2-nnn-nnnnn ~]$ cd ~
[alma@tk2-nnn-nnnnn ~]$ ls .ssh
authorized_keys
[alma@tk2-nnn-nnnnn ~]$ 
authorized_keysの中身は事前に登録した「id_rsa.pub ファイル」であるはずです。
2.2 Windows11のコンソールでSSH接続
>ssh -o StrictHostKeyChecking=no alma@nnn.nnn.nnn.nnn -i C:\Users\user1\Documents\id_rsa
或いは
>ssh -o StrictHostKeyChecking=no -l alma nnn.nnn.nnn.nnn -i C:\Users\user1\Documents\id_rsa

SAKURA internet [Virtual Private Server SERVICE]
Last login: Thu Aug 10 22:29:33 2023 from 122.103.nnn.nn
[alma@tk2-nnn-nnnnn ~]$

2.3 scp コマンド実行してみます。
>scp -o StrictHostKeyChecking=no -i C:\Users\user1\Documents\id_rsa 1.txt alma@nnn.nnn.nnn.nnn:~/1.txt
このコマンドを実行すると、Windows11側の1.txtをVPSの/home/alma/の直下にコピーします。
[alma@tk2-nnn-nnnnn ~]$ ll
total 4
-rw-r--r-- 1 alma alma 8 Aug 19 12:52 1.txt
[alma@tk2-nnn-nnnnn ~]$ cat 1.txt
hello 
[alma@tk2-nnn-nnnnn ~]$

2.4 ファイル転送ツールWinSCPから接続
WinSCPのバージョンは5.21です。
【転送プロトコル】:scp
【ホスト名】:nnn.nnn.nnn.nnn
【ポート番号】:22
【設定】→【設定】→【SSH-認証】→【秘密鍵】:id_rsa ファイルを指定
【OK】→【ログイン】→警告ダイアログ【更新】
【ユーザ名】←「alma」
【OK】


3.httpd設定
3.1 準備
事前にtst1.kazetest.com、tst2.kazetest.com(例)を用意しておきます。テスト用のhtmlファイルやphpファイル、及びphpが入っているhtmlファイルを /var/www/htmlの下にコピーします。
chmod -R 777 /var/www/html
...コピー...
chmod -R 777 /var/www/html
chown -R apache:apache /var/www/html
3.2 VirtualHost作成
#vi /etc/httpd/conf.d/x.conf
<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName tst1.kazetest.com
    AddDefaultCharset UTF-8
    <Directory "/var/www/html">
      DirectoryIndex index.html index.php
    </Directory>
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot /var/www/html/tst2
    ServerName tst2.kazetest.com
    AddDefaultCharset UTF-8
    <Directory "/var/www/html/tst2">
      DirectoryIndex index.html index.php
    </Directory>
</VirtualHost>
3.3 confファイルの編集
◆php文を埋め込むhtmlファイルとhtmファイルのため
cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.org
vi /etc/php-fpm.d/www.conf
;security.limit_extensions = .php .php3 .php4 .php5 .php7
	下記のように コメントアウトを解除し、末尾に .html .htmlを追加する
security.limit_extensions = .php .php3 .php4 .php5 .php7 .html .htm
◆php.confファイルも編集します。
cp /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.org
vi /etc/httpd/conf.d/php.conf
22行目の近くは、下記の記述です。
<IfModule !mod_php.c>
    # Enable http authorization headers
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1

    <FilesMatch \.(php|phar)$>
        SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
    </FilesMatch>
</IfModule>

FilesMatchの拡張子にhtmlとhtmを追加して、下記のようにします。

<IfModule !mod_php.c>
    # Enable http authorization headers
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1

    <FilesMatch \.(html|htm|php|phar)$>
        SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
    </FilesMatch>
</IfModule>
◆ディレクトリ一覧を表示させないようにする
cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
vi /etc/httpd/conf/httpd.conf

149行目の近く
Options Indexes FollowSymLinks
をコメントアウトする
#Options Indexes FollowSymLinks
◆welcome.confファイルを削除
mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.org
◆httpdリスタート
systemctl reload httpd php-fpm
systemctl restart httpd
◆ Windows ChromeでHPの確認
http://tst1.kazetest.com
http://tst2.kazetest.com


4.CertbotでLet's EncryptのSSL証明書の導入
4.1 Certbotインストール
dnf install epel-release mod_ssl -y
dnf install certbot python3-certbot-apache -y
certbot certonly --webroot -w /var/www/html -m aaaa@kazetest.com -d tst1.kazetest.com -n --agree-tos
certbot certonly --webroot -w /var/www/html/tst2 -m aaaa@kazetest.com -d tst2.kazetest.com -n --agree-tos
certbot --apache certificates
◆httpdリスタート
systemctl restart httpd
◆SSL証明書の保存場所の確認
ls /etc/letsencrypt/live
[root@tk2-nnn-nnnnn conf.d]# ls /etc/letsencrypt/live
tst1.kazetest.com  README  tst2.kazetest.com
[root@tk2-nnn-nnnnn conf.d]#
4.2 confファイル設定
◆/etc/httpd/conf.d/ssl.confファイルの
40 <VirtualHost *:443>
...
...
202 </VirtualHost>
の部分を削除します。
cp /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/ssl.conf.org
vi /etc/httpd/conf.d/ssl.conf
4.3 VirtualHost作成
◆x.ssl.conf ファイルを下記の内容で作成します。
vi /etc/httpd/conf.d/x.ssl.conf

--------------------------------------------------------------------------------------------
<VirtualHost *:443>
  DocumentRoot "/var/www/html"
  ServerName tst1.kazetest.com
  AddDefaultCharset UTF-8
  <Directory "/var/www/html">
     DirectoryIndex index.html index.php
  </Directory>
  SSLEngine on
  SSLHonorCipherOrder on
  SSLCipherSuite PROFILE=SYSTEM
  SSLProxyCipherSuite PROFILE=SYSTEM
  SSLCertificateFile /etc/letsencrypt/live/tst1.kazetest.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/tst1.kazetest.com/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/tst1.kazetest.com/chain.pem

  <FilesMatch "\.(cgi|shtml|phtml|php|html|htm)$">
    SSLOptions +StdEnvVars
  </FilesMatch>
  <Directory "/var/www/cgi-bin">
    SSLOptions +StdEnvVars
  </Directory>

  BrowserMatch "MSIE [2-5]" \
         nokeepalive ssl-unclean-shutdown \
         downgrade-1.0 force-response-1.0
</VirtualHost>

<VirtualHost *:443>
  DocumentRoot "/var/www/html/tst2"
  ServerName tst2.kazetest.com
  AddDefaultCharset UTF-8
  <Directory "/var/www/html/tst2">
     DirectoryIndex index.html index.php
  </Directory>
  SSLEngine on
  SSLHonorCipherOrder on
  SSLCipherSuite PROFILE=SYSTEM
  SSLProxyCipherSuite PROFILE=SYSTEM
  SSLCertificateFile /etc/letsencrypt/live/tst2.kazetest.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/tst2.kazetest.com/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/tst2.kazetest.com/chain.pem

  <FilesMatch "\.(cgi|shtml|phtml|php|html|htm)$">
    SSLOptions +StdEnvVars
  </FilesMatch>
  <Directory "/var/www/cgi-bin">
    SSLOptions +StdEnvVars
  </Directory>

  BrowserMatch "MSIE [2-5]" \
         nokeepalive ssl-unclean-shutdown \
         downgrade-1.0 force-response-1.0
</VirtualHost>

-----------------------------------------------------------------------------------------------
設定は以上です。
◆httpdリスタート
systemctl restart httpd
◆ChromeでsslのHP確認
https://tst1.kazetest.com
https://tst2.kazetest.com
4.4 月ごとにSSL証明書更新
◆cron.d設定ファイル作成
vi /etc/cron.d/dailyjobs

----------------------------------------------------------------
# Run the daily, weekly, and monthly jobs if cronie-anacron is not installed
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# run-parts
01 01 1 * * root certbot renew --preferred-challenges http && systemctl restart httpd
01 01 1 * * root date >> /home/alma/month.txt
01 * * * * root date >> /home/alma/date.txt
-----------------------------------------------------------------
◆crondリスタート
systemctl restart crond
(1時間後、 /home/alma/date.txtを確認)

5.FFmpegインストール
5.1 RPM Fusion Repository追加します。
◆まずは、EPEL repository と PowerTools(CRB)をイネーブルします。
[root@tk2-nnn-nnnnn etc]# dnf install epel-release
[root@tk2-nnn-nnnnn etc]# dnf config-manager --set-enabled crb
◆次は、RPM Fusion repo をインストールします。
[root@tk2-nnn-nnnnn etc]# dnf install --nogpgcheck https://mirrors.rpmfusion.org/free/el/rpmfusion-free-release-$(rpm -E %rhel).noarch.rpm -y
[root@tk2-nnn-nnnnn etc]# dnf install --nogpgcheck https://mirrors.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-$(rpm -E %rhel).noarch.rpm -y
5.2 FFmpegをインストールします
[root@tk2-nnn-nnnnn etc]# dnf install ffmpeg ffmpeg-devel
◆ffmpegバージョン確認
[alma@tk2-nnn-nnnnn ~]$ ffmpeg -version
ffmpeg version 5.1.3 Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 11 (GCC)


6.yt-dlpインストール
◆python3のバージョン確認
[alma@tk2-124-62201 ~]$ python3 -V
Python 3.9.16
[alma@tk2-124-62201 ~]$
◆PIPをインストール
[root@tk2-nnn-nnnnn etc]# dnf install python3-pip
◆pipのバージョン確認
[root@tk2-nnn-nnnnn etc]# pip --version
pip 21.2.3 from /usr/lib/python3.9/site-packages/pip (python 3.9)
◆yt-dlpインストール
[root@tk2-nnn-nnnnn etc]# python3 -m pip install -U yt-dlp
◆yt-dlpのバージョン確認
[alma@tk2-nnn-nnnnn ~]$ yt-dlp --version
2023.07.06