Project

General

Profile

サーバーインストール2022 » History » Version 1

Kiyotaka NEMOTO, 05/13/2022 03:04 PM
ARCHESno

1 1 Kiyotaka NEMOTO
h1. サーバーインストール2022
2
3
まだ数例インストールした記録。今後記述を検証していく。
4
5
h2. ホスティングサービス
6
7
次のホスティングサービスを想定
8
* GMOクラウドALTUS(Plesk不使用) - AlmaLinux 8.5
9
* AWS EC2 - Amazon Linux 2
10
11
予想される管理操作に応じてホスティングサービスでアクセス制限をかける。
12
☆接続元IPアドレス
13
(正)	:183.77.171.12(会社)
14
(副)	:153.122.45.46(新ARCHESサーバ)
15
(予備)	:153.122.73.247(ARCHES ALTUS2)
16
17
18
h2. 初期設定
19
20
sudo yum -y update
21
22
操作記録を容易にするため、プロンプトに時刻を付けておく
23
vi ~/.bashrc
24
<pre>
25
export PS1='\[\e[1;36m\]\d \[\e[1;32m\]\t \[\e[1;33m\]\u@\[\e[1;35m\]\h:\w\$\[\e[m\] ' (←末尾に追加)
26
</pre>source ~/.bashrc
27
28
h2. スワップファイル作成
29
30
top
31
Swapが0ならまだ作成されていない
32
GMOは自動作成、EC2は未作成?
33
https://aws.amazon.com/jp/premiumsupport/knowledge-center/ec2-memory-swap-file/
34
35
ストレージにもよるが、メモリが大きければ同サイズ、少なければ倍
36
sudo dd if=/dev/zero of=/swapfile bs=128M count=128 (←16GBの場合)
37
sudo chmod 600 /swapfile
38
sudo mkswap /swapfile
39
sudo swapon /swapfile
40
sudo swapon -s
41
sudo vi /etc/fstab
42
cat /etc/fstab
43
<pre>
44
UUID=008cdb03-aa96-41b3-8e92-b11c5b511efd     /           xfs    defaults,noatime  1   1
45
/swapfile swap swap defaults 0 0 (←追加)
46
</pre>
47
48
49
h2. SSHの設定
50
51
rootのログインを禁止し、SSH用ユーザのみ許可する
52
EC2はデフォルト禁止?
53
sudo vi /etc/ssh/sshd_config
54
sudo grep -i root /etc/ssh/sshd_config
55
<pre>
56
#PermitRootLogin yes
57
PermitRootLogin no (←追加)
58
# the setting of "PermitRootLogin without-password".
59
#ChrootDirectory none
60
</pre>sudo systemctl restart sshd
61
62
63
h2. 基本的なソフトのインストール
64
65
h3. rsyslog
66
67
sudo yum -y install rsyslog
68
69
h3. Apache2
70
71
sudo yum -y install httpd
72
sudo systemctl enable httpd
73
sudo systemctl start httpd
74
75
sudo vi /etc/httpd/conf/httpd.conf
76
<pre>
77
    #
78
    # AllowOverride controls what directives may be placed in .htaccess files.
79
    # It can be "All", "None", or any combination of the keywords:
80
    #   Options FileInfo AuthConfig Limit
81
    #
82
    #AllowOverride None
83
    AllowOverride All
84
</pre>
85
アクセスが多いサイトはパラメータをチューニングする。
86
D-Agreeの2022年3月の/etc/httpd/conf/httpd.conf
87
<pre>
88
<IfModule mpm_prefork_module>
89
    StartServers 10
90
    MinSpareServers 20
91
    MaxSpareServers 60
92
    ServerLimit 200
93
    MaxClients 200
94
    MaxRequestsPerChild 100
