0

I have an issue where my PHP version is affecting a project I'm trying to run inside of a docker container.

When I run php -v in the container I get this.

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. 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')

Changing the PHP version seems to fix it. 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.

My docker file looks like below.

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. I looked around on Google and couldn't find anything. Any help is appreciated!

If needed below is my docker-compose.yml file.

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. Where the jumble of #'s is the container id after you run docker-compose up etc...

For those curious the line in PHP it currently errors, its below.

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

$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

The error is because of the UTC, but for some reason this doesn't cause an issue in 5.5.30?

Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154
  • 1
    Are you able to post your PHP code? I know this isn't what you asked, but I think it would be better to fix that error instead of downgrading your PHP version... – klenium Jan 14 '17 at 00:21
  • I mentioned in my post that I can't, there is to many places where errors occur. We are talking millions of lines of code. I will eventually fix code to adapt to php version at a later point in time. Right now I just need it to work for now. – Joseph Astrahan Jan 14 '17 at 00:48
  • How about 5.6 instead since [5.5 is EOLed](http://php.net/eol.php)? Or, better yet, [PHP 7.1](http://php.net/supported-versions.php)? – Sammitch Jan 14 '17 at 00:51
  • Those don't work and 7.0 is even worse for compatibility. I def did try all those. – Joseph Astrahan Jan 14 '17 at 01:16
  • I updated question with the exact php line where it messes up because of the php version just so you have something to reference. By the way, even if 7.0 fixes this particular error, 7.0 broke tons of other things compatibility wise, so only 5.6 is potentially a candidate if it would work anyways. – Joseph Astrahan Jan 14 '17 at 01:28
  • You might want to look at this: http://stackoverflow.com/q/22806870/2625561 I don't think the `date()` causes this exception, rather, the framework you use. You should try removing "UTC" from the end / convert the date to UTC in your program. You said it works on your working machine, maybe it has different MySQL, which ignores the invalid given datetime, and inserts 0000-00-00 00:00:00 instead, which is allowed in older MySQL. Howver, sadly I can't help you in the original question. Good luck! – klenium Jan 14 '17 at 10:51
  • Out of curiosity: why you run `apt-get update` twice, almost in a row? – Marcin Orlowski Jan 17 '17 at 01:03
  • that is a good question, I probably only need it once, I copied from another answer and missed that. – Joseph Astrahan Jan 21 '17 at 23:25

1 Answers1

0

Thanks to help from xn20c I was able to find the solution.

First make your dockerfile look like this.

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.

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. Usually PHP installs this package by default but when you compile by source you have to enable it. This goes for any other packages you might need just look up what the --enable flag would be and add it there.

Finally my run script does some last minute changes to the php.ini file that I think are nice.

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...

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

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)

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...

Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154