Docker 簡易虛擬建站平臺(應用級容器)

準備工作:僅需一臺Ubuntu16服務器(宿主機)即可
初始化宿主機
# apt install openssh-server
# /etc/init.d/ssh restart
# apt install software-properties-common
# add-apt-repository -y ppa:nginx/stable
# add-apt-repository -y ppa:ondrej/php
# apt update

第一步:宿主機安裝Docker
# apt install docker.io
# apt install docker.compose #快速編排 .yml
# apt update

第二步:注意宿主機無需安裝Nginx及其它,全由鏡像完成


第三步:準備三個鏡像
1、安裝Nginx鏡像
# docker pull nginx
# 宿主機準備站點文件目錄,分別存放入 /var/www/ 下
# 如目錄 /site1/    /site2/,且分別有頁面文件
# 如 phpinfo();
-----------------------------------------------
<?php
    phpinfo();
?>
-----------------------------------------------
# 宿主機準備站點CONF文件[site1.conf/site2.conf]存放入  /usr/local/nginx/conf/conf.d/ 下
site1.conf--------------------------------------------------------------------------------------------------------------------------------
# php
server {
    charset utf-8;
    client_max_body_size 128M;

    listen 80; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name site1.ichmuseum.com;        # 綁定域名
    root        /var/www/site1/;            # 站點目錄
    index       index.php;

    location / {
        #-e表示只要filename存在,則爲真
        if (!-e $request_filename){
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
        }
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # uncomment to avoid processing of calls to non-existing static files by Yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

    # deny accessing php files for the /assets directory
    location ~ ^/assets/.*\.php$ {
        deny all;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 172.17.0.3:9000;    # PHP容器內部IP
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $uri =404;
    }

    location ~* /\. {
        deny all;
    }
}
-------------------------------------------------------------------------------------------------------------------------------------------
site2.conf--------------------------------------------------------------------------------------------------------------------------------
# php
server {
    charset utf-8;
    client_max_body_size 128M;

    listen 80; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name site2.ichmuseum.com;        # 綁定域名
    root        /var/www/site1/;            # 站點目錄
    index       index.php;

    location / {
        #-e表示只要filename存在,則爲真
        if (!-e $request_filename){
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
        }
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # uncomment to avoid processing of calls to non-existing static files by Yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

    # deny accessing php files for the /assets directory
    location ~ ^/assets/.*\.php$ {
        deny all;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 172.17.0.3:9000;    # PHP容器內部IP
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $uri =404;
    }

    location ~* /\. {
        deny all;
    }
}
-------------------------------------------------------------------------------------------------------------------------------------------
# 宿主Nginx先關閉(如果有的話)
# service nginx stop
# docker run --name mynginx -p 80:80 -v /var/www:/var/www -v /usr/local/nginx/conf/conf.d:/etc/nginx/conf.d -d nginx
# site1.conf 和 site2.conf 經過 docker run 命令映射到容器中
# 如需再增加站點,先在宿主機中新建 site.conf 文件後(格式參考上述),restart 容器。
# docker restart [container ID]
2、安裝PHP鏡像
# docker pull php:5.6-fpm
# docker run -p 9000:9000 --name  phpfpm -v /var/www:/var/www -d php:5.6-fpm
# 獲知PHP容器內部IP
# docker inspect --format '{{.NetworkSettings.IPAddress}}'  [container ID]    

3、安裝MySQL鏡像
# docker pull mysql:5.6
# docker run -p 3306:3306 --name mysql -v /usr/local/mysql:/etc/mysql/sqlinit -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
# 獲知MySQL容器內部IP
# docker inspect --format '{{.NetworkSettings.IPAddress}}'  [container ID]    

4、PHP容器開啓PDO MySQL
# 準備php.ini文件 拷貝入PHP容器:/usr/local/etc/php
# 拷貝宿主機當前目錄下文件到容器指定目錄中
# docker cp php.ini  [container ID]:/usr/local/etc/php
# 拷貝容器中文件或目錄到指定宿主機目錄中
# docker container cp -a [container ID]:/usr/local/etc/php  /var/www/
# 重啓PHP容器
# docker restart [container ID]

第四步:檢查容器和服務器端口
# docker ps    
# netstat -antlp|grep LISTEN
# 重啓宿主機Nginx
# service nginx restart

最後訪問站點 http://site1.ichmuseum.com    http://site2.ichmuseum.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章