GCEで事前準備(サーバー設定編)
前回はえらく長くなってしまった…
他のこともあるけど、自分のローカルPCユーザ名日本語であんなに時間取られるとは思わなかった…( ;∀;)
職場復帰まで!!!時間が!!!!!!ないの!!!!!(地団駄)
ローカルクライアントからSSH接続出来るまで出来たので続き。
サーバー設定残作業
1.rootのパスワード設定
前回ローカルクライアントからSSH接続が出来たので、残りの作業をやってしまう。
というかApache再起動するときにrootのパスワード訊かれるから事前にやっときましょう。
【クリックとコピペだけでできる】Google Cloud Platform上にWebサーバー(Nginx)を構築してWordPressを動かす【CentOS】 - Qiita
$ sudo passwd root
パスワード2回入れたら設定OK!
2.ポート22を閉じるルールの追加
GCE の無料枠のサーバを立るときに、初見でハマりそうなところ - Qiita
前記事で書いたポート開けたことの反対を行う。ポート22を閉じるファイアウォールルールを作成する。
SSH接続出来ることを確認して終了。
3.タイムゾーンを東京に設定
遙かデジタリアへ: RubyのCGIをGoogle Cloud Platformの無料枠で動かしたい ~(1)セットアップ
表題の通り
$ sudo timedatectl set-timezone Asia/Tokyo
Apacheインストール(追記:アンイストールしました)
追記(18/04/09)
自分のGCE無料枠のスペック(メモリ0.6G)にビビりすぎでApacheより軽量とされるNginxをインストールすることにしました。そのため、進む場合は「Nginxのインストール」→「MySQLのインストール」で進んでください…
1.Apacheインストール
WebサーバーのソフトウェアのひとつであるApacheをインストールする。
遙かデジタリアへ: RubyのCGIをGoogle Cloud Platformの無料枠で動かしたい ~(1)セットアップ
Apacheをインストール&初期設定 - ゼロからはじめるWEBプログラミング入門
Google Compute Engine を使ってみる(3) http サーバーを公開する #gcloud #gce - jitsu102's blog
Google Cloud Platformで無料Webサーバを作る – mktiaの備忘録
SSH接続して、キャッシュ削除で綺麗にしてから
$ sudo yum clean all
もちろん入ってないよね~の確認
$ httpd -v
-bash: httpd: command not found
Apacheインストール
途中でホンマにインストールするか訊いてくるので「y」と答えつつ、「Complete!」表示されたらOK
Apache起動
$ sudo service httpd start
自分のVMインスタンスの外部IPをブラウザで確認したら下図が表示された。
もうこれだけでうひょー(・∀・)底辺
ちなみにhttps接続はまだできず。SSL設定してないのでそりゃそうだ。
2.Apache初期設定
①Apacheをインストール&初期設定 - ゼロからはじめるWEBプログラミング入門
②Apache:インストール(httpd)、初期設定、その他 - Qiita
Apache設定ファイルhttpd.confのバックアップを作成して、上記リンク①の通り編集。
ちなみにApache設定ファイルhttpd.confはインストールの仕方によって場所は変わるようだよ。
httpd.confについて調べたのでまとめたよ - ✘╹◡╹✘
今回はCentOSでソースから取得したわけじゃないので/etc/httpd/conf/httpd.confです。
$ sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd_bk180406.conf
$ vim /etc/httpd/conf/httpd.conf
▼/etc/httpd/conf.d/autoindex.conf
$ sudo cp /etc/httpd/conf.d/autoindex.conf /etc/httpd/conf.d/autoindex_bk180406.conf
$ sudo vim /etc/httpd/conf.d/autoindex.conf
▼/etc/httpd/conf.d/userdir.conf
$ sudo cp /etc/httpd/conf.d/userdir.conf /etc/httpd/conf.d/userdir_bk180406.conf
$ sudo vim /etc/httpd/conf.d/userdir.conf
設定反映のためのApache再起動
$ sudo systemctl reload httpd
再起動コマンドが他に「service httpd restart」や「/usr/local/apache2/bin/apachectl restart」もあったがCentOS7のサービス制御は「systemctl」を使えとのこと。
CentOS 7.0 では systemctl を使う。 - たこはちの「へのかっぱ」日記
apachectlコマンドとhttpdの違い - 女子WEBエンジニアのTechメモ
あとWEBサービスを公開し始めたら、再起動は「reload」や「restart」より「graceful」の方がいいみたい。しかしhttpd.confの反映は「reload」ですって。
apache再起動にはrestartよりもgracefulを使うべき - 女子WEBエンジニアのTechメモ
遙かデジタリアへ: RubyのCGIをGoogle Cloud Platformの無料枠で動かしたい ~(1)セットアップ
MySQLのインストール
1.MySQLのインストール
MySQL5.6と5.7に悩んだが、違いを見てざっくり言うとユーザのパスワード管理が5.7の方が強固っぽく、パフォーマンスが3倍らしい。しかし長期パスワード変更しないユーザがいると強制的にパスワードを変更させられるらしいので注意が必要。
MySQL 5.7にやられないためにおぼえておいてほしいこと
MySQL5.6と5.7のちょっとした違いとinnodb_thread_concurrencyの影響 - hiroi10の日記
第10回 yum, rpmインストールにおけるMySQL 5.6とMySQL 5.7の違い:MySQL道普請便り|gihyo.jp … 技術評論社
Oracle、5.6より三倍速い「MySQL 5.7.9 GA」をリリース | ソフトアンテナブログ
CentOS7にMySQL5.7をインストール
CentOS 7 に MySQL 5.7 を yum インストールして初期設定までやってみた - enomotodev’s blog
CentOS7.3にMySQL5.7をyumでインストールする - Qiita
MariaDBの削除
CentOS7からデフォルトDBがMariaDBになり、MySQLと競合する可能性があるので削除しておく。(MariaDBの存在確認してから 本体とデータフォルダを削除)
削除するときってドキドキするね…
CentOS7に公式MySQLのリポジトリを追加
$ sudo rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
追加したリポジトリを指定してインストールして確認
$ sudo yum install --enablerepo=mysql57-community mysql-community-server
$ mysqld --version
mysqld Ver 5.7.21 for Linux on x86_64 (MySQL Community Server (GPL))
よしよし入ったよ~
MySQL起動して、自動起動も設定しておく
$ sudo systemctl start mysqld.service
$ sudo systemctl enable mysqld.service
2.MySQLの初期設定
MySQLの初期パスワード設定
MySQL5.7からセキュリティ強固になって、勝手に設定されるので確認しないといけないらしいですわ。
$ sudo cat /var/log/mysqld.log | grep password
2018-04-09T02:35:44.722093Z 1 [Note] A temporary password is generated for root@localhost: 【初期パスワード】
MySQLのセキュリティ設定
対話形式で最低限のセキュリティ設定をしておく。
$ mysql_secure_installation
(略)
Enter password for user root:【先程の初期パスワード入力】
The existing password for the user account root has expired. Please set a new password.
New password:【新しいパスワード入力】
Re-enter new password:【再度新しいパスワード入力】
... Failed! Error: Your password does not satisfy the current policy requirements
(怒られました(・_・;)公式のパスワード要件を満たしていないようです)
New password:【再度要件満たした新しいパスワード入力】
Re-enter new password:【再度新しいパスワード入力】
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) :y(匿名ユーザアカウントanonymous削除)
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? (Press y|Y for Yes, any other key for No) :y(ローカルホスト以外のリモートからアクセス可能なroot削除)
Success.
By default, MySQL 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? (Press y|Y for Yes, any other key for No) :y(テストDB削除)
- Dropping test database...
Success.
- 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? (Press y|Y for Yes, any other key for No) :y(今すぐ特権テーブルリロードする)
Success.
All done!
できた\(^o^)/ちなみに公式のパスワード要件は以下
- 英数字はどちらも1文字以上使用
- 英字は小文字大文字どちらも1文字以上使用
- 特殊文字は1文字以上使用
厳しいよ…
ではもう一度アクセス
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.21 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
うああああああああアクセス出来た嬉しい~~~~~~~
もう初めてアクセス出来たような喜び…会社で何度かアクセスしてたけどもう長らくしてないや…何かDB好きなのよね…その割に勉強進んでないけど…頑張ろう…
ちなみに中身確認
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.22 sec)
あ、こんなけ初期データベースあるのね、ふむふむ。
ここにRailsアプリ用のデータベース追加するよ。後々。
MySQLの設定変更
上述通り、MySQL5.7は長期パスワード変更しないユーザがいると強制的にパスワードを変更させられるらしいのでその設定をOFFする。(長期→360日)
$ sudo cp my.cnf my_bk180409.cnf
$ ls
$ sudo vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
## 追加180409
# 文字コードをutf-8に
character-set-server=utf8
# デフォルトのストレージエンジン
default-storage-engine=InnoDB
## 追加180409
[mysql]
ちなみに
[mysqld]セクションはmysqlサーバーへの設定
[mysql]セクションはmysqlクライアントツールへの設定
とのこと。
基礎MySQL ~その2~ my.cnf (設定ファイル) - Qiita
ストレージエンジンはInnoDBとMyISAMがあって、初心者はInnoDBにしとけばいい…と思う…MySQL5.5以降ではいちいち設定しなくてもデフォルトがInnoDBのようですが。
MySQLの「InnoDB」と「MyISAM」についての易しめな違い - (2015年までの)odaillyjp blog
設定の反映
$ sudo systemctl restart mysqld.service
以上でMySQL5.7の初期設定は完了。
MySQL5.7に拘らなければ以下参考にしても良かったかも。ってか拘る必要もなかったな(・_・;)
Google Cloud Platformで無料Webサーバを作る – mktiaの備忘録
【クリックとコピペだけでできる】Google Cloud Platform上にWebサーバー(Nginx)を構築してWordPressを動かす【CentOS】 - Qiita
途中で気づいた話
次にRailsアプリを作りたかったんですが、下記の記事を見て今の環境でいいのかビビってる…OSをUbuntuにすればよかったとかApacheじゃなくてNginxにすればよかったとか…またやり直しになっちゃうので、とりあえずこのまま進めてメモリ不足になったらスワップメモリを追加設定すればいいのかな?あ、でもApacheアンイストールは簡単かな…
[GCE] f1-microでのWordPress運営が重すぎたのでg1-smallにしてみた │ revdev
GCEのf1-microインスタンスがハングアップする話 – きゃわいどっとてっく
[GoogleComputeEngine]GCEにてf1-microを利用してWPを運用した結果(状況、料金等) - Qiita
GCEが重いのでベンチマークしたら驚きの結果に | A-tak-dot-com
Apacheのアンイストール
Apacheのアンイストールしちゃおうか…ごめんなさい。
Apache確認。
$ httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Oct 19 2017 20:39:16
Apacheのアンイストール。
【yum remove】yumで安全にアンインストールする方法 | UX MILK
しちゃった…
MySQLに影響ないかログイン確認。
$ mysql -u root -p
Nginxのインストール
1.Nginxのインストール
CentOS7.1でnginxを用いたウェブサーバの構築 - Qiita
yumリポジトリにNginxを追加して、yumでインストールできるようにする。
$ sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
$ sudo yum -y update nginx-release-centos
$ sudo yum -y --enablerepo=nginx install nginx
Nginxのバージョン確認
$ nginx -v
nginx version: nginx/1.12.2
Nginx公式サイトでStable version(安定版)として1.12.2があるのでOK(18/04現在)
CentOS 7 (5, 6) で "安定版 (最新版)" のNginxをインストールする方法 - Qiita
自動起動設定
$ sudo systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
Nginx起動
$ sudo systemctl start nginx
VMインスタンスの外部アドレスをブラウザで確認したら、下図の表示。\(^o^)/
デフォルトでは/usr/share/nginx/html/index.htmlのファイルが表示されている。
2.Nginxの初期設定
Nginxの設定ファイルをconf.dディレクトリに作成して、Mezzanineを動かしてみる | kazsoga blog
ちなみにNngixの設定ファイルは、/etc/nginx/nginx.confにある。
参考リンクに記述のあった/etc/nginx/conf.d/default.confはなかった。
しかし/etc/nginx/conf.d/以下に○○.confファイルを作成すると、起動時に読み込んでくれる。
【アプリ名】.confファイル(名前何でもいい)を作成して中身を追加。
$ sudo vim /etc/nginx/conf.d/【アプリ名】.conf
server {
listen 80;
server_name 【アクセス可能なIPアドレス、もしくはドメイン】;
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
root /var/www/【アプリ名】/public;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
}
再起動
$ sudo systemctl restart nginx
再度外部アドレスにアクセスすると
そりゃそうだrootを「/var/www/【アプリ名】/public」に変更したから。
これにRailsアプリを入れていこう。
残作業メモ
やりたいことをメモっとく。
必要になれば
- PHPインストール
MySQLと一緒に使うことが多いPHPを必要になればどっかで入れる。ビルドがf1-microのメモリ0.6GBじゃ耐えられないとか聞くので、とりあえず必要になるまでは放置。
開発一通り終わって公開する前
- ドメイン取得(とりあえず無料ドメイン予定 freenpm.comで取得予定)
- SSL証明書の取得(とりあえず無料のLet's Encrypt予定)
▼参考
GCE の無料枠のサーバを立るときに、初見でハマりそうなところ - Qiita
GCEでWordPressがほぼ無料運用できるようになったので改めてまとめる – codes / cipher
今回はここまで。