虽说php已经更新到7了,但还是有很多旧系统跑在php5.x,那程序员就经常需要使用到php5.x版本去维护它们。
由于本人开发php7的系统用的是laradock做的环境,laradock支持切换php版本,但不支持2个版本同时运行,每次开发还要编辑下配置文件,还是挺烦的,所以我加了个配置,允许它同时支持2个版本,当然更多版本的原理也是一样的。
laradock真香!自己配置了一个docker,但是对比了laradock的配置,看了他的.env、docker-compose.yml和其他的dockfile文件,差的不是一点半点,于是决定还是用大神的轮子吧。
在docker-compose.yml文件中复制php-fpm的配置,改为下面的代码
php-fpm56:
build:
context: ./php-fpm
#引用Dockerfile-56
dockerfile: Dockerfile-56
args:
- PHP_VERSION=5.6
- INSTALL_XDEBUG=${PHP_FPM_INSTALL_XDEBUG}
- INSTALL_BLACKFIRE=${INSTALL_BLACKFIRE}
- INSTALL_SOAP=${PHP_FPM_INSTALL_SOAP}
- INSTALL_IMAP=${PHP_FPM_INSTALL_IMAP}
- INSTALL_MONGO=${PHP_FPM_INSTALL_MONGO}
- INSTALL_AMQP=${PHP_FPM_INSTALL_AMQP}
- INSTALL_MSSQL=${PHP_FPM_INSTALL_MSSQL}
- INSTALL_ZIP_ARCHIVE=${PHP_FPM_INSTALL_ZIP_ARCHIVE}
- INSTALL_BCMATH=${PHP_FPM_INSTALL_BCMATH}
- INSTALL_GMP=${PHP_FPM_INSTALL_GMP}
- INSTALL_PHPREDIS=${PHP_FPM_INSTALL_PHPREDIS}
- INSTALL_MEMCACHED=${PHP_FPM_INSTALL_MEMCACHED}
- INSTALL_OPCACHE=${PHP_FPM_INSTALL_OPCACHE}
- INSTALL_EXIF=${PHP_FPM_INSTALL_EXIF}
- INSTALL_AEROSPIKE=${PHP_FPM_INSTALL_AEROSPIKE}
- INSTALL_MYSQLI=${PHP_FPM_INSTALL_MYSQLI}
- INSTALL_PGSQL=${PHP_FPM_INSTALL_PGSQL}
- INSTALL_PG_CLIENT=${PHP_FPM_INSTALL_PG_CLIENT}
- INSTALL_TOKENIZER=${PHP_FPM_INSTALL_TOKENIZER}
- INSTALL_INTL=${PHP_FPM_INSTALL_INTL}
- INSTALL_GHOSTSCRIPT=${PHP_FPM_INSTALL_GHOSTSCRIPT}
- INSTALL_LDAP=${PHP_FPM_INSTALL_LDAP}
- INSTALL_SWOOLE=${PHP_FPM_INSTALL_SWOOLE}
- INSTALL_IMAGE_OPTIMIZERS=${PHP_FPM_INSTALL_IMAGE_OPTIMIZERS}
- INSTALL_IMAGEMAGICK=${PHP_FPM_INSTALL_IMAGEMAGICK}
volumes:
- ./php-fpm/php${PHP_VERSION}.ini:/usr/local/etc/php/php.ini
- ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}
expose:
- "9000"
ports:
- 9001:9000
extra_hosts:
- "dockerhost:${DOCKER_HOST_IP}"
environment:
- PHP_IDE_CONFIG=${PHP_IDE_CONFIG}
networks:
- backend
修改nginx的配置
nginx:
build:
context: ./nginx
args:
- PHP_UPSTREAM_CONTAINER=${NGINX_PHP_UPSTREAM_CONTAINER}
- PHP_UPSTREAM_PORT=${NGINX_PHP_UPSTREAM_PORT}
volumes:
- ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}
- ${NGINX_HOST_LOG_PATH}:/var/log/nginx
- ${NGINX_SITES_PATH}:/etc/nginx/sites-available
ports:
- "${NGINX_HOST_HTTP_PORT}:80"
- "${NGINX_HOST_HTTPS_PORT}:443"
depends_on:
- php-fpm
#增加links,后面在配置host里的fastcgi_pass的时候用
links:
- php-fpm56
networks:
- frontend
- backend
在php-fpm文件夹复制Dockerfile,重命名为Dockerfile-56 由于perl安装phpredis扩展的方式不支持php5.6了,所以需要在这里做下判断,换一种安装方式,修改代码如下:
ARG INSTALL_PHPREDIS=false
RUN if [ ${INSTALL_PHPREDIS} = true ]; then \
if [ "${PHP_VERSION}" -ge 7 ] ; then \
# Install Php Redis Extension
printf "\n" | pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& docker-php-ext-enable redis ; \
else \
curl -L -o /tmp/phpredis.tar.gz https://github.com/phpredis/phpredis/archive/3.1.4.tar.gz \
&& mkdir -p phpredis \
&& tar -C phpredis -zxvf /tmp/phpredis.tar.gz --strip 1 \
&& ( \
cd phpredis \
&& phpize \
&& ./configure \
&& make -j$(nproc) \
&& make install \
) \
#&& rm -r phpredis \
&& rm /tmp/phpredis.tar.gz \
&& docker-php-ext-enable phpredis ; \
fi \
;fi
如果phpredis/archive/3.1.4.tar.gz这个包还是不支持,那么可以再换一个,搜索php5.6支持的phpredis扩展版本
同理,安装过程中如果memchached和swoole扩展报错,也可能是memchached和swoole的版本问题,和上面一样使用编译安装即可。
在nginx/sites下新增一个tesg.conf
server {
listen 80;
server_name test.test;
index index.php index.htm index.html;
#项目目录
set $wwwroot /var/www/project;
root $wwwroot;
index index.php index.htm index.html;
#sendfile off;
location ~ .*\.(php|php5)?$ {
root $wwwroot;
#和之前的links对应
fastcgi_pass php-fpm56:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $wwwroot$fastcgi_script_name;
include fastcgi_params;
}
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
#access log
#access_log /home/wwwlogs/queue.log main;
}
在项目project下新增index.php
<?php phpinfo();?>
测试是否是php5.6版本
本文为林明潭原创文章,转载无需和我联系,但请注明来自林明潭的博客blog.umaske.com
最新评论