見出し画像

MiniPCにLinux(Almalinux)構築WebサーバとデータベースP004

前回の記事P003までで、Alumalinuxのベース環境がインストールできましたので、業務アプリを動作させるための必要なサービスの追加インストールや
環境設定変更をしていきます。
設定作業は、基本は、ssh接続で実施します。ssh接続をteratermツールで実施すれば、ログ保存もでき、後で資料作成などに活用できます。

SELinux:動作モードの設定

Linux環境を構築して動作させるとき、SELinux:動作モードを変更しないと
いろいろな場面でトラブルします。
通常は、SELINUX=disabledにしましょう。
永続的に設定するために、/etc/selinux/configを変数します。
rootユーザで/etc/selinux/configを編集します。

#SELINUX =enforcing
SELINUX=disabled

参考となるサイト情報です。
https://www.server-world.info/query?os=AlmaLinux_9&p=selinux&f=1

編集保存後に、rebootして、
再度、sshで接続し、rootユーザで動作モードを
確認します。

[root@localhost sunsun]# getenforce
Disabled

続いて、OSなどの最新版にアップデード操作です。
# dnf update
実行時の環境条件やインストール状況によって、更新させれるものが変化しますが、初めてのときは、200個以上の更新処理が実行されますので、時間かかります。

以降、Webサーバとしてapache2 データベースとしてMariaDBをインストールします。ネットでalmalinux9.3 apacheなどで検索すると、インストール手順が紹介されていますので、参考にしてみてください。多くの方が情報提供してくれていて、ありがたいです。


以下に記述している操作は、基本rootユーザで実施しています。
まずは、apache2(httpd)のインストール
# dnf -y install httpd

インストール済み:
  almalinux-logos-httpd-90.5.1-1.1.el9.noarch                                   
  apr-1.7.0-12.el9_3.x86_64                                                     
  apr-util-1.6.1-23.el9.x86_64                                                  
  apr-util-bdb-1.6.1-23.el9.x86_64                                              
  apr-util-openssl-1.6.1-23.el9.x86_64                                          
  httpd-2.4.57-5.el9.x86_64                                                     
  httpd-core-2.4.57-5.el9.x86_64                                                
  httpd-filesystem-2.4.57-5.el9.noarch                                          
  httpd-tools-2.4.57-5.el9.x86_64                                               
  mod_http2-1.15.19-5.el9.x86_64                                                
  mod_lua-2.4.57-5.el9.x86_64                                                   

完了しました!

インストールされたapache2の版数を確認します。

#  httpd -v
Server version: Apache/2.4.57 (AlmaLinux)
Server built:   Jul 20 2023 00:00:00

続いて、httpd.confの編集作業ですが、その前にホスト名の変更をしておきます。ホスト名は、自分で決める必要があります。この記事では、webixserver01としました。

#  hostnamectl set-hostname webixserver01.localdomain

httpd.confの編集作業ですが、元のconfigを先にコピーして保存しておきましょう。

cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org

変更内容ですが、元とのdiffで紹介します。上記の参考URLをみていただければ、設定内容を理解できると思います。

[root@webixserver01 conf]# diff httpd.conf  httpd.conf.org
101d100
< ServerName webix_server01.localdomain:80
109c108
<     AllowOverride All
---
>     AllowOverride none
131c130
<     AllowOverride All
---
>     AllowOverride None
150c149
<     Options FollowSymLinks
---
>     Options Indexes FollowSymLinks
157c156
<     AllowOverride All
---
>     AllowOverride None
170c169
<     DirectoryIndex index.html index.php
---
>     DirectoryIndex index.html
360,361d358
< # サーバーの応答ヘッダ
< ServerTokens Prod

次に、ポート開放の設定をしておきます。最終的にはhttpsでのアクセスをしますので。httpとhttpsの開放設定をしておきます。

[root@localhost conf]#  firewall-cmd --add-service=http --permanent
success
[root@localhost conf]#  firewall-cmd --add-service=https --permanent
success
[root@localhost conf]#firewall-cmd --reload
success

あとは、起動とBoot時の自動起動設定です。

[root@localhost conf]# systemctl start httpd
[root@localhost conf]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

 データベース環境構築(MariaDB)
Linuxサーバ上にデータベースを構築する事例を紹介します。構築するデータベースは、MariaDBです。MySQLと互換性が高く、オープンソースで構築も簡単です。ネット上にも多くの記事があります。今回は、以下のサイトを参考に構築しています。

#  dnf -y install mariadb-server

簡単にデータベースのインストールができます。
インストール後に、環境ファイルの編集作業が必要です。
使用する文字コードの定義です。

# vi /etc/my.cnf.d/charset.cnf

# デフォルトの文字コードを設定
# 未設定の場合のデフォルトは [latin1]
# 絵文字等 4 バイト長の文字を扱う場合は [utf8mb4]
[mysqld]
character-set-server = utf8mb4

[client]
default-character-set = utf8mb4

自動起動の設定です。

systemctl enable --now mariadb

ポート開放をします。

[root@webixserver01 my.cnf.d]# firewall-cmd --add-service=mysqlfirewall-cmd --add-service=mysql
success
[root@webixserver01 my.cnf.d]#  firewall-cmd --runtime-to-permanent firewall-cmd --runtime-to-permanent
success

MariaDB の初期設定です。

[root@webixserver01 my.cnf.d]#  mysql_secure_installation mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
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'.

Change the root password? [Y/n] n
 ... skipping.

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
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

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

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... 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!

初期設定が完了したので、一度、rootユーザで接続してみます。

[root@webixserver01 my.cnf.d]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.5.22-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit
Bye

以上でApache2とMariaDBの基本的なインストール作業は完了です。
今後、業務アプリを動かすユーザのアカウント登録やPHPの環境設定なども必要ですが、今回の内容はここまでとします。

この記事が気に入ったらサポートをしてみませんか?