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

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