ブログ 〜技術と思考の図書館〜
VPSサーバーでのサーバー構築
2021/07/19 13:27:21
備忘録を兼ねてサーバー構築で
備忘録を兼ねてサーバー構築で行ったこと・手順などを書いていきますー
ちなみに環境はCentOS7・apacheです
まずはネットで調べる!
なんか手当たり次第で調べながらやろうかなーは結構危険。というのも、サーバー設定は後から変更するのが結構めんどくさいものが多かったりするので、まずはネットでサーバー構築について調べまくって、流れだけでもつかんでおく。YouTubeとかでサーバー構築の動画を見つけるのもあり。
https://knowledge.sakura.ad.jp/2751/
VPS契約
まずはVPS契約。契約の時にrootのパスワードを設定する(はず)。
rootでSSHログイン & 色々設定
rootユーザーでサーバーのシェルにSSHログインして、以下の設定を済ませる。
◯一般ユーザーの作成
毎回rootユーザーでログインするのは良くないので、一般ユーザーを作成してパスワードも設定する。一般ユーザーのsudoの使用設定も適宜行う。
# useradd [ユーザー名]
# passwd [ユーザー名]
◯ssh公開鍵認証設定(ローカルでsshキーを生成)
ローカル側でsshキーを生成
$ ssh-keygen -t rsa -b 4096
生成されたid_rsa(秘密鍵)はローカルの「/Users/[ローカルPCのユーザー名]/.ssh/」配下(Macの場合)に置いておく。id_rsa.pub(公開鍵)はサーバー側の「/home/[sshでログインしたいユーザー名]/.ssh/authorized_keys」にキーを1行で書き込む。(複数行で書けば複数設置可能)。
サーバー側の.sshは
# chmod 700 .ssh
サーバー側のauthorized_keysは
# chmod 600 authorized_keys
公開鍵認証で(パスワード入力なしで)ログインできることを確認
sshの設定
(rootでログインして)「/etc/ssh/sshd_config」を変更
rootのログインを禁止 PermitRootLogin no パスワードでのログインを禁止 PasswordAuthentication no
設定反映
# systemctl reload sshd.service
ファイアウォール設定
◯以下を参考に、使用するポートを開放し、それ以外を閉じる。後から変更もできるので、最低限のもののみ開放しておく。
firewalld開始
# systemctl start firewalld
firewalld終了
# systemctl stop firewalld
firewalldステータス
# systemctl status firewalld
firewall設定確認
# firewall-cmd --list-all
firewall設定追加
# firewall-cmd --permanent --zone=public --add-service=http
firewall設定追加
# firewall-cmd --permanent --zone=public --add-service=https
firewall設定削除
# firewall-cmd --permanent --zone=public --remove-service=dhcpv6-client
firewall設定再起動
# firewall-cmd --reload
apacheインストール
◯apacheインストール。一番簡単。
インストール
# yum install httpd
起動
# systemctl start httpd
/var/www/htmlのパーミッション変更
◯一般ユーザーやapacheから変更できるようにパーミッションを変更
# cd /var/www/
# chown apache:[ユーザー名] html
# chmod 775 html
phpインストール & 各種php設定
◯php7.4をインストールする場合
# yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
# yum install -y --enablerepo=remi-php74 php which
◯php_errorファイル作成
# touch /var/log/php_error.log
# chown apache:apache /var/log/php_error.log
◯php.ini設定(環境などによって変更する)
595行目 error_log = /var/log/php_error.log
722行目 default_charset = "UTF-8"
932行目 date.timezone="Asia/Tokyo"
1504行目 mbstring.language = Japanese
1511行目 mbstring.internal_encoding = UTF-8
1519行目 mbstring.http_input = UTF-8
1529行目 mbstring.http_output = pass
1537行目 mbstring.encoding_translation = On
1542行目 mbstring.detect_order = UTF-8
1547行目 mbstring.substitute_character = none;
430行目 memory_limit=128M
703行目 post_max_size=25M
855行目 upload_max_filesize=20M
(memory_limit >= post_max_size >= upload_max_filesizeと設定する必要がある)
apache再起動
# systemctl restart httpd
MariaDBインストール & セットアップ
◯MariaDBインストール
# yum -y install mariadb-server
# systemctl start mariadb
# systemctl enable mariadb
◯MariaDBセットアップ
# mysql_secure_installation
rootパスワード設定
全部Yを入力
ポート3306開放
# firewall-cmd --permanent --zone=public --add-service=mysql
# firewall-cmd --reload
◯(MariaDBログイン方法)
$ mysql -h localhost -u root -p
パスワード入力してログイン
phpとMariaDBの動作確認
◯「/var/www/html/index.php」を作成
◯「http://[サーバーのIPアドレス]/」にアクセス
↓サンプルプログラム
<?php
$mysqli = new mysqli( "host_name", "user_name", "password", "performance_schema");
if(is_null($mysqli->connect_error)){
echo "db connected";
}else{
echo $mysqli->connect_error;
}
phpinfo();
?>
ドメイン設定
◯独自ドメインがある場合はDNSの設定などを済ませておく。
◯/etc/httpd/conf.d/vhost.confにて設定
apache再起動で反映
# systemctl restart httpd
↓設定例
https://httpd.apache.org/docs/2.4/ja/vhosts/examples.html
SSL証明書発行
◯必要なものをインストール
EPELインストール
# yum install epel-release
python-certbot-apacheインストール
# yum install certbot python-certbot-apache
◯証明書発行コマンド
# certbot certonly --webroot -w [ドメインのルートディレクトリ] -d [ドメイン名]
メールアドレス入力
同意(Y)
お知らせメール(Y/N)
◯証明書が生成されたか確認(cert.pem, chain.pem, fullchain.pem, privkey.pemの4つ)
# ls -l /etc/letsencrypt/live/[ドメイン名]/
SSL証明書設置 & 自動更新設定
◯/etc/httpd/conf.d/vhost.confに下記を追記
[変更] <VirtualHost *:443>
[追加] SSLEngine on
[追加] SSLCertificateFile /etc/letsencrypt/live/[ドメイン名]/cert.pem
[追加] SSLCertificateKeyFile /etc/letsencrypt/live/[ドメイン名]/privkey.pem
[追加] SSLCertificateChainFile /etc/letsencrypt/live/[ドメイン名]/chain.pem
◯SSL証明書自動更新設定
以下を実行して下記を追記する。
# crontab -u root -e
[追加] 00 04 01 * * certbot renew && systemctl restart httpd
常時SSL化
◯/etc/httpd/conf.d/vhost.confに下記を追記
<VirtualHost *:80>
ServerName [ドメイン名]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^[セカンドレベルドメイン]\.[トップレベルドメイン]
RewriteRule ^/(.*)$ https://[ドメイン名]/$1 [R=301,L]
</VirtualHost>
gitダウンロード
◯以下のコマンドを実行
# sudo yum -y install gcc curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker autoconf
Basic認証
◯htpasswd生成(rootで作成)
htpasswd -c /etc/httpd/htpasswd ユーザ名
◯アクセス権限の変更(rootで変更)
chown apache:apache /etc/httpd/htpasswd
chmod 600 /etc/httpd/htpasswd
◯vhost設定に追加(Directory内)
# Basic Auth
AuthType Basic
AuthName "auth"
AuthUserFile /etc/httpd/htpasswd
Require valid-user
まとめ
と、大まかにはこんな感じですが、他にも細かい設定があると思うので、サーバーの環境や用途によって変えていけばいいのではないかと思います。
2021/08/21 22:27:31 更新