docker 搭建nginx+php5.6+mysql

    因为要重新做服务器系统,正好把公司官网,放到容器中,生成个镜像,以后就不用管它了。我看看公司官网使用php写的,并且使用thinkPHP3.1的框架,看到这里崩溃了。没办法,服务器得做系统,硬着头皮上吧。泪奔~就在这里发发牢骚吧。

准备把公司官网的项目简称php项目,放到docker中,既然都放到容器中了,就把nginx、mysql、php的环境一块搭建起来吧,以后再迁移,直接pull就行了。

       在本地我做了测试,遇到的坑太多了,thinkPHP3.1好多东西都不兼容php7,选择安装php5.6。nginx版本安装最新的就行了。

       你们看过配置的过程,会有疑问为什么不直接在Dockerfile中写脚本,安装这些,我也试了,因为要使用apt,太慢了,我接受不了,所以我直接下载centos7镜像,在这里面安装配置的。

一、下载Centos7

    docker pull centos  这里使用的最新的centos

二、进入容器

    docker run -tid --name mycentos1 --privileged=true -v /sys/fs/cgroup:/sys/fs/cgroup centos /usr/sbin/init

  遇到在容器内无法执行systemctl命令,挂载 /sys/fs/cgroup 目录到容器的 /sys/fs/cgroup,但是docker run 命令必须加上-d参数,如果不加-d参数仍然会卡死。

    docker ps 查看容器id

    执行docker exec -it bd9ef607c59f  /bin/bash

三、安装vim

  1. yum update
  2. rpm -qa|grep vim

        

       3.yum -y install vim*

        

四、安装nginx

        请参考:https://my.oschina.net/lwenhao/blog/1830477

        安装nginx几乎不会遇到什么问题。

五、安装php

        1. yum install epel-release

        2. rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

            

        3.查看可安装的包

            yum list php*

        4.安装php6

yum -y install php56w.x86_64 yum -y --enablerepo=webtatic install php56w-devel   yum -y install php56w-xml.x86_64 php56w-gd.x86_64 php56w-ldap.x86_64 php56w-mbstring.x86_64 php56w-mcrypt.x86_64 php56w-mysql.x86_64 php56w-pdo.x86_64 php56w-opcache.x86_64   yum -y install php56w-fpm

 六、安装mysql

        请参考:https://my.oschina.net/lwenhao/blog/1841235

         在执行systemctl start mysql时遇到了启动失败的问题。

            

        解决方法:rm -Rf /var/lib/mysql/*

七、把php项目放入容器中的/usr/share/nginx/html/

            

八、配置容器中的nginx

    配置如下:        server {             listen       80;             server_name  localhost;                 #charset koi8-r;             #access_log  /var/log/nginx/host.access.log  main;                          root   /usr/share/nginx/html; #项目的路径             index   index.php;                  location / {                 if (!-e $request_filename){                     rewrite ^(.*)$ /index.php?s=$1 last;                 }             }                          #error_page  404              /404.html;             # redirect server error pages to the static page /50x.html             #                          error_page   500 502 503 504  /50x.html;             location = /50x.html {                 root   /usr/share/nginx/html;             }                          # proxy the PHP scripts to Apache listening on 127.0.0.1:80             #             #location ~ \.php$ {             #    proxy_pass   http://127.0.0.1;             #}                          # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000             #             location ~ \.php$ {                 fastcgi_pass   127.0.0.1:9000;                 fastcgi_index  index.php;                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;                 include        fastcgi_params;             }                      # deny access to .htaccess files, if Apache's document root             # concurs with nginx's one             #             #location ~ /\.ht {             #    deny  all;            #}         }

八、配置宿主机的nginx

       给定端口号为12001配置如下:

    upstream website {``        server localhost:12001; #你给定的端口 }     server {         listen       80;         server_name  www.lwenhao.cn; #测试的域名   #charset koi8-r;         #access_log  logs/host.access.log  main;                  location / {             # root   html;             # index  index.html index.htm;             proxy_pass http://website/;             proxy_set_header Host $host:$server_port;             proxy_set_header X-Real_IP $remote_addr;             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;             proxy_set_header X-Forwarded-Scheme $scheme;             proxy_connect_timeout 3;             proxy_read_timeout 3;             proxy_send_timeout 3;             access_log off;             break;         }                  error_page  404              /404.html;                  # redirect server error pages to the static page /50x.html         error_page   500 502 503 504  /50x.html;         location = /50x.html {             #   root   html;         } }

十、重新生成新的容器

        1.查看运行的容器

            docker ps

        2.生成新的容器

         docker commit -m "add nginx_php_mysql" -a "liuwenhao" bd9ef607c59f myphp

十一、启动改容器

    1.启动

            docker run -tid -p 12001:80  myphp

    2.nginx、mysql、php-frm是否启动

        如果没有启动,可通过docker exec –it bd9ef607c59f nginx 命令启动

        也可以进入容器内部启动,也可以在Dockerfile中写上要执行的命令,重新生成。我的正常启动了,就不用写。

十二、导出镜像

    1.查看容器

        docker images

    2.导出并且存为website.tar文件

        docker save website -o /home/liuwenhao/images/website.tar

        

十三、部署到其他服务器上

        1.下载到另一台服务器

scp -r root@www.lwenhao.cn:/home/liuwenhao/website.tar /home/liuwenhao/

        2.导入

            docker load -i website.tar

    

        3.启动

docker run --privileged  -tid --name website -p 12001:80 -v /sys/fs/cgroup:/sys/fs/cgroup  website

        4.配置该服务器上的nginx

                重复第九步!就ok了

转载于:https://my.oschina.net/lwenhao/blog/1858585

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值