简体   繁体   English

通过DockerFile而不是PHP 5.5.9安装PHP 5.5.30

[英]Install PHP 5.5.30 via DockerFile instead of PHP 5.5.9

I have an issue where my PHP version is affecting a project I'm trying to run inside of a docker container. 我有一个问题,我的PHP版本正在影响我试图在docker容器中运行的项目。

When I run php -v in the container I get this. 当我在容器中运行php -v时,我得到了这个。

PHP 5.5.9-1ubuntu4.20 (cli) (built: Oct  3 2016 13:00:37) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

My goal is to make the "php -v" command results look like below . 我的目标是使“ php -v”命令结果如下所示 Which it does right now on a working machine. 现在可以在正在运行的计算机上执行的操作。

PHP 5.5.30 (cli) (built: Dec 12 2015 21:28:27)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
    with Suhosin v0.9.36, Copyright (c) 2007-2014, by SektionEins GmbH

The reason I need to downgrade is something changed in regards to how the date() function works between these two versions that causes a crashing error in the PDO Extention, something to this tune ( exception 'PDOException' with message 'SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2017-01-13 23:47:31 UTC' ) 我需要降级的原因是,关于date()函数在这两个版本之间的工作方式发生了一些变化,导致PDO扩展崩溃,此调整( 消息为'SQLSTATE [22007]的异常'PDOException':无效的日期时间格式:1292不正确的日期时间值:'2017-01-13 23:47:31 UTC'

Changing the PHP version seems to fix it. 更改PHP版本似乎可以解决该问题。 To save time since its a huge project and I need to just deploy it to active users I need to switch the PHP version down. 为了节省时间,因为它是一个庞大的项目,因此我只需要将其部署到活动用户中,就需要关闭PHP版本。

My docker file looks like below. 我的docker文件如下所示。

FROM ubuntu:14.04
MAINTAINER Joe Astrahan <****>

VOLUME ["/var/www"]

RUN apt-get update && \
    apt-get install -y software-properties-common && \
    apt-get update && \
    apt-get install -y \
      apache2 \
      curl \
      libcurl3 \
      libcurl3-dev \
      php5 \
      php5-cli \
      libapache2-mod-php5 \
      php5-gd \
      php5-json \
      php5-ldap \
      php5-mysql \
      php5-pgsql \
      php5-curl

COPY config/apache_default.conf /etc/apache2/sites-available/000-default.conf
COPY config/run /usr/local/bin/run
RUN chmod +x /usr/local/bin/run
RUN a2enmod rewrite

EXPOSE 80
CMD ["/usr/local/bin/run"]

The run file is below: 运行文件如下:

#!/bin/bash
set -e

PHP_ERROR_REPORTING=${PHP_ERROR_REPORTING:-"E_ALL & ~E_DEPRECATED & ~E_NOTICE"}
sed -ri 's/^display_errors\s*=\s*Off/display_errors = On/g' /etc/php5/apache2/php.ini
sed -ri 's/^display_errors\s*=\s*Off/display_errors = On/g' /etc/php5/cli/php.ini
sed -ri "s/^error_reporting\s*=.*$//g" /etc/php5/apache2/php.ini
sed -ri "s/^error_reporting\s*=.*$//g" /etc/php5/cli/php.ini
echo "error_reporting = $PHP_ERROR_REPORTING" >> /etc/php5/apache2/php.ini
echo "error_reporting = $PHP_ERROR_REPORTING" >> /etc/php5/cli/php.ini

source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND

I am not sure how to install a particular version of php 5.5.30 since it always installs 5.9. 我不确定如何安装特定版本的php 5.5.30,因为它始终安装5.9。 I looked around on Google and couldn't find anything. 我在Google上四处张望,找不到任何东西。 Any help is appreciated! 任何帮助表示赞赏!

If needed below is my docker-compose.yml file. 如果需要的话,下面是我的docker-compose.yml文件。

version: '2'
services:
    dblive:
        image: mysql:5.7.17
        volumes:
            - ./db_data_live:/var/lib/mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: ****
            MYSQL_DATABASE: ****
            MYSQL_USER: ****
            MYSQL_PASSWORD: ****

    dbdev:
        image: mysql:5.7.17
        volumes:
            - ./db_data_dev:/var/lib/mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: ****
            MYSQL_DATABASE: ****
            MYSQL_USER: ****
            MYSQL_PASSWORD: ****

    phpmyadmin:
        depends_on:
            - dblive
            - dbdev
        image: phpmyadmin/phpmyadmin
        environment:
            PMA_ARBITRARY : 1
        restart: always
        ports:
            - "8081:80"

    web:
        build: ./
        depends_on:
            - dblive
            - dbdev
        volumes:
            - ./web:/var/www
            - ./config/php.ini:/usr/local/etc/php/php.ini
            - ./config/apache_default.conf:/etc/apache2/sites-enabled/000-default.conf
        restart: always
        ports: 
            - "80:80"
            - "443:443"
        environment:
           TEST: testenviromentvar

I blurred out the passwords but just put your own to make the database work. 我模糊了密码,但只输入了自己的密码以使数据库正常工作。 Note, use docker exec 8d8a115f6d6b php -v to test php version in the container. 请注意,使用docker exec 8d8a115f6d6b php -v测试容器中的php版本。 Where the jumble of #'s is the container id after you run docker-compose up etc... 在您运行docker-compose up等之后,#的混乱是容器ID ...

For those curious the line in PHP it currently errors, its below. 对于那些好奇的PHP当前错误行,如下。

$class->created = date('Ymd H:i:s', time()); $ class-> created = date('Ymd H:i:s',time());

$validated = $class->save(); $ validated = $ class-> save();

I get this exact error message, exception 'PDOException' with message 'SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2017-01-13 23:47:31 UTC' for column 'created' at row 1' in /var/www/public_html/devsite/vendor/php-activerecord/php-activerecord/lib/Connection.php:322 我收到此确切的错误消息,消息为'SQLSTATE [22007]的异常'PDOException':无效的日期时间格式:1292错误的日期时间值:'第1行'列'的'2017-01-13 23:47:31 UTC'在/var/www/public_html/devsite/vendor/php-activerecord/php-activerecord/lib/Connection.php:322中

The error is because of the UTC, but for some reason this doesn't cause an issue in 5.5.30? 该错误是由于UTC引起的,但是由于某种原因,这不会在5.5.30中引起问题吗?

Thanks to help from xn20c I was able to find the solution. 多亏了xn20c的帮助,我才能够找到解决方案。

First make your dockerfile look like this. 首先使您的dockerfile看起来像这样。

FROM ubuntu:14.04
MAINTAINER Your Name <your@email.com>

VOLUME ["/var/www"]


RUN apt-get update && \
    apt-get install -y software-properties-common && \
    apt-get update && \
    apt-get install -y \
      apache2 \
      curl \
      libcurl3 \
      libcurl3-dev \
      php5 \
      php5-cli \
      libapache2-mod-php5 \
      php5-gd \
      php5-json \
      php5-ldap \
      php5-mysqlnd \
      php5-pgsql \
      php5-curl \
      mysql-client

COPY config/php.ini /etc/php5/apache2/php.ini

# install php-5.5.30
COPY config/install_php-5.5.30.sh /tmp/install_php-5.5.30.sh
RUN /bin/bash /tmp/install_php-5.5.30.sh


COPY config/apache_default.conf /etc/apache2/sites-available/000-default.conf
COPY config/run /usr/local/bin/run

RUN chmod +x /usr/local/bin/run
RUN a2enmod rewrite

EXPOSE 80
CMD ["/usr/local/bin/run"]

Next you will notice its looking for a run script at the end and it also has a special script which installs php 5.5.30 from source. 接下来,您会注意到它在结尾处寻找运行脚本,并且它还有一个特殊的脚本,该脚本从源代码安装php 5.5.30。

install_php-5.5.30.sh install_php-5.5.30.sh

#!/bin/bash

# install dependencies
apt-get -y update && \
apt-get install -y \
  build-essential \
  apache2-dev \
  libxml2-dev

# download PHP 5.5.30 source code
cd /tmp
curl -fsSL http://php.net/get/php-5.5.30.tar.bz2/from/this/mirror | tar xjf -
cd php-5.5.30

# configure build options
./configure --prefix=/usr \
            --with-config-file-path=/etc/php5/apache2 \
            --with-config-file-scan-dir=/etc/php5/apache2/conf.d \
            --disable-pdo \
            --disable-json \
            --enable-mbstring \
            --with-apxs2

# compile and install
NUM_CORES=`cat /proc/cpuinfo | grep processor | wc -l`
make -j $NUM_CORES
make install

# configure extension directory
echo 'extension_dir="/usr/lib/php5/20121212"' >> /etc/php5/apache2/php.ini

# cleanup
rm -rf /tmp/php-5.5.30 /tmp/install_php-5.5.30.sh

The above installs PHP and enables the mbstring module with --enable-mbstring. 上面安装了PHP,并使用--enable-mbstring启用了mbstring模块。 Usually PHP installs this package by default but when you compile by source you have to enable it. 通常,PHP默认情况下会默认安装此软件包,但是当您按源代码编译时,必须启用它。 This goes for any other packages you might need just look up what the --enable flag would be and add it there. 这适用于任何其他软件包,您可能只需要查找--enable标志并将其添加到那里即可。

Finally my run script does some last minute changes to the php.ini file that I think are nice. 最后,我的运行脚本在最后一刻对php.ini文件做了一些更改,我认为这很好。

run

#!/bin/bash
set -e

PHP_ERROR_REPORTING=${PHP_ERROR_REPORTING:-"E_ALL & ~E_DEPRECATED & ~E_NOTICE"}
sed -ri 's/^display_errors\s*=\s*Off/display_errors = On/g' /etc/php5/apache2/php.ini
sed -ri 's/^display_errors\s*=\s*Off/display_errors = On/g' /etc/php5/cli/php.ini
sed -ri "s/^error_reporting\s*=.*$//g" /etc/php5/apache2/php.ini
sed -ri "s/^error_reporting\s*=.*$//g" /etc/php5/cli/php.ini
echo "error_reporting = $PHP_ERROR_REPORTING" >> /etc/php5/apache2/php.ini
echo "error_reporting = $PHP_ERROR_REPORTING" >> /etc/php5/cli/php.ini

source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND

Finally put it all together with a docker-compose file like this... 最后将所有内容与这样的docker-compose文件放在一起...

version: '2'
services:
    dblive:
        image: mysql:5.5.52
        volumes:
            - ./db_data_live:/var/lib/mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: ****
            MYSQL_DATABASE: ****
            MYSQL_USER: ****
            MYSQL_PASSWORD: ****

    dbdev:
        image: mysql:5.5.52
        volumes:
            - ./db_data_dev:/var/lib/mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: ****
            MYSQL_DATABASE: ****
            MYSQL_USER: ****
            MYSQL_PASSWORD: ****

    phpmyadmin:
        depends_on:
            - dblive
            - dbdev
        image: phpmyadmin/phpmyadmin
        environment:
            PMA_ARBITRARY : 1
        restart: always
        ports:
            - "8081:80"

    web:
        build: ./
        depends_on:
            - dblive
            - dbdev
        volumes:
            - ./web:/var/www
            - ./config/apache_default.conf:/etc/apache2/sites-enabled/000-default.conf
        restart: always
        ports: 
            - "80:80"
            - "443:443"

Then simply run... docker-compose up -d --remove-orphans 然后只需运行... docker-compose up -d --remove-orphans

and put the Dockerfile in the same directory and you have a completely working php 5.5.30 server with MySQL support (live & dev databases) and even PHPMyAdmin which can be accessed on port 8081 (localhost:8081) 并将Dockerfile放在同一目录中,您就可以使用MySQL支持(实时和开发数据库)的完全正常工作的php 5.5.30服务器,甚至可以通过端口8081(localhost:8081)访问的PHPMyAdmin

Hope this helps someone! 希望这对某人有帮助!

-- Important Side Notes -- -重要注意事项-

Make sure your php.ini file does not change the extention_dir or you will have problems like CURL not finding the correct extension path working etc... 确保您的php.ini文件没有更改extensionion_dir,否则您将遇到诸如CURL无法找到正确的扩展路径等问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM