├── .gitignore ├── README.md ├── Step0.md ├── Step1.md ├── Step2.md ├── Step3.md ├── Step4.md └── images ├── step1 └── virtualbox-1.png ├── step2 ├── httpd-1.png ├── wordpress-1.png ├── wordpress-2.png ├── wordpress-3.png ├── wordpress-4.png ├── wordpress-5.png ├── wordpress-6.png ├── wordpress-7.png ├── wordpress-8.png └── wordpress-9.png └── step3 ├── docker-wordpress-1.png ├── error-wordpress-1.png └── error-wordpress-2.png /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Local-Development-Environment-Hands-On 2 | 3 | ## 概要 4 | ローカルPCにVirtualBox、Vagrantを用いて基本的なLAMP開発環境を構築し、その環境からDockerコンテナを立ち上げてVirtualBox、Vagrant、Dockerの基本的な扱い方を学びます。 5 | 6 | [Step1](https://github.com/hironomiu/Local-Development-Environment-Hands-On/blob/master/Step1.md) 7 | [Step2](https://github.com/hironomiu/Local-Development-Environment-Hands-On/blob/master/Step2.md) 8 | [Step3](https://github.com/hironomiu/Local-Development-Environment-Hands-On/blob/master/Step3.md) 9 | [Step4](https://github.com/hironomiu/Local-Development-Environment-Hands-On/blob/master/Step4.md) 10 | 11 | ## 前提環境 12 | 13 | **VirtualBox、Vagrantがインストールされていること** 14 | 15 | [VirtualBox公式](https://www.virtualbox.org/) 16 | 17 | [Vagrant公式](https://www.vagrantup.com/) 18 | 19 | SSHターミナルが利用できること 20 | 21 | **重要 上記環境を整え、事前に[Step0](https://github.com/hironomiu/Local-Development-Environment-Hands-On/blob/master/Step0.md)を終えること** 22 | 23 | ## 前提知識 24 | 一般的なLinuxコマンド(ディレクトリ遷移、ファイル移動、ファイル編集)が扱えること。ハンズオン中ファイル編集はvimにて実演するため自身が扱うファイル編集方法に読み替えられること 25 | 26 | ## 表記、用語について 27 | - GUEST(ゲスト) OSは仮想サーバと同じ意味です 28 | - HOST(ホスト) OSは持参したPC(のターミナル)を指します   29 | - 「#」はGUEST OS中でのrootユーザでのオペレーションを指します 30 | - 「$」はHOST OSと表記が無い限りはGUEST OS vagrantユーザでのオペレーションを指します 31 | - インラインコード中、行の先頭に「+」「-」がある場合「+」は以降にある文字列を追加、「-」は存在する文字列を消去と読み取ってください 32 | -------------------------------------------------------------------------------- /Step0.md: -------------------------------------------------------------------------------- 1 | # Step0 2 | 事前確認。このStepでは以降のStepを行うための準備と確認を行います。 3 | 4 | ## VirtualBox、Vagrantを以下の公式サイトからインストールしましょう 5 | 6 | [VirtualBox公式](https://www.virtualbox.org/) 7 | 8 | [Vagrant公式](https://www.vagrantup.com/) 9 | 10 | ## 事前確認 11 | 任意のディレクトリで以下のコマンどを実行しエラーとならず`Vagrantfile`が作成されていること 12 | 13 | ``` 14 | $ vagrant init 15 | $ ls -la 16 | -rw-r--r-- 1 xxxx staff 3011 7 9 16:02 Vagrantfile 17 | ``` 18 | 19 | Vagrantfileを以下に書き換える 20 | ``` 21 | # -*- mode: ruby -*- 22 | # vi: set ft=ruby : 23 | 24 | Vagrant.configure("2") do |config| 25 | config.vm.box = "centos/8" 26 | config.vm.hostname = "test.local" 27 | config.vm.network :private_network, ip: "192.168.56.60" 28 | config.vm.provider :virtualbox do |vb| 29 | vb.name = "test" 30 | vb.customize ["modifyvm", :id, "--memory", "768"] 31 | end 32 | end 33 | ``` 34 | 35 | 書き換えの確認 36 | ``` 37 | $ cat Vagrantfile 38 | # -*- mode: ruby -*- 39 | # vi: set ft=ruby : 40 | 41 | Vagrant.configure("2") do |config| 42 | config.vm.box = "centos/8" 43 | config.vm.hostname = "test.local" 44 | config.vm.network :private_network, ip: "192.168.56.60" 45 | config.vm.provider :virtualbox do |vb| 46 | vb.name = "test" 47 | vb.customize ["modifyvm", :id, "--memory", "768"] 48 | end 49 | end 50 | ``` 51 | 52 | 実行しエラーとならずssh接続できること 53 | ``` 54 | $ vagrant up 55 | 56 | ==> default: Setting hostname... 57 | ==> default: Configuring and enabling network interfaces... 58 | ==> default: Rsyncing folder: /test/ => /vagrant 59 | 60 | $ vagrant ssh 61 | [vagrant@test ~]$ exit 62 | 63 | $ vagrant destroy 64 | default: Are you sure you want to destroy the 'default' VM? [y/N] y 65 | ==> default: Destroying VM and associated drives... 66 | ``` 67 | 68 | ここまででStep0は完了です。 69 | -------------------------------------------------------------------------------- /Step1.md: -------------------------------------------------------------------------------- 1 | # Step1 2 | このStepではVagrant、VirtualBoxを用いて仮装環境を構築します 3 | 4 | ## ハンズオン用ディレクトリの作成 5 | HOST OS上の任意のディレクトリで`1day`ディレクトリを作成し、遷移しましょう。以降`1day`ディレクトリをカレントディレクトリとします。 6 | 7 | ``` 8 | $ mkdir 1day 9 | $ cd 1day 10 | ``` 11 | 12 | ## Vagrant設定ファイル(Vagrantfile)の作成 13 | `Vagrantfile`が作成されること 14 | 15 | ``` 16 | $ vagrant init 17 | $ ls -la 18 | -rw-r--r-- 1 miurahironori staff 3008 9 7 17:24 Vagrantfile 19 | ``` 20 | 21 | ## boxファイルの配布 22 | インターネットからDLするとそれなりのサイズのため、ネットワーク環境に不安がある場合に限り、事前に渡したUSBの中にある `centos/8`ディレクトリを指定したディレクトリにコピーしましょう 23 | 24 | `~/.vagrant.d/boxes` ボックスファイルが格納されるディレクトリ   25 | `vagrant box list` ローカルで利用可能なボックスのリスト表示。今回`centos/8 (virtualbox, 1905.1)`が初回に表示されないこと     26 | 27 | ``` 28 | $ vagrant box list 29 | 30 | $ cp -R /Volumes/NO\ NAME/centos-VAGRANTSLASH-8 ~/.vagrant.d/boxes/. 31 | $ vagrant box list 32 | centos/8 (virtualbox, 1905.1) 33 | ``` 34 | 35 | ## Vagrantfile編集 36 | カレントディレクトリで`Vagrantfile`を以下の内容で編集しましょう 37 | 38 | |設定項目|設定値| 39 | |:-|:-| 40 | |boxファイル|centos/8| 41 | |IPアドレス|192.168.56.50| 42 | |VirtualBoxマシン名|1day| 43 | |メモリ|768M| 44 | 45 | `vi Vagrantfile`以降の内容をペースト 46 | 47 | ``` 48 | $ vi Vagrantfile 49 | # -*- mode: ruby -*- 50 | # vi: set ft=ruby : 51 | 52 | Vagrant.configure("2") do |config| 53 | config.vm.box = "centos/8" 54 | config.vm.hostname = "1day.local" 55 | config.vm.network :private_network, ip: "192.168.56.50" 56 | config.vm.provider :virtualbox do |vb| 57 | vb.name = "1day" 58 | vb.customize ["modifyvm", :id, "--memory", "768"] 59 | end 60 | end 61 | ``` 62 | 63 | ## Vagrantによる仮想環境の起動(初回の作成) 64 | `not created (virtualbox)`から`running (virtualbox)`となること 65 | 66 | ``` 67 | $ vagrant status 68 | Current machine states: 69 | 70 | default not created (virtualbox) 71 | 72 | $ vagrant up 73 | 74 | $ vagrant status 75 | Current machine states: 76 | 77 | default running (virtualbox) 78 | ``` 79 | 80 | ## ssh-configの確認 81 | 今回のVagrant環境に接続する際の設定を確認する 82 | ``` 83 | $ vagrant ssh-config 84 | Host default 85 | HostName 127.0.0.1 86 | User vagrant 87 | Port 2222 88 | UserKnownHostsFile /dev/null 89 | StrictHostKeyChecking no 90 | PasswordAuthentication no 91 | IdentityFile /Users/miurahironori/Vagrant/1day/.vagrant/machines/default/virtualbox/private_key 92 | IdentitiesOnly yes 93 | LogLevel FATAL 94 | ``` 95 | 96 | ### Question 97 | `vagrant ssh-config`の結果を元に仮想環境へssh接続をしてみましょう 98 | 99 | ## VirtualBoxでの確認 100 | VirtualBoxのGUIコンソールで`1day`と言う仮想サーバが存在し「実行中」となっていることを確認 101 | 102 | ネットワークに「アダプター1」「アダプター2」が存在することを確認 103 | 104 | ![virtualbox-1](./images/step1/virtualbox-1.png "virtualbox-1") 105 | 106 | ## 接続とrootユーザ遷移 107 | `vagrant ssh`にて仮想環境に`vagrant`ユーザでログインし`root`まで遷移できることを確認する 108 | 109 | **注 以降`#`でのコマンドは`root`で行う** 110 | 111 | ``` 112 | $ vagrant ssh 113 | Last login: Wed Sep 4 07:39:34 2019 from 192.168.56.1 114 | 115 | $ who 116 | vagrant pts/0 Sep 4 07:40 (10.0.2.2) 117 | ``` 118 | 119 | rootに遷移 120 | 121 | ``` 122 | $ sudo su - 123 | # who -H 124 | NAME LINE TIME COMMENT 125 | vagrant pts/0 2019-09-07 00:53 (10.0.2.2) 126 | # 127 | ``` 128 | 129 | ### Question 130 | whoコマンドについて調べてみましょう 131 | 132 | ## 設定内容の確認 133 | NICの確認IPアドレスが`Vagrantfile`で設定したIP`192.168.56.50`が設定されていること。 134 | 135 | ``` 136 | # ip a 137 | 1: lo: mtu 65536 qdisc noqueue state UNKNOWN 138 | link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 139 | inet 127.0.0.1/8 scope host lo 140 | valid_lft forever preferred_lft forever 141 | inet6 ::1/128 scope host 142 | valid_lft forever preferred_lft forever 143 | 2: enp0s3: mtu 1500 qdisc pfifo_fast state UP qlen 1000 144 | link/ether 08:00:27:b7:f3:af brd ff:ff:ff:ff:ff:ff 145 | inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3 146 | valid_lft 63945sec preferred_lft 63945sec 147 | inet6 fe80::a00:27ff:feb7:f3af/64 scope link 148 | valid_lft forever preferred_lft forever 149 | 3: enp0s8: mtu 1500 qdisc pfifo_fast state UP qlen 1000 150 | link/ether 08:00:27:27:cb:87 brd ff:ff:ff:ff:ff:ff 151 | inet 192.168.56.50/24 brd 192.168.56.255 scope global enp0s8 152 | valid_lft forever preferred_lft forever 153 | inet6 fe80::a00:27ff:fe27:cb87/64 scope link 154 | valid_lft forever preferred_lft forever 155 | ``` 156 | 157 | デフォルトゲートウェイの確認 158 | 159 | ``` 160 | # ip r 161 | default via 10.0.2.2 dev eth0 proto dhcp metric 100 162 | 10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15 metric 100 163 | 192.168.56.0/24 dev eth1 proto kernel scope link src 192.168.56.50 metric 101 164 | ``` 165 | 166 | `resolv.conf`の確認 167 | 168 | ``` 169 | # cat /etc/resolv.conf 170 | # Generated by NetworkManager 171 | search local 172 | nameserver 10.0.2.3 173 | ``` 174 | 175 | メモリの確認 `Mem: 758812`にて約768M確保されていること 176 | 177 | ``` 178 | # free 179 | total used free shared buff/cache available 180 | Mem: 758812 89916 534236 5324 134660 549976 181 | Swap: 1048572 0 1048572 182 | ``` 183 | 184 | 同様にメモリの確認 185 | 186 | ``` 187 | # cat /proc/meminfo 188 | MemTotal: 758812 kB 189 | MemFree: 534236 kB 190 | MemAvailable: 549996 kB 191 | Buffers: 948 kB 192 | Cached: 105660 kB 193 | SwapCached: 0 kB 194 | Active: 101816 kB 195 | Inactive: 68732 kB 196 | Active(anon): 64092 kB 197 | Inactive(anon): 5172 kB 198 | Active(file): 37724 kB 199 | Inactive(file): 63560 kB 200 | Unevictable: 0 kB 201 | Mlocked: 0 kB 202 | SwapTotal: 1048572 kB 203 | SwapFree: 1048572 kB 204 | Dirty: 0 kB 205 | Writeback: 0 kB 206 | AnonPages: 63972 kB 207 | Mapped: 22472 kB 208 | Shmem: 5324 kB 209 | Slab: 28072 kB 210 | SReclaimable: 16904 kB 211 | SUnreclaim: 11168 kB 212 | KernelStack: 1840 kB 213 | PageTables: 4380 kB 214 | NFS_Unstable: 0 kB 215 | Bounce: 0 kB 216 | WritebackTmp: 0 kB 217 | CommitLimit: 1427976 kB 218 | Committed_AS: 333512 kB 219 | VmallocTotal: 34359738367 kB 220 | VmallocUsed: 9856 kB 221 | VmallocChunk: 34359719420 kB 222 | HardwareCorrupted: 0 kB 223 | AnonHugePages: 4096 kB 224 | HugePages_Total: 0 225 | HugePages_Free: 0 226 | HugePages_Rsvd: 0 227 | HugePages_Surp: 0 228 | Hugepagesize: 2048 kB 229 | DirectMap4k: 51136 kB 230 | DirectMap2M: 735232 kB 231 | ``` 232 | 233 | ## Question 234 | 起動した仮想サーバの確認をしましょう 235 | 236 | - CPU数 237 | - Disk容量 238 | - その他 239 | 240 | ## ssh接続 241 | `vagrant ssh`、前述のshh以外のsshログイン 242 | 243 | ``` 244 | $ ssh -i /vagrant ssh-configで調べた鍵を指定 -o StrictHostKeyChecking=no vagrant@192.168.56.50 245 | Last login: Wed Sep 4 07:40:16 2019 from 10.0.2.2 246 | 247 | $ who 248 | vagrant pts/0 Sep 4 07:46 (192.168.56.1) 249 | $ exit 250 | logout 251 | Connection to 192.168.56.50 closed. 252 | ``` 253 | 254 | ### Question 255 | whoの結果が違う理由を考えてみましょう 256 | 257 | ## ファイル共有 258 | デフォルトではHOST-GUEST間でファイルが共有できないため`vagrant-vbguest`の機能を使いHOST-GUEST間のファイル共有を実現しましょう 259 | 260 | HOST OSからVagrantfileが存在するカレントディレクトリにてshare.txtを作成し、GUEST OSから参照できることを確認 261 | 262 | ディレクトリ確認 263 | ``` 264 | $ pwd 265 | /xxxxxxx/1day 266 | ``` 267 | 268 | ファイル作成 269 | 270 | ``` 271 | $ vi share.txt 272 | $ cat share.txt 273 | share 274 | $ vagrant ssh 275 | $ cd /vagrant 276 | $ ll 277 | ファイルが存在しないことを確認 278 | ``` 279 | 280 | ### kernel-develのインストール 281 | ``` 282 | $ vagrant ssh 283 | # sudo su - 284 | # yum groupinstall -y "Development Tools" 285 | # yum install -y https://people.centos.org/arrfab/shim/results.c8/kernel/20190604090648/4.18.0-80.el8.x86_64/kernel-devel-4.18.0-80.el8.x86_64.rpm 286 | ``` 287 | 288 | ### vagrant-vbguest 289 | `vagrant-vbguest`のインストール 290 | 291 | 確認(`vagrant-vbguest`が存在しないこと) 292 | ``` 293 | $ vagrant plugin list 294 | No plugins installed. 295 | ``` 296 | 297 | インストール 298 | ``` 299 | $ vagrant plugin install vagrant-vbguest 300 | Installing the 'vagrant-vbguest' plugin. This can take a few minutes... 301 | Fetching: micromachine-3.0.0.gem (100%) 302 | Fetching: vagrant-vbguest-0.19.0.gem (100%) 303 | Installed the plugin 'vagrant-vbguest (0.19.0)'! 304 | ``` 305 | 306 | 確認 307 | ``` 308 | $ vagrant plugin list 309 | vagrant-vbguest (0.19.0, global) 310 | ``` 311 | 312 | ### Vagrantfile編集 313 | Vagrantfileに以下のように`config.vm.synced_folder ".", "/vagrant", type: "virtualbox"`を追記しましょう 314 | 315 | ``` 316 | $ vi Vagrantfile 317 | $ cat Vagrantfile 318 | # -*- mode: ruby -*- 319 | # vi: set ft=ruby : 320 | 321 | Vagrant.configure("2") do |config| 322 | config.vm.box = "centos/8" 323 | config.vm.synced_folder ".", "/vagrant", type: "virtualbox" 324 | config.vm.hostname = "1day.local" 325 | config.vm.network :private_network, ip: "192.168.56.50" 326 | config.vm.provider :virtualbox do |vb| 327 | vb.name = "1day" 328 | vb.customize ["modifyvm", :id, "--memory", "768"] 329 | end 330 | end 331 | ``` 332 | 333 | ### Vagrant再起動 334 | vagrantへ追記反映 335 | 336 | ``` 337 | $ vagrant reload 338 | ``` 339 | 340 | ファイル共有の確認 341 | 342 | ``` 343 | $ vi share.txt 344 | $ cat share.txt 345 | share 346 | 347 | $ vagrant ssh 348 | 349 | $ cd /vagrant 350 | $ ll 351 | total 8 352 | -rw-r--r--. 1 vagrant vagrant 388 9月 4 08:12 Vagrantfile 353 | -rw-r--r--. 1 vagrant vagrant 6 9月 4 08:16 share.txt 354 | 355 | $ cat share.txt 356 | share 357 | ``` 358 | 359 | ## ロケールとタイムゾーンの設定 360 | boxで設定されたLANG en_US.UTF-8、タイムゾーンUTCをja_JP.utf8、JSTに変更する 361 | 362 | localeの確認 363 | ``` 364 | $ vagrant ssh 365 | $ sudo su - 366 | # localectl list-locales 367 | ``` 368 | 369 | langpack-jaのインストール 370 | ``` 371 | # dnf install -y glibc-langpack-ja.x86_64 372 | ``` 373 | 374 | ロケールの指定 375 | 376 | ``` 377 | # localectl 378 | System Locale: LANG=en_US.UTF-8 379 | VC Keymap: us 380 | X11 Layout: n/a 381 | 382 | # localectl set-locale LANG=ja_JP.utf8 383 | 384 | # localectl 385 | System Locale: LANG=ja_JP.utf8 386 | VC Keymap: us 387 | X11 Layout: n/a 388 | # vi /etc/locale.conf 389 | LANG="ja_JP.utf8" 390 | ``` 391 | 392 | タイムゾーンJSTの指定 393 | 394 | ``` 395 | # timedatectl 396 | Local time: Sat 2019-09-07 10:13:22 JST 397 | Universal time: Sat 2019-09-07 01:13:22 UTC 398 | RTC time: Sat 2019-09-07 01:13:21 399 | Time zone: UTC (JST, +0900) 400 | NTP enabled: yes 401 | NTP synchronized: yes 402 | RTC in local TZ: no 403 | DST active: n/a 404 | 405 | # timedatectl set-timezone Asia/Tokyo 406 | 407 | # timedatectl 408 | Local time: Sat 2019-09-07 10:13:33 JST 409 | Universal time: Sat 2019-09-07 01:13:33 UTC 410 | RTC time: Sat 2019-09-07 01:13:32 411 | Time zone: Asia/Tokyo (JST, +0900) 412 | NTP enabled: yes 413 | NTP synchronized: yes 414 | RTC in local TZ: no 415 | DST active: n/a 416 | ``` 417 | 418 | ## エディタのインストール 419 | 今後作業する上で使い慣れたエディタが良い人は必須ではありませんがインストールを行いましょう 420 | 421 | - vim 422 | ``` 423 | # dnf install -y vim 424 | ``` 425 | 426 | - emacs 427 | ``` 428 | # dnf install -y emacs 429 | ``` 430 | 431 | ## Tips(ハンズオンでは行わない) 432 | ### 仮想環境の再構築、pluginの削除 433 | pluginが不要な場合の削除方法 434 | 435 | pluginの確認 436 | ``` 437 | $ vagrant plugin list 438 | ``` 439 | 440 | 対象のpluginを指定しuninnstall 441 | ``` 442 | $ vagrant plugin uninstall vagrant-vbguest 443 | ``` 444 | 445 | 確認 446 | ``` 447 | $ vagrant plugin list 448 | No plugins installed. 449 | ``` 450 | 451 | Vagrantfileにてpluginの利用記述がある場合は削除 452 | ``` 453 | $ vi Vagrantfile 454 | - config.vm.synced_folder ".", "/vagrant", type: "virtualbox" 455 | ``` 456 | 457 | ### Vagrant基本操作 458 | 459 | 起動 460 | ``` 461 | $ vagrant up 462 | ``` 463 | 464 | 停止 465 | ``` 466 | $vagrant halt 467 | ``` 468 | 469 | 削除 470 | ``` 471 | $ vagrant destroy 472 | ``` 473 | 474 | 状態確認 475 | ``` 476 | $ vagrant status 477 | ``` 478 | 479 | これでStep1は完了です。 480 | 481 | -------------------------------------------------------------------------------- /Step2.md: -------------------------------------------------------------------------------- 1 | # Step2 2 | このStepではStep1で作成した仮装環境をLAMP環境にカスタマイズしていきます。 3 | 4 | ## LAMP環境の構築 5 | Step1で構築した環境にapache、php、mysqlをインストールしLAMP環境を構築します 6 | 7 | ## yum repo 8 | これから各種ミドルウェア(apache、MySQL)やPHPをインストールする際に利用するリポジトリの設定を行う 9 | 10 | **`/etc/yum.repos.d/`ディレクトリと内容について簡単に確認** 11 | 12 | ``` 13 | # ll /etc/yum.repos.d/ 14 | total 44 15 | -rw-r--r--. 1 root root 731 Aug 14 2019 CentOS-AppStream.repo 16 | -rw-r--r--. 1 root root 712 Aug 14 2019 CentOS-Base.repo 17 | -rw-r--r--. 1 root root 798 Aug 14 2019 CentOS-centosplus.repo 18 | -rw-r--r--. 1 root root 1320 Aug 14 2019 CentOS-CR.repo 19 | -rw-r--r--. 1 root root 668 Aug 14 2019 CentOS-Debuginfo.repo 20 | -rw-r--r--. 1 root root 756 Aug 14 2019 CentOS-Extras.repo 21 | -rw-r--r--. 1 root root 338 Aug 14 2019 CentOS-fasttrack.repo 22 | -rw-r--r--. 1 root root 928 Aug 14 2019 CentOS-Media.repo 23 | -rw-r--r--. 1 root root 736 Aug 14 2019 CentOS-PowerTools.repo 24 | -rw-r--r--. 1 root root 1382 Aug 14 2019 CentOS-Sources.repo 25 | -rw-r--r--. 1 root root 74 Aug 14 2019 CentOS-Vault.repo 26 | 27 | # cat /etc/yum.repos.d/CentOS-Base.repo 28 | 29 | # CentOS-Base.repo 30 | # 31 | # The mirror system uses the connecting IP address of the client and the 32 | # update status of each mirror to pick mirrors that are updated to and 33 | # geographically close to the client. You should use this for CentOS updates 34 | # unless you are manually picking other mirrors. 35 | # 36 | # If the mirrorlist= does not work for you, as a fall back you can try the 37 | # remarked out baseurl= line instead. 38 | # 39 | # 40 | 41 | [base] 42 | name=CentOS-$releasever - Base 43 | mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra 44 | #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/ 45 | gpgcheck=1 46 | gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 47 | 48 | #released updates 49 | [updates] 50 | name=CentOS-$releasever - Updates 51 | mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra 52 | #baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/ 53 | gpgcheck=1 54 | gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 55 | 56 | #additional packages that may be useful 57 | [extras] 58 | name=CentOS-$releasever - Extras 59 | mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra 60 | #baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/ 61 | gpgcheck=1 62 | gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 63 | 64 | #additional packages that extend functionality of existing packages 65 | [centosplus] 66 | name=CentOS-$releasever - Plus 67 | mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra 68 | #baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/ 69 | gpgcheck=1 70 | enabled=0 71 | gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 72 | ``` 73 | 74 | ### epel 75 | epelのインストール(注 `yum -y update`は数分ほど掛かる) 76 | 77 | ``` 78 | # dnf -y install epel-release 79 | # dnf -y update 80 | ``` 81 | 82 | ### remi 83 | remiのインストール 84 | 85 | ``` 86 | # dnf -y install http://rpms.famillecollet.com/enterprise/remi-release-8.rpm 87 | # dnf -y update 88 | ``` 89 | 90 | **`/etc/yum.repos.d/`ディレクトリと内容について簡単に確認** 91 | 92 | ``` 93 | # ll /etc/yum.repos.d/ 94 | total 84 95 | -rw-r--r--. 1 root root 731 Sep 16 05:10 CentOS-AppStream.repo 96 | -rw-r--r--. 1 root root 712 Sep 16 05:10 CentOS-Base.repo 97 | -rw-r--r--. 1 root root 798 Sep 16 05:10 CentOS-centosplus.repo 98 | -rw-r--r--. 1 root root 1043 Sep 16 05:10 CentOS-CR.repo 99 | -rw-r--r--. 1 root root 668 Sep 16 05:10 CentOS-Debuginfo.repo 100 | -rw-r--r--. 1 root root 743 Sep 16 05:10 CentOS-Devel.repo 101 | -rw-r--r--. 1 root root 756 Sep 16 05:10 CentOS-Extras.repo 102 | -rw-r--r--. 1 root root 338 Sep 16 05:10 CentOS-fasttrack.repo 103 | -rw-r--r--. 1 root root 738 Sep 16 05:10 CentOS-HA.repo 104 | -rw-r--r--. 1 root root 928 Sep 16 05:10 CentOS-Media.repo 105 | -rw-r--r--. 1 root root 736 Sep 16 05:10 CentOS-PowerTools.repo 106 | -rw-r--r--. 1 root root 1382 Sep 16 05:10 CentOS-Sources.repo 107 | -rw-r--r--. 1 root root 74 Sep 16 05:10 CentOS-Vault.repo 108 | -rw-r--r--. 1 root root 1167 Dec 19 2019 epel-modular.repo 109 | -rw-r--r--. 1 root root 1249 Dec 19 2019 epel-playground.repo 110 | -rw-r--r--. 1 root root 1104 Dec 19 2019 epel.repo 111 | -rw-r--r--. 1 root root 1266 Dec 19 2019 epel-testing-modular.repo 112 | -rw-r--r--. 1 root root 1203 Dec 19 2019 epel-testing.repo 113 | -rw-r--r--. 1 root root 903 Feb 27 2020 remi-modular.repo 114 | -rw-r--r--. 1 root root 1384 Feb 27 2020 remi.repo 115 | -rw-r--r--. 1 root root 778 Feb 27 2020 remi-safe.repo 116 | ``` 117 | 118 | dnfコマンドでも確認 119 | 120 | ``` 121 | # dnf repolist 122 | repo id repo name 123 | AppStream CentOS-8 - AppStream 124 | BaseOS CentOS-8 - Base 125 | epel Extra Packages for Enterprise Linux 8 - x86_64 126 | epel-modular Extra Packages for Enterprise Linux Modular 8 - x86_64 127 | extras CentOS-8 - Extras 128 | remi-modular Remi's Modular repository for Enterprise Linux 8 - x86_64 129 | remi-safe Safe Remi's RPM repository for Enterprise Linux 8 - x86_64 130 | ``` 131 | 132 | ### Question 133 | dnf、yum、RPMについて調べてみましょう 134 | 135 | ## PHP7、apache(httpd) 136 | PHP7、apache(httpd)のインストールを行う 137 | 138 | ``` 139 | # dnf -y module disable php 140 | # dnf module install -y php:remi-7.3 141 | # dnf -y install php-mbstring php-pdo php-gd php-xml php-mcrypt php-mysql 142 | ``` 143 | 144 | PHPのバージョンを確認(PHP 7.XX.XXであること) 145 | 146 | ``` 147 | # php -v 148 | PHP 7.3.23 (cli) (built: Sep 29 2020 08:33:03) ( NTS ) 149 | Copyright (c) 1997-2018 The PHP Group 150 | Zend Engine v3.3.23, Copyright (c) 1998-2018 Zend Technologies 151 | ``` 152 | ### apacheのインストール 153 | ``` 154 | # dnf install -y httpd 155 | ``` 156 | 157 | ### apache起動、自動起動 158 | apache(httpd)の起動、自動起動の設定を行う 159 | 160 | apache(httpd)がインストールされていることを確認 161 | ``` 162 | # dnf list installed | grep httpd 163 | centos-logos-httpd.noarch 80.5-2.el8 @BaseOS 164 | httpd.x86_64 2.4.37-21.module_el8.2.0+494+1df74eae @AppStream 165 | httpd-filesystem.noarch 2.4.37-21.module_el8.2.0+494+1df74eae @AppStream 166 | httpd-tools.x86_64 2.4.37-21.module_el8.2.0+494+1df74eae @AppStream 167 | ``` 168 | 起動 169 | ``` 170 | # systemctl start httpd.service 171 | ``` 172 | 自動起動設定 173 | ``` 174 | # systemctl enable httpd.service 175 | ``` 176 | 確認(runningであること) 177 | ``` 178 | # systemctl status httpd.service 179 | ``` 180 | 確認(enabledであること) 181 | ``` 182 | # systemctl is-enabled httpd.service 183 | ``` 184 | php-fpmの確認 185 | ``` 186 | # systemctl status php-fpm 187 | ``` 188 | 189 | localhostに対しhttpリクエストを投げWebサーバ(httpd)が動作していることを確認 190 | 191 | ``` 192 | # curl localhost 193 | ``` 194 | ### Question 195 | `curl`コマンドでレスポンスヘッダも表示させましょう 196 | 197 | **ブラウザでも同様に192.168.56.50で確認する** 198 | 199 | ![httpd-1](./images/step2/httpd-1.png "httpd-1") 200 | 201 | ### lsof 202 | apache(httpd)がPORT80番をLISTENしているか確認するため`lsof`をインストール 203 | 204 | ``` 205 | # dnf -y install lsof 206 | ``` 207 | 208 | 確認 209 | 210 | ``` 211 | # lsof -i:80 212 | COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME 213 | httpd 734 root 4u IPv6 16183 0t0 TCP *:http (LISTEN) 214 | httpd 769 apache 4u IPv6 16183 0t0 TCP *:http (LISTEN) 215 | httpd 770 apache 4u IPv6 16183 0t0 TCP *:http (LISTEN) 216 | httpd 771 apache 4u IPv6 16183 0t0 TCP *:http (LISTEN) 217 | httpd 772 apache 4u IPv6 16183 0t0 TCP *:http (LISTEN) 218 | httpd 773 apache 4u IPv6 16183 0t0 TCP *:http (LISTEN) 219 | httpd 3007 apache 4u IPv6 16183 0t0 TCP *:http (LISTEN) 220 | ``` 221 | 222 | ### viの設定 223 | `~/.virc`に追記する 224 | ``` 225 | # vi ~/.virc 226 | # cat ~/.virc 227 | set encoding=utf-8 228 | set fileencodings=iso-2022-jp,euc-jp,sjis,utf-8 229 | set fileformats=unix,dos,mac 230 | ``` 231 | ### Question 232 | ``` 233 | # cd /var/www/html/ 234 | ``` 235 | 236 | `/var/www/html/`配下に以下のindex.phpを配置しブラウザ、CLI`curl localhost`で確認してみましょう 237 | 238 | ``` 239 | "; 241 | echo date('Y年m月d日 H時i分s秒'); 242 | ``` 243 | curlで確認 244 | ``` 245 | # curl localhost 246 | ``` 247 | 248 | ### Question 249 | PHPのBuiltinWebServerの機能を使いPORT 8008番でindex.phpを表示してみましょう(CTL + cで停止) 250 | 251 | **注 SELinuxが有効なため表示できない場合があります。は別ターミナルから`curl localhost`のみ表示しましょう** 252 | 253 | SELinuxの確認 254 | ``` 255 | # semanage port -l | grep http 256 | http_cache_port_t tcp 8080, 8118, 8123, 10001-10010 257 | http_cache_port_t udp 3130 258 | http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000 259 | pegasus_http_port_t tcp 5988 260 | pegasus_https_port_t tcp 5989 261 | ``` 262 | 実行 263 | ``` 264 | # cd /var/www/html 265 | # php -S 192.168.56.50:8008 266 | ``` 267 | 表示 268 | ``` 269 | # curl localhost 270 | # curl 192.168.56.50:8008 271 | ``` 272 | 273 | ### PHP設定変更 274 | php.iniのタイムゾーンを変更する 275 | ``` 276 | # vi /etc/php.ini 277 | - ;date.timezone = 278 | + date.timezone = "Asia/Tokyo" 279 | ``` 280 | apache(httpd),php-fpm再起動 281 | ``` 282 | # systemctl restart php-fpm 283 | # systemctl restart httpd.service 284 | ``` 285 | 確認 286 | ``` 287 | # curl localhost 288 | ``` 289 | 290 | 291 | ## MySQL 292 | MySQLのインストールを行う 293 | 294 | ### mariadb削除 295 | 標準でインストールされているmariadbを削除する 296 | 297 | 確認 298 | ``` 299 | # rpm -qa | grep mariadb 300 | mariadb-libs-5.5.60-1.el7_5.x86_64 301 | ``` 302 | 303 | 存在する場合削除 304 | ``` 305 | # yum -y remove mariadb-libs 306 | ``` 307 | 308 | ディレクトリも削除 309 | ``` 310 | # rm -rf /var/lib/mysql/ 311 | ``` 312 | 313 | ### MySQL yumrepo 314 | 315 | ### repoのインストール 316 | MySQL8.0をインストール 317 | 318 | ``` 319 | # dnf -y localinstall https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm 320 | ``` 321 | 322 | ### 確認とrepoの設定 323 | ``` 324 | # ll /etc/yum.repos.d 325 | # dnf repolist enabled | grep "mysql.*-community.*" 326 | ``` 327 | 328 | ### モジュールの抑止 329 | ``` 330 | # dnf -y module disable mysql 331 | ``` 332 | 333 | ### インストール 334 | ``` 335 | # dnf -y install mysql-community-server 336 | ``` 337 | 338 | ### MySQLの起動、自動起動を設定 339 | 340 | 起動 341 | ``` 342 | # systemctl start mysqld.service 343 | ``` 344 | 自動起動の設定 345 | ``` 346 | # systemctl enable mysqld.service 347 | ``` 348 | 確認(runningであること) 349 | ``` 350 | # systemctl status mysqld.service 351 | ``` 352 | 確認(enabledであること) 353 | ``` 354 | # systemctl is-enabled mysqld.service 355 | ``` 356 | 357 | ### PORT確認 358 | MySQLがPORT3306番をLISTENしているか確認 359 | ``` 360 | # lsof -i:3306 361 | COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME 362 | mysqld 31699 mysql 30u IPv6 65873 0t0 TCP *:mysql (LISTEN) 363 | ``` 364 | 365 | ### rootパスワード 366 | MySQLのrootパスワードをログから確認`A temporary password is generated for root@localhost:`の右を確認 367 | 368 | ``` 369 | # cat /var/log/mysqld.log 370 | # grep password /var/log/mysqld.log 371 | ``` 372 | 373 | ### 初期設定 374 | `mysql_secure_installation`を実行 375 | 376 | ``` 377 | # mysql_secure_installation 378 | ``` 379 | 380 | 確認したパスワードを入力 381 | 382 | ``` 383 | Enter password for user root: 384 | ``` 385 | 386 | i1db+abd8kD 387 | 388 | ``` 389 | New password: 390 | ``` 391 | 392 | i1db+abd8kD 393 | 394 | ``` 395 | Re-enter new password: 396 | ``` 397 | 398 | y 399 | 400 | ``` 401 | Change the password for root ? ((Press y|Y for Yes, any other key for No) : 402 | ``` 403 | 404 | i1db+abd8kD 405 | 406 | ``` 407 | New password: 408 | ``` 409 | 410 | i1db+abd8kD 411 | 412 | ``` 413 | Re-enter new password: 414 | ``` 415 | 416 | y 417 | 418 | ``` 419 | Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : 420 | ``` 421 | 422 | y 423 | 424 | ``` 425 | Remove anonymous users? (Press y|Y for Yes, any other key for No) : 426 | ``` 427 | 428 | y 429 | 430 | ``` 431 | Disallow root login remotely? (Press y|Y for Yes, any other key for No) : 432 | ``` 433 | 434 | y 435 | 436 | ``` 437 | Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 438 | ``` 439 | 440 | y 441 | 442 | ``` 443 | Reload privilege tables now? (Press y|Y for Yes, any other key for No) : 444 | ``` 445 | 446 | ### 設定変更 447 | MySQLの設定ファイル`my.cnf`を以下に上書きする 448 | ``` 449 | # vi /etc/my.cnf 450 | [mysqld] 451 | innodb-buffer-pool-size=128M 452 | default-authentication-plugin=mysql_native_password 453 | ``` 454 | 455 | 再起動 456 | ``` 457 | # systemctl restart mysqld.service 458 | ``` 459 | 460 | ### ログイン 461 | MySQLクライアントからログイン。パスワードは`mysql_secure_installation`で設定したものを入力 462 | 463 | パスワード:i1db+abd8kD 464 | 465 | ``` 466 | # mysql -u root -p 467 | Enter password: 468 | 469 | mysql> show databases; 470 | +--------------------+ 471 | | Database | 472 | +--------------------+ 473 | | information_schema | 474 | | mysql | 475 | | performance_schema | 476 | | sys | 477 | +--------------------+ 478 | 4 rows in set (0.00 sec) 479 | 480 | mysql> 481 | ``` 482 | 483 | ### アプリケーション用DB作成 484 | DB`wordpress`の作成 485 | 486 | ``` 487 | mysql> create database wordpress charset = utf8mb4; 488 | Query OK, 1 row affected (0.07 sec) 489 | ``` 490 | 491 | 作成したDBの確認 492 | 493 | ``` 494 | mysql> show databases; 495 | +--------------------+ 496 | | Database | 497 | +--------------------+ 498 | | information_schema | 499 | | mysql | 500 | | performance_schema | 501 | | sys | 502 | | wordpress | 503 | +--------------------+ 504 | 5 rows in set (0.04 sec) 505 | ``` 506 | 507 | DDLの確認 508 | 509 | ``` 510 | mysql> show create database wordpress; 511 | +-----------+-------------------------------------------------------------------------------------------------------------------------------------+ 512 | | Database | Create Database | 513 | +-----------+-------------------------------------------------------------------------------------------------------------------------------------+ 514 | | wordpress | CREATE DATABASE `wordpress` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ | 515 | +-----------+-------------------------------------------------------------------------------------------------------------------------------------+ 516 | 1 row in set (0.00 sec) 517 | ``` 518 | 519 | 520 | ### アプリケーションユーザの作成 521 | `'admin'@'localhost'`ユーザを作成 522 | 523 | ``` 524 | mysql> create user 'admin'@'localhost' identified by 'qk9Baa29+sL'; 525 | 526 | mysql> grant all on *.* to 'admin'@'localhost'; 527 | 528 | mysql> alter user 'admin'@'localhost' identified with mysql_native_password by 'qk9Baa29+sL'; 529 | 530 | mysql> exit 531 | ``` 532 | 533 | ## サンプルアプリのデプロイ 534 | 535 | `wget`コマンドのインストール 536 | 537 | ``` 538 | # dnf -y install wget 539 | ``` 540 | 541 | 今回アプリケーションをデプロイするディレクトリへ遷移 542 | ``` 543 | # cd /var/www/html 544 | ``` 545 | 546 | Wordpressを取得し`tar`で展開 547 | 548 | ``` 549 | # wget http://wordpress.org/latest.tar.gz 550 | # tar -xzvf latest.tar.gz 551 | ``` 552 | 553 | ファイル所有者の変更 554 | 555 | ``` 556 | # chown apache:apache -R wordpress 557 | ``` 558 | 559 | SELinuxの設定 560 | 561 | ``` 562 | # chcon -R -t httpd_sys_content_t /var/www/html/wordpress 563 | # chcon -R -t httpd_sys_rw_content_t /var/www/html/wordpress 564 | ``` 565 | 566 | ### apache(httpd)の設定変更と再起動 567 | apache(httpd)の設定ファイル`httpd.conf`でDocuemtRootとDirectoryの変更を行い再起動 568 | 569 | ``` 570 | # vi /etc/httpd/conf/httpd.conf 571 | - DocumentRoot "/var/www/html" 572 | + DocumentRoot "/var/www/html/wordpress" 573 | - 574 | + 575 | ``` 576 | 577 | apache(httpd)の再起動 578 | 579 | ``` 580 | # systemctl restart httpd.service 581 | ``` 582 | 583 | 確認 584 | ``` 585 | # systemctl status httpd.service 586 | ``` 587 | 588 | ### Qumestion 589 | DocumentRootについて調べましょう 590 | 591 | ### WordPressを表示 592 | **ブラウザで192.168.56.50を表示。Wordpressの設定画面が表示されれば成功** 593 | 594 | ![wordpress-1](./images/step2/wordpress-1.png "wordpress-1") 595 | 596 | ## Wordpressの設定 597 | 日本語を選択し続けるを押下 598 | 599 | ![wordpress-2](./images/step2/wordpress-2.png "wordpress-2") 600 | 601 | さあ、始めましょうを押下 602 | 603 | ![wordpress-3](./images/step2/wordpress-3.png "wordpress-3") 604 | 605 | ユーザ:admin、パスワード:qk9Baa29+sL 606 | 607 | ![wordpress-4](./images/step2/wordpress-4.png "wordpress-4") 608 | 609 | インストール実行を押下 610 | 611 | ![wordpress-5](./images/step2/wordpress-5.png "wordpress-5") 612 | 613 | サイトのタイトル「1dayインターン」、ユーザ名:admin、パスワードはテキスト保存しましょう、メールアドレスは任意で設定、検索エンジンでの表示はチェックしWordPressをインストールを押下 614 | 615 | ![wordpress-6](./images/step2/wordpress-6.png "wordpress-6") 616 | 617 | ログインを押下 618 | 619 | ![wordpress-7](./images/step2/wordpress-7.png "wordpress-7") 620 | 621 | ユーザ名:admin、パスワードはテキスト保存したパスワードを入力しログイン 622 | 623 | ![wordpress-8](./images/step2/wordpress-8.png "wordpress-8") 624 | 625 | 管理画面が表示されること 626 | 627 | ![wordpress-9](./images/step2/wordpress-9.png "wordpress-9") 628 | 629 | これでStep2は完了です。 630 | 631 | -------------------------------------------------------------------------------- /Step3.md: -------------------------------------------------------------------------------- 1 | # Step3 2 | Step2で構築したLAMP環境のMySQLをDockerコンテナに置き換えます 3 | 4 | ## Docker 5 | MySQLをdockerで利用するため、dockerのインストール、起動を行いましょう 6 | 7 | リポジトリの追加 8 | ``` 9 | # dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 10 | ``` 11 | 12 | インストール 13 | ``` 14 | # dnf -y install docker-ce docker-ce-cli containerd.io 15 | ``` 16 | 17 | ### 起動と自動起動の設定 18 | 19 | 起動 20 | ``` 21 | # systemctl start docker.service 22 | ``` 23 | 自動起動の設定 24 | ``` 25 | # systemctl enable docker.service 26 | ``` 27 | 確認(runningであること) 28 | ``` 29 | # systemctl status docker.service 30 | ``` 31 | 確認(enabledであること) 32 | ``` 33 | # systemctl is-enabled docker.service 34 | ``` 35 | 36 | バージョンの確認 37 | 38 | ``` 39 | # docker version 40 | Client: Docker Engine - Community 41 | Version: 19.03.13 42 | API version: 1.40 43 | Go version: go1.13.15 44 | Git commit: 4484c46d9d 45 | Built: Wed Sep 16 17:02:36 2020 46 | OS/Arch: linux/amd64 47 | Experimental: false 48 | 49 | Server: Docker Engine - Community 50 | Engine: 51 | Version: 19.03.13 52 | API version: 1.40 (minimum version 1.12) 53 | Go version: go1.13.15 54 | Git commit: 4484c46d9d 55 | Built: Wed Sep 16 17:01:11 2020 56 | OS/Arch: linux/amd64 57 | Experimental: false 58 | containerd: 59 | Version: 1.3.7 60 | GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175 61 | runc: 62 | Version: 1.0.0-rc10 63 | GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd 64 | docker-init: 65 | Version: 0.18.0 66 | GitCommit: fec3683 67 | ``` 68 | 69 | ## Docker MySQL 70 | 71 | dockerイメージの確認 72 | 73 | ``` 74 | # docker images 75 | REPOSITORY TAG IMAGE ID CREATED SIZE 76 | ``` 77 | 78 | MySQLのdockerイメージをpullします 79 | 80 | ``` 81 | # docker pull mysql:latest 82 | ``` 83 | 84 | dockerイメージの確認 85 | 86 | ``` 87 | # docker images 88 | REPOSITORY TAG IMAGE ID CREATED SIZE 89 | docker.io/mysql latest 62a9f311b99c 3 weeks ago 445 MB 90 | ``` 91 | 92 | ### コンテナの起動と確認 93 | MySQLイメージからMYSQL_ROOT_PASSWORD=mysqlにてパスワードを設定しコンテナを起動します。今回はポートフォワードを3307から3306で行います。docker run後docker psでコンテナが立ち上がってることを確認しましょう 94 | 95 | 確認 96 | 97 | ``` 98 | # docker ps 99 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS 100 | ``` 101 | 102 | コンテナ起動 103 | 104 | ``` 105 | # docker run --name mysqld -e MYSQL_ROOT_PASSWORD=mysql -d -p 3307:3306 mysql 106 | ``` 107 | 108 | 確認(CONTAINER IDを確認) 109 | 110 | ``` 111 | # docker ps 112 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 113 | 1c0bb6af2ee4 mysql "docker-entrypoint..." 30 seconds ago Up 29 seconds 3306/tcp, 33060/tcp, 0.0.0.0:3307->3306/tcp mysqld 114 | ``` 115 | 116 | ### bashモードで接続 117 | コンテナに直接接続します。docker psにて出力された**CONTAINER ID**を指定しましょう。 118 | 119 | ``` 120 | # docker exec -it 1c0bb6af2ee4 bash 121 | ``` 122 | 123 | コンテナ内でMySQLに接続します(パスワード:mysql) 124 | 125 | ``` 126 | # mysql -u root -p 127 | Enter password: 128 | ``` 129 | 130 | 存在するデータベースの確認 131 | 132 | ``` 133 | mysql> show databases; 134 | +--------------------+ 135 | | Database | 136 | +--------------------+ 137 | | information_schema | 138 | | mysql | 139 | | performance_schema | 140 | | sys | 141 | +--------------------+ 142 | 4 rows in set (0.10 sec) 143 | 144 | mysql> exit 145 | Bye 146 | ``` 147 | 148 | コンテナから抜けます 149 | 150 | ``` 151 | # exit 152 | exit 153 | ``` 154 | 155 | ### 仮想サーバからの接続 156 | 仮想サーバのMySQL Clientから接続(パスワード:mysql) 157 | 158 | ``` 159 | # mysql -u root -p --port=3307 -h127.0.0.1 160 | Enter password: 161 | ``` 162 | 163 | 存在するデータベースの確認 164 | 165 | ``` 166 | mysql> show databases; 167 | +--------------------+ 168 | | Database | 169 | +--------------------+ 170 | | information_schema | 171 | | mysql | 172 | | performance_schema | 173 | | sys | 174 | +--------------------+ 175 | 4 rows in set (0.10 sec) 176 | 177 | mysql> exit 178 | ``` 179 | 180 | ### Qestion 181 | MySQLClientで接続する際に`mysql -u root -p --port=3307 -hlocalhost`で接続するために指定するオプションを調べましょう 182 | 183 | ### dockerコンテナの起動停止と削除 184 | 起動したdockerコンテナを使い起動停止と削除を行う 185 | 186 | CONTAINER IDを確認 187 | 188 | ``` 189 | # docker ps -a 190 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 191 | 4fc496c089a1 mysql "docker-entrypoint..." About an hour ago Up About an hour 33060/tcp, 0.0.0.0:3307->3306/tcp mysqld 192 | ``` 193 | 194 | 停止(stop以降は実際のCONTAINER IDを指定) 195 | ``` 196 | # docker stop 4fc496c089a1 197 | ``` 198 | 199 | 起動 200 | ``` 201 | # docker start 4fc496c089a1 202 | ``` 203 | 204 | 停止し削除(CONTAINER IDではなくNAMESでの指定も可能) 205 | 206 | ``` 207 | # docker stop mysqld 208 | # docker rm mysqld 209 | ``` 210 | 211 | 確認 212 | 213 | ``` 214 | # docker ps -a 215 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 216 | ``` 217 | 218 | ## docker-compose 219 | 220 | docker-composeのインストールとファイル権限変更 221 | 222 | [docker compose公式](https://docs.docker.com/compose/install/) 223 | 224 | ``` 225 | # curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose 226 | # chmod +x /usr/local/bin/docker-compose 227 | ``` 228 | 229 | バージョンの確認 230 | 231 | ``` 232 | # docker-compose -version 233 | docker-compose version 1.27.4, build 40524192 234 | ``` 235 | 236 | docker-compose用のディレクトリ作成と遷移 237 | 238 | ``` 239 | # cd /vagrant 240 | # mkdir docker_mysql 241 | # cd docker_mysql 242 | ``` 243 | 244 | docker-compose.ymlの作成(vi以下の内容をペースト) 245 | 246 | ``` 247 | # vi docker-compose.yml 248 | version: '3.7' 249 | services: 250 | db: 251 | build: ./mysql/ 252 | image: mysql:latest 253 | environment: 254 | MYSQL_DATABASE: wordpress 255 | MYSQL_ROOT_PASSWORD: mysql 256 | MYSQL_USER: admin 257 | MYSQL_PASSWORD: qk9Baa29+sL 258 | expose: 259 | - "3306" 260 | ports: 261 | - "3307:3306" 262 | ``` 263 | 264 | docker MySQL用のディレクトリ作成 265 | 266 | ``` 267 | # mkdir mysql 268 | # cd mysql 269 | ``` 270 | 271 | Dockerfileの作成(vi以下をペースト) 272 | 273 | ``` 274 | # vi Dockerfile 275 | FROM mysql:latest 276 | ADD ./conf.d/my.cnf /etc/mysql/conf.d/my.cnf 277 | ``` 278 | 279 | MySQL設定ファイルmy.cnfの作成(vi以下をペースト) 280 | 281 | ``` 282 | # mkdir conf.d 283 | # cd conf.d 284 | 285 | # vi my.cnf 286 | [mysqld] 287 | innodb-buffer-pool-size=128M 288 | default-authentication-plugin=mysql_native_password 289 | ``` 290 | 291 | docker-composeでbuild 292 | 293 | ``` 294 | # cd ../.. 295 | # docker-compose build 296 | ``` 297 | 298 | docker imegeの確認 299 | 300 | ``` 301 | # docker images 302 | ``` 303 | 304 | dockerコンテナの起動 305 | 306 | ``` 307 | # docker-compose up -d 308 | ``` 309 | 310 | docker MySql コンテナの確認 311 | 312 | ``` 313 | # docker ps 314 | ``` 315 | 316 | ### bashモードで接続 317 | コンテナに直接接続します。docker psにて出力された**CONTAINER ID**を指定しましょう。 318 | 319 | ``` 320 | # docker exec -it 84336a6fb964 bash 321 | ``` 322 | 323 | ### MySQL接続 324 | コンテナ内でMySQLに接続(パスワード:mysql) 325 | 326 | ``` 327 | # mysql -u root -p 328 | Enter password: 329 | ``` 330 | 331 | 存在するデータベースの確認(wordpressが存在すること) 332 | 333 | ``` 334 | mysql> show databases; 335 | +--------------------+ 336 | | Database | 337 | +--------------------+ 338 | | information_schema | 339 | | mysql | 340 | | performance_schema | 341 | | sys | 342 | | wordpress | 343 | +--------------------+ 344 | 5 rows in set (0.00 sec) 345 | ``` 346 | 347 | アプリケーションユーザの確認(admin % が存在すること) 348 | 349 | ``` 350 | mysql> use mysql 351 | 352 | mysql> select user,host from user; 353 | +------------------+-----------+ 354 | | user | host | 355 | +------------------+-----------+ 356 | | admin | % | 357 | | root | % | 358 | | mysql.infoschema | localhost | 359 | | mysql.session | localhost | 360 | | mysql.sys | localhost | 361 | | root | localhost | 362 | +------------------+-----------+ 363 | 6 rows in set (0.00 sec) 364 | 365 | mysql> exit 366 | Bye 367 | # 368 | ``` 369 | 370 | DBアプリケーションユーザで接続確認 371 | 372 | パスワード:qk9Baa29+sL 373 | 374 | ``` 375 | # mysql -u admin -p -hlocalhost --protocol=tcp --port=3307 376 | 377 | mysql> show databases; 378 | mysql> exit 379 | ``` 380 | 381 | ## データ移行 382 | mysqldumpを使いlocalhostで動作しているMySQLのデータをdockerコンテナのMySQLへ移行する 383 | 384 | パスワード:i1db+abd8kD 385 | 386 | ``` 387 | # cd /vagrant 388 | # mysqldump -u root -p wordpress > export.sql 389 | ``` 390 | 391 | LocalhostのMySQLを自動起動抑止と停止 392 | 393 | ``` 394 | # systemctl disable mysqld.service 395 | # systemctl stop mysqld.service 396 | ``` 397 | 398 | 確認(Active: inactive (dead)であること) 399 | 400 | ``` 401 | # systemctl status mysqld.service 402 | ``` 403 | 404 | ブラウザでWordpressのサイトを確認しましょう 405 | 406 | ![error-wordpress-1](./images/step3/error-wordpress-1.png "error-wordpress-1") 407 | 408 | データベースのリストア 409 | 410 | パスワード:mysql 411 | 412 | ``` 413 | # cd /vagrant 414 | # mysql -u root -p -h127.0.0.1 --port=3307 wordpress < export.sql 415 | Enter password: 416 | ``` 417 | 418 | ### SELinuxの設定変更 419 | 420 | ``` 421 | # setsebool -P httpd_can_network_connect=1 422 | ``` 423 | 424 | ### WordpressのDB接続変更 425 | 426 | ``` 427 | # vi /var/www/html/wordpress/wp-config.php 428 | 429 | - define('DB_HOST', 'localhost'); 430 | + define('DB_HOST', '127.0.0.1:3307'); 431 | ``` 432 | 433 | ブラウザでエラーだった192.168.56.50をリロードし管理画面が表示されれば成功 434 | 435 | ![error-wordpress-2](./images/step3/error-wordpress-2.png "error-wordpress-2") 436 | 437 | ## WordPressのDocker化 438 | PORT8000番にDockerコンテナのWordPressを起動することでWordPress+MySQLをDockerで連携させる 439 | 440 | 停止と削除 441 | 442 | ``` 443 | # cd docker_mysql 444 | # docker-compose down -v 445 | ``` 446 | 447 | Dockerイメージの削除`REPOSITORY`がmysqlの`IMAGE ID`を指定 448 | 449 | ``` 450 | # docker images 451 | REPOSITORY TAG IMAGE ID CREATED SIZE 452 | mysql latest 32ea220bd41c 7 minutes ago 445 MB 453 | docker.io/mysql latest 62a9f311b99c 3 weeks ago 445 MB 454 | 455 | # docker rmi 32ea220bd41c 456 | ``` 457 | 458 | docker-compose.ymlの変更(vi以降を全て上書き) 459 | 460 | ``` 461 | # vi docker-compose.yml 462 | version: '3.7' 463 | services: 464 | db: 465 | build: ./mysql/ 466 | image: mysql:latest 467 | environment: 468 | MYSQL_DATABASE: wordpress 469 | MYSQL_ROOT_PASSWORD: mysql 470 | MYSQL_USER: admin 471 | MYSQL_PASSWORD: qk9Baa29+sL 472 | expose: 473 | - "3306" 474 | ports: 475 | - "3307:3306" 476 | wordpress: 477 | build: ./wordpress/ 478 | image: wordpress:latest 479 | expose: 480 | - "80" 481 | ports: 482 | - "8000:80" 483 | restart: always 484 | environment: 485 | WORDPRESS_DB_HOST: db 486 | WORDPRESS_DB_USER: admin 487 | WORDPRESS_DB_PASSWORD: qk9Baa29+sL 488 | WORDPRESS_DB_NAME: wordpress 489 | depends_on: 490 | - db 491 | ``` 492 | 493 | wordpress用のディレクトリ作成と遷移 494 | 495 | ``` 496 | # mkdir wordpress 497 | # cd wordpress 498 | ``` 499 | 500 | Dockerfileの作成(vi以降をペースト) 501 | 502 | ``` 503 | # vi Dockerfile 504 | FROM wordpress:latest 505 | ENV DEBIAN_FRONTEND noninteractive 506 | 507 | RUN apt-get update 508 | RUN apt-get install -y locales 509 | 510 | RUN echo "ja_JP.UTF-8 UTF-8" > /etc/locale.gen && \ 511 | locale-gen ja_JP.UTF-8 && \ 512 | dpkg-reconfigure locales && \ 513 | /usr/sbin/update-locale LANG=ja_JP.UTF-8 514 | 515 | ENV LC_ALL ja_JP.UTF-8 516 | ``` 517 | 518 | 元のディレクトリへ 519 | 520 | ``` 521 | # cd .. 522 | ``` 523 | 524 | `debconf: delaying package configuration, since apt-utils is not installed`は無視して良い 525 | 526 | ``` 527 | # docker-compose build 528 | 529 | # docker-compose up -d 530 | ``` 531 | 532 | DBのログイン確認(パスワード:mysql) 533 | 534 | ``` 535 | # mysql -u root -p -h127.0.0.1 --port=3307 536 | ``` 537 | 538 | データのリストア(パスワード:mysql) 539 | 540 | ``` 541 | # cd /vagrant 542 | # mysql -u root -p -h127.0.0.1 --port=3307 wordpress < export.sql 543 | Enter password: 544 | ``` 545 | 546 | 確認 547 | ``` 548 | # curl -L localhost:8000 549 | # curl localhost 550 | ``` 551 | 552 | ブラウザで192.168.56.50、192.168.56.50:8000を開いて確認する 553 | 554 | ![docker-wordpress-1](./images/step3/docker-wordpress-1.png "docker-wordpress-1") 555 | 556 | ### Quesiton 557 | ブラウザで192.168.56.50、192.168.56.50:8000を開き、挙動について調べましょう 558 | 559 | ## 正規のportで動かす 560 | SELinuxの設定 561 | ``` 562 | # setsebool -P container_connect_any 1 563 | ``` 564 | 565 | httpdの停止 566 | ``` 567 | # systemctl stop httpd.service 568 | # systemctl disable httpd.service 569 | ``` 570 | 571 | 停止と削除 572 | 573 | ``` 574 | # cd /vagrant/docker_mysql 575 | # docker-compose down -v 576 | ``` 577 | 578 | Dockerイメージの削除。mysql、wordpressの`IMAGE ID`を全て指定 579 | 580 | ``` 581 | # docker images 582 | REPOSITORY TAG IMAGE ID CREATED SIZE 583 | wordpress latest 5e943a6f5d83 13 minutes ago 585MB 584 | mysql latest 10dccfee5786 15 minutes ago 545MB 585 | wordpress cac87f4b1402 28 hours ago 546MB 586 | mysql db2b37ec6181 8 days ago 545MB 587 | 588 | # docker rmi 32ea220bd41c 589 | ``` 590 | 591 | docker-compose.ymlを8000:80から80:80に修正 592 | ``` 593 | ports: 594 | - "80:80" 595 | ``` 596 | 597 | ``` 598 | # cd /vagrant/docker_mysql 599 | # docker-compose build 600 | 601 | # docker-compose up -d 602 | ``` 603 | 604 | DBのログイン確認(パスワード:mysql) 605 | 606 | ``` 607 | # mysql -u root -p -h127.0.0.1 --port=3307 608 | ``` 609 | 610 | データのリストアは行わない(8000番で動作するよう設定されているため) 611 | 612 | ブラウザで192.168.56.50を確認 613 | 614 | これでStep3は完了です。 615 | -------------------------------------------------------------------------------- /Step4.md: -------------------------------------------------------------------------------- 1 | # Step4 2 | このStepではこれまでのStepで作成した環境をスクリプトで自動生成します 3 | 4 | (このStepはネットワーク帯域に余裕があり、時間がある場合のみ行う) 5 | 6 | ## 設定ファイルのコピー 7 | Step3までに作成したミドルウェアの設定ファイルをコピー 8 | 9 | ``` 10 | $ cd 1day 11 | $ vagrant ssh 12 | $ sudo su - 13 | # cd /vagrant 14 | # mkdir conf 15 | 16 | # cp /etc/httpd/conf/httpd.conf conf/. 17 | 18 | # cp /etc/php.ini conf/. 19 | 20 | # cp /etc/my.cnf conf/. 21 | # exit 22 | $ exit 23 | ``` 24 | 25 | ## ハンズオン用ディレクトリの作成 26 | HOST OS上の任意のディレクトリで`1day-script`ディレクトリを作成し、遷移しましょう。以降`1day-script`ディレクトリをカレントディレクトリとします。 27 | 28 | ``` 29 | $ mkdir 1day-script 30 | $ cd 1day-script 31 | ``` 32 | 33 | ## Vagrant設定ファイル(Vagrantfile)のコピー 34 | Step3までに利用した`Vagrantfile`をコピー 35 | 36 | ``` 37 | ``` 38 | -------------------------------------------------------------------------------- /images/step1/virtualbox-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step1/virtualbox-1.png -------------------------------------------------------------------------------- /images/step2/httpd-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step2/httpd-1.png -------------------------------------------------------------------------------- /images/step2/wordpress-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step2/wordpress-1.png -------------------------------------------------------------------------------- /images/step2/wordpress-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step2/wordpress-2.png -------------------------------------------------------------------------------- /images/step2/wordpress-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step2/wordpress-3.png -------------------------------------------------------------------------------- /images/step2/wordpress-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step2/wordpress-4.png -------------------------------------------------------------------------------- /images/step2/wordpress-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step2/wordpress-5.png -------------------------------------------------------------------------------- /images/step2/wordpress-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step2/wordpress-6.png -------------------------------------------------------------------------------- /images/step2/wordpress-7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step2/wordpress-7.png -------------------------------------------------------------------------------- /images/step2/wordpress-8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step2/wordpress-8.png -------------------------------------------------------------------------------- /images/step2/wordpress-9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step2/wordpress-9.png -------------------------------------------------------------------------------- /images/step3/docker-wordpress-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step3/docker-wordpress-1.png -------------------------------------------------------------------------------- /images/step3/error-wordpress-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step3/error-wordpress-1.png -------------------------------------------------------------------------------- /images/step3/error-wordpress-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hironomiu/Local-Development-Environment-Hands-On/8f195aa81f82b244fecad08f560e1e487e2b1713/images/step3/error-wordpress-2.png --------------------------------------------------------------------------------