クリエイター:メタボ兔

ウェブやアプリの開発者で利用する色な技術やサーバーや開発環境の設定について共有する場

Macでphpenvを利用して7.4インストール

Macでインストールでエラー

krb5のインストールが必要な時

No package 'krb5-gssapi' found
No package 'krb5' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables KERBEROS_CFLAGS
and KERBEROS_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

opensslのインストール(Update)が必要な時

configure: error: Package requirements (openssl >= 1.0.1) were not met:

No package 'openssl' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables OPENSSL_CFLAGS
and OPENSSL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

icu4cのインストールが必要な時

configure: WARNING: unrecognized options: --with-png-dir, --with-libxml-dir, --with-icu-dir
configure: error: Package requirements (icu-uc >= 50.1 icu-io icu-i18n) were not met:

No package 'icu-uc' found
No package 'icu-io' found
No package 'icu-i18n' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables ICU_CFLAGS
and ICU_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

libeditのインストールが必要な時

configure: WARNING: unrecognized options: --with-png-dir, --with-libxml-dir, --with-icu-dir
configure: WARNING: libedit directory ignored, rely on pkg-config
configure: error: Package requirements (libedit) were not met:

No package 'libedit' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables EDIT_CFLAGS
and EDIT_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

libxml2のインストールやパス設定が必要な時

