Docker 简易虚拟建站平台(应用级容器)

这篇博客详细介绍了如何在Ubuntu 16服务器上利用Docker容器搭建多站点虚拟服务器。通过安装Docker,拉取Nginx、PHP和MySQL镜像,配置Nginx的多个站点配置文件,实现了通过不同域名访问独立的网站。同时,通过容器间的网络配置,确保了PHP和MySQL的正常通信。最后,博主提供了检查和重启服务的步骤,确保所有站点都能正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备工作:仅需一台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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值