dns設定
osインストール
macターミナル接続
リポジトリインストール
アパッチ2インストール
phpインストール
データベースインストール
sslサーバー設定
vsftpdインストール
dns設定
example.com
A
3600
<IPアドレス>
osインストール
VPSサーバーのコントロールパネルからCentOS7.9.2009
をインストール
rootパスワードを設定
macターミナル接続
Host <任意の接続ネーム>
HostName <IPアドレス>
User root
Password:_
設定したパスワードを入力してログイン
OSバージョンの確認
cat /etc/*release
CentOS Linux release 7.9.2009 (Core)
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
CentOS Linux release 7.9.2009 (Core)
CentOS Linux release 7.9.2009 (Core)
リポジトリインストール
yum-prioritiesインストール
# yum -y install yum-plugin-priorities
CentOS-Base.repo編集
[base]
priority=1 ←追加
[updates]
priority=1 ←追加
[extras]
priority=1 ←追加
EPELリポジトリ追加
インストールされていないことを確認
# rpm -q epel-release
パッケージ epel-release はインストールされていません。
インストール
# rpm --import http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7
# rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
インストール確認
# rpm -q epel-release
epel-release-7-10.noarch
ELRepoリポジトリ追加
インストールされていないことを確認
# rpm -q elrepo-release
パッケージ elrepo-release はインストールされていません。
インストール
# rpm --import http://elrepo.org/RPM-GPG-KEY-elrepo.org
# rpm -ivh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
インストール確認
# rpm -q elrepo-release
elrepo-release-7.0-2.el7.elrepo.noarch
Remiリポジトリ追加
インストールされていないことを確認
# rpm -q remi-release
パッケージ remi-release はインストールされていません。
インストール
# rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rp
インストール確認
# rpm -q remi-release
remi-release-7.1-2.el7.remi.noarch
epel.repo設定確認
enabled=1を確認
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
# baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1 ←enabled=1を確認
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
elrepo.repo設定確認
enabled=1を確認
[elrepo]
name=ELRepo.org Community Enterprise Linux Repository - el6
baseurl=http://elrepo.org/linux/elrepo/el6/$basearch/
mirrorlist=http://elrepo.org/mirrors-elrepo.el6
enabled=1 ←enabled=1を確認
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0
remi.repo編集
priority=1を追加
enabled=0を確認
[remi]
priority=1 ←追加
name=Les RPM de remi pour Enterprise Linux $releasever - $basearch
# baseurl=http://rpms.famillecollet.com/enterprise/$releasever/remi/$basearch/
mirrorlist=http://rpms.famillecollet.com/enterprise/$releasever/remi/mirror
enabled=0 ←enabled=0を確認
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
failovermethod=priority
epel,elrepo,remiアップデート
# yum -y update epel-release
# yum -y update elrepo-release
# yum -y update remi-release
yum-plugin-versionlockインストール
# yum -y install yum-plugin-versionlock
アパッチ2インストール
インストール
# yum -y install httpd
設定ファイル編集
ServerAdmin root@localhost
↓
ServerAdmin <管理者メールアドレス>
# ServerName www.example.com:80
↓
ServerName <ドメイン名>:80
<Directory "/var/www/html">
#
#
Options Indexes FollowSymLinks
↓
Options Includes ExecCGI FollowSymLinks ←変更(CGI,SSIを許可。ファイル一覧表示禁止)
#
#
AllowOverride None
↓
AllowOverride All ←変更(.htaccessを許可)
#
#
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
↓
DirectoryIndex index.html index.htm index.cgi ←変更(index.htm index.cgiを許可)
</IfModule>
ErrorLog logs/error_log
↓
ErrorLog /var/log/httpd/error_log ←変更(エラーログ)
<IfModule log_config_module>
CustomLog logs/access_log combined
↓
SetEnvIf Request_URI "default\.ida" nolog
SetEnvIf Request_URI "cmd\.exe" nolog
SetEnvIf Request_URI "root\.exe" nolog
SetEnvIf Request_URI "Admin\.dll" nolog
SetEnvIf Request_URI "NULL\.IDA" nolog
SetEnvIf Request_URI "\.(gif)|(jpg)|(png)|(ico)|(css)$" nolog
CustomLog /var/log/httpd/access_log combined env=!nolog ←変更(アクセスログ)
</IfModule>
<IfModule mime_module>
#AddHandler cgi-script .cgi
↓
AddHandler cgi-script .cgi .pl ←コメント解除&変更(.cgiと.plを許可)
</IfModule>
AddDefaultCharset UTF-8
↓
# AddDefaultCharset UTF-8 ←コメントアウト(デフォルトの文字コードを指定しない)
ファイル圧縮転送設定
↓下記を記入
<IfModule mod_deflate.c>
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css
# Level of compression (Highest 9 - Lowest 1)
DeflateCompressionLevel 9
# Netscape 4.x has some problems.
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
</IfModule>
welcome.conf無効化
# mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.org
index.html作成
# echo '<h1>This is my top!</h1>' > /var/www/html/index.html
シンボリックリンク作成
# ln -s /usr/bin/perl /usr/local/bin/perl
Apache起動
# systemctl start httpd
# systemctl enable httpd
# systemctl is-enabled httpd
enabled
ブラウザ確認
http://example.com
This is my top!
phpインストール
php8.0をインストール
yumインストール
# yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yumでインストールできるphp80系パッケージ名称確認
# yum list | grep php80
phpインストール
# yum -y install php80 php80-php php80-php-bcmath php80-php-json php80-php-mbstring php80-php-pdo php80-php-xml php80-php-mysqlnd --enablerepo=remi-php80
インストール確認
# php80 -v
PHP 8.0.2 (cli) (built: Feb 2 2021 19:28:42) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.2, Copyright (c) Zend Technologies
シンボリックリンク作成
パスの確認
# which php80
/usr/bin/php80
シンボリック作成
# ln -sf /usr/bin/php80 /usr/bin/php
確認
# php -v
PHP 8.0.2 (cli) (built: Feb 2 2021 19:28:42) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.2, Copyright (c) Zend Technologies
php設定
php.iniの場所を確認
# php -i | grep php.ini
Configuration File (php.ini) Path => /etc/opt/remi/php80
Loaded Configuration File => /etc/opt/remi/php80/php.ini
設定ファイル編集
date.timezone = "Asia/Tokyo"
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = pass
mbstring.http_output = pass
mbstring.encoding_translation = Off
mbstring.detect_order = auto
mbstring.substitute_character = none;
mbstring.strict_detection = Off
memory_limit = 32MB
post_max_size = 16M
upload_max_filesize = 8M
expose_php = Off
再起動
# systemctl restart httpd
データベースインストール
MariaDBインストール
# yum -y install mariadb-server
設定ファイル編集
[mysqld]
character-set-server=utf8
MariaDB起動
# systemctl start mariadb
# systemctl enable mariadb
# systemctl is-enabled mariadb
enabled
MariaDB初期設定
# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
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
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
空Enter
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Set root password? [Y/n]
y
New password:
任意のパスワードを設定
Re-enter new password:
パスポート再入力
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
... 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...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed! Not critical, keep moving...
- 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!
データベース確認
# mysql -u root -p
Enter password:
パスワードでエントリー
MariaDB [(none)]>
登録ユーザー一覧
MariaDB [(none)]> SELECT user,host FROM mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+------+-----------+
3 rows in set (0.00 sec)
データベース一覧
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
sslサーバー設定
Let's Encryptを利用
snapdインストール
# yum --enablerepo=epel install snapd
snapd自動起動設定、起動
# systemctl enable --now snapd.socket
--
Created symlink from /etc/systemd/system/sockets.target.wants/snapd.socket to /usr/lib/systemd/system/snapd.socket.
--
シンボリックリンク作成
# ln -s /var/lib/snapd/snap /snap
coreパッケージインストール
# snap install core
--
2021-03-30T13:12:34+09:00 INFO Waiting for automatic snapd restart...
Warning: /var/lib/snapd/snap/bin was not found in your $PATH. If you've not
restarted your session since you installed snapd, try doing that.
Please see https://forum.snapcraft.io/t/9469 for more details.
core 16-2.49.1 from Canonical? installed
--
coreパッケージアップデート
# snap refresh core
snapバージョン確認
# snap --version
certbotコマンドインストール
# snap install --classic certbot
シンボリックリンク作成
# ln -s /snap/bin/certbot /usr/bin/certbot
certbotバージョン確認
# certbot --version
ポート開放
firewall 動作確認
# firewall-cmd --state
running
ゾーン確認
# firewall-cmd --get-default-zone
public
publicゾーンの設定確認 http、httpsが開いていることを確認
# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: dhcpv6-client ftp http https
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
開いていなければサービス追加
# firewall-cmd --zone=public --add-service=http --permanent
# firewall-cmd --zone=public --add-service=https --permanent
success
リロード
# firewall-cmd --reload
確認
# firewall-cmd --list-all
Let's Encrypt証明書取得
設定事項を確認しておく
certonly
:証明書のみを取得
--webroot
:httpdが動いていて、無停止で取得
-w
:ルートディレクトリ
-d
:ドメイン名
対話形式で証明書取得
# certbot certonly --webroot -w /var/www/html/example.com/ -d example.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel):
管理者メールアドレス入力
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
(A)gree/(C)ancel:
Aを入力(規約に同意)
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
(Y)es/(N)o:
Nを入力(メーリングリストへの登録不要)
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Your cert will expire on 2021-01-18. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
登録が完了
証明書の確認
# ls -l /etc/letsencrypt/live/example.com/
total 4
lrwxrwxrwx 1 root root 58 Mar 22 11:26 cert.pem -> ../../archive/example.com/cert1.pem
lrwxrwxrwx 1 root root 59 Mar 22 11:26 chain.pem -> ../../archive/example.com/chain1.pem
lrwxrwxrwx 1 root root 63 Mar 22 11:26 fullchain.pem -> ../../archive/example.com/fullchain1.pem
lrwxrwxrwx 1 root root 61 Mar 22 11:26 privkey.pem -> ../../archive/example.com/privkey1.pem
sslサーバー自動更新設定
Snap版certbotをインストールすると、証明書の自動更新機能も併せてインストールされている
systemdのタイマー機能確認
# systemctl list-timers | less
NEXT LEFT LAST PASSED UNIT ACTIVATES
金 2021-04-09 18:53:31 JST 1h 40min left 木 2021-04-08 18:53:31 JST 22h ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
土 2021-04-10 02:55:00 JST 9h left 金 2021-04-09 15:56:01 JST 1h 17min ago snap.certbot.renew.timer snap.certbot.renew.service
snap.certbot.renew.timer のユニットファイル確認
# view /etc/systemd/system/snap.certbot.renew.timer
--
[Unit]
# Auto-generated, DO NOT EDIT
Description=Timer renew for snap application certbot.renew
Requires=var-lib-snapd-snap-certbot-1093.mount
After=var-lib-snapd-snap-certbot-1093.mount
X-Snappy=yes
[Timer]
Unit=snap.certbot.renew.service
OnCalendar=*-*-* 02:55
OnCalendar=*-*-* 15:56
[Install]
WantedBy=timers.target
--
snap.certbot.renew.serviceのユニットファイル確認
# view /etc/systemd/system/snap.certbot.renew.service
--
[Unit]
# Auto-generated, DO NOT EDIT
Description=Service for snap application certbot.renew
Requires=var-lib-snapd-snap-certbot-1093.mount
Wants=network.target
After=var-lib-snapd-snap-certbot-1093.mount network.target snapd.apparmor.service
X-Snappy=yes
[Service]
EnvironmentFile=-/etc/environment
ExecStart=/usr/bin/snap run --timer="00:00~24:00/2" certbot.renew
SyslogIdentifier=certbot.renew
Restart=no
WorkingDirectory=/var/snap/certbot/1093
TimeoutStopSec=30
Type=oneshot
[Install]
WantedBy=multi-user.target
--
Let'sEncrypt更新後にAppahe再起動する設定
# !/bin/bash
systemctl restart httpd
パーミッションの変更
chmod 755 /etc/letsencrypt/renewal-hooks/post/web_restart.sh
Apache設定
etc/httpd/conf.d内にvirtual.conf作成
--
<virtualHost *:80>
ServerName <IPアドレス>
DocumentRoot /var/www/html
</VirtualHost>
<virtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html/example.com
RewriteEngine on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</VirtualHost>
<IfModule mod_ssl.c>
<virtualHost *:443>
ServerName example.com
DocumentRoot /var/www/html/example.com
ServerAlias mail.example.com
AllowEncodedSlashes On
CustomLog logs/example.com.access.log combined
ErrorLog logs/example.com.error.log
<Directory /var/www/html/example.com>
SetEnv HTTP_X_FORWARDED_PROTO http
SetEnv REAL_HOST_NAME example.com
AllowOverride All
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</virtualHost>
</IfModule>
文法チェック
# apachectl -t
再起動
# service httpd restart
# service httpd status
ブラウザ確認
https://example.com
vsftpdインストール
インストール
yum -y install vsftpd
設定ファイル編集
# 匿名(anonymous)でのログインを禁止
anonymous_enable=NO
# コメント(#)を削除して、ASCIIモード転送を許可
ascii_upload_enable=YES
ascii_download_enable=YES
# コメント(#)を削除して、ログインしたディレクトリのみアクセス可能
chroot_local_user=YES
# コメント(#)を削除して、chroot_list_fileに記述されたユーザーはログインしたディレクトリ以外もアクセス可能に
chroot_list_enable=YES と chroot_list_file=...を設定すると後述の allow_writeable_chroot=YES を設定してもログインできません
chroot_list_enable=YES
# コメント(#)を削除して、ログインしたディレクトリ以外もアクセスが可能なユーザーのリストファイルを指定
chroot_list_file=/etc/vsftpd/chroot_list
# コメント(#)を削除して、ディレクトリごとのファイル一括転送を可能に
ls_recurse_enable=YES
# IPv4でのファイル転送を受付
listen=YES
# IPv6でのファイル転送を受付ない
listen_ipv6=NO
# 最終行へ追記
# ルートディレクトリを指定
local_root=public_html
# ローカルタイム(日本時間)を使用
use_localtime=YES
# ユーザーリストを有効に
userlist_deny=NOと併せて、/etc/vsftpd/user_list ※2にのっているユーザーだけがログイン可
userlist_enable=YES
userlist_deny=NO
# vsftp3 系からセキュリティが厳しくなって、ftp ログインするユーザーのホームディレクトリが
# 上記のchroot_list_enable=YES と chroot_list_file=...を設定していないときに限り有効
allow_writeable_chroot=YES
# VSFTPD configuration
pasv_enable=YES
pasv_addr_resolve=YES
pasv_address=example.com
pasv_min_port=4000
pasv_max_port=4029
use_localtime=YES
ssl_enable=YES
rsa_cert_file=/etc/letsencrypt/live/example.com/fullchain.pem
rsa_private_key_file=/etc/letsencrypt/live/example.com/privkey.pem
# require_ssl_reuse=NO
force_local_logins_ssl=NO
force_local_data_ssl=NO
force_dot_files=YES
chroot_listにFTPサービスを許可するユーザーを追加
<ユーザー名>
user_listにFTPサービスを許可するユーザーを追加
<ユーザー名>
/etc/hosts.allow編集
vsftpd: ALL
ポート開放
firewall 動作確認
# firewall-cmd --state
running
ゾーン確認
# firewall-cmd --get-default-zone
public
publicゾーンの設定確認 ftpが開いていることを確認
# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: dhcpv6-client ftp http https
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
開いていなければサービス追加
# firewall-cmd --zone=public --add-service=ftp --permanent
success
リロード
# firewall-cmd --reload
確認
# firewall-cmd --list-all
vsftpd起動
# systemctl start vsftpd
# systemctl enable vsftpd
# systemctl is-enabled vsftpd
enabled
参考記事
https://centos.server-manual.com/
https://inaba-serverdesign.jp/blog/20210331/snap-lets-encrypt-ssl-certificate-update.html
https://qiita.com/shojimotio/items/4fa82b21390e8a6d8446
https://zenn.dev/dai0916/articles/58923df5c8e8818d02f7
http://www.kentokyo.com/unix/centos/vsftpd/