CentOS 7 チュートリアル (身内向け)¶
CentOS 7: 初期セットアップ手順¶
準備¶
以下の情報をネットワーク管理者に確認すること。 なお, SSH 鍵のパスフレーズは, 一般ユーザーのパスワードと同一とする。
- ホスト名
- IP アドレス
- ネットマスク
- デフォルトゲートウェイ
- 参照先 DNS サーバー
- root パスワード
- 一般ユーザー名
- 一般ユーザーのパスワード
また, ISO ファイルを書き込むため, USB メモリーの貸与を受けること。
ISO ファイルダウンロード, 書き込み¶
- Web ブラウザーで CentOS Project にアクセスし, Get CentOS Now より DVD ISO をダウンロードするのが正攻法。
- ただ, 回線が遅い場合があるので, ミラーサイト (IIJ) にアクセスし, 最新の NetInstall な ISO ファイルをダウンロードする。(torrent じゃない方。)
- Windows であれば,
Rufus
をダウンロードし, USB メモリーに ISO ファイルを書き込む。
- ただし, 管理者権限が必要。
- なお, ファイル書き込みではなく, イメージ書き込みであることに注意すること。
OS インストール¶
ISO ファイルを書き込んだ USB メモリーを筐体に挿し, 電源を投入する。
F12 キーなどを押下し, USB メモリーからインストーラーを起動する。(メーカー, 機種によって押下するキーは異なる。)
Install CentOS 7 を選択する。
検索欄に
jap
と入力し, 日本語 (日本) を選択, 続行 ボタンを押下する。ネットワークとホスト名 をクリックする。
- 画面左下の ホスト名 欄にホスト名を入力し, 適用
ボタンを押下, そのまま画面右側の 設定 ボタンを押下する。
- 全般 タブを開き,
この接続が利用可能になったときは自動的に接続する
にチェックを入れる。
- IPv4 のセッティング タブを開き, 方式 より 手動 を選択,
追加 ボタンを押下し, アドレス, ネットマスク, ゲートウェイ,
DNS サーバーを入力, 保存 ボタンを押下する。
- なお, インターネット接続用 NIC が別に存在する場合は, アドレスとネットマスクのみで OK.
- 画面右上のスライドが オン に変更されることを確認し, 画面左上の
完了 ボタンを押下する。
KDUMP をクリックする。
- kdump を有効にする のチェックを 外し, 完了
ボタンを押下する。
インストール先 をクリックする。
- インストール先のディスクを選択し, 完了 ボタンを押下する。
- なお, 既存の領域は全て削除するものとする。
日付と時刻 をクリックする。
- 画面右上の 歯車 ボタンを押下する。
- 稼働中 に緑のチェックが付いていることを確認し, OK
ボタンを押下する。なお, どれか一つに付いていれば OK.
- 画面左上の 完了 ボタンを押下する。
インストールソース をクリックする。
- インストールソースに
ftp.iij.ad.jp/pub/linux/centos/7/os/x86_64
と入力する。 - 追加のリポジトリー の プラス (+) ボタンを押下し, 名前に
updates
, URL にftp.iij.ad.jp/pub/linux/centos/7/updates/x86_64
と入力する。- なお, リポジトリー名が重複する場合は,
update2
など, 適当な数字を付与する。
- なお, リポジトリー名が重複する場合は,
- 更に 追加のリポジトリー の プラス (+) ボタンを押下し,
名前に
extras
, URL にftp.iij.ad.jp/pub/linux/centos/7/extras/x86_64
と入力する。- 先ほどと同様, リポジトリー名が重複する場合は,
extras2
など, 適当な数字を付与する。
- 先ほどと同様, リポジトリー名が重複する場合は,
- 画面左上の 完了 ボタンを押下する。
ソフトウェアの選択 をクリックする。
- 最小限のインストール が選択されていることを確認し, 画面左上の
完了 ボタンを押下する。
インストールの開始 ボタンを押下する。
ROOT パスワード をクリックする。
- root パスワードを入力し, 完了 ボタンを押下する。
インストールが完了するまで待機する。
インストール完了後, 再起動 ボタンを押下する。
OS が起動し, ログインプロンプトが表示されることを確認する。
最初に挿入した USB メモリーを抜く。
root ログイン¶
- 自端末の Tera Term を起動する。
- ホスト 欄に CentOS の IP アドレスを入力し, OK
ボタンを押下する。
- 初回接続時のみ, セキュリティ警告が表示されるが, そのまま 続行
ボタンを押下する。
- ユーザ名 に
root
, パスフレーズに root のパスワードを入力し, OK ボタンを押下する。 - root ユーザーのプロンプトが表示されることを確認する。
Tera Term ログ取得¶
- Tera Term の ファイル - ログ をクリックする。
- ログを保存するフォルダーを開き, ファイル名を指定, 保存
ボタンを押下する。
- 保存先に困った場合, フォルダーは ドキュメント, ファイル名は
YYYYMMDD_teraterm.log
とする。 - ついでに, 追記, プレーンテキスト, タイムスタンプ にチェックを入れておく。
- 保存先に困った場合, フォルダーは ドキュメント, ファイル名は
- root ユーザーのプロンプト上で Enter キーを何回か空打ちしてから,
ファイル - ログを表示 をクリック,
ログが記録されていることを確認する。
- 確認後はログを閉じること。
一般ユーザー作成¶
一般ユーザーを作成する。
# useradd -G wheel -N ryo-sato
一般ユーザーにパスワードを設定する。なお, 入力中のパスワードは表示されない ので注意する。
# passwd ryo-sato
ユーザー ryo-sato のパスワードを変更。 新しいパスワード: ## パスワードを入力する。 新しいパスワードを再入力してください: ## パスワードを入力する。 passwd: すべての認証トークンが正しく更新できました。
一般ユーザーが作成されたことを確認する: no such user と表示されなければ OK.
# id ryo-sato
uid=1000(ryo-sato) gid=100(users) groups=100(users),10(wheel)
ログアウトする。
# exit
一般ユーザーでログインする。
SSH 鍵 (秘密鍵・公開鍵) 生成¶
SSH 鍵を生成する。
$ ssh-keygen -t ed25519
Generating public/private ed25519 key pair. Enter file in which to save the key (/home/ryo-sato/.ssh/id_ed25519): ## ココはそのまま Enter. Created directory '/home/ryo-sato/.ssh'. Enter passphrase (empty for no passphrase): ## パスフレーズを入力する。 Enter same passphrase again: ## パスフレーズを入力する。 Your identification has been saved in /home/ryo-sato/.ssh/id_ed25519. Your public key has been saved in /home/ryo-sato/.ssh/id_ed25519.pub. The key fingerprint is: SHA256:PIUA2ZXwuFUhnQ54Y7kORVxBM2G1hQ11lZ7yfGCIm1Q ryo-sato@centos7 The key's randomart image is: +--[ED25519 256]--+ | .+o*+*@*o=o =| | . o+X=ooEo.o | | .++=.o... .| | .+..+ ..oo | | .oS. o .+. | | ..o o.| | .| | | | | +----[SHA256]-----+
公開鍵登録¶
公開鍵を登録する。
$ ssh-copy-id localhost
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/ryo-sato/.ssh/id_ed25519.pub" The authenticity of host 'localhost (::1)' can't be established. ECDSA key fingerprint is SHA256:QpFLRM8JHPy5gVhjyPhIwaM+iAcWY/tzd0zK2+7We8M. ECDSA key fingerprint is MD5:26:ec:2f:aa:0c:09:47:91:a1:e9:af:6e:91:e0:03:e0. Are you sure you want to continue connecting (yes/no)? ## yes と入力する。 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys ryo-sato@localhost's password: ## パスワードを入力する。 Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'localhost'" and check to make sure that only the key(s) you wanted were added.
Tera Term の ファイル - SSH SCP より, 秘密鍵をダウンロードする。
- ポップアップ下部の From: 欄に .ssh/id_ed25519 と入力する。
- 同様に To: 欄右の … ボタンを押下し, ダウンロード先フォルダを指定する。
- Receive ボタンを押下する。
- ダウンロードした秘密鍵は, ドキュメントフォルダーに
ssh鍵
とサブフォルダーを作って保管しておくこと。
Tera Term の ファイル - 新しい接続 より, 秘密鍵でログインできることを確認する。
sshd 設定変更¶
作業ディレクトリーを変更する。
$ cd /etc/ssh; pwd
設定ファイルをバックアップする。
$ sudo cp -aiv sshd_config sshd_config.`date +%Y%m%d`
設定ファイルがバックアップされたことを確認する: パーミッション, オーナー, グループ, タイムスタンプ, コンテキストが同一であれば OK.
$ ls -l sshd_config sshd_config.`date +%Y%m%d`
-rw-------. 1 root root 3907 10月 20 06:52 sshd_config -rw-------. 1 root root 3907 10月 20 06:52 sshd_config.20180410
$ ls -Z sshd_config sshd_config.`date +%Y%m%d`
-rw-------. root root system_u:object_r:etc_t:s0 sshd_config -rw-------. root root system_u:object_r:etc_t:s0 sshd_config.20180410
設定ファイルを編集する。
$ sudo vi sshd_config
- vi の使い方は調べること。(参考: vi 使い方 - Google 検索)
差分を確認する。
$ sudo diff -u sshd_config.`date +%Y%m%d` sshd_config
--- sshd_config.20180410 2017-10-20 06:52:25.000000000 +0900 +++ sshd_config 2018-04-10 14:24:08.699437905 +0900 @@ -35,7 +35,7 @@ # Authentication: #LoginGraceTime 2m -#PermitRootLogin yes +PermitRootLogin no #StrictModes yes #MaxAuthTries 6 #MaxSessions 10 @@ -60,9 +60,8 @@ #IgnoreRhosts yes # To disable tunneled clear text passwords, change to no here! -#PasswordAuthentication yes -#PermitEmptyPasswords no -PasswordAuthentication yes +PasswordAuthentication no +PermitEmptyPasswords no # Change to no to disable s/key passwords #ChallengeResponseAuthentication yes @@ -98,7 +97,7 @@ #AllowAgentForwarding yes #AllowTcpForwarding yes #GatewayPorts no -X11Forwarding yes +X11Forwarding no #X11DisplayOffset 10 #X11UseLocalhost yes #PermitTTY yes @@ -112,7 +111,7 @@ #ClientAliveInterval 0 #ClientAliveCountMax 3 #ShowPatchLevel no -#UseDNS yes +UseDNS no #PidFile /var/run/sshd.pid #MaxStartups 10:30:100 #PermitTunnel no
現在のサービスの状態を確認する: active (running) であれば OK.
$ sudo systemctl status sshd -l
● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since 火 2018-04-10 13:36:17 JST; 48min ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 1016 (sshd) CGroup: /system.slice/sshd.service mq1016 /usr/sbin/sshd -D 4月 10 13:36:17 centos7 systemd[1]: Started OpenSSH server daemon. 4月 10 13:48:53 centos7 sshd[1352]: Accepted password for root from 192.168.56.1 port 53782 ssh2 4月 10 14:04:14 centos7 sshd[1463]: Accepted password for ryo-sato from 192.168.56.1 port 53932 ssh2 4月 10 14:12:47 centos7 sshd[1515]: Connection closed by ::1 port 36190 [preauth] 4月 10 14:12:47 centos7 sshd[1523]: Connection closed by ::1 port 36192 [preauth] 4月 10 14:12:51 centos7 sshd[1534]: Accepted password for ryo-sato from ::1 port 36194 ssh2 4月 10 14:16:00 centos7 sshd[1573]: Accepted publickey for ryo-sato from 192.168.56.1 port 53977 ssh2: ED25519 SHA256:PIUA2ZXwuFUhnQ54Y7kORVxBM2G1hQ11lZ7yfGCIm1Q 4月 10 14:20:37 centos7 sshd[1598]: Received disconnect from 192.168.56.1 port 53999:11: authentication cancelled [preauth] 4月 10 14:20:37 centos7 sshd[1598]: Disconnected from 192.168.56.1 port 53999 [preauth] 4月 10 14:20:59 centos7 sshd[1600]: Accepted publickey for ryo-sato from 192.168.56.1 port 54000 ssh2: ED25519 SHA256:PIUA2ZXwuFUhnQ54Y7kORVxBM2G1hQ11lZ7yfGCIm1Q
サービスを再起動する。
$ sudo systemctl condrestart sshd
サービスが再起動されたことを確認する: active (running) であれば OK.
$ sudo systemctl status sshd -l
● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since 火 2018-04-10 14:25:46 JST; 4s ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 1652 (sshd) CGroup: /system.slice/sshd.service mq1652 /usr/sbin/sshd -D 4月 10 14:25:46 centos7 systemd[1]: Starting OpenSSH server daemon... 4月 10 14:25:46 centos7 sshd[1652]: Server listening on 0.0.0.0 port 22. 4月 10 14:25:46 centos7 sshd[1652]: Server listening on :: port 22. 4月 10 14:25:46 centos7 systemd[1]: Started OpenSSH server daemon.
Tera Term の ファイル - 新しい接続 より, パスワード認証でログインでき ない ことを確認する。
同様に, Tera Term の ファイル - 新しい接続 より, root ユーザーでログインでき ない ことを確認する。
どっとはらい。
CentOS 7: LAMP セットアップ手順¶
参考文献¶
パッケージインストール¶
- yum でパッケージをインストールする。
$ sudo yum -y install php php-mysqlnd mariadb-server
MariaDB¶
文字コード設定¶
- 作業ディレクトリーを変更する。
$ cd /etc/my.cnf.d; pwd
client.cnf¶
設定ファイルをバックアップする。
$ sudo cp -aiv client.cnf client.cnf.`date +%Y%m%d`
設定ファイルがバックアップされたことを確認する。
$ ls -l client.cnf client.cnf.`date +%Y%m%d`
-rw-r--r--. 1 root root 295 4月 30 2017 client.cnf -rw-r--r--. 1 root root 295 4月 30 2017 client.cnf.20180409
$ ls -Z client.cnf client.cnf.`date +%Y%m%d`
-rw-r--r--. root root system_u:object_r:mysqld_etc_t:s0 client.cnf -rw-r--r--. root root system_u:object_r:mysqld_etc_t:s0 client.cnf.20180409
設定ファイルを編集する。
$ sudo vi client.cnf
差分を確認する。
$ diff -u client.cnf.`date +%Y%m%d` client.cnf
--- client.cnf.20180409 2017-04-30 20:09:34.000000000 +0900 +++ client.cnf 2018-04-09 16:08:00.813968051 +0900 @@ -5,6 +5,7 @@ [client] +default-character-set=utf8mb4 # This group is not read by mysql client library, # If you use the same .cnf file for MySQL and MariaDB,
mysql-clients.cnf¶
設定ファイルをバックアップする。
$ sudo cp -aiv mysql-clients.cnf mysql-clients.cnf.`date +%Y%m%d`
設定ファイルがバックアップされたことを確認する。
$ ls -l mysql-clients.cnf mysql-clients.cnf.`date +%Y%m%d`
-rw-r--r--. 1 root root 232 4月 30 2017 mysql-clients.cnf -rw-r--r--. 1 root root 232 4月 30 2017 mysql-clients.cnf.20180409
$ ls -Z mysql-clients.cnf mysql-clients.cnf.`date +%Y%m%d`
-rw-r--r--. root root system_u:object_r:mysqld_etc_t:s0 mysql-clients.cnf -rw-r--r--. root root system_u:object_r:mysqld_etc_t:s0 mysql-clients.cnf.20180409
設定ファイルを編集する。
$ sudo vi mysql-clients.cnf
差分を確認する。
$ diff -u mysql-clients.cnf.`date +%Y%m%d` mysql-clients.cnf
--- mysql-clients.cnf.20180409 2017-04-30 20:09:34.000000000 +0900 +++ mysql-clients.cnf 2018-04-09 16:08:38.159235163 +0900 @@ -4,6 +4,7 @@ # [mysql] +default-character-set=utf8mb4 [mysql_upgrade]
server.cnf¶
設定ファイルをバックアップする。
$ sudo cp -aiv server.cnf server.cnf.`date +%Y%m%d`
設定ファイルがバックアップされたことを確認する。
$ ls -l server.cnf server.cnf.`date +%Y%m%d`
-rw-r--r--. 1 root root 744 4月 30 2017 server.cnf -rw-r--r--. 1 root root 744 4月 30 2017 server.cnf.20180409
$ ls -Z server.cnf server.cnf.`date +%Y%m%d`
-rw-r--r--. root root system_u:object_r:mysqld_etc_t:s0 server.cnf -rw-r--r--. root root system_u:object_r:mysqld_etc_t:s0 server.cnf.20180409
設定ファイルを編集する。
$ sudo vi server.cnf
差分を確認する。
$ diff -u server.cnf.`date +%Y%m%d` server.cnf
--- server.cnf.20180409 2017-04-30 20:09:34.000000000 +0900 +++ server.cnf 2018-04-09 16:09:33.687632330 +0900 @@ -10,6 +10,9 @@ # this is only for the mysqld standalone daemon [mysqld] +collation-server = utf8mb4_unicode_ci +init-connect='SET NAMES utf8mb4' +character-set-server = utf8mb4 # this is only for embedded server [embedded]
自動起動有効化, サービス起動¶
現在のサービスの状態を確認する。
$ sudo systemctl status mariadb -l
● mariadb.service - MariaDB database server Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled) Active: inactive (dead)
自動起動を有効化する。
$ sudo systemctl enable mariadb
サービスを起動する。
$ sudo systemctl start mariadb
サービスが起動されたことを確認する。
$ sudo systemctl status mariadb -l
● mariadb.service - MariaDB database server Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled) Active: active (running) since 月 2018-04-09 16:13:24 JST; 24s ago Process: 1882 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS) Process: 1803 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS) Main PID: 1881 (mysqld_safe) CGroup: /system.slice/mariadb.service tq1881 /bin/sh /usr/bin/mysqld_safe --basedir=/usr mq2103 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock 4月 09 16:13:21 centos7 mariadb-prepare-db-dir[1803]: MySQL manual for more instructions. 4月 09 16:13:21 centos7 mariadb-prepare-db-dir[1803]: Please report any problems at http://mariadb.org/jira 4月 09 16:13:21 centos7 mariadb-prepare-db-dir[1803]: The latest information about MariaDB is available at http://mariadb.org/. 4月 09 16:13:21 centos7 mariadb-prepare-db-dir[1803]: You can find additional information about the MySQL part at: 4月 09 16:13:21 centos7 mariadb-prepare-db-dir[1803]: http://dev.mysql.com 4月 09 16:13:21 centos7 mariadb-prepare-db-dir[1803]: Consider joining MariaDB's strong and vibrant community: 4月 09 16:13:21 centos7 mariadb-prepare-db-dir[1803]: https://mariadb.org/get-involved/ 4月 09 16:13:22 centos7 mysqld_safe[1881]: 180409 16:13:22 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'. 4月 09 16:13:22 centos7 mysqld_safe[1881]: 180409 16:13:22 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 4月 09 16:13:24 centos7 systemd[1]: Started MariaDB database server.
初期設定¶
初期設定を開始する。
$ sudo mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.
MariaDB 用の root パスワードを設定する。
Set root password? [Y/n] New password: Re-enter new password:
Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB 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? [Y/n]
... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
root の遠隔ログインを無効化する。
Disallow root login remotely? [Y/n]
... Success! By default, MariaDB 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? [Y/n]
- 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? [Y/n]
... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
root ログイン¶
mysql クライアントを起動する。
$ mysql -u root -p
root パスワードを入力する。
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 10 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
文字コード確認: utf8mb4 なので OK.¶
文字コードを確認する。
> show variables like 'char%';
character_set_system は utf8 のままで OK.
+--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8mb4 | | character_set_connection | utf8mb4 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | utf8mb4 | | character_set_server | utf8mb4 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)
データベース作成¶
目的のデータベースが存在しないことを確認する。
> show databases;
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec)
データベースを作成する。
> CREATE DATABASE db01;
Query OK, 1 row affected (0.00 sec)
データベースが作成されたことを確認する。
> show databases;
+--------------------+ | Database | +--------------------+ | information_schema | | db01 | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.00 sec)
一般ユーザー作成¶
目的の一般ユーザーが存在しないことを確認する。
> SELECT host,user FROM mysql.user;
+-----------+------+ | host | user | +-----------+------+ | 127.0.0.1 | root | | ::1 | root | | localhost | root | +-----------+------+ 3 rows in set (0.00 sec)
一般ユーザーを作成する。
> GRANT ALL ON db01.* TO user01@localhost IDENTIFIED BY '********';
Query OK, 0 rows affected (0.00 sec)
一般ユーザーが作成されたことを確認する。
> SELECT host,user FROM mysql.user;
+-----------+--------+ | host | user | +-----------+--------+ | 127.0.0.1 | root | | ::1 | root | | localhost | root | | localhost | user01 | +-----------+--------+ 4 rows in set (0.00 sec)
一般ユーザーログイン¶
一旦 MariaDB クライアントからログアウトする。
> exit
一般ユーザーでログインする。
$ mysql -u user01 -p db01
一般ユーザーのパスワードを入力する。
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 11 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [db01]>
データベース文字コード確認: utf8mb4 なので OK.¶
データベースの文字コードを確認する。
> show variables like 'char%';
+--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8mb4 | | character_set_connection | utf8mb4 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | utf8mb4 | | character_set_server | utf8mb4 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)
テーブル作成¶
テーブルを作成する。
> CREATE TABLE address_book ( -> id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, -> first_name VARCHAR(30) NOT NULL, -> last_name VARCHAR(30) NOT NULL, -> email VARCHAR(50), -> reg_date TIMESTAMP -> );
テーブルが作成されたことを確認する。
> show CREATE TABLE address_book;
+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | address_book | CREATE TABLE `address_book` ( `id` int(6) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, `last_name` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, `email` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `reg_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci | +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
ログアウト¶
- ログアウトする。
> exit
Apache HTTP Server¶
サーバー名設定¶
作業ディレクトリーを変更する。
$ cd /etc/httpd/conf; pwd
設定ファイルをバックアップする。
$ sudo cp -aiv httpd.conf httpd.conf.`date +%Y%m%d`
設定ファイルがバックアップされたことを確認する。
$ ls -l httpd.conf httpd.conf.`date +%Y%m%d`
-rw-r--r--. 1 root root 11753 10月 20 01:44 httpd.conf -rw-r--r--. 1 root root 11753 10月 20 01:44 httpd.conf.20180409
$ ls -Z httpd.conf httpd.conf.`date +%Y%m%d`
-rw-r--r--. root root system_u:object_r:httpd_config_t:s0 httpd.conf -rw-r--r--. root root system_u:object_r:httpd_config_t:s0 httpd.conf.20180409
設定ファイルを編集する。
$ sudo vi httpd.conf
差分を確認する。
$ diff -u httpd.conf.`date +%Y%m%d` httpd.conf
--- httpd.conf.20180409 2017-10-20 01:44:27.000000000 +0900 +++ httpd.conf 2018-04-09 16:32:10.296471166 +0900 @@ -92,7 +92,7 @@ # # If your host doesn't have a registered DNS name, enter its IP address here. # -#ServerName www.example.com:80 +ServerName centos7.localdomain:80 # # Deny access to the entirety of your server's filesystem. You must
初期コンテンツ作成¶
作業ディレクトリーを変更する。
$ cd /var/www/html; pwd
チュートリアル用ディレクトリーを作成する。
$ sudo mkdir -v tutorial
ディレクトリーのオーナーを変更する。
$ sudo chown -v `whoami` tutorial
ディレクトリーの状態を確認する。
$ ls -ld tutorial
drwxr-xr-x. 2 ryo-sato root 6 4月 9 16:58 tutorial
$ ls -dZ tutorial
drwxr-xr-x. ryo-sato root unconfined_u:object_r:httpd_sys_content_t:s0 tutorial
作業ディレクトリーを変更する。
$ cd tutorial; pwd
PHP ファイルを作成する。
$ echo '<?php phpinfo(); ?>' > index.php
PHP ファイルの状態を確認する。
$ ls -l index.php
-rw-r--r--. 1 ryo-sato users 20 4月 9 17:01 index.php
$ ls -Z index.php
-rw-r--r--. ryo-sato users unconfined_u:object_r:httpd_sys_content_t:s0 index.php
$ cat index.php
<?php phpinfo(); ?>
自動起動有効化, サービス起動¶
現在のサービスの状態を確認する。
$ sudo systemctl status httpd -l
● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: inactive (dead) Docs: man:httpd(8) man:apachectl(8)
自動起動を有効化する。
$ sudo systemctl enable httpd
サービスを起動する。
$ sudo systemctl start httpd
サービスが起動されたことを確認する。
$ sudo systemctl status httpd -l
● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) Active: active (running) since 月 2018-04-09 16:34:40 JST; 4s ago Docs: man:httpd(8) man:apachectl(8) Main PID: 5813 (httpd) Status: "Processing requests..." CGroup: /system.slice/httpd.service tq5813 /usr/sbin/httpd -DFOREGROUND tq5814 /usr/sbin/httpd -DFOREGROUND tq5815 /usr/sbin/httpd -DFOREGROUND tq5816 /usr/sbin/httpd -DFOREGROUND tq5817 /usr/sbin/httpd -DFOREGROUND mq5818 /usr/sbin/httpd -DFOREGROUND 4月 09 16:34:40 centos7 systemd[1]: Starting The Apache HTTP Server... 4月 09 16:34:40 centos7 systemd[1]: Started The Apache HTTP Server.
パケットフィルタリング設定¶
現在許可されているサービスを確認する。
$ sudo firewall-cmd --list-services
ssh dhcpv6-client
http を許可する。
$ sudo firewall-cmd --permanent --add-service=http
success
現在の firewalld の状態を確認する。
$ sudo systemctl status firewalld -l
firewalld を再起動する。
$ sudo systemctl condrestart firewalld
firewalld が再起動されたことを確認する。
$ sudo systemctl status firewalld -l
http が許可されたことを確認する。
$ sudo firewall-cmd --list-services
ssh dhcpv6-client http
http://<IP アドレス>/tutorial/
にアクセスし, phpinfo が表示されることを確認する。- IP アドレスは
$ ip addr show
コマンドで確認できる。 - 例えば IP アドレスが 192.168.56.126 の場合, URL は
http://192.168.56.126/tutorial/
となる。
- IP アドレスは
データ登録用コンテンツ作成¶
データ登録用コンテンツを作成する。
$ vi insert.php
PHP ファイルの状態を確認する。
$ ls -l insert.php
-rw-r--r--. 1 ryo-sato users 628 4月 9 17:02 insert.php
$ ls -Z insert.php
-rw-r--r--. ryo-sato users unconfined_u:object_r:httpd_sys_content_t:s0 insert.php
$ cat insert.php
<?php $servername = "localhost"; $username = "user01"; $password = "********"; $dbname = "db01"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "INSERT INTO address_book (first_name, last_name, email) VALUES ('John', 'Doe', 'john@example.com')"; if ($conn->query($sql) === TRUE) { $last_id = $conn->insert_id; echo "New record created successfully. Last inserted ID is: " . $last_id; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>
http://<IP アドレス>/tutorial/insert.php
にアクセスすると, 以下のメッセージが表示される。New record created successfully. Last inserted ID is: 1
データ登録確認¶
MariaDB に接続する。
$ mysql -u user01 -p db01
先ほど登録したデータが存在することを確認する。
> SELECT * FROM address_book;
+----+------------+-----------+------------------+---------------------+ | id | first_name | last_name | email | reg_date | +----+------------+-----------+------------------+---------------------+ | 1 | John | Doe | john@example.com | 2018-04-09 16:47:52 | +----+------------+-----------+------------------+---------------------+ 1 row in set (0.00 sec)
ログアウトする。
> exit
データ参照用コンテンツ作成¶
データ参照用コンテンツを作成する。
$ vi select.php
PHP ファイルの状態を確認する。
$ ls -l select.php
-rw-r--r--. 1 ryo-sato users 644 4月 9 17:05 select.php
$ ls -Z select.php
-rw-r--r--. ryo-sato users unconfined_u:object_r:httpd_sys_content_t:s0 select.php
$ cat select.php
<?php $servername = "localhost"; $username = "user01"; $password = "********"; $dbname = "db01"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT id, first_name, last_name FROM address_book"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["first_name"]. " " . $row["last_name"]. "<br>"; } } else { echo "0 results"; } $conn->close(); ?>
http://<IP アドレス>/tutorial/select.php
にアクセスすると, 以下のメッセージが表示される。id: 1 - Name: John Doe
登録用コンテンツ修正¶
登録用コンテンツをコピーする。
$ cp -aiv insert.php insert2.php
$ ls -l insert.php insert2.php
-rw-r--r--. 1 ryo-sato users 628 4月 9 17:02 insert.php -rw-r--r--. 1 ryo-sato users 628 4月 9 17:02 insert2.php
$ ls -Z insert.php insert2.php
-rw-r--r--. ryo-sato users unconfined_u:object_r:httpd_sys_content_t:s0 insert.php -rw-r--r--. ryo-sato users unconfined_u:object_r:httpd_sys_content_t:s0 insert2.php
VALUES 行の内容を修正する。(データは適当です。)
$ vi insert2.php
$ cat insert2.php
<?php $servername = "localhost"; $username = "user01"; $password = "********"; $dbname = "db01"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "INSERT INTO address_book (first_name, last_name, email) VALUES ('霖之助', '森近', 'rinnosuke@example.com')"; if ($conn->query($sql) === TRUE) { $last_id = $conn->insert_id; echo "New record created successfully. Last inserted ID is: " . $last_id; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>
http://<IP アドレス>/tutorial/insert2.php
にアクセスすると, 以下のメッセージが表示される。New record created successfully. Last inserted ID is: 2
その後,
http://<IP アドレス>/tutorial/select.php
にアクセスすると, 以下のメッセージが表示される。id: 1 - Name: John Doe id: 2 - Name: 霖之助 森近
どっとはらい。
C コンパイル環境セットアップ手順¶
パッケージインストール¶
C のパッケージをインストールする。
$ sudo yum -y install gcc vim-enhanced emacs-nox
読み込んだプラグイン:fastestmirror Loading mirror speeds from cached hostfile (中略) 完了しました!
インストールしたコンパイラーのバージョンが表示されることを確認する。
$ gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16) Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
作業用ディレクトリー作成¶
作業用のディレクトリーを作成する。
$ mkdir -v ~/clang
mkdir: ディレクトリ `/home/ryo-sato/clang' を作成しました
作業用ディレクトリーに移動する。
$ cd ~/clang; pwd
/home/ryo-sato/clang
C プログラム作成¶
プログラムを作成する。
$ vim helloworld.c
もしくは$ emacs helloworld.c
#include <stdio.h> int main() { /* printf() displays the string inside quotation */ printf("Hello, World!"); return 0; }
コンパイル, 実行¶
プログラムをコンパイルする。
$ gcc -Wall -pedantic helloworld.c
-Wall -pedantic
はエラーだけでなく警告も表示させるオプション。
プログラムを実行する。
$ ./a.out
Hello, World!
どっとはらい。
Java コンパイル環境セットアップ手順¶
参考文献¶
パッケージインストール¶
Java のパッケージをインストールする。
$ sudo yum -y install java-1.8.0-openjdk-devel vim-enhanced emacs-nox
読み込んだプラグイン:fastestmirror Loading mirror speeds from cached hostfile (中略) 完了しました!
インストールしたコンパイラーのバージョンが表示されることを確認する。
$ javac -version
javac 1.8.0_161
作業用ディレクトリー作成¶
作業用のディレクトリーを作成する。
$ mkdir -v ~/java
mkdir: ディレクトリ `/home/ryo-sato/java' を作成しました
作業用ディレクトリーに移動する。
$ cd ~/java; pwd
/home/ryo-sato/java
Java プログラム作成¶
プログラムを作成する。
$ vim HelloWorld.java
もしくは$ emacs HelloWorld.java
public class HelloWorld { public static void main(String[] args){ // Prints "Hello, World" to the terminal window. System.out.println("Hello, World"); } }
コンパイル, 実行¶
プログラムをコンパイルする。
$ javac -Xlint:all HelloWorld.java
-Xlint:all
はエラーだけでなく警告も表示させるオプション。
プログラムを実行する。
$ java HelloWorld
実行時はファイルの拡張子:
.class
を付けないのがポイント。Hello, World
どっとはらい。