95
</IfModule>
96
</pre>sudo vi /etc/httpd/conf/httpd.conf
97
sudo apachectl configtest
98
sudo systemctl reload httpd
99
100
h3. PHP7.4
101
102
CentOS7, AlmaLinux8の場合
103
dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
104
dnf -y module install php:remi-7.4
105
106
Amazon Linux 2の場合
107
sudo amazon-linux-extras install php7.4
108
109
共通?
110
sudo yum install php-mbstring php-xml php-xmlrpc php-gd
111
sudo systemctl restart httpd
112
sudo systemctl enable php-fpm
113
sudo systemctl restart php-fpm
114
115
/etc/php.iniの次の箇所をプロジェクトに合わせて修正
116
<pre>
117
404,407c404
118
< ; memory_limit = 128M
119
< memory_limit = 96M
120
< ; memory_limit = 64M
121
< ; memory_limit = 1024M
122
---
123
> memory_limit = 128M
124
675,677c672
125
< ;post_max_size = 8M
126
< post_max_size = 20M
127
<
128
---
129
> post_max_size = 8M
130
830,831c825
131
< ;upload_max_filesize = 2M
132
< upload_max_filesize = 16M
133
---
134
> upload_max_filesize = 2M
135
909d902
136
< date.timezone = "Asia/Tokyo"
137
1516c1509
138
< mbstring.language = Japanese
139
---
140
> ;mbstring.language = Japanese
141
1523c1516
142
< mbstring.internal_encoding = UTF-8
143
---
144
> ;mbstring.internal_encoding =
145
</pre>sudo systemctl restart php-fpm
146
147
h3. MySQLまたはMariaDB
148
149
AlmaLinux, MariaDB10.6の場合
150
https://mariadb.com/ja/resources/blog/install-mariadb-server-centos7/ に沿う
151
curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
152
153
日本語を扱えるようにする
154
/etc/my.cnf.d/server.cnf
155
[mariadb]
156
character-set-server=utf8mb4
157
158
[client-mariadb]
159
default-character-set=utf8mb4
160
161
systemctl restart mariadb
162
systemctl enable mariadb
163
164
mariadb-secure-installation
165
166
Amazon Linux2, MySQL8.0の場合
167
sudo yum -y install --enablerepo=mysql80-community mysql-community-server
168
sudo systemctl start mysqld
169
sudo systemctl enable mysqld
170
171
アクセスが多いサイトはパラメータをチューニングする。
172
D-Agreeの2022年3月の/etc/my.cnfは下記を追記
173
<pre>
174
secure-file-priv= NULL
175
character_set_server=utf8mb4
176
collation-server=utf8mb4_general_ci
177
178
slow_query_log=1
179
long_query_time=1
180
log_queries_not_using_indexes=1
181
log_throttle_queries_not_using_indexes=1
182
max_connections=300
183
wait_timeout=12
184
interactive_timeout=12
185
innodb_buffer_pool_size=8G
186
innodb_log_file_size=1G
187
innodb_buffer_pool_instances=8
188
innodb_lock_wait_timeout=20
189
thread_cache_size=50
190
191
early-plugin-load=keyring_file.so
192
keyring_file_data=/var/lib/mysql/mysql-keyring/keyring
193
</pre>sudo vi /etc/my.cnf
194
sudo systemctl restart mysqld
195
196
sudo mysql_secure_installation
197
198
h3. SSL証明書
199
200
商用SSLを使わない場合、Let's encryptの概要
201
202
CentOS7,AlmaLinux8の場合
203
204
https://certbot.eff.org/instructions?ws=apache&os=centosrhel7 を参照
205
206
Amazon Linux 2の場合
207
208
https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/SSL-on-amazon-linux-2.html#letsencrypt を参照
209
210
Let's encryptのワイルドカード証明書はDNS認証が必須で自動化できない。
211
毎回次のコマンドでDNS認証をしないといけない模様。
212
sudo certbot certonly --manual \
213
   --preferred-challenges dns-01 \
214
   --server https://acme-v02.api.letsencrypt.org/directory \
215
   -m server@arches.co.jp \
216
   -d r-agree.com \
217
   -d *.r-agree.com