configure: WARNING: unrecognized options: --with-png-dir, --with-libxml-dir, --with-icu-dir
configure: WARNING: unrecognized options: --with-png-dir, --with-libxml-dir, --with-icu-dir
warning: unsupported relocation in debug_info section.
note: while processing /private/var/tmp/php-build/source/7.4.0/ext/opcache/.libs/zend_accelerator_util_funcs.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ext/opcache/.libs/opcache.a(shared_alloc_shm.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ext/opcache/.libs/opcache.a(shared_alloc_shm.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: /var/tmp/php-build/source/7.4.0/modules/opcache.a(shared_alloc_shm.o) has no symbols
/var/tmp/php-build/source/7.4.0/ext/libxml/libxml.c:34:10: fatal error: 'libxml/parser.h' file not found
#include <libxml/parser.h>
         ^~~~~~~~~~~~~~~~~
1 error generated.
make: *** [ext/libxml/libxml.lo] Error 1

bzip2のインストールやパス設定が必要な時

configure: WARNING: unrecognized options: --with-png-dir, --with-libxml-dir, --with-icu-dir
configure: error: Please reinstall the BZip2 distribution

libiconvのインストールやパス設定な時

configure: WARNING: unrecognized options: --with-png-dir, --with-libxml-dir, --with-icu-dir
configure: error: Please specify the install prefix of iconv with --with-iconv=<DIR>

解決

% brew install autoconf bzip2 icu4c krb5 libedit libiconv libjpeg libpng libxml2 libzip oniguruma openssl@1.1 pkg-config tidy-html5
% PKG_CONFIG_PATH="/usr/local/opt/krb5/lib/pkgconfig:/usr/local/opt/icu4c/lib/pkgconfig:/usr/local/opt/libedit/lib/pkgconfig:/usr/local/opt/libjpeg/lib/pkgconfig:/usr/local/opt/libpng/lib/pkgconfig:/usr/local/opt/libxml2/lib/pkgconfig:/usr/local/opt/libzip/lib/pkgconfig:/usr/local/opt/oniguruma/lib/pkgconfig:/usr/local/opt/openssl@1.1/lib/pkgconfig:/usr/local/opt/tidy-html5/lib/pkgconfig" \
PHP_BUILD_CONFIGURE_OPTS="--with-bz2=/usr/local/opt/bzip2 --with-iconv=/usr/local/opt/libiconv" \
phpenv install 7.4snapshot

PHPのDTOの作成:カプセル化

背景

Javaのエンジニアの経験から見るとPhpのクラスの甘さが良いとも悪いとも言えます。その中でDTOで使用するクラスをもうちょっとある属性&関数のみ使用するようにしてカプセル化をしたいところです。

定義してない属性の設定や参照の禁止

    public function __set($key, $value){
        if (property_exists($this, $key)) {
            $this->{$key} = $value;
        }
        throw new \BadMethodCallException('Method "'.$key.'" does not exist.');
    }

    public function __get($key)
    {
        if (property_exists($this, $key)) {
            return $this->{$key};
        }
        throw new \BadMethodCallException('Method "'.$key.'" does not exist.');
    }

初期ソースは簡単に考えましたが、ちょっと待って!! 「property_exists」ってpublicだけじゃない〜〜〜

それでこんな感じにしました。

    public function __set($key, $value){
        if ($this->existsProperty($key)) {
            $this->{$key} = $value;
        }
        throw new \BadMethodCallException('Method "'.$key.'" does not exist.');
    }

    public function __get($key)
    {
        if ($this->existsProperty($key)) {
            return $this->{$key};
        }
        throw new \BadMethodCallException('Method "'.$key.'" does not exist.');
    }

    private function existsProperty($propertyName) {
        $reflection = new ReflectionObject($this);
        $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
        foreach ($properties as $property) {
            if ($property->getName() === $propertyName) return true;
        }
        return false;
    }

Setter/Getterの自動化

Javaの実装でgetter/setterの作成の面倒です。Lombokのような物もありますね。それで以下のソースを用意しました。

    public function __call($name, $args) {
        if (strncmp($name, 'get', 3) === 0) {
            $pascalCName = substr($name, 3);
            $snakeCName = $this->pascalCase2SnakeCase($pascalCName);

            if (property_exists($this, $snakeCName)) {
                return $this->{$snakeCName};
            }
        } else if (strncmp($name, 'set', 3) === 0) {
            $pascalCName = substr($name, 3);
            $snakeCName = $this->pascalCase2SnakeCase($pascalCName);

            if (property_exists($this, $snakeCName)) {
                return $this->{$snakeCName} = reset($args);
            }
        }
        throw new \BadMethodCallException('Method "'.$name.'" does not exist.');
    }

    private function pascalCase2SnakeCase($input) {
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
        $ret = $matches[0];
        foreach ($ret as &$match) {
            $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
        }
        return implode('_', $ret);
    }

このソースではpublic以外のも設定可能にしないといけないので「property_exists」を利用しました。

完成

abstract class Dto
{
    public function __call($name, $args) {
        if (strncmp($name, 'get', 3) === 0) {
            $pascalCName = substr($name, 3);
            $snakeCName = $this->pascalCase2SnakeCase($pascalCName);

            if (property_exists($this, $snakeCName)) {
                return $this->{$snakeCName};
            }
        } else if (strncmp($name, 'set', 3) === 0) {
            $pascalCName = substr($name, 3);
            $snakeCName = $this->pascalCase2SnakeCase($pascalCName);

            if (property_exists($this, $snakeCName)) {
                return $this->{$snakeCName} = reset($args);
            }
        }
        throw new \BadMethodCallException('Method "'.$name.'" does not exist.');
    }

    public function __set($key, $value){
        if ($this->existsProperty($key)) {
            $this->{$key} = $value;
        }
        throw new \BadMethodCallException('Method "'.$key.'" does not exist.');
    }

    public function __get($key)
    {
        if ($this->existsProperty($key)) {
            return $this->{$key};
        }
        throw new \BadMethodCallException('Method "'.$key.'" does not exist.');
    }

    private function pascalCase2SnakeCase($input) {
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
        $ret = $matches[0];
        foreach ($ret as &$match) {
            $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
        }
        return implode('_', $ret);
    }

    private function existsProperty($propertyName) {
        $reflection = new ReflectionObject($this);
        $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
        foreach ($properties as $property) {
            if ($property->getName() === $propertyName) r[f:id:FattyRabbit:20201230123544j:plain]eturn true;
        }
        return false;
    }
}

クラス名のセンスはないので。。。

phpenvでインストール:configure: error: Cannot find libz

f:id:FattyRabbit:20201229193310p:plain

インストール環境

OS

macOS Catalina
version 10.15.3

Error

$ phpenv install 7.1.19
[Info]: Loaded extension plugin
[Info]: Loaded apc Plugin. [Info]: Loaded composer Plugin.
[Info]: Loaded github Plugin. [Info]: Loaded uprofiler Plugin.
[Info]: Loaded xdebug Plugin. [Info]: Loaded xhprof Plugin.
[Info]: Loaded zendopcache Plugin.
[Info]: php.ini-production gets used as php.ini
[Info]: Building 7.1.19 into /Users/******/.anyenv/envs/phpenv/versions/7.1.19
[Skipping]: Already downloaded and extracted https://secure.php.net/distributions/php-7.1.19.tar.bz2 [Preparing]: /var/tmp/php-build/source/7.1.19
-----------------
| BUILD ERROR |
-----------------
Here are the last 10 lines from the log:
-----------------------------------------
configure: error: Cannot find libz
-----------------------------------------
The full Log is available at '/tmp/php-build.7.1.19.20200227165241.log'. [Warn]: Aborting build.

解決

$ brew install autoconf bison bzip2 curl icu4c libedit libjpeg libiconv libpng libxml2 libzip openssl re2c tidy-html5 zlib
$ CONFIGURE_OPTS="--with-zlib-dir=$(brew --prefix zlib) --with-bz2=$(brew --prefix bzip2) --with-curl=$(brew --prefix curl) --with-iconv=$(brew --prefix libiconv) --with-libedit=$(brew --prefix libedit) --with-readline=$(brew --prefix readline) --with-tidy=$(brew --prefix tidy-html5)" phpenv install [version]

環境変数等をまとめて「phpenv install [version]」だけ出来る方法がまだ見つからない状態です。

7.ウェブサーバー:Dockerfile作成

じゃ、いよいよウェブサーバーのDockerファイルを作成してみましょう!!

f:id:FattyRabbit:20201229190022p:plain

docker-composer.yml

先ずは使用する環境変数ファイル「.env」を作成します。

SERVER_NAME=local

こちらで使用するメインドメインを設定します。

次はdocker-compose.ymlに使用する内容です。

version: "3"
services:
  web:
    image: web
    build:
      context: ./web
      dockerfile: Dockerfile
      args:
        SERVER_NAME: $SERVER_NAME
    container_name: web
    command: /sbin/init
    volumes:
      - ./web/www:/var/www:rw
    tty: true
    stdin_open: true
    privileged: true
    restart: always
    external_links:
      - dba
    logging:
      options:
        max-size: 5m
        max-file: "5"
    networks:
      local-server-net:
        ipv4_address: 172.16.0.5
  • build.context: ./web:docker-composeのフォルダ構成です。
  • build.dockerfile: Dockerfile:dockerfile名です。
  • build.args.SERVER_NAME: $SERVER_NAME:Dockerfileに渡すパラメーターで.envのSERVER_NAME(local)を渡します。
  • volumes:docker-compose内のフォルダにコンテイナーのapacheのdocument-rootへマウントします。webフォルダにはプロジェクトことにフォルダを作成するかシンボルリングを作成するつもりです。

Dockerfile

次はDockerfileです。内容が多く一々説明をするのでは面倒でも時間がないことでファイルのみ共有させていただきます。以前使用したDockerfileなので実際作成する際に修正が加えるかもです。

FROM centos:7
MAINTAINER fatty.rabbit.dev@gmail.com

# Declare Arguments
ARG SERVER_NAME=example.com:80

# Set the environment of anyenv
ENV ANYENV_HOME /opt/anyenv
ENV ANYENV_ENV  $ANYENV_HOME/envs
ENV ANYENV_DEFINITION_ROOT $ANYENV_HOME/share/anyenv-install/
# Set the environment of phpenv
ENV PHPENV_ROOT $ANYENV_ENV/phpenv

# TimeZoneの設定
RUN rm -rf /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
RUN echo "LC_ALL=ja_JP.UTF-8" >> /etc/environment && \
    echo "LC_CTYPE=ja_JP.UTF-8" >> /etc/environment && \
    echo "LANG=ja_JP.UTF-8" >> /etc/environment
RUN localedef -f UTF-8 -i ja_JP /usr/lib/locale/ja_JP.UTF-8
RUN localedef -f UTF-8 -i ja_JP ja_JP

# yumアップデート
RUN yum -y update

# apacheのインストール:v2.4.6
RUN yum install -y httpd

# apacheの設定
RUN rm -rf /etc/httpd/conf.d/welcome.conf
RUN cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak
RUN sed -ri '/<Directory "\/var\/www\/html">/,/<\/Directory>/s/    AllowOverride None/    AllowOverride All/' /etc/httpd/conf/httpd.conf && \
    sed -ri '/<Directory "\/var\/www\/html">/,/<\/Directory>/s/    Options Indexes FollowSymLinks/    Options Indexes FollowSymLinks Includes/' /etc/httpd/conf/httpd.conf && \
    sed -ri 's/DirectoryIndex index.html/DirectoryIndex index.html index.php/' /etc/httpd/conf/httpd.conf && \
    sed -i -e "s/\#ServerName www.example.com:80/ServerName ${SERVER_NAME}/g" /etc/httpd/conf/httpd.conf

# remiリポジトリの登録
RUN yum install -y epel-release
RUN rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

# SSHDのインストール
RUN yum -y install openssh-server

# SSHDの設定
RUN sed -ri 's/^#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config && \
    sed -ri 's/^#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config && \
    sed -ri 's/^UsePAM yes/UsePAM no/' /etc/ssh/sshd_config

# rootユーザーのパスワード変更
RUN echo 'root:12345678' | chpasswd

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

# PHPの依存するパッケージをインストール
RUN yum install -y gcc gcc-c++ wget make file php-fpm unzip git rsync automake libcurl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker

# system wide
# anyenvを入れる
RUN git clone https://github.com/riywo/anyenv $ANYENV_HOME
RUN git clone https://github.com/anyenv/anyenv-install $ANYENV_DEFINITION_ROOT
RUN echo 'export PATH="'$ANYENV_HOME'/bin:$PATH"' >> /etc/profile.d/anyenv.sh && \
    echo 'export ANYENV_ROOT="'$ANYENV_HOME'"'  >> /etc/profile.d/anyenv.sh && \
    echo 'export ANYENV_DEFINITION_ROOT="'$ANYENV_HOME'/share/anyenv-install"' >> /etc/profile.d/anyenv.sh && \
    chmod 755 /etc/profile.d/anyenv.sh

ENV PATH $ANYENV_HOME/bin:$PATH
ENV ANYENV_ROOT $ANYENV_HOME
ENV ANYENV_DEFINITION_ROOT $ANYENV_HOME/share/anyenv-install

# phpenvを入れる
RUN anyenv install phpenv

# phpのビルド環境を整える
RUN yum install -y libxml2-devel bison bison-devel libjpeg-devel libpng-devel readline-devel libmcrypt libmcrypt-devel libxslt-devel httpd-devel enchant-devel libXpm libXpm-devel freetype-devel t1lib t1lib-devel gmp-devel libicu-devel net-snmp net-snmp-devel bzip2 bzip2-devel
RUN sed -i -e '/--with-mcrypt/d' $ANYENV_HOME/envs/phpenv/plugins/php-build/share/php-build/default_configure_options
RUN sed -i -e '/--with-tidy/d' $ANYENV_HOME/envs/phpenv/plugins/php-build/share/php-build/default_configure_options
RUN yum --enablerepo=epel install -y libtidy-devel re2c

# cmakeのインストール
RUN wget https://cmake.org/files/v3.12/cmake-3.12.0-Linux-x86_64.tar.gz -O cmake-3.12.0-Linux-x86_64.tar.gz && \
    tar zxvf cmake-3.12.0-Linux-x86_64.tar.gz

RUN rm -rf /usr/share/aclocal && \
    rm -rf /usr/share/applications && \
    rm -rf /usr/share/mime

RUN mkdir -p /usr/bin && \
    cp -r cmake-3.12.0-Linux-x86_64/bin/* /usr/bin/ && \
    mkdir -p /usr/share && \
    cp -r cmake-3.12.0-Linux-x86_64/share/* /usr/share/ && \
    rm -rf cmake-3.12.0-Linux-x86_64

# libzipのインストール
RUN yum remove -y libzip libzip-devel
RUN mkdir /tmp/libzip && \
    cd /tmp/libzip && \
    curl -sSLO https://libzip.org/download/libzip-1.4.0.tar.gz && \
    tar zxf libzip-1.4.0.tar.gz && \
    cd libzip-1.4.0/ && \
    mkdir build && \
    cd build && \
    cmake ../ && \
    make && \
    make install

# ERROR:configure: error: off_t undefined; check your library configuration
# 以下を削除:Mysqlを利用してない場合
# 参考:http://www.8wave.net/ldconfig.html
# vi /etc/ld.so.conf
RUN echo '/usr/local/lib' >> /etc/ld.so.conf
RUN echo '/usr/lib ' >> /etc/ld.so.conf
RUN ldconfig

# php7.3をインストル
ENV PATH $ANYENV_ENV/phpenv/bin:$ANYENV_ENV/phpenv/shims:$PATH
RUN ls -al $ANYENV_ENV/phpenv
RUN PHP_BUILD_CONFIGURE_OPTS=--with-pear PHP_BUILD_EXTRA_MAKE_ARGUMENTS=-j4 phpenv install -v 7.3.0
# php7.1.20をインストル
RUN PHP_BUILD_CONFIGURE_OPTS=--with-pear PHP_BUILD_EXTRA_MAKE_ARGUMENTS=-j4 phpenv install -v 7.1.20
# php7.3.3をインストル
RUN PHP_BUILD_CONFIGURE_OPTS=--with-pear PHP_BUILD_EXTRA_MAKE_ARGUMENTS=-j4 phpenv install -v 7.3.3

# linkを作成
RUN ln -s /opt/anyenv/envs/phpenv/shims/php /bin/php
RUN ln -s /opt/anyenv/envs/phpenv/shims/pecl /bin/pecl

# imageMagickをインストール
RUN yum install -y ImageMagick-devel ImageMagick

# php環境を設定
# 7.1.20
RUN phpenv global 7.1.20 && \
    phpenv rehash && \
    yes '' | pecl install imagick
RUN echo "extension = imagick.so" >> $PHPENV_ROOT/versions/7.1.20/etc/php.ini && \
    echo 'xdebug.remote_enable = 1' >> $PHPENV_ROOT/versions/7.1.20/etc/conf.d/xdebug.ini && \
    echo 'xdebug.remote_connect_back = 1' >> $PHPENV_ROOT/versions/7.1.20/etc/conf.d/xdebug.ini && \
    echo 'xdebug.remote_port = 9001' >> $PHPENV_ROOT/versions/7.1.20/etc/conf.d/xdebug.ini && \
    echo 'xdebug.max_nesting_level = 512' >> $PHPENV_ROOT/versions/7.1.20/etc/conf.d/xdebug.ini && \
    echo 'xdebug.idekey = "PHPSTORM"' >> $PHPENV_ROOT/versions/7.1.20/etc/conf.d/xdebug.ini
# 7.3.0
RUN phpenv global 7.3.0 && \
    phpenv rehash && \
    yes '' | pecl install imagick
RUN echo "extension = imagick.so" >> $PHPENV_ROOT/versions/7.3.0/etc/php.ini && \
    echo 'xdebug.remote_enable = 1' >> $PHPENV_ROOT/versions/7.3.0/etc/conf.d/xdebug.ini && \
    echo 'xdebug.remote_connect_back = 1' >> $PHPENV_ROOT/versions/7.3.0/etc/conf.d/xdebug.ini && \
    echo 'xdebug.remote_port = 9001' >> $PHPENV_ROOT/versions/7.3.0/etc/conf.d/xdebug.ini && \
    echo 'xdebug.max_nesting_level = 512' >> $PHPENV_ROOT/versions/7.3.0/etc/conf.d/xdebug.ini && \
    echo 'xdebug.idekey = "PHPSTORM"' >> $PHPENV_ROOT/versions/7.3.0/etc/conf.d/xdebug.ini
# 7.3.3
RUN phpenv global 7.3.3 && \
    phpenv rehash && \
    yes '' | pecl install imagick
RUN echo "extension = imagick.so" >> $PHPENV_ROOT/versions/7.3.3/etc/php.ini && \
    echo 'xdebug.remote_enable = 1' >> $PHPENV_ROOT/versions/7.3.3/etc/conf.d/xdebug.ini && \
    echo 'xdebug.remote_connect_back = 1' >> $PHPENV_ROOT/versions/7.3.3/etc/conf.d/xdebug.ini && \
    echo 'xdebug.remote_port = 9001' >> $PHPENV_ROOT/versions/7.3.3/etc/conf.d/xdebug.ini && \
    echo 'xdebug.max_nesting_level = 512' >> $PHPENV_ROOT/versions/7.3.3/etc/conf.d/xdebug.ini && \
    echo 'xdebug.idekey = "PHPSTORM"' >> $PHPENV_ROOT/versions/7.3.3/etc/conf.d/xdebug.ini

# composerのインストール
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer

# supervisordのインストール
RUN yum install -y supervisor

# php-fpmサービスを追加
COPY php-fpm*.service /etc/systemd/system/
RUN grep -l '$PHPENV_ROOT' /etc/systemd/system/php-fpm*.service | xargs sed -i -e "s/\$PHPENV_ROOT/$(echo $PHPENV_ROOT | sed -e 's/\//\\\//g')/g"
RUN cat /etc/systemd/system/php-fpm7.3.3.service

# サンプルのVirtualhostの設定ファイルを追加
RUN mkdir /var/www/sample-7.1.20
RUN mkdir /var/www/sample-7.3.0
COPY *_virtual.conf /etc/httpd/conf.d/
RUN grep -l '$SERVER_NAME' /etc/httpd/conf.d/*_virtual.conf | xargs sed -i -e "s/\$SERVER_NAME/$(echo $SERVER_NAME | sed -e 's/\//\\\//g; s/\:/\\\:/g; s/\./\\\./g;')/g"
RUN cat /etc/httpd/conf.d/7.3.0_virtual.conf

# postfixをインストール
RUN yum install -y postfix mailx
RUN systemctl enable postfix

# syslogの追加
RUN yum install -y rsyslog
RUN systemctl enable rsyslog

# supervisordの設定
RUN touch /etc/supervisord.conf
RUN sed -i -e 's/nodaemon=false/nodaemon=true/g' /etc/supervisord.conf
COPY supervisord-fpm*-service.ini /etc/supervisord.d/
RUN touch /etc/supervisord.d/supervisord-ssh.ini && \
    echo '[program:sshd]' >> /etc/supervisord.d/supervisord-ssh.ini && \
    echo 'command=systemctl start sshd' >> /etc/supervisord.d/supervisord-ssh.ini
RUN touch /etc/supervisord.d/supervisord-httpd.ini && \
    echo '[program:apache]' >> /etc/supervisord.d/supervisord-httpd.ini && \
    echo 'command=systemctl start httpd' >> /etc/supervisord.d/supervisord-httpd.ini
RUN touch /etc/supervisord.d/supervisord-postfix.ini && \
    echo '[program:postfix]' >> /etc/supervisord.d/supervisord-postfix.ini && \
    echo 'command=systemctl start postfix' >> /etc/supervisord.d/supervisord-postfix.ini

# php-fpmの設定
RUN sed -i -e 's/^user = nobody/user = apache/g' $PHPENV_ROOT/versions/7.1.20/etc/php-fpm.d/www.conf && \
    sed -i -e 's/^group = nobody/group = apache/g' $PHPENV_ROOT/versions/7.1.20/etc/php-fpm.d/www.conf && \
    sed -i -e 's/^listen = 127.0.0.1:9000/listen = \/var\/run\/php-fpm7.1.20.sock/g' $PHPENV_ROOT/versions/7.1.20/etc/php-fpm.d/www.conf && \
    sed -i -e 's/^;listen.owner = nobody/listen.owner = apache/g' $PHPENV_ROOT/versions/7.1.20/etc/php-fpm.d/www.conf && \
    sed -i -e 's/^;listen.group = nobody/listen.group = apache/g' $PHPENV_ROOT/versions/7.1.20/etc/php-fpm.d/www.conf
RUN sed -i -e 's/^user = nobody/user = apache/g' $PHPENV_ROOT/versions/7.3.0/etc/php-fpm.d/www.conf && \
    sed -i -e 's/^group = nobody/group = apache/g' $PHPENV_ROOT/versions/7.3.0/etc/php-fpm.d/www.conf && \
    sed -i -e 's/^listen = 127.0.0.1:9000/listen = \/var\/run\/php-fpm7.3.0.sock/g' $PHPENV_ROOT/versions/7.3.0/etc/php-fpm.d/www.conf && \
    sed -i -e 's/^;listen.owner = nobody/listen.owner = apache/g' $PHPENV_ROOT/versions/7.3.0/etc/php-fpm.d/www.conf && \
    sed -i -e 's/^;listen.group = nobody/listen.group = apache/g' $PHPENV_ROOT/versions/7.3.0/etc/php-fpm.d/www.conf
RUN sed -i -e 's/^user = nobody/user = apache/g' $PHPENV_ROOT/versions/7.3.3/etc/php-fpm.d/www.conf && \
    sed -i -e 's/^group = nobody/group = apache/g' $PHPENV_ROOT/versions/7.3.3/etc/php-fpm.d/www.conf && \
    sed -i -e 's/^listen = 127.0.0.1:9000/listen = \/var\/run\/php-fpm7.3.3.sock/g' $PHPENV_ROOT/versions/7.3.3/etc/php-fpm.d/www.conf && \
    sed -i -e 's/^;listen.owner = nobody/listen.owner = apache/g' $PHPENV_ROOT/versions/7.3.3/etc/php-fpm.d/www.conf && \
    sed -i -e 's/^;listen.group = nobody/listen.group = apache/g' $PHPENV_ROOT/versions/7.3.3/etc/php-fpm.d/www.conf

EXPOSE 80 443

CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]

大きく以下のデモン・ツールをインストールします。

使用ポートは80と443のみです。また、serviceファイルやsupervisordのファイルは一部COPYですが、実際の時はcomposeでシンボルリングにする可能性もあります。

6.ウェブサーバー:anyenv

anyenvは個人でも主に開発環境のところで便利でよく使うものです。今回のウェブサーバーのcontainerでphpenvを利用するためにシステムワイドにインストールする方法をやって考えてみます。複数のプロジェクトの複数のphpを使うことを想定しております。

f:id:FattyRabbit:20201229185515p:plain

システムワイドのインストール

既存のウロジェクトでやった部分があるのでそれを使います。内容をDockerfile化する必要があります。

$ cd /opt
$ git clone https://github.com/riywo/anyenv
$ echo 'export PATH="/opt/anyenv/bin:$PATH"' >> /etc/profile.d/anyenv.sh
$ echo 'export ANYENV_ROOT="/opt/anyenv"' >> /etc/profile.d/anyenv.sh
$ echo 'export ANYENV_DEFINITION_ROOT="/opt/anyenv/share/anyenv-install"' >> /etc/profile.d/anyenv.sh
$ echo 'eval "$(anyenv init -)"' >> /etc/profile.d/anyenv.sh
$ exec $SHELL -l

リンクの作成

上記の設定だけでも使う時、上手く動かない場合がありますので以下のリンクも作成します。

$ ln -s /opt/anyenv/envs/phpenv/shims/php /bin/php
$ ln -s /opt/anyenv/envs/phpenv/shims/pecl /bin/pecl