centos 7使用docker安装lnmp和redis环境

#================Docker基础操作==========================

#启动docker服务
service docker start
#搜索可用docker镜像
#https://docs.docker.com/engine/reference/commandline/search/
docker search --filter "stars=3" --filter "is-official=true" mysql
#搜索并指定版本的镜像(搜索并查看镜像版本号)
docker search mysql:5.7
#查看本地镜像或容器的版本信息
docker inspect --format "{{.Config.Env}}" mysql:5.6
#查看本地容器的ip地址
docker inspect --format='{{.NetworkSettings.IPAddress}}' test_phpfpm
#在Docker里面安装vim编辑器
apt-get update
apt-get install vim
#docker容器操作
docker stop test_nginx
docker start test_nginx
docker restart test_nginx
docker rm test_nginx
docker rename test_nginx test_nginx2

#=================MySQL==============================

#从仓库拉取一个MySql的镜像
docker pull mysql:5.6
#查看我们刚刚拉下来的mysql的镜像
docker images
#运行并启动一个容器
mkdir -p /docker/mysql/data /docker/log/mysql
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=dbPwd000 -v /docker/mysql/data:/var/lib/mysql -v /docker/log/mysql:/var/log/mysql --name test_mysql mysql:5.6
#查看我们刚刚创建的容器
docker ps -a
#进入到我们刚刚创建的容器中,输入命令
docker exec -ti test_mysql /bin/bash
#登录MySQL
mysql -uroot -p dbPwd000
#创建一个应用用户
GRANT ALL PRIVILEGES ON *.* TO 'fhzbuser'@'%' IDENTIFIED BY 'fhzbPwd!23_';
FLUSH PRIVILEGES;
#创建一个数据库
CREATE DATABASE IF NOT EXISTS db_test DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
#创建一个数据表
create table if not exists db_test.t_test (id int auto_increment primary key,title varchar(32) not null default '',createdTime timestamp not null default CURRENT_TIMESTAMP) ENGINE=InnoDB default CHARSET=utf8;
insert into db_test.t_test (title) values ('title 1'),('title 2'),('title 3'),('title 4'),('title 5');


#===========================PHP======================

#拉取php-fpm的镜像
docker pull php:5.6-fpm
#在本地创建默认配置文件
mkdir -p /docker/conf/php/
echo -e "data.timezone = PRC memory_limit = 128m upload_max_filesize = 16m post_max_size = 32m max_execution_time = 600 max_input_time = 600 " > /docker/conf/php/test_phpfpm.ini
#创建一个phpfpm容器(php.int配置文件默认不存在,需手动创建)
docker run -d -p 9000:9000 --link test_mysql:mysql -v /docker/www/html/test_html:/var/www/html -v /docker/conf/php/test_phpfpm.ini:/usr/local/etc/php/conf.d/php.ini --name test_phpfpm php:5.6-fpm
#进入到我们刚刚创建的容器中,输入命令
docker exec -ti test_phpfpm /bin/bash
#在/var/www/html目录下新建一个index.php文件
touch /var/www/html/index.php
#在容器中安装pdo扩展
docker-php-ext-install pdo_mysql
#在容器中安装非php官方扩展(redis扩展)
# 参考文章:https://stackoverflow.com/questions/31369867/how-to-install-php-redis-extension-using-the-official-php-docker-image-approach
mkdir -p /usr/src/php/ext/
curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/2.2.8.tar.gz
&& tar xfz /tmp/redis.tar.gz
&& rm -r /tmp/redis.tar.gz
&& mv phpredis-2.2.8 /usr/src/php/ext/redis
&& docker-php-ext-install redis
#执行php -m查看已安装的扩展模块
php -m
#重启容器,使配置生效
docker restart test_phpfpm


#==================Nginx======================

#拉取一个nginx镜像
docker pull ngixn
#运行nginx容器
docker run -d -p 80:80 --privileged=true --link test_phpfpm -v /docker/www/html/test_html:/var/www/html --name test_nginx nginx
#进入nginx容器
docker exec -ti test_nginx /bin/bash
#修改默认站点配置文件以支持php脚本(注意php节点的root设置,很多时候提示php文件未找到都是因为这里设置错了)

vi /etc/nginx/conf.d/default.conf
server {
    listen 80;
    server_name localhost;
    root  /var/www/html;
    index index.html index.htm index.php;

    access_log  /var/www/wwwlogs/test_nginx_access.log;
    #error_page  500 502 503 504 /50x.html;
    location = /50x.html {
        root  /usr/share/nginx/html;
    }

    # Deny access to PHP files in specific directory
    #location ~ /(wp-content|uploads|wp-includes|images)/.*.php$ { deny all; }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #include enable-php.conf;    
    location ~ .php$ {
        # important, php file not found is because root setting wrong.
        root           /var/www/html;
        fastcgi_index  index.php;
        fastcgi_pass   172.17.0.3:9000;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location /nginx_status
    {
        stub_status on;
        access_log   off;
    }

    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*.(js|css)?$
    {
        expires      12h;
    }

    location ~ /.well-known {
        allow all;
    }

    location ~ /.
    {
        deny all;
    }
}

#在容器里面重启nginx

service nginx reload
#编辑index.php

vi /var/www/html/index.php
try {
    $con = new PDO('mysql:host=mysql;dbname=db_test', 'fhzbuser', 'fhzbPwd!23_');
    $con->query('SET NAMES UTF8');
    $res =  $con->query('select * from t_test');
    while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
        echo "id:{$row['id']}, title:{$row['title']}";
    }
} catch (PDOException $e) {
     echo $e->getMessage();
}


#==================Redis======================

#拉取镜像
docker pull redis:latest
#现在本地创建redis.conf文件并设置密码,否则-v redis.conf会自动创建为目录
mkdir -p /docker/conf/redis/
echo "requirepass 123" > /docker/conf/redis/redis.conf
#运行实例并指定配置文件
docker run -d -p 6379:6379 -v /docker/conf/redis/redis.conf:/etc/redis/redis.conf -v /docker/redis/data:/data --name test_redis redis:latest redis-server /etc/redis/redis.conf --appendonly yes
#查看redis的IP地址
docker inspect test_redis
#打开redis-cli模式
docker run -it redis:latest redis-cli -h 172.17.0.5

#在浏览器打开服务器IP地址并访问网站

http://ip

#==================Rinetd端口转发======================

rinetd端口映射教程:https://help.aliyun.com/document_detail/43850.html
#下载rinetd
wget http://www.boutell.com/rinetd/http/rinetd.tar.gz&&tar -xvf rinetd.tar.gz&&cd rinetd
#修改端口号
sed -i 's/65536/65535/g' rinetd.c
#安装rinetd
mkdir /usr/man&&make&&make install
#编辑配置文件,加入端口转发规则
vi /etc/rinetd.conf
0.0.0.0 6399 127.0.0.1 6379
0.0.0.0 3366 127.0.0.1 3306
#重启rinetd
pkill rinetd&&rinetd
#查看设置是否生效
netstat -anp | grep 6399

参考文章:

使用Nginx的官方Docker镜像,启动容器后无法显示自己网站页面,总显示Nginx官方默认页面的问题的解决方法 (http://blog.csdn.net/zhangchao19890805/article/details/77802811)
搜索可用docker镜像 (http://www.docker.org.cn/book/docker/docker-search-image-6.html)
基于Docker搭建LNMP环境 (http://blog.csdn.net/xy752068432/article/details/75975065)
Docker搭建可一键部署的多域名LNMP环境 (https://www.awaimai.com/2120.html)
Docker多容器部署LNMP环境 (http://www.jianshu.com/p/fcd0e542a6b2)
Docker多容器搭建应用栈(lnmp) (http://www.jianshu.com/p/f244eb57820c)
Docker简介以及使用docker搭建lnmp的过程(多PHP版本) (https://www.cnblogs.com/LO-gin/p/6958720.html)
使用dockerfile 部署lnmpr环境 (https://segmentfault.com/a/1190000009009661)
利用docker搭建php7和nginx运行环境全过程 (http://www.jb51.net/article/113296.htm)
docker安装redis 指定配置文件且设置了密码 (https://www.cnblogs.com/cgpei/p/7151612.html)

 

版权声明:本文采用署名-非商业性使用-相同方式共享(CC BY-NC-SA 3.0 CN)国际许可协议进行许可,转载请注明作者及出处。
本文标题:centos 7使用docker安装lnmp和redis环境
本文链接:http://www.cnblogs.com/sochishun/p/7955004.html
本文作者:SoChishun (邮箱:14507247#qq.com | 博客:http://www.cnblogs.com/sochishun/)
发表日期:2017年12月2日

原文地址:https://www.cnblogs.com/sochishun/p/7955004.html