218
certbotがssl.confを見付けられないケースあり? 手動で直した。
219
220
h3. iptables
221
222
ホスティングサービスで制限しているので今のところ使わない。
223
224
h3. vsftpd
225
226
サーバーを最初から設定するような場合はSFTPのみでFTPは必要ないことが多い。
227
228
h3. ClamAV
229
230
sudo yum install clamav clamav-update clamd
231
sudo freshclam
232
sudo vi /etc/clamd.d/scan.conf
233
<pre>
234
# Path to a local socket file the daemon will listen on.
235
# Default: disabled (must be specified by a user)
236
#LocalSocket /run/clamd.scan/clamd.sock
237
LocalSocket /run/clamd.scan/clamd.sock (←追加)
238
</pre>sudo systemctl start clamd@scan
239
sudo systemctl enable clamd@scan
240
241
h3. Postfix
242
243
詳細要確認
244
245
sudo yum -y install postfix
246
sudo yum -y install cyrus-sasl-plain
247
sudo yum -y install cyrus-sasl-md5
248
sudo vi /etc/postfix/main.cf
249
<pre>
250
# INTERNET HOST AND DOMAIN NAMES
251
#
252
# The myhostname parameter specifies the internet hostname of this
253
# mail system. The default is to use the fully-qualified domain name
254
# from gethostname(). $myhostname is used as a default value for many
255
# other configuration parameters.
256
#
257
#myhostname = host.domain.tld
258
#myhostname = virtual.domain.tld
259
myhostname = r-agree.com
260
261
# The mydomain parameter specifies the local internet domain name.
262
# The default is to use $myhostname minus the first component.
263
# $mydomain is used as a default value for many other configuration
264
# parameters.
265
#
266
#mydomain = domain.tld
267
mydomain = r-agree.com
268
269
#mydestination = $myhostname, localhost.$mydomain, localhost
270
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
271
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
272
#       mail.$mydomain, www.$mydomain, ftp.$mydomain
273
274
#home_mailbox = Mailbox
275
home_mailbox = Maildir/
276
</pre>sudo vi /etc/postfix/master.cf
277
<pre>
278
smtp      inet  n       -       n       -       -       smtpd
279
#smtp      inet  n       -       n       -       1       postscreen
280
#smtpd     pass  -       -       n       -       -       smtpd
281
#dnsblog   unix  -       -       n       -       0       dnsblog
282
#tlsproxy  unix  -       -       n       -       0       tlsproxy
283
submission inet n       -       n       -       -       smtpd
284
#  -o syslog_name=postfix/submission
285
#  -o smtpd_tls_security_level=encrypt
286
  -o smtpd_sasl_auth_enable=yes
287
#  -o smtpd_reject_unlisted_recipient=no
288
#  -o smtpd_client_restrictions=$mua_client_restrictions
289
#  -o smtpd_helo_restrictions=$mua_helo_restrictions
290
#  -o smtpd_sender_restrictions=$mua_sender_restrictions
291
  -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
292
#  -o milter_macro_daemon_name=ORIGINATING
293
</pre>
294
sudo vi /etc/sasl2/smtpd.conf
295
<pre>
296
# pwcheck_method: saslauthd
297
pwcheck_method: auxprop
298
# mech_list: plain login
299
mech_list: cram-md5 plain login
300
</pre>
301
sudo mkdir -p /etc/skel/Maildir/{new,cur,tmp}
302
sudo chmod -R 700 /etc/skel/Maildir/
303
sudo yum -y install cyrus-sasl
304
sudo systemctl start saslauthd
305
sudo systemctl enable saslauthd
306
307
sudo postfix check
308
sudo systemctl restart postfix
309
sudo systemctl enable postfix
310
311
h3. logwatch
312
313
sudo yum install logwatch
314
sudo vi /etc/logwatch/conf/logwatch.conf
315
<pre>
316
MailTo = server@arches.co.jp
317
Detail = High
318
</pre>sudo logwatch –output mail
319
320
なぜかこの設定は無視されてcronが届かない。
321
sudo vi /etc/cron.daily/0logwatch
322
<pre>
323
#OPTIONS="--output mail"
324
OPTIONS="--output mail --mailto server@arches.co.jp"
325
</pre>としておく。
326
327
h3. Dovecot
328
329
今のところ必要な場面がない。
330
331
h3. Munin
332
333
sudo yum install munin
334
sudo yum install munin-node
335
sudo systemctl enable munin-node
336
sudo systemctl start  munin-node
337
338
sudo su - munin --shell=/usr/bin/munin-cron
339
340
sudo vi /etc/cron.d/munin
341
<pre>
342
#
343
# cron-jobs for munin
344
#
345
346
MAILTO=root
347
348
*/5 * * * *     munin test -x /usr/bin/munin-cron && /usr/bin/munin-cron
349
</pre>
350
351
cd /etc/munin
352
sudo htpasswd -c munin-htpasswd munin
353
354
sudo grep -r html/munin /etc/
355
<pre>
356
/etc/selinux/targeted/contexts/files/file_contexts:/var/www/html/munin(/.*)?    system_u:object_r:munin_content_t:s0
357
/etc/selinux/targeted/contexts/files/file_contexts:/var/www/html/munin/cgi(/.*)?        system_u:object_r:munin_script_exec_t:s0
358
Binary file /etc/selinux/targeted/contexts/files/file_contexts.bin matches
359
/etc/selinux/targeted/active/file_contexts:/var/www/html/munin(/.*)?    system_u:object_r:munin_content_t:s0
360
/etc/selinux/targeted/active/file_contexts:/var/www/html/munin/cgi(/.*)?        system_u:object_r:munin_script_exec_t:s0
361
/etc/munin/munin.conf:#htmldir   /var/www/html/munin
362
</pre>muninのパスを見せたくないのでパス類を一通り変える。
363
cd /var/www/html
364
sudo mv munin munin-valley/
365
sudo systemctl restart munin-node
366
367
cd munin-valley
368
sudo vi .htaccess
369
<pre>
370
# This file can be used as a .htaccess file, or a part of your apache
371
# config file.
372
#
373
# For the .htaccess file option to work the munin www directory
374
# (@@HTMLDIR@@) must have "AllowOverride all" or something close
375
# to that set.
376
#
377
# As a config file enclose it in <directory> like so:
378
#
379
# <directory @@HTMLDIR@@>
380
381
# AuthUserFile @@CONFDIR@@/munin-htpasswd
382
AuthUserFile /etc/munin/munin-htpasswd
383
AuthName "Munin"
384
AuthType Basic
385
require valid-user
386
387
# This next part requires mod_expires to be enabled.
388
#
389
# We could use <IfModule mod_expires> around here, but I want it to be
390
# as evident as possible that you either have to load mod_expires _or_
391
# you comment out/remove these lines.
392
393
# Set the default expiery time for files 5 minutes 10 seconds from
394
# their creation (modification) time.  There are probably new files by
395
# that time.
396
397
ExpiresActive On
398
ExpiresDefault M310
399
400
# </directory>
401
</pre>
402
403
Apache関係とMySQL関係のプラグインを追加
404
ls -lF /etc/munin/plugins/
405
ls -lF /usr/share/munin/plugins/
406
ls -lF /usr/share/munin/plugins/apache_*
407
ls -lF /usr/share/munin/plugins/mysql_*
408
sudo ln -s /usr/share/munin/plugins/apache_accesses /etc/munin/plugins/
409
sudo ln -s /usr/share/munin/plugins/apache_processes /etc/munin/plugins/
410
sudo ln -s /usr/share/munin/plugins/apache_volume /etc/munin/plugins/
411
412
sudo ln -s /usr/share/munin/plugins/mysql_bytes /etc/munin/plugins/mysql_bytes
413
sudo ln -s /usr/share/munin/plugins/mysql_queries /etc/munin/plugins/mysql_queries
414
sudo ln -s /usr/share/munin/plugins/mysql_slowqueries /etc/munin/plugins/mysql_slowqueries
415
sudo ln -s /usr/share/munin/plugins/mysql_threads /etc/munin/plugins/mysql_threads
416
sudo ln -s /usr/share/munin/plugins/mysql_threads /etc/munin/plugins/mysql_innodb
417
418
Apacheの状態報告
419
sudo vi /etc/httpd/conf.d/server-status.conf
420
<pre>
421
ExtendedStatus On
422
<Location /server-status>
423
        SetHandler server-status
