LoginSignup
24
29

More than 5 years have passed since last update.

ConoHaのVPSでWebサーバを立てる

Posted at

はじめに

セールエンジニアをやっています。
日々提案書を作り、提案する毎日。技術力や経験がないと充実した提案になりません。

技術をつける。運用経験を持つ。そんなことを目的にWebサーバを立てて、運用してみようと思います。

サーバを借りる

  • ConoHa
    • CentOS 7.1 64bit
    • メモリ:1GB
  • ドメイン
    • example.com(仮)
    • IPアドレス:x.x.x.x
  • 公開鍵
    • conoha.pem(仮)

Macに鍵認証でsshできるようにする

鍵を.ssh/に移動させる

$ mv key-2015-12-13-17-21.pem ~/.ssh/conoha.pem

鍵が移動しているか確認する

$ ls ~/.ssh/ | grep conoha.pem
conoha.pem

鍵のアクセス制御のモードを変更する

$ chmod 600 ~/.ssh/conoha.pem

鍵をコンフィグに登録する

$ vi ~/.ssh/config
Host conoha
    HostName example.com
    Port 22
    User root
    IdentityFile ~/conoha.pem
# fingerprintが異なっている場合があるので、注意。

パッケージの更新

初期インストールパッケージの更新

$ sudo yum update

ロケールとキーマップの変更

現在の設定を確認する

$ localectl status
   System Locale: LANG=en_US.UTF-8
       VC Keymap: jp
      X11 Layout: jp

設定可能な値を確認する

$ sudo localectl list-locales | grep ja_JP.*
ja_JP
ja_JP.eucjp
ja_JP.ujis
ja_JP.utf8

ロケールの設定をja_JP.utf8に設定する

$ localectl set-locale LANG=jp_JP.utf8

設定が変更されたことを確認する

$ sudo localectl status
   System Locale: LANG=jp_JP.utf8
       VC Keymap: jp
      X11 Layout: jp

設定可能なキーマップを確認する

$ localectl list-keymaps | grep jp*
jp
jp-OADG109A
jp-dvorak
jp-kana86
jp106
tj_alt-UTF8

キーマップをjp106に変更する

$ sudo localectl set-keymap jp106

キーマップが変更されたかを確認する

$ localectl status
   System Locale: LANG=jp_JP.utf8
       VC Keymap: jp106
      X11 Layout: jp
       X11 Model: jp106
     X11 Options: terminate:ctrl_alt_bksp

タイムゾーンの変更

タイムゾーンの設定を確認する

$ timedatectl status
      Local time: 日 2015-12-13 18:52:02 JST
  Universal time: 日 2015-12-13 09:52:02 UTC
        RTC time: 日 2015-12-13 09:52:02
        Timezone: n/a (JST, +0900)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

設定可能なタイムゾーンを確認する

$ timedatectl list-timezones | grep Asia/Tokyo
Asia/Tokyo

タイムゾーンをAsia/Tokyoに変更する

$ timedatectl set-timezone Asia/Tokyo 

タイムゾーンが変更されたか確認する

timedatectl status
      Local time: 日 2015-12-13 18:55:00 JST
  Universal time: 日 2015-12-13 09:55:00 UTC
        RTC time: 日 2015-12-13 09:54:59
        Timezone: Asia/Tokyo (JST, +0900)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

不要なサービスの停止

OS起動時に読み込まれるサービスを確認する

$ systemctl list-unit-files --type service | grep enabled
abrt-ccpp.service                           enabled 
abrt-oops.service                           enabled 
abrt-vmcore.service                         enabled 
abrt-xorg.service                           enabled 
abrtd.service                               enabled 
atd.service                                 enabled 
auditd.service                              enabled 
chronyd.service                             enabled 
cloud-config.service                        enabled 
cloud-final.service                         enabled 
cloud-init-local.service                    enabled 
cloud-init.service                          enabled 
crond.service                               enabled 
dbus-org.fedoraproject.FirewallD1.service   enabled 
dbus-org.freedesktop.NetworkManager.service enabled 
dbus-org.freedesktop.nm-dispatcher.service  enabled 
dmraid-activation.service                   enabled 
firewalld.service                           enabled 
getty@.service                              enabled 
irqbalance.service                          enabled 
kdump.service                               enabled 
libstoragemgmt.service                      enabled 
lvm2-monitor.service                        enabled 
mdmonitor.service                           enabled 
microcode.service                           enabled 
NetworkManager-dispatcher.service           enabled 
NetworkManager.service                      enabled 
ntpd.service                                enabled 
postfix.service                             enabled 
rngd.service                                enabled 
rsyslog.service                             enabled 
smartd.service                              enabled 
sshd.service                                enabled 
sysstat.service                             enabled 
systemd-readahead-collect.service           enabled 
systemd-readahead-drop.service              enabled 
systemd-readahead-replay.service            enabled 
tuned.service                               enabled 

