CentOS 7 使用 docker 搭建基本的 lnmp 環境

bestcyt發表於2018-03-28

準備:無毒無害綠色純潔的centos 7一隻

前言:

由於買了個新的伺服器,空空白白的,所以一直由於是搭建lnmp還是用docker,早上提了個問答,謝謝大家的回覆,下午就嘗試自己用docker來搭建lnmp,都是比較基礎的命令,因為我也比較菜,所以也有遇到挺多坑的,這邊就記錄下來,分享一下。
然後伺服器因為就一個使用者,所以我就省略sudo了

1.準備docker

1.下載安裝依賴包
yum install -y yum-utils  device-mapper-persistent-data lvm2
2.網路問題就換源咯
yum-config-manager --add-repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
3.更新快取,安裝docker-ce
yum makecache fast
yum install docker-ce
4.啟動docker
systemctl enable docker
systemctl start docker

docker run hello-world
run 提示timeout的話,就要國內映象加速了
5.國內映象加速

在 /etc/docker/daemon.json 中寫入如下內容(如果檔案不存在請新建該檔案)

{
  "registry-mirrors": [
    "https://registry.docker-cn.com"
  ]
}
6.之重新啟動服務
systemctl daemon-reload
systemctl restart docker

如果能看到下面資訊就代表正確咯

$ docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

更多參考
docker中文文件

2.拉取映象(nginx1.12.2,mysql5.7,php7.2)

1.獲取mysql映象

docker pull mysql:5.7
啟動容器cyt_mysql
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name cyt_mysql mysql:5.7
命令引數解釋:
-p:埠對映,對映容器的3306
後面就是密碼和名稱

2.獲取php7.2映象

docker pull php:7.2-fpm
建立PHPfpm容器
docker run -d -v /var/nginx/www/html:/var/www/html -p 9000:9000 --link cyt_mysql:mysql --name cyt_phpfpm php:7.2-fpm 
命令引數解釋:
-v 前面是主機的目錄對映容器的目錄
link:掛上msyql
測試目錄對映:
進入到PHP容器 (可以用name也可以用容器id)
docker exec cyt_phpfpm /bin/bash
就會到var/www/html 目錄,新建一個PHP檔案
touch test.php
然後退出容器
exit
到主機的 var/nginx/www/html 目錄下也出現了個test.php
php的擴充套件安裝
docker-php-ext-install pdo_mysql(curl ...)

要安裝php-redis的話,需要另外下載,執行下面這個命令就可以了,有問就no或者空格就好
pecl install redis && docker-php-ext-enable redis

安裝後 php-m
發現就都有了哦

3.獲取Nginx1.12.2映象

docker pull ngixn:1.12.2
執行Nginx容器
docker run -d -p 80:80 --name cyt_nginx -v /var/nginx/www/html:/var/www/html --link cyt_phpfpm:phpfpm --name cyt_nginx nginx:1.12.2
引數解釋
-p:對映80埠
-v:對映目錄,最好和PHP的一樣
-name:容器名
-link:跟PHP關聯
修改Nginx容器配置讓他支援PHP

進入Nginx容器:

docker exec -it cyt_nginx /bin/bash

cd到conf目錄

cd etc/nginx/conf.d/

修改配置檔案default.conf

vi default.conf

如果提示vi命令不存在,那就下一個vi

apt-get update
apt-get install vim

繼續改配置,把對php支援的註釋去掉並修改路徑

前面有#號去掉
location ~ \.php$ {
        root           /var/www/html;
        fastcgi_pass   phpfpm:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME/var/www/html/$fastcgi_script_name;
        include        fastcgi_params;
    }
重新載入Nginx
nginx -s reload
退出容器
exit

醬紫基本環境就搭建結束了

測試:
找到那個index.php
echo phpinfo();
訪問下ip,看是不是PHPinfo?

如果有404什麼的問題一般就是Nginx配置問題了,可以根據Nginx解析順序自己改改

location = / {
     root   /var/www/html/;
     index  index.htm index.html;
}
location / {
     root   /usr/local/nginx/html;
     index  index.html index.htm;
}

location配置如上,若訪問http://xxx.com/,定位的流程是:

1:精準匹配命中"/",得到index頁為index.htm,所以請求的地址變為http://xxx.com/index.htm

2:再次匹配"/index.htm",此次內部轉跳uri已經是"/index.htm",命中普通匹配"/",根目錄為/usr/local/nginx/html
3:最終結果,訪問了/usr/local/nginx/html/index.htm

第一次寫,有不足的請大家提出了,多多指教:)
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章