在 Min 18.2 下,預裝 php 版本已是 php 7.x,只支援 mysqli 和 pdo、pdo_mysql。舊版 php 5.x 所寫的程式,需要支援 mysql,不然程式已全然不能跑了...。例如 mysql_result() 在 php5 之後已經被棄用 (deprecated) 了。
長期之計還是改程式使用 php7.x 支援的 mysql 語法,但暫時方案可以用虛擬機器 (例如 virtualbox), 或 docker,各有利弊。此次試用 docker 這種方案。
試做用 docker 安裝 LAMP + phpmyadmin
試過 docker 和 docker-compose,在有限的時間了解之後,覺得 docker-compose 似乎比較「系統化」,以下介紹步驟。
安裝 docker 環境
參考:https://yinung2.blogspot.com/2018/07/min-182-docker-wordpress.html
$ sudo apt install docker-ce
安裝 Docker Compose
$ sudo curl -L
"https://github.com/docker/compose/releases/download/1.10.0/docker-compose-$(uname
-s)-$(uname -m)" -o /usr/local/bin/docker-compose給予 Docker Compose 一定的執行權限
$sudo chmod +x /usr/local/bin/docker-compose
在 /home/username/ 下建新的子目錄(pwb0 是示範, 可自訂),例如
$mkdir pwb0
$cd pwb0
再建一個 ./www 子目錄,用來放「你自已的」程式
建立一個 docker-compose.yml 檔案 + 一個 Dockerfile
在剛建的 /home/username/pwb0 子目錄下,建立兩個文字檔,docker-compose.yml 檔案 和 Dockerfile,內容如下附註說明:
(藍色字表示版本,需要時可以自行),用以下的設定,安裝的 LAMP 各版本是Linux c80dfb980100 4.15.0-33-generic #36~16.04.1-Ubuntu SMP
Apache/2.4.25 (Debian)
mysqlnd 5.0.11-dev - 20120503
PHP Version 5.6.37
docker-compose.yml
來源: https://gist.github.com/jcavat/2ed51c6371b9b488d6a940ba1049189b
內容是:
#-------------------------docker-compose.yml---------------------------------
version: "2"
services:
www:
build: .
ports:
- "8000:80"
volumes:
- ./www:/var/www/html/
links:
- db
networks:
- default
db:
image: mysql:5.5
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: my_webgame
MYSQL_USER: my_webgame
MYSQL_PASSWORD: my_webgame_password
MYSQL_ROOT_PASSWORD: root_password
volumes:
- ./dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8001:80
environment:
MYSQL_USER: phpmyadmin_user
MYSQL_PASSWORD: phpmyadmin_password
MYSQL_ROOT_PASSWORD: root_password
volumes:
persistent:
#-------------------------end of docker-compose.yml--------------------------------- services:
www:
build: .
ports:
- "8000:80"
volumes:
- ./www:/var/www/html/
links:
- db
networks:
- default
db:
image: mysql:5.5
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: my_webgame
MYSQL_USER: my_webgame
MYSQL_PASSWORD: my_webgame_password
MYSQL_ROOT_PASSWORD: root_password
volumes:
- ./dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8001:80
environment:
MYSQL_USER: phpmyadmin_user
MYSQL_PASSWORD: phpmyadmin_password
MYSQL_ROOT_PASSWORD: root_password
volumes:
persistent:
Dockerfile
內容是裝 apache+php5 和 mysql 函式庫,如果需要,可改 mysqli, 或 :
#-------------------------dockerfile----------------------------------------
FROM php:5-apache
RUN docker-php-ext-install mysql
RUN docker-php-ext-install mysql
#-------------------------end of dockerfile---------------------------------
啟動 yml
在同一 ./pwb0 的子目錄,下指令 (docker-compose up 是啟動,參數 -d 是在背景執行):
$docker-compose up -d
程式會幫忙把所有需要的 images 檔案,從 Docker Hub抓下來,稍後一下就可以了。
停止 yml
在同一 ./pwb0 的子目錄,下指令:
$docker-compose down
其它相關指令:
查運做中的 images
$docker-compose ps
會出現類似:
username@Mint182 ~/myDocker/pwb0 $ docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------------
pwb0_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
pwb0_phpmyadmin_1 /run.sh supervisord -n Up 0.0.0.0:8001->80/tcp, 9000/tcp
pwb0_www_1 docker-php-entrypoint apac ... Up 0.0.0.0:8000->80/tcp
Name Command State Ports
-------------------------------------------------------------------------------------------
pwb0_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
pwb0_phpmyadmin_1 /run.sh supervisord -n Up 0.0.0.0:8001->80/tcp, 9000/tcp
pwb0_www_1 docker-php-entrypoint apac ... Up 0.0.0.0:8000->80/tcp
如果你替換了 php, phpmyadmin, 或 mysql server 的版本,要重新 build, 用此指令啟動
$docker-compose up -d --build
如果想登入 docker image 裡面去查看,下指令可進入 bash 裡
$sudo docker exec -it pwb0_db_1 /bin/bash
查看你曾下載了哪些 images
$docker images
會出現類似:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
pwb0_www latest 98fbf911ec11 About an hour ago 355MB
wordpress latest 51d61ae2bb49 10 days ago 408MB
mariadb latest 85d4ebaca2a6 10 days ago 363MB
phpmyadmin/phpmyadmin latest 126b8717cebb 10 days ago 166MB
wordpress 4.9.8-php5.6 0a2533f1f86c 2 weeks ago 385MB
mysql 5.7 43b029b6b640 2 weeks ago 372MB
mysql latest 29e0ae3b69b9 2 weeks ago 484MB
mysql 5.5 c43b4117afc4 4 weeks ago 205MB
php 5-apache 12398af6f4de 6 weeks ago 355MB
php 5.5-apache ea0a3d41ce6c 2 years ago 390MB
php 5.4-apache f04f77c9186f 2 years ago 470MB
更改 image 的 tag 名稱
$docker tag pwb0_www:latest pwb0_www:5.0
REPOSITORY TAG IMAGE ID CREATED SIZE
pwb0_www 5.0 98fbf911ec11 7 hours ago 355MB
pwb0_www latest 98fbf911ec11 7 hours ago 355MB
wordpress latest 51d61ae2bb49 10 days ago 408MB
mariadb latest 85d4ebaca2a6 10 days ago 363MB
phpmyadmin/phpmyadmin latest 126b8717cebb 11 days ago 166MB
wordpress 4.9.8-php5.6 0a2533f1f86c 2 weeks ago 385MB
mysql 5.7 43b029b6b640 2 weeks ago 372MB
mysql latest 29e0ae3b69b9 2 weeks ago 484MB
mysql 5.5 c43b4117afc4 4 weeks ago 205MB
php 5-apache 12398af6f4de 6 weeks ago 355MB
php 5.5-apache ea0a3d41ce6c 2 years ago 390MB
php 5.4-apache f04f77c9186f 2 years ago 470MB
echo dirname(__FILE__).'';
echo 'Current PHP version: ' . phpversion();
phpinfo();
?>
REPOSITORY TAG IMAGE ID CREATED SIZE
pwb0_www latest 98fbf911ec11 About an hour ago 355MB
mariadb latest 85d4ebaca2a6 10 days ago 363MB
phpmyadmin/phpmyadmin latest 126b8717cebb 10 days ago 166MB
wordpress 4.9.8-php5.6 0a2533f1f86c 2 weeks ago 385MB
mysql 5.7 43b029b6b640 2 weeks ago 372MB
mysql latest 29e0ae3b69b9 2 weeks ago 484MB
mysql 5.5 c43b4117afc4 4 weeks ago 205MB
php 5-apache 12398af6f4de 6 weeks ago 355MB
php 5.5-apache ea0a3d41ce6c 2 years ago 390MB
php 5.4-apache f04f77c9186f 2 years ago 470MB
更改 image 的 tag 名稱
$docker tag pwb0_www:latest pwb0_www:5.0
REPOSITORY TAG IMAGE ID CREATED SIZE
pwb0_www 5.0 98fbf911ec11 7 hours ago 355MB
pwb0_www latest 98fbf911ec11 7 hours ago 355MB
wordpress latest 51d61ae2bb49 10 days ago 408MB
mariadb latest 85d4ebaca2a6 10 days ago 363MB
phpmyadmin/phpmyadmin latest 126b8717cebb 11 days ago 166MB
wordpress 4.9.8-php5.6 0a2533f1f86c 2 weeks ago 385MB
mysql 5.7 43b029b6b640 2 weeks ago 372MB
mysql latest 29e0ae3b69b9 2 weeks ago 484MB
mysql 5.5 c43b4117afc4 4 weeks ago 205MB
php 5-apache 12398af6f4de 6 weeks ago 355MB
php 5.5-apache ea0a3d41ce6c 2 years ago 390MB
php 5.4-apache f04f77c9186f 2 years ago 470MB
==自已寫的PHP程式要放在哪? ==
在剛有建建的 ~/pwb0/www 的這個子目錄下,就是放自寫的 PHP 程式處,你可以在這個子目錄建立一個 index.php, 內容如下:echo dirname(__FILE__).'';
echo 'Current PHP version: ' . phpversion();
phpinfo();
?>
如果看得到以下畫面,就成功啦!
sudo docker-compose images
內容可能如下:
REPOSITORY TAG IMAGE ID CREATED SIZE
1xxxxxxxxxx latest ebaa2a7dee5e 20 minutes ago 355MB2xxxxxxxxxx latest 837092bc87de 2 weeks ago 421MB3xxxxxxxxxx 5.x 4ee01d11a48b 4 weeks ago 205MB4xxxxxxxxxx latest c6ba363e7c9b 3 months ago 166MB5xxxxxxxxxx 5.x 24c791995c1e 3 months ago 355MB
若要刪除 ebaa2a7dee5e ,則下指令:
sudo docker rmi ebaa2a7dee5e
==移除已安裝的 docker image==
先查已裝的 imagessudo docker-compose images
內容可能如下:
REPOSITORY TAG IMAGE ID CREATED SIZE
1xxxxxxxxxx latest ebaa2a7dee5e 20 minutes ago 355MB2xxxxxxxxxx latest 837092bc87de 2 weeks ago 421MB3xxxxxxxxxx 5.x 4ee01d11a48b 4 weeks ago 205MB4xxxxxxxxxx latest c6ba363e7c9b 3 months ago 166MB5xxxxxxxxxx 5.x 24c791995c1e 3 months ago 355MB
若要刪除 ebaa2a7dee5e ,則下指令:
sudo docker rmi ebaa2a7dee5e
==待研究、紀錄問題 ==
- 存檔 docker save
- 到其它伺服器匯入
- mysql 備份檔「自動」匯入
- 網路設定
- mysql 自動備份
==參考網頁==
- 安裝 docker https://docs.docker.com/install/linux/docker-ce/ubuntu/#set-up-the-repository
- Docker Hub 找各種別人做好的 image 檔: https://hub.docker.com/
- 在 Min 18.2 下試做用 docker 安裝 Wordpress:https://yinung2.blogspot.com/2018/07/min-182-docker-wordpress.html
- phpmyadmin image: https://hub.docker.com/r/phpmyadmin/phpmyadmin/
- docker拉取的php配置文件在哪里:
- Docker 筆記:常用指令 http://sypswiki.dcs.tn.edu.tw/wiki/index.php?title=Docker_%E7%AD%86%E8%A8%98
- 查詢已安裝的 PHP 版本:https://www.phpini.com/php/check-php-version
echo 'Current PHP version: ' . phpversion();
echo dirname(__FILE__);
phpinfo();
?> - docker-compose with php/mysql/phpmyadmin/apache
沒有留言:
張貼留言