LoginSignup
2
0

【覚え書き】AlmaLinux9.3でのLAMP環境構築手順(Apache+MariaDB+PHP)

Last updated at Posted at 2024-04-23

はじめに

CentOS Linux 7 は、2024 年 6 月 30 日にサポート終了 (EOL) となります。この後継としてAlmalinuxを選ぶことが多くなってきました。そこで現時点の最新のAlmalinux9のインストール後の初期設定とLAMP環境構築手順を、備忘録的に記しておきたいと思います。

構築するLAMP環境

L: AlmaLinux 9.3 (Shamrock Pampas Cat)
A: Apache/2.4.57 (AlmaLinux)
M: mysql Ver 15.1 Distrib 10.5.22-MariaDB
P: PHP 8.3.6

この手順を始める前の前提

  • 物理サーバーもしくは仮想サーバーへは、AlmaLinux9.3 がインストール済みであること
  • インストール時に、言語を「日本語」に設定済みであること
  • インストール時に、ネットワーク設定済みであること
  • インストール時に、NTP設定済みであること

1. dnfまわり

1-1. メタデータキャッシュの作成

dnf -y makecache

#実行結果
AlmaLinux 9 - AppStream  3.5 MB/s |  11 MB     00:03    
AlmaLinux 9 - BaseOS     478 kB/s | 8.1 MB     00:17    
AlmaLinux 9 - Extras      17 kB/s |  17 kB     00:01    

メタデータキャッシュを作成しました。

1-2. Baseパッケージのインストール

dnf -y groupinstall "Base"

1-3. Development Toolsパッケージのインストール

dnf -y groupinstall "Development Tools"

1-4. 自動更新の設定

dnf-automaticパッケージをインストールします

dnf -y install dnf-automatic

/etc/dnf/automatic.confファイルを開き、自動更新の設定を編集します

  • upgrade_typedefaultからsecurity
  • apply_updatesnoからyes
vim /etc/dnf/automatic.conf

upgrade_type = security  #修正します
apply_updates = yes      #修正します

自動更新を有効にします

systemctl enable --now dnf-automatic.timer

2. ユーザーまわり

2-1. ユーザーの新規作成(必要に応じて)

useraddコマンドでユーザを作成します

useradd ユーザ名

2-2. ユーザーのパスワード設定

passwdコマンドでユーザに対するパスワードを設定します

passwd ユーザ名

パスワードを2回入力するよう求められます

2-3. sudoresの編集

visudoコマンドでsudoresに作成したユーザを追記し、sudoコマンドを使えるようにします。

visudo

以下のrootの行の下に追加する

##
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
ユーザ名    ALL=(ALL)       ALL

作成したユーザ名の行を追加します。sudoの細かい権限については必要に応じて調整してください。

3. sshまわり

3-1. sshの最低限の初期設定(ユーザ制限と認証方法の制限)

/etc/ssh/sshd_config を編集して、sshできるユーザの制限と認証方法を制限します

  • ポートを22に指定
  • rootを指定したsshログインを無効化
  • 鍵認証を有効化
  • パスワードなしでのsshログインを無効化
  • パスワード認証を無効化(ユーザ個別にパスワード認証を有効化しますが、全体的には無効化します)
vim /etc/ssh/sshd_config

#主な修正箇所
Port 22                     #コメントアウトを外す
PermitRootLogin no          #noに書き換える
PubkeyAuthentication yes    #コメントアウトを外す
PermitEmptyPasswords no     #コメントアウトを外す
PasswordAuthentication no   #noに書き換える

#追記する内容(最終行の下に追加するもの)
AllowUsers ユーザ名1 ユーザ名2  #sshできるユーザを記載します

#追記する内容(AllowUsersの下に追加するもの)
#パスワード認証をするユーザを記載します
Match User ユーザ名1
        PasswordAuthentication yes
Match User ユーザ名2
        PasswordAuthentication yes

AllowUsersでログインできるユーザを指定することで、それ以外のユーザのログインを無効化します

Match User ユーザ名 でユーザ個別に認証方法やsshの設定を分岐させることができます

3-2. 設定の反映

sshdを再起動して、編集したsshd_configの設定を反映させます

