Docker搭建PHP/Nginx/MySQL/Composer环境

Stephen 后端开发 2019-05-12 15:34:43

一、安装PHP:

#PHP镜像
docker pull php:7.2-fpm

#PHP容器,监听9000端口,映射主机项目目录/data/www
docker run --name php -d -p 9000:9000 -v /data/www:/www php:7.2-fpm
二、安装Nginx

docker pull nginx

docker run --name php-nginx -d -p 80:80 \ 
-v /data/www:/usr/share/nginx/html \ 
-v ~/nginx/conf/conf.d:/etc/nginx/conf.d \ 
--link php:php \ 
nginx
修改nginx配置

vi ~/nginx/conf/conf.d/nginx.conf


server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;
        include        fastcgi_params;
    }
}
编辑php文件

vi /data/www/index.php

<?php
echo date('Y-m-d H:i:s');
echo phpinfo();
?>
重启nginx容器

docker restart php-nginx
浏览器打开 http://127.0.0.1:80

三、安装MySQL

docker pull mysql:8.0.16

docker run --name mysql -d -p 3306:3306 -v ~/mysql/data:/var/lib/mysql \ 
-e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.16
进入mysql容器,设置mysql远程连接,就可在navcat上连接MySQL了

#进入mysql容器
docker exec -it mysql bash

#在mysql容器下进入mysql
mysql -uroot -p123456

#新增test账号,并设置远程连接
CREATE USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'test'@'%';
四、安装composer

docker pull composer
如果我有个项目需要laravel框架,那么在docker环境下怎么搭建呢,具体请参考:《Docker下用composer国内镜像安装Laravel


分享

评论