LoginSignup
0
0

More than 1 year has passed since last update.

さくらのVPS(CentOS8 x86_64)でWordPressやMediawiki、VPNなどをする話(1)

Last updated at Posted at 2021-08-29

どちらかと言えば備忘録的なお話。
書いている人間は、レンタルサーバーでWordPressを使って個人ブログを運営しているレベルの人間です。以前、PanasonicのレッツノートでVPNサーバーを作ったり、Raspberry Piで簡易NASを作ったりしたことはありますが、ネットに載っている情報を見様見真似でやった程度で、自力でPythonを書いたりはできません。

VPSの入門編として、さくらインターネット自体が「ネコでもわかる!さくらのVPS講座」を公開しているが、公開から4年程度経過しているし、使おうとしているOSが異なるため、注意が必要である。

やりたいこと

  • ApacheをインストールしてHTMLサーバーとして運用する。
  • Let's Encryptを用いてHTTPS接続を作る。
  • PHP、MySQL、MyPHPAdminをインストールする。
  • WordPressをインストールしてブログを公開する。
  • MediaWikiをインストールして個人用のWikiを作る。
  • SoftEtherVPNをインストールして個人用のVPNを作る。

インストールする主たるソフトウェア

  • CentOS8 x86_64
  • Apache 2.4.37
  • WordPress 5.7
  • MediaWiki 1.36.1

OSのインストール

OSが無ければ話は始まらない。標準インストールパッケージとしてCentOS8 x86_64のほか、同7、同Stream8、Ubuntu20.04、同18.04、KUSANAGIが選べる。とりあえずCentOS8を選択した。管理パスワードは出来るだけ推測されにくいものが良い。記号が使えるので、これを交えてパスワードを作る。

また、この時にスタートアップスクリプトを用いてSoftEther VPNをインストールする。検索で「VPN」と打つと「自分専用VPNサーバー (L2TP over IPSec)」が表示されるので、ログインID、パスワード、PSKを適宜設定する。

あとはインストールボタンを押すことでインストールが完了する。

一般ユーザー作成とrootログインの無効化

一般ユーザー作成

一般用のアカウントを作るため、sshでサーバーにログインする。

command
$ ssh root@xxx.xxx.xxx.xxx

The authenticity of host 'xxx.xxx.xxx.xxx)' can't be established.
ECDSA key fingerprint is SHA256:hogehoge.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xxx.xxx.xxx.xxx' (ECDSA) to the list of known hosts.

root@xxx.xxx.xxx.xxx's password: 

SAKURA internet [Virtual Private Server SERVICE]

[root@hogehoge ~]#

サーバーにログインできた。adduserコマンドで一般ユーザーhogehogeを追加し、パスワードを設定する。

command
[root@hogehoge ~]# adduser hogehoge
[root@hogehoge ~]# passwd hogehoge
Changing password for user hogehoge.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

exitコマンドで一度sshからログアウトし、作成したhogehogeユーザーでログインしてみる。

command
$ ssh hogehoge@xxx.xxx.xxx.xxx
SAKURA internet [Virtual Private Server SERVICE]

[hogehoge@hogehoge ~]$

rootログインの無効化

rootログインを開放しておくと悪戯(で済まないことが多いだろう)される可能性があるため、sshでのrootログインを無効化する。

一般ユーザーのまま、su -コマンドでrootへログインする。

command
[root@hogehoge ~]# cd /etc/ssh   <---sshディレクトリへの移動
[root@hogehoge ssh]# cp sshd_config sshd_config.old   <---sshd_configファイルのバックアップを取る
[root@hogehoge ssh]# vi sshd_config   <--- sshd_configの編集

viでsshd_configを編集し、PermitRootLogin YesPermitRootLogin noに変更、rootログインを禁止にする。
あとはsshd.serviceを再起動する。

command
[root@hogehoge ssh]# systemctl restart sshd.service   <--- sshd.serviceの再起動。

何も表示されないがこれで再起動出来ている。sshでrootユーザーでログインしようとしてもPermission Denied.と表示されてログイン出来なくなっていれば成功である。

Apacheのインストール

command
[root@hogehoge]# yum install httpd
----
Total download size: 2.1 M
Installed size: 5.6 M
Is this ok [y/N]: y
----
Complete!

[root@hogehoge]# systemctl start httpd
[root@hogehoge]# systemctl enable httpd

これでApacheをインストール出来た。ウェブブラウザでhttp://xxx.xxx.xxx.xxxにアクセスすれば、Apacheの初期ページが見られるはずである。

ファイアウォール(firewalld)の設定

このままでは全てのポートが開放状態のままであり非常に危険である。firewalldを利用してファイアウォールを設定する。

command
[root@hogehoge]# systemctl start firewalld.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ====
Authentication is required to start 'firewalld.service'.
Authenticating as: root
Password: 
==== AUTHENTICATION COMPLETE ====
[root@hogehoge]# systemctl enable firewalld.service
[root@hogehoge]# firewall-cmd --add-service=http --zone=public --permanent
[root@hogehoge]# firewall-cmd --add-service=https --zone=public --permanent
[root@hogehoge]# systemctl restart firewalld

続いて、firewalldで有効になっているサービスを確認する。

command
[root@hogehoge]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens3
  sources: 
  services: cockpit dhcpv6-client http https ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

services: cockpit dhcpv6-client http https sshでhttp、https、sshが有効になっている。

続いて、次のsshポート変更に備えてsshサービスを複製、新ポート用サービスを作る。

command
[root@hogehoge]# cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/ssh-no2.xml
[root@hogehoge]# vi /etc/firewalld/services/ssh-no2.xml

<port protocol="tcp" port="22"/>

<port protocol="tcp" port="xxxxx"/>   <--- xxxxxは新ポート

[root@hogehoge]# firewall-cmd --reload
[root@hogehoge]# firewall-cmd --add-service=ssh-no2 --zone=public --permanent
[root@hogehoge]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens3
  sources: 
  services: cockpit dhcpv6-client http https ssh ssh-no2 <--- ssh-no2が追加されている。
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

sshのポート変更

sshは通常ポート22を使用しているが、これを変更して不正ログインを防止する。

command
[root@hogehoge]# vi sshd_config
#Port 22

Port xxxxx   <--- #を外しPort番号を任意の数値にする。

[root@hogehoge]# systemctl restart sshd.service
[root@hogehoge]# reboot now

これ以降、ログイン時にはssh hogehoge@xxx.xxx.xxx.xxxではログイン出来なくなる。
このため、次の構文でログインする必要がある。

command
$ ssh -p xxxxx hogehoge@xxx.xxx.xxx.xxx

続いて、firewalldからポート22のサービスを削除する。

command
[root@hogehoge]# firewall-cmd --permanent --zone=public --remove-service=ssh
[root@hogehoge]# firewall-cmd --reload
[root@hogehoge]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens3
  sources: 
  services: cockpit dhcpv6-client http https ssh-no2 <--- sshが削除された。
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

[root@hogehoge]# reboot now

これでPort xxxxxからでのみsshでアクセス出来る。

参考URL

0
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
0
0