systemctl restart sshd

4. ファイアウォールの無効化

ファイアウォールを無効化させます。ファイアウォールが必要な場合は、個別に設定を投入してください。

systemctl disable --now firewalld

5. SElinuxの無効化

SELinuxを無効化させます。SELinuxが必要な場合は、個別に設定してください。

  • SELINUX=enforcingSELINUX=disabled
vim /etc/selinux/config

#修正箇所
SELINUX=disabled   #disabledに修正します
  • カーネルコマンドラインに selinux=0 を追加するように、ブートローダーを設定します
grubby --update-kernel ALL --args selinux=0
  • 再起動して、SELinuxの設定を反映させます。
shutdown -r now

6. Apacheまわり

6-1. Apacheのインストール

  • Apacheをインストールします。あわせてSSL化もできるようsslのモジュールもあわせてインストールしておきます。
dnf -y install httpd httpd-devel mod_ssl
  • Apacheのインストールバージョンを確認しておきます
httpd -v

# 実行結果 
Server version: Apache/2.4.57 (AlmaLinux)
Server built:   Jul 20 2023 00:00:00
  • Apacheをシステム起動時に自動起動するように設定します
systemctl enable httpd

6-2. Apacheの設定

  • Apacheのwelcomeページを表示させないようにします
mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.org
  • ドキュメントルート/var/www/htmlにアクセスしたときにディレクトリ構造が表示されないようにします
# confファイルを修正するのでバックアップしておきます
cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bk

vim /etc/httpd/conf/httpd.conf

#修正内容
・・・中略

<Directory "/var/www/html">

・・・中略

Options FollowSymLinks #Indexesを削除します
  • Apacheのセキュリティ対策の設定を投入しておきます。
vim /etc/httpd/conf.d/security.conf

#記載内容
ServerTokens Prod
Header unset "X-Powered-By"
RequestHeader unset Proxy
Header append X-Frame-Options SAMEORIGIN
Header set X-XSS-Protection "1; mode=block"
Header set X-Content-Type-Options nosniff
TraceEnable Off
  • mod_deflateの設定を投入しておきます。
vim /etc/httpd/conf.d/deflate.conf

#記載内容
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
DeflateCompressionLevel 1

BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

SetEnvIfNoCase Request_URI\.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI _\.utxt$ no-gzip
</IfModule>
  • 投入した設定の表記に誤りが無いか確認します。mod_sslを導入した場合、ssl.confでエラーがでますが無視して問題ありません。
httpd -t

#実行結果
AH00526: Syntax error on line 85 of /etc/httpd/conf.d/ssl.conf:
SSLCertificateFile: file '/etc/pki/tls/certs/localhost.crt' does not exist or is empty

6-3. Apacheの起動

  • 最後に、Apacheを起動します
systemctl start httpd

7. MariaDBまわり

7-1. MariaDBのインストール

MariaDBをインストールします。

dnf -y install mariadb-server mariadb

MariaDBのインストールバージョンを確認しておきます

mysql --version

# 実行結果 
mysql  Ver 15.1 Distrib 10.5.22-MariaDB, for Linux (aarch64) using  EditLine wrapper

MariaDBをシステム起動時に自動起動するように設定します

systemctl enable mariadb

7-2. MariaDBの文字コード設定

  • 編集前にconfファイルをバックアップしておきます。
cp /etc/my.cnf.d/mariadb-server.cnf /etc/my.cnf.d/mariadb-server.cnf.bk
  • MariaDBで作成するDBやTableの文字コードをutf8mb4に設定します。
vim /etc/my.cnf.d/mariadb-server.cnf

# 追記内容 [mariadb]以下に追記します
character-set-server = utf8mb4
[client-mariadb]
default-character-set = utf8mb4

7-3. MariaDBの起動

Mariadbを起動します。

systemctl start mariadb

7-4. MariaDBの初期設定

MariaDBの初期設定(mysql_secure_installation)をし、rootのパスワードを設定します

mysql_secure_installation

# 実行結果 初期設定の流れ
Enter current password for root (enter for none):  # Enterする
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n # Nキーを押して、Enterする
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] Y         # パスワードを設定するので Y で Enterする
New password:                             # root用のパスワードを入力
Re-enter new password:                    # root用のパスワードを入力(確認)
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y                     # Y で Enterする
 ... Success!

