0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DockerでCentOS7(PHP7.4 Apatch2.4 MySQL8)を構築してSSHで外部から接続するまで

Posted at

#CentOS7

###説明
CentOS7のコンテナ内でphpとapatchを構成し、MySQLは別のコンテナを用意してCentOSから接続をする。

####ディレクトリ構成

centos
├── Dockerfile
└── php  
     └── php.ini
docker-compose.yml
html
  └── index.php
MySQL  
  └── data

####docker-compose.yml

version: '3.0'
services:
  web:
    build: "./centos7"
    container_name: "centos7"
    ports:
      - 80:80
      - 20022:20022
    volumes:
      - "./html:/var/www/html"
    command: /sbin/init
    privileged: true

  db:
    image: mysql:8.0
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
    ports:
      - "3306:3306"
    volumes:
      - ./mysql/data:/var/lib/mysql
    depends_on:
      - web

####Dockerfile

FROM centos:7

RUN yum -y update && yum clean all

# Repository
# EPEL
RUN yum install -y epel-release
# remi
RUN yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

# Apache2.4 インストール
RUN yum -y install httpd

# Apacheの自動起動設定
RUN systemctl enable httpd

# PHP7.4 インストール
RUN yum -y install --enablerepo=remi,remi-php74 php php-devel php-mbstring php-pdo php-xml php-gd php-fpm php-mysqlnd php-opcache php-pecl-zip libzip5

# php.iniコピー
# COPY ./php/php.ini /etc/php.ini

# vim インストール
RUN yum -y install vim

# SSH接続ここから--------
RUN yum -y update \
    && yum install -y openssh-server \
    openssh-clients \
    && yum clean all

# rootでのログインを許可
# ポートを22から20022に変更
# rootのパスワードをpasswordに設定
RUN sed -ri 's/^#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config \
    && sed -ri 's/^#Port 22/Port 20022/' /etc/ssh/sshd_config \
    && echo 'root:ronaldo7' | chpasswd

EXPOSE 20022
# SSH接続ここまで--------

CMD ["/sbin/init"]

####php.ini

[Date]
date.timezone = "Asia/Tokyo";
[mbstring]
mbstring.internal_encording = "UTF-8"
mbstring.language = "Japanese"

###1. CentOS7インストール
#####参考URL

#MySQL

###1. MySQLインストール
#####参考URL

###2. MySQL設定
#####参考URL

###3. PDO接続確認

#####参考URL

###エラー備忘録

#####1. PDO接続エラー

SQLSTATE[HY000] [2002] No such file or directory

####対応手順

#####1. コンテナに接続

docker container exec -it mydoker_db_1 /bin/bash

#####2. ホストipアドレス確認

root@a81b73e7516c:/# cat /etc/hosts //ipアドレス確認
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.30.0.3      a81b73e7516c

#####3. PDOのホスト名に確認したipアドレスを入力

define('HOSTNAME', '172.30.0.3');

##SSH接続

###1. ssh接続設定

#####参考URL

###エラー備忘録

#####1. composeを再構築した時に発生

//compose再構築
docker-compose build

//sshで接続
ssh -p 20022 root@localhost

//エラー
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:SoL3yQ5xw8FyrICsKI46vZRiaG1wd4BpnuBrjNO4bJ0.
Please contact your system administrator.
Add correct host key in /Users/user/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/user/.ssh/known_hosts:1
ECDSA host key for [localhost]:20022 has changed and you have requested strict checking.
Host key verification failed.

####対処方法

#####1. ローカルPCの.sshにあるknown_hostsファイルを削除

#####2. ssh再接続すると新しいknown_hostsが作成され、接続できる

#####参考URL

#apatch

###備忘録
####1.htaccessが効かない
#####対処方法

  1. httpd.confのAllowOverride Noneの設定をNONEからALLに変更
AllowOverride None

AllowOverride ALL

参考URL

#使ったコマンド

//起動中のコンテナ確認
docker ps

//全てのコンテナ確認
docker ps -a

//compose起動
docker-compose up -d

//compose停止
docker-compose stop

//compose再構築(設定ファイル等を編集した時)
docker-compose build

//compose再起動
docker-compose restart

//コンテナに接続
docker container exec -it mydoker_db_1 /bin/bash
docker exec -it centos7 /bin/bash

//apatch起動
systemctl start httpd.service
systemctl restart httpd

//apatch状態確認
systemctl status httpd.service

//sshd再起動
systemctl restart sshd

//ホストipアドレス確認
cat /etc/hosts

//vimインストール
yum install vim

//エラー表示
ini_set( 'display_errors', 1 );

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?