新建
php-apache
目录1
mkdir php-apache
复制配置文件
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
修改
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>创建
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22FROM 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生成镜像并上传,然后运行容器
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访问
服务器IP
docker-compose.yml
配置1
2
3
4
5
6
7
8
9
10
11
12
13php-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
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修改
httpd.conf
1
2
3
4
5
6
7
8
9
10
11LoadModule 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修改
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>创建
Dockerfile
1
2FROM httpd:2.4
RUN mkdir -p /data/app生成镜像并运行容器
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访问
服务器IP
docker-compose.yml
配置1
2
3
4
5
6
7
8
9
10
11
12
13
14httpd:
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 | httpd.conf 设置 DirectoryIndex index.php index.html |