停止するサービス(Postfix)の設定状態を確認する

$ systemctl status postfix.service 
postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled)
   Active: active (running) since 日 2015-12-13 17:52:06 JST; 1h 6min ago
 Main PID: 1669 (master)
   CGroup: /system.slice/postfix.service
           ├─1669 /usr/libexec/postfix/master -w
           ├─1676 pickup -l -t unix -u
           └─1677 qmgr -l -t unix -u

12月 13 17:52:06 x-x-x-x systemd[1]: Starting Postfix Mail Transport ....
12月 13 17:52:06 x-x-x-x postfix/master[1669]: daemon started -- versi...
12月 13 17:52:06 x-x-x-x systemd[1]: Started Postfix Mail Transport A....
Hint: Some lines were ellipsized, use -l to show in full.

対象のサービスを停止し、OS起動時に起動する設定を無効化する

$ sudo systemctl stop postfix.service 
$ sudo systemctl disable postfix.service 
rm '/etc/systemd/system/multi-user.target.wants/postfix.service'

無効化になっているか確認する

$ systemctl status postfix.service
postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; disabled)
   Active: inactive (dead)

12月 13 17:52:06 x-x-x-x systemd[1]: Starting Postfix Mail Transport ....
12月 13 17:52:06 x-x-x-x postfix/master[1669]: daemon started -- versi...
12月 13 17:52:06 x-x-x-x systemd[1]: Started Postfix Mail Transport A....
12月 13 19:04:03 x-x-x-x systemd[1]: Stopping Postfix Mail Transport ....
12月 13 19:04:03 x-x-x-x systemd[1]: Stopped Postfix Mail Transport A....
Hint: Some lines were ellipsized, use -l to show in full.

Webサーバのインストール・設定

Apache (sshd) をインストールする

$ sudo yum install httpd

インストール直後のサービスの状態を確認する

$ systemctl status httpd.service
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
   Active: inactive (dead)

起動時に起動しない設定(disabled)になっており、現在も起動していない(dead)ので、起動し、OS起動時に起動するように設定する

$ sudo systemctl start httpd.service
$ sudo systemctl enable httpd.service
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

設定されているか確認する

$ systemctl status httpd.service
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since 日 2015-12-13 19:14:36 JST; 1min 39s ago
 Main PID: 14029 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─14029 /usr/sbin/httpd -DFOREGROUND
           ├─14030 /usr/sbin/httpd -DFOREGROUND
           ├─14031 /usr/sbin/httpd -DFOREGROUND
           ├─14032 /usr/sbin/httpd -DFOREGROUND
           ├─14033 /usr/sbin/httpd -DFOREGROUND
           └─14034 /usr/sbin/httpd -DFOREGROUND

12月 13 19:14:36 x-x-x-x systemd[1]: Starting The Apache HTTP Server...
12月 13 19:14:36 x-x-x-x httpd[14029]: AH00558: httpd: Could not reli...e
12月 13 19:14:36 x-x-x-x systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

OSのファイアウォールの設定

サービスの存在を確認する

$ systemctl status firewalld.service
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: active (running) since 日 2015-12-13 17:52:05 JST; 1h 27min ago
 Main PID: 597 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─597 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

12月 13 17:52:05 x-x-x-x systemd[1]: Started firewalld - dynamic fire....
Hint: Some lines were ellipsized, use -l to show in full.

ConoHaは標準でenabledかつrunningになっている。ファイアウォールの設定を確認する。

$ firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources: 
  services: dhcpv6-client ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 

servicesにhttp(80)を追加する。設定ファイルに反映するため、--permanentオプションをつける。

$ sudo firewall-cmd --permanent --add-service=http --zone=public
success

設定を反映する。

$ firewall-cmd --reload
success

設定を確認する。

$ firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources: 
  services: dhcpv6-client http ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 

Webサイトの動作確認

example.comにアクセスして動作を確認する。

参考文献

24
29
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
24
29