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 <?php echo “hello word!” > index.php

//測試下是否可以訪問

在這裏插入圖片描述

訪問服務器ip即可

發佈了31 篇原創文章 · 獲贊 6 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章