424
        Require ip 127.0.0.1
425
</Location>
426
</pre>sudo systemctl restart httpd
427
428
MuninのMySQLアカウントを追加
429
mysql -u root -p
430
CREATE DATABASE munin;
431
CREATE USER munin@localhost IDENTIFIED BY 'NwyzFWp_dXBM2hjP';
432
GRANT SELECT ON munin.* TO munin@localhost;
433
FLUSH PRIVILEGES;
434
435
sudo vi /etc/munin/plugin-conf.d/munin-node
436
<pre>
437
# We moved the default config to file 00-default
438
# as config files in this directory are read in alphabetical order.
439
#
440
# Documentation can be found in Munin-Guide:
441
# http://guide.munin-monitoring.org/en/latest/plugin/use.html#configuring
442
443
[mysql*]
444
env.mysqlopts -u munin --password=NwyzFWp_dXBM2hjP
445
</pre>
446
447
sudo yum install -y perl-Cache-Cache perl-DBD-MySQL
448
sudo munin-node-configure --shell | sh
449
sudo systemctl restart munin-node
450
sudo su - munin --shell=/usr/bin/munin-cron
451
452
h2. その他
453
454
h3. プログラムのアップロード
455
456
ファイル権限に注意。ログ系やアップロード系のディレクトリは所有者apacheにしないといけないことが多い。
457
458
h3. データベース初期化
459
460
初期化コマンドのメモ
461
CREATE DATABASE IF NOT EXISTS `RAgree2022_db` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
462
USE `RAgree2022_db`;
463
CREATE USER RAgree2022_db@localhost IDENTIFIED BY 'nVTrw%z4uBkEipLV';
464
GRANT ALL PRIVILEGES ON RAgree2022_db.* TO RAgree2022_db@localhost;
465
FLUSH PRIVILEGES;