さくらVPSでサーバ構築(CentOS7.x)


インスタンスを作成

さくらVPSのコントロールパネルからログイン
サーバから「サーバ新規追加」ボタンをクリックする
「ゾーン」「プラン」「OS」等を選択すると作成される

メンテ用ユーザーの作成

# useradd <ユーザ名>
# passwd <ユーザ名>

さらにsudoできるように所属グループを追加

# usermod -G wheel <ユーザー名>

visudoでsudoファイルを編集

visudo
先頭の#を削除する
%wheel  ALL=(ALL)       NOPASSWD: ALL

セキュリティ系の設定

ターミナル接続(SSH)のポート変更(22⇒XXX22などに)

/etc/ssh/sshd_config
Port XXX22

再起動

# systemctl restart sshd

Firewallの設定

現在の設定値を確認

# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: dhcpv6-client ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

httpとhttpsを追加する

# firewall-cmd --permanent --add-service=http
success.
# firewall-cmd --permanent --add-service=https
success.
# firewall-cmd --reload
success.
# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: dhcpv6-client http https ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

sshは標準で22なので設定をコピーして再設定する

# cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/ssh-ext.xml
# vi /etc/firewalld/services/ssh-ext.xml
port="22"port="XXX22"

firewallの設定変更&再起動

# firewall-cmd --permanent --remove-service=ssh
# firewall-cmd --permanent --add-service=ssh-ext
# firewall-cmd --reload

nginxを動かすための設定

yumのレポジトリを編集

EPEL、remi(php7.3)、nginx

$ sudo curl https://yoshiken.github.io/nginx.repo-api/centos.html > /etc/yum.repos.d/nginx.repo
$ sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm

各種モジュールのインストール

$ sudo yum install -y yum-utils
$ sudo yum install -y --enablerepo=remi-php73 php
$ sudo yum install -y --enablerepo=remi-php73 php-fpm
$ sudo yum install -y nginx
$ sudo systemctl enable nginx
$ sudo systemctl enable php-fpm

php-fpmの設定変更
user/groupの行をnginxにする

/etc/php-fpm.d/www.conf
user = nginx
group = nginx

サービス(再)起動

$ sudo systemctl start nginx
$ sudo systemctl start php-fpm

とりあえず、IPアドレスでブラウザアクセス。

ドメインを設定

DNSを設定。
さくらの場合、さくらインターネット会員メニューから
ドメインメニューを選択し、使用するドメインのゾーン編集をクリック
変更をクリックしてサブドメインを追加する
エントリー名:sub1.domain.com
種別:IPアドレス(A)
値:VPSでインスタンス生成時に表示されるIPアドレス

新規登録ボタンをクリック
データ送信ボタンをクリックしてしばらくするとドメイン名でもブラウザアクセス可能になる

nginxの設定

default.confが用意されているので、コピーして使用する

/etc/nginx/conf.d/default.conf
server {
    listen 80;
    server_name sub1.domain.com;
    location / {
        root /var/www/html/sub1:
        index index.php index.html index.htm;
    }
    location ~ \.php$ {
        root           /var/www/html/sub1;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
#        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME  /$document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
  1. server_nameを設定。
  2. 「location /」の括り内で root行を設定。 index行にindex.phpを設定
  3. nginxでphpを使うための設定として「location ~ .php$」のコメント化を解除 fastcgi_param行を変更。 /srciptsのままでは動かない

index.phpの確認

index.phpにアクセスし、phpinfo()の内容が表示されること

index.php
<?php
phpinfo();
?>