Docker 筆記:Ubuntu 12.04 + Drupal6

總算有一個試成功的案例了~



本機 Ubuntu 18.04
Docker容器 ubuntu1204 (docker影像檔 ubuntu:12.04)
本機透過 nginx proxy_pass http://localhost:9000 設定去run php5.3的環境

先提醒這篇的方式是個笨方法,把所有的東西都塞在Docker容器中
因為我試了獨自run nginx php5.3-fpm mysql postfix串不起來,所以就乾脆直接在完整的Ubuntu 12.04跑所有的服務,再透過 proxy_pass 串接

先建立容器
sudo docker run -d -it --name ubuntu1204 -p 9000:80 ubuntu:12.04

進入容器
sudo docker exec -it ubuntu1204 bin/bash

補齊套件
apt-get update

安裝 MariaDB
apt-get install python-software-properties
apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
add-apt-repository 'deb http://mirror.klaus-uwe.me/mariadb/repo/5.5/ubuntu precise main'
apt-get update
apt-get install mariadb-server
(password maria)

安裝php5.3
apt-get install php5-curl php5-mysql php5-fpm php5-gd
vi
/etc/php5/fpm/pool.d/www.conf ( 這裡非常重要,卡關卡很久! )
listen = 127.0.0.1:9000 => listen = /var/run/php5-fpm.sock
remark
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

安裝nginx
apt-get install nginx

apt-get install postfix 選2
PS.信有寄成功,我點忘記密碼確實有收到信

設定語系(可有可無)
apt-get install vim
locale-gen zh_TW.UTF-8
vi /etc/default/locale
#  File generated by update-locale
LANG="zh_TW.UTF-8"
LC_NUMERIC="zh_TW.UTF-8"
LC_TIME="zh_TW.UTF-8"
LC_MONETARY="zh_TW.UTF-8"
LC_PAPER="zh_TW.UTF-8"
LC_NAME="zh_TW.UTF-8"
LC_ADDRESS="zh_TW.UTF-8"
LC_TELEPHONE="zh_TW.UTF-8"
LC_MEASUREMENT="zh_TW.UTF-8"
LC_IDENTIFICATION="zh_TW.UTF-8"
PS.其實後來發現改了不會作用(即使寫在 ~/.bashrc or /etc/environment 都不會作用)

修改時區
dpkg-reconfigure tzdata (1.選Asia 2.選Taipei)

安裝drupal
apt-get install wget unzip
cd /usr/share/nginx/www
wget https://github.com/d6lts/drupal/archive/6.x.zip
unzip 6.x.zip

docker nginx conf這樣寫
cat /etc/nginx/sites-enabled/d6test
server {
    server_name d6test.com;
    client_max_body_size 20M;
    root /usr/share/nginx/www/drupal6; <-- only="" p="" path="" reference.="" your="">

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # Very rarely should these ever be accessed outside of your lan
    location ~* \.(txt|log)$ {
        allow 192.168.0.0/16;
        deny all;
    }

    location ~ \..*/.*\.php$ {
        return 403;
    }

    location ~ ^/sites/.*/private/ {
        return 403;
    }

    # Block access to "hidden" files and directories whose names begin with a
    # period. This includes directories used by version control systems such
    # as Subversion or Git to store control files.
    location ~ (^|/)\. {
        return 403;
    }

    location / {
        try_files $uri @rewrite; # For Drupal <= 6
        # try_files $uri /index.php?$query_string; # For Drupal >= 7
    }

    location @rewrite {
        rewrite ^/(.*)$ /index.php?q=$1;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

    # Fighting with Styles? This little gem is amazing.
    location ~ ^/sites/.*/files/imagecache/ { # For Drupal <= 6
    # location ~ ^/sites/.*/files/styles/ { # For Drpal >= 7
        try_files $uri @rewrite;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
}


本機nginx的conf檔這樣寫
cat /etc/nginx/sites-enabled/d6
server {
<-- only="" p="" path="" reference.="" your="">    client_max_body_size 20M;
    listen 80;
    server_name d6test.com;
    proxy_connect_timeout       300;
    proxy_send_timeout          300;
    proxy_read_timeout          300;
    send_timeout                300;
    location ~ / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:9000;
    }
}



修改上傳檔案大小限制(本機和docker都要改)
01. nginx config 加  client_max_body_size 20M;

02.vi /etc/php5/fpm/php.ini
post_max_size = 20M
upload_max_filesize = 20M


安裝drush
apt-get install drush
( ubuntu 12.04 時期有內建drush的版本正好適合)

SSL的部份
這裡我有實測成功,在本機執行 certbot -d d6test.com,就能把SSL設定寫入conf檔


後記
後來發現docker重啟後,服務都不會作用

這兩招都沒用
update-rc.d nginx enable
update-rc.d php5-fpm enable
update-rc.d mysql enable
update-rc.d postfix enable

vi /etc/rc.local
/etc/init.d/nginx start
/etc/init.d/php5-pfm start
/etc/init.d/mysql start
/etc/init.d/postfix start

另外,這種作法不知道SSL能否跑的起來,要試才知道

後面如果想要備份容器可參考
https://stackoverflow.com/questions/25135897/how-to-automatically-start-a-service-when-running-a-docker-container

如果這個容器沒問題,可以用 docker commit 把影像跟容器執行過的事情做一個自用的影像檔

我在安裝後想要更新語系檔,語系伺服器都沒有啦~ 只能從舊的匯出使用
Drupal 6已經完全沒支援了很久很久啦,所以建議除非必要,否則.... XD

串接獨立容器的方式
sudo docker run --name mariadb -e MYSQL_ROOT_PASSWORD=liaozi -p 3306:3306 -d mariadb
sudo docker run --name php-fpm --link mariadb -p 9000:9000 -d php:fpm
sudo docker run --name nginx --link php-fpm -p 80:80 -d nginx
若要更方便,就要再學習使用 Docker Compose

把影像跟容器執行過的事情做一個自用的影像檔
查CONTAINER ID
sudo docker ps
製作自己的ubuntu:drupal6
sudo docker commit 64dac5c31e76 ubuntu:drupal6
匯出成獨立檔案
sudo docker save ubuntu:drupal6 | gzip > drupal6.tgz
還原ubuntu:drupal6
gunzip -c drupal6.tgz |sudo docker load
執行
sudo docker run -d -it --name drupal6 -p 9000:80 ubuntu:drupal6
進入
sudo docker exec -it drupal6 bin/bash

留言

熱門文章