Use docker to run multiple versions of php on the server
- 2021-01-22 08:14:27
- OfStack
php7 has been around for a while now and it is well known for its significant performance improvements. Now suppose we have an older server running some old projects on centos6, which by default comes with php version 5.3.
Although, we can upgrade to php7 version, but, the old is not compatible with php7, we can not 1 knife cut. The best way is to coexist php5.3 with php7. Therefore, we can consider using docker to install other versions of php, which can be environment independent with very little performance loss.
The following is an example of how to install php7 version.
centos6 install docker:
yum install -y https://get.docker.com/rpm/1.7.1/centos-6/RPMS/x86_64/docker-engine-1.7.1-1.el6.x86_64.rpm
service docker start
chkconfig docker on
Pull the mirror image of php7.2
docker pull php:7.2-fpm
Create a new directory and file /usr/local/ docker-php7 / zz-docker. conf and save the following:
[global]
daemonize = no
[www]
listen = 9001
; Changes to modify php. ini variables inside php_value array corresponding attributes
php_value[session.save_handler] = redis
php_value[session.save_path] = tcp://127.0.0.1:6379
php_value[post_max_size] = 20M
php_value[upload_max_filesize] = 20M
php_value[date.timezone] = Asia/Shanghai
php_value[opcache.enable] = 1
php_value[opcache.enable_cli] = 1
Run the container and use host mode to communicate with the host
docker run -d -v /var/www/html:/var/www/html -v /usr/local/docker-php7/zz-docker.conf:/usr/local/etc/php-fpm.d/zz-docker.conf --net=host --name php7.2 php:7.2-fpm
Install various commonly used php extensions
docker exec php7.2 apt-get update -y
docker exec php7.2 apt-get install -y libfreetype6-dev
docker exec php7.2 apt-get install -y libjpeg62-turbo-dev
docker exec php7.2 apt-get install -y libpng-dev
docker exec php7.2 docker-php-ext-install pdo_mysql
docker exec php7.2 docker-php-ext-install mysqli
docker exec php7.2 docker-php-ext-install iconv
docker exec php7.2 docker-php-ext-install gd
docker exec php7.2 docker-php-ext-install mbstring
docker exec php7.2 docker-php-ext-install opcache
# Change the configuration by the way
docker exec php7.2 mv /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini
Unable to get through
docker-php-ext-install
Install the redis extension, so you need to install it manually
# Enter the command line in the container
docker exec -it php7.2 sh
docker-php-source extract
curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/4.2.0.tar.gz
tar -zxvf /tmp/redis.tar.gz -C /usr/src/php/ext
mv /usr/src/php/ext/phpredis-* /usr/src/php/ext/phpredis
docker-php-ext-install phpredis
# Here by ctr+p and ctrl+q To exit the container
docker restart php7.2
The above command has successfully run php7.2 on port 9001. The php script will point to port 9001 in the nginx configuration.
conclusion