Disallow root login remotely? [Y/n] Y            # Y で Enterする
 ... Success!

Remove test database and access to it? [Y/n] Y   # Y で Enterする
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reload privilege tables now? [Y/n] Y             # Y で Enterする
 ... Success!

 Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

8. PHPまわり

8-1. epelリポジトリ、remiリポジトリの追加

  • PHPを外部リポジトリからインストールします。リポジトリを追加します。
# epelリポジトリの追加
dnf -y install epel-release

# remiリポジトリの追加
dnf -y install https://rpms.remirepo.net/enterprise/remi-release-9.rpm
  • dnfのメタデータキャッシュを再度作成しておきます。
dnf clean all && dnf -y makecache

8-2. インストールするPHPモジュールの指定

  • dnf module list phpで、インストールできるPHPのモジュールリストを確認できます。
dnf module list php

#実行結果
AlmaLinux 9 - AppStream
Name                                     Stream                                      Profiles                                                      Summary                                                  
php                                      8.1                                         common [d], devel, minimal                                    PHP scripting language                                   

Remi's Modular repository for Enterprise Linux 9 - aarch64
Name                                     Stream                                      Profiles                                                      Summary                                                  
php                                      remi-7.4                                    common [d], devel, minimal                                    PHP scripting language                                   
php                                      remi-8.0                                    common [d], devel, minimal                                    PHP scripting language                                   
php                                      remi-8.1                                    common [d], devel, minimal                                    PHP scripting language                                   
php                                      remi-8.2                                    common [d], devel, minimal                                    PHP scripting language                                   
php                                      remi-8.3                                    common [d], devel, minimal                                    PHP scripting language                                   

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled 
  • この手順では外部リポジトリのPHP8.3 remi-8.3 をデフォルトに指定することとします。
dnf -y module reset php && dnf -y module enable php:remi-8.3

8-3. PHPのインストール

  • PHPとPHPモジュールをインストールします
dnf -y install php php-cli php-fpm php-curl php-mysqlnd php-gd php-opcache php-zip php-intl php-common php-bcmath php-imagick php-xmlrpc php-json php-readline php-memcached php-redis php-mbstring php-apcu php-xml php-dom php-redis php-memcached php-memcache php-process
  • PHPのインストールバージョンを確認しておきます
php -v

#実行結果
PHP 8.3.6 (cli) (built: Apr 10 2024 14:21:20) (NTS gcc aarch64)
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
  • php-fpmを自動起動するように設定します
systemctl enable php-fpm

8-4.php.iniの設定

/etc/php.iniを修正し、phpを初期設定します

cp /etc/php.ini /etc/php.ini.bk      # 編集前にバックアップをしておきます

touch /var/log/php_errors.log        # phpphpエラーログファイルを作成しておきます

vim /etc/php.ini

# 編集内容
memory_limit = 2G                     #RAM容量にあわせて調整してください
error_log = /var/log/php_errors.log   #phpエラーログファイルパスを指定する
post_max_size = 300M                  #利用目的にあわせて調整してください
upload_max_filesize = 300M            #利用目的にあわせて調整してください
date.timezone = Asia/Tokyo            #コメントアウトを外して、Asia/Tokyoを指定してください
mbstring.language = Japanese                    #コメントアウトを外してください

8-5.phpの動作確認

ドキュメントルート/var/www/htmlにphpの動作確認用のphpファイルを作成します

echo "<?php phpinfo(); ?>" > /var/www/html/info.php

Apacheとphp-fpmを起動します

systemctl restart httpd && systemctl restart php-fpm

ブラウザを開いて、動作確認用のphpファイルにアクセスします

  • http://サーバのIP/info.php
  • php.iniに設定した内容が表示されていることを確認します

確認が終わったら、動作確認用のphpファイルを削除します

rm -rf /var/www/html/info.php

以上で、AlmaLinux9.3のLAMP環境構築が終わりです

おわりに

検証、開発などの用途でAlmaLinux9をさわってみたいなどのときの参考にしていただけたらと思います。

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