Docker 搭建 LAMP

  1. 新建 php-apache 目录

    1
    mkdir php-apache
  2. 复制配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    # 1.进入宿主机 php-apache 目录
    cd /data/php-apache

    # 2.拉取 php:7.2-apache
    docker pull php:7.2-apache

    # 3.拉取 php-extension-installer
    docker pull mlocati/php-extension-installer

    # 4. 复制 php.ini-development
    docker run --rm php:7.2-apache cat /usr/local/etc/php/php.ini-development > php.ini

    # 5.复制 000-default.conf 至 conf.d 目录
    docker run --rm php:7.2-apache cat /etc/apache2/sites-enabled/000-default.conf > ./conf.d/default.conf
  1. 修改 default.conf

    1
    2
    3
    4
    5
    6
    7
    <VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "/data/app/default"
    ServerName localhost
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
  2. 创建 Dockerfile

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    FROM php:7.2-apache
    RUN mkdir -p /data/app

    RUN ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load

    ENV APACHE_DOCUMENT_ROOT /data/app
    RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
    RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

    # Install Extensions
    COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
    RUN install-php-extensions zip gd xdebug redis \
    && docker-php-ext-install bcmath pcntl pdo_mysql

    # Add Composer
    RUN php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');" \
    && php composer-setup.php \
    && php -r "unlink('composer-setup.php');" \
    && mv composer.phar /usr/local/bin/composer

    ENV TZ=Asia/Shanghai
    RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
  3. 生成镜像并上传,然后运行容器

    1
    2
    3
    4
    5
    6
    7
    8
    # 生成镜像
    docker build -t sevming/php72-apache:0.1 .

    # 上传镜像
    docker push sevming/php72-apache:0.1

    # 运行容器
    docker run -itd --rm --name php-apache -p 80:80 -v /data/app:/data/app -v /data/php-apache/php.ini:/usr/local/etc/php/php.ini -v /data/php-apache/conf.d:/etc/apache2/sites-enabled --net lnmp --ip 172.18.0.8 sevming/php72-apache:0.1
  4. 访问 服务器IP

  5. docker-compose.yml 配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    php-apache:
    build: ./php-apache
    container_name: php-apache
    restart: always
    ports:
    - 80:80
    volumes:
    - /data/php-apache/php.ini:/usr/local/etc/php/php.ini
    - /data/php-apache/conf.d:/etc/apache2/sites-enabled
    - /data/app:/data/app
    networks:
    lnmp:
    ipv4_address: 172.18.0.8

以上 PHP 连接 Apache 采用的 mod_php, 下面是 PHP-FPM 的安装方式, 但是有个问题暂未解决, 暂时不推荐

安装步骤
  1. 复制配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # 1.进入 httpd 目录
    cd /data/httpd

    # 2.拉取 httpd
    docker pull httpd:2.4

    # 3.复制 httpd.conf 至当前目录
    docker run --rm httpd:2.4 cat /usr/local/apache2/conf/httpd.conf > httpd.conf

    # 4.复制 default.conf 至当前 conf.d 目录
    docker run --rm httpd:2.4 cat /usr/local/apache2/conf/conf.d/default.conf > ./conf.d/default.conf
  2. 修改 httpd.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
    LoadModule rewrite_module modules/mod_rewrite.so
    <Directory />
    AllowOverride all
    Require all granted
    </Directory>
    <IfModule dir_module>
    DirectoryIndex index.php index.html
    </IfModule>
    include conf.d/*.conf
  3. 修改 default.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/data/app/default"
    ServerName localhost
    #ServerAlias www.dummy-host.example.com
    ErrorLog "logs/default.com-error_log"
    CustomLog "logs/default.com-access_log" common

    <Directory "/data/app/default">
    Options None
    Require all granted
    </Directory>

    ProxyPassMatch "^/(.*\.php(/.*)?)$" "fcgi://172.18.0.7:9000/data/app/default/$1"
    </VirtualHost>
  4. 创建 Dockerfile

    1
    2
    FROM httpd:2.4
    RUN mkdir -p /data/app
  5. 生成镜像并运行容器

    1
    2
    3
    4
    5
    # 生成镜像
    docker build -t sevming/httpd .

    # 运行容器
    docker run -itd --rm --name httpd -p 80:80 -v /data/app:/data/app -v /data/httpd/httpd.conf:/usr/local/apache2/conf/httpd.conf -v /data/httpd/conf.d:/usr/local/apache2/conf.d --net lnmp --ip 172.18.0.8 sevming/httpd
  6. 访问 服务器IP

  7. docker-compose.yml 配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    httpd:
    build: ./httpd
    container_name: httpd
    restart: always
    ports:
    - 80:80
    - 443:443
    volumes:
    - /data/httpd/httpd.conf:/usr/local/apache2/conf/httpd.conf
    - /data/httpd/conf.d:/usr/local/apache2/conf.d
    - /data/app:/data/app
    networks:
    lamp:
    ipv4_address: 172.18.0.8
问题
1
2
3
httpd.conf 设置 DirectoryIndex index.php index.html
项目目录下只有 index.html, 访问项目会报 File not found, 不会识别 index.html
目前未找到解决方案, 所以采用 php:7.2-apache 作为镜像源
0%