php Docker化开发环境部署配置

简介

记录一个Docker化的php开发环境部署及配置。包括Mysql5.7和php7-apache,apache开启mod-rewrite,php包括一些常用扩展及imagick和xdebug。

php Dockerfile

FROM php:7.0-apache

RUN apt-get update && apt-get install -y \
libcurl4-gnutls-dev \
libmcrypt-dev \
libmagickwand-dev --no-install-recommends \
&& pecl install imagick xdebug \
&& docker-php-ext-enable imagick xdebug \
&& echo "xdebug.remote_enable=on\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_autostart=off\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_port=9000\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& docker-php-ext-install \
iconv \
mcrypt \
opcache \
curl \
gd \
mysqli \
pdo \
pdo_mysql \
zip
RUN /usr/sbin/a2dismod 'mpm_*' && /usr/sbin/a2enmod mpm_prefork rewrite

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

运行docker build -t local/php:7.0-apache .制作镜像。

整体 docker-compose.yml

version: "3"
services:

  db:
    image: mysql:5.7
    volumes:
      - <PATH/TO>/apache-php/data/mysql/:/var/lib/mysql/
    ports:
      - "3306:3306"
    expose:
      - "3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
    networks:
      - db_net

  php7:
    build: .
    image: local/php:7.0-apache
    depends_on:
      - db
    volumes:
      - <PATH/TO>/apache-php/config/php/custom.ini:/usr/local/etc/php/conf.d/custom.ini
      - <PATH/TO>/apache-php/config/apache/apache2.conf:/etc/apache2/apache2.conf
      - <PATH/TO>/apache-php/config/apache/my-conf/:/etc/apache2/my-conf/
      - <PATH/TO>/apache-php/log/php/:/var/log/apache2/
      - <PATH/TO>/webroot:/var/www/html/
    ports:
      - "80:80"
    environment:
      XDEBUG_CONFIG: remote_host=<YOUR-LOCAL-IP, eg.192.168.1.100>
    restart: always
    networks:
      - db_net

networks:
  db_net:
    driver: bridge

custom.ini空文件即可,之后可加入自定义php配置。
apache2.conf

Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
	Options FollowSymLinks
	AllowOverride None
	Require all denied
</Directory>

<Directory /usr/share>
	AllowOverride None
	Require all granted
</Directory>

<Directory /var/www/>
	Options Indexes FollowSymLinks
	AllowOverride None
	Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
	Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf

# my config
IncludeOptional my-conf/*.conf

之后就可以在docker-compose.yml的目录执行docker-compose up -d启动整套环境了。

phpstorm的xdebug配置

打开phpstorm的设置,一次进入Languages & Frameworks > PHP > Debug > DBGp Proxy,填上docker-compose中一样的本机ip及默认端口9000。之后就能用phpstorm监听xdebug了。
php中如需用到本机的mysql,地址需用db代替原本的localhost

Leave a comment

* - Required fields