docker安装lnmp并上线php项目完整版

//安装docker

sudo apt-get update
//卸载docker相关依赖
sudo apt-get remove docker
docker-engine
docker.io
//安装docker
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
sudo echo “deb [arch=amd64] https://download.docker.com/linux/debian stretch stable” | sudo tee -a /etc/apt/sources.list
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io

docker -v 或者 docker run hello-world//测试是否成功

//镜像加速
对于使用 systemd 的系统,请在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件)

{
“registry-mirrors”: [
“https://dockerhub.azk8s.cn”,
“https://reg-mirror.qiniu.com”
]
}

//重启dokcer

sudo systemctl daemon-reload
sudo systemctl restart docker

//安装镜像

docker pull nginx
docker pull php:7.0-fpm
docker pull mysql:5.6
docker pull redis
docker pull jenkins/jenkin
docker pull phpmyadmin

//启动mysql容器

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.6

//启动php容器

docker run -d -v /var/www/php:/var/www/html --link mysql:mysql -p 9000:9000 --name myphp7.0-fpm php:7.0-fpm

//启动nginx容器

docker run -p 80:80 -d --name mynginx --link myphp7.0-fpm:php -v /var/www/myprotect:/var/www/html nginx

//安装扩展

docker-php-ext-install mysqli //在php容器里 也可以不用安装

//配置nginx

#将default.conf 从nginx容器中导出到当前目录上

docker cp mynginx :/etc/nginx/conf.d/default.conf ./default.conf

修改default.conf文件 细节详细:

server { 
    listen       80;     //  80 启动nginx时   是你开启nginx 容器的端口
    server_name  localhost; 

    #charset koi8-r; 
    #access_log  /var/log/nginx/host.access.log  main; 
 
    location / { 
        root   /var/www/html;    //启动nginx时    你宿主机目录对应的容器目录
        index  index.php  index.html index.htm;   // index.php解析index.php
    } 
 
    #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$ { 
        root           /var/www/html;     //   php容器的目录
        fastcgi_pass   172.17.0.3:9000; //php容器的ip
        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; 
    #} 
} 

//修改完成后 把default.conf复制到nginx 容器里 因容器没有vim命令

docker cp ./default.conf mynginx:/etc/nginx/conf.d/

//进入nginx容器

docker exec -it mynginx bash
cd /etc/nginx/conf.d/
cat default.conf
nginx -t
nginx -s reload
exit

进入宿主机对应项目根目录

echo index.php

//测试下是否可以访问

docker安装lnmp并上线php项目完整版_第1张图片

访问服务器ip即可

你可能感兴趣的:(docker搭建lnmp,docker部署项目)