centos7下用docker搭建lnmp环境以及安装composer

本文介绍如何使用Docker安装并配置LNMP环境,包括安装Docker、设置开机启动、安装PHP、MySQL及Nginx容器,配置容器间的链接以及通过命令行进行连接和配置。

安装docker
yum -y install docker

设置docker开机启动
systemctl enable docker

安装php mysql nginx
docker pull php-fpm
docker pull nginx
docker pull mysql:5.7

docker logs 容器名称-----查看容器日志情况

先要关闭防火墙
systemctl stop firewalld

创建mysql容器(删除容器 docker rm 容器id) --privileged=true 给容器加上特定权限 --restart=always开机启动容器
docker run  --privileged=true --restart=always --name docker-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /data/mysql/conf.d:/etc/mysql/conf.d -d mysql:5.7
启动mysql容器
docker start docker-mysql 
配置通过命令行连接mysql(-it在容器内产生伪终端交互,进入后不能用vim命令要在里面通过apt-get install vim安装)
docker exec -it docker-mysql /bin/bash
mysql -u root -p

创建php容器(--link和mysql容器关联)
docker run --privileged=true --restart=always --name docker-php -v /data/nginx/www/html:/var/www/html -p 9000:9000 --link docker-mysql -d php:7.0-fpm
进入容器内使用php命令
docker exec -it docker-php /bin/bash
安装php扩展
docker-php-ext-install mysqli
容器外使用php命令
docker exec docker-php /usr/local/bin/php -v

创建nginx容器
docker run --privileged=true --restart=always --name docker-nginx -v /data/nginx/www/html:/var/www/html -p 80:80 --link docker-php -d nginx:latest
进入nginx(默认配置文件/etc/nginx/nginx.conf)
docker exec -it docker-nginx /bin/bash
从docker容器复制nginx配置文件方便修改
docker cp docker-nginx:/etc/nginx/nginx.conf /data/nginx/conf/nginx.conf
docker cp docker-nginx:/etc/nginx/conf.d/default.conf /data/nginx/conf/default.conf
编辑/data/nginx/conf/default.conf文件
server {
    listen 80;
    server_name www.webtest.com;

    location / {
        root   /var/www/html;
        index index.html index.htm index.php;
    }

    location ~ \.php$ {
        root         /var/www/html;
        fastcgi_pass docker-php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
导入配置文件到容器
docker cp /data/nginx/conf/default.conf docker-nginx:/etc/nginx/conf.d/default.conf 

docker restart docker-nginx
在/data/nginx/www/html目录下创建一个php文件测试下应该可以成功

安装php的composer包管理工具
进入php容器 
docker exec -it docker-php /bin/bash
cd /usr/local/bin
执行 curl -s https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
之后在php容器就可以使用composer命令了

docker上安装的php扩展默认只在cli模式下生效通过phpinfo查看并未在fpm模式下生效需要手动修改php.ini文件添加扩展
重命名下php.ini文件
mv /usr/local/php/etc/php.ini-production /usr/local/php/etc/php.ini

退出php容器
exit

docker cp docker-php:/usr/local/etc/php/php.ini /data/nginx/conf/php.ini
vim /data/nginx/conf/php.ini
docker cp /data/nginx/conf/php.ini docker-php:/usr/local/etc/php/php.ini 

docker restart docker-php
之后扩展在fpm下就生效了

对已经创建的容器可以使用如下更新命令
docker update --restart=always 容器名称1 容器名称2

apt-get update&&apt-get install -y vim

更改docker镜像
1、cd /etc/docker/
2、touch daemon.json
3、chmod 777 daemon.json
4、vi daemon.json
   添加  {"registry-mirrors": ["https://nsodgxr5.mirror.aliyuncs.com"]}

5、重启docker:   systemctl daemon-reload  systemctl restart docker

When we first began developing OpenShift in Action, it was going to be a book focusing on the ops side of DevOps in OpenShift. Existing books focus on OpenShift’s devel- oper experience, and we wanted to be their counterpoint. As we continued to work on the content and look at what we wanted to teach, it became apparent that we didn’t want to be a counterpoint. We didn’t want to represent one side of anything. Instead, we decided to create a complete example of OpenShift with a strong experience for both operators and developers. OpenShift in Action takes a holistic view of OpenShift, giving equal weight to both perspectives represented in DevOps. We think this is important, because the ultimate goal of DevOps is to enable and enhance communication between developer and operations teams that historically have been placed in adversarial (at best) relationships. To accomplish this, the two authors each specialize in one of these roles. For us, writing this book has been an amazing learning experience in how DevOps can work for just about anything, includ- ing writing a book. We can’t cover every OpenShift topic in a single book. But we hope OpenShift in Action gives you the fundamental knowledge from both developer and operation per- spectives to allow you to deploy and successfully use OpenShift in your own environ- ments. We also hope you can use OpenShift to accomplish meaningful work using containers. Most important, we hope the content in this book expands your knowl- edge through hands-on experience with OpenShift and becomes a reference for you for years to come.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值