首頁 > 軟體

Docker快速搭建PHP+Nginx+Mysql環境及踩坑

2022-08-07 22:00:45

準備

建立目錄

mkdir -p /Users/mwqnice/Documents/Program/phpProgram &&
mkdir -p /Users/mwqnice/Documents/Program/docker/php/php7.1.33/conf &&
mkdir -p /Users/mwqnice/Documents/Program/docker/php/php7.1.33/logs &&
mkdir -p /Users/mwqnice/Documents/Program/docker/nginx/conf.d &&
mkdir -p /Users/mwqnice/Documents/Program/docker/mysql/conf &&
mkdir -p /Users/mwqnice/Documents/Program/docker/mysql/logs &&
mkdir -p /Users/mwqnice/Documents/Program/docker/mysql/data &&
cd /Users/mwqnice/Documents/Program/docker/nginx/conf.d && sudo touch default.conf

設定PHP

拉取php-fpm映象

docker pull php:7.1.33-fpm #版本7.1.33

啟動php-fpm

docker run --name  mwq-php 
-v /Users/mwqnice/Documents/Program/phpProgram:/var/www/html 
-v /Users/mwqnice/Documents/Program/docker/php/php7.1.33/conf:/usr/local/etc/php 
-v /Users/mwqnice/Documents/Program/docker/php/php7.1.33/logs:/phplogs 
-d --link mwq-php php:7.1.33-fpm

--name mwq-php是容器的名字

/Users/mwqnice/Documents/Program/phpProgram是本地專案目錄,/var/www/html是容器內專案儲存目錄

設定Nginx

拉取Nginx映象

docker pull nginx:latest //拉取最新版本

設定nginx.conf

server {
    listen  80;
    server_name localhost;
    set $root /var/www/html/localhost;
    
    #access_log  /tmp/nginx/logs/localhost.net.access.log main;
    #error_log  /tmp/nginx/logs/localhost.net.error.log notice;

    location ~ .*.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
        root $root;
    }

    location / {
        root $root;
        index  index.php index.html index.htm;
        if ( -f $request_filename) {
            break;
        }
        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php/$1 last;
            break;
        }
    }

    location ~ .php(.*)$ {
        root $root;
        set $script $uri;
        set $path_info "";
        if ($uri ~ "^(.+.php)(/.+)") {
            set $script $1;
            set $path_info $2;
        }
        fastcgi_pass mwq-php:9000;
        #fastcgi_index index.php;
        fastcgi_index    index.php?IF_REWRITE=1;
        fastcgi_param    PATH_INFO    $path_info;
        fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param    SCRIPT_NAME    $script;
        include          fastcgi_params;

    }

    location ~ /.ht {
        deny  all;
    }
    location ~ /.svn {
        deny  all;
    }
    location ~ /.git/ {
        deny  all;
    }
    location ~ /Logs/ {
        deny  all;
    }
    location ~ /Logs/.* {
    }
    location ~ /Logs/.* {
        deny  all;
    }
    location ~ .*.(sql|tar.gz|zip|gz|tar|rariso|rpm|apk|bak)$ {
        deny  all;
    }
  
}

啟動Nginx

docker run --name mwq-nginx -p 80:80 -d 
-v /Users/mwqnice/Documents/Program/phpProgram:/var/www/html:ro 
-v /Users/mwqnice/Documents/Program/docker/nginx/conf.d:/etc/nginx/conf.d:ro 
--link mwq-php:php 
nginx

設定MySQL

拉取MySQL映象

docker pull mysql:5.7.36 #版本5.7.36

啟動MySQL

docker run -p 3306:3306 --name mwq-mysql 
-v /Users/mwqnice/Documents/Program/docker/mysql/conf:/etc/mysql/conf.d 
-v /Users/mwqnice/Documents/Program/docker/mysql/logs:/logs 
-v /Users/mwqnice/Documents/Program/docker/mysql/data:/mysql_data 
-e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.36
  • -p 3306:3306: 將容器的3306埠對映到主機的3306埠
  • -v /Users/mwqnice/Documents/Program/docker/mysql/conf:/etc/mysql 將主機/Users/mwqnice/Documents/Program/docker/mysql/conf目錄掛載到容器的/etc/mysql
  • -e MYSQL_ROOT_PASSWORD=123456: 初始化root使用者的密碼
  • -d: 後臺執行容器,並返回容器ID

常見問題

1、thinkphp報錯 Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'

缺少pdo_mysql擴充套件,連線資料庫失敗

找到php.ini,docker中在/usr/local/etc/php中,複製一份php.ini,增加extension=pdo_mysql.so,重啟php-fpm,如果還是不行,存取phpinfo頁面,檢視是否有pdo_mysql

如果沒有,說明沒有pdo_mysql擴充套件,需要編譯

編譯方法如下:

到docker的php容器中,在php資料夾下:

docker-php-ext-install pdo pdo_mysql

如果報 /usr/local/bin/docker-php-ext-enable: cannot create /usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini: Directory nonexistent

解決方案:直接在/usr/local/etc/php目錄下面新建 conf.d目錄和對應的docker-php-ext-pdo_msql.ini檔案

其中docker-php-ext-pdo_msql.ini的內容為:

extension=pdo_mysql.so

2、thinkphp 報錯 STORAGE_WRITE_ERROR:./Application/Runtime/Cache/Home/4e64ea6a2012f26b832b14cbc2152b28.php

是因為伺服器快取資料夾的操作許可權不夠,即Runtime沒有許可權,把快取檔案全部刪除,再給Runtime777許可權就行了

sudo chmod 777 Runtime 或者直接對程式碼庫最外層設定777許可權

3、thinkphp驗證碼圖片顯示不出來

缺少gd擴充套件,安裝:

docker-php-ext-install gd

可能以下報錯:

If configure fails try --with-webp-dir=<DIR>
If configure fails try --with-jpeg-dir=<DIR>
configure: error: png.h not found.

安裝:

apt-get install libpng-dev libjpeg-dev

再次執行:

// 增加freetype設定
docker-php-ext-configure gd --enable-gd-native-ttf --with-freetype-dir=/usr/include/freetype2 --with-png-dir=/usr/include --with-jpeg-dir=/usr/include

// 安裝
docker-php-ext-install gd

php.ini增加php_gd2.so

phpinfo中顯示gd庫

注意如果phpinfo的gd庫中沒有freetype的支援,驗證碼依然顯示不出來, 會報錯:

Call to undefined function Thinkimagettftext()

如果gd庫中沒有freeType,則按照以下步驟進行:

docker-php-ext-configure gd --enable-gd-native-ttf --with-freetype-dir=/usr/include/freetype2 --with-png-dir=/usr/include 

重新編譯:

docker-php-ext-install gd

如果報錯: configure: error: freetype-config not found. 執行:

apt-get -y install libfreetype6-dev

然後再繼續執行上面的命令。gd庫中有了freetype,則驗證碼顯示正常了

到此這篇關於Docker快速搭建PHP+Nginx+Mysql環境及踩坑 的文章就介紹到這了,更多相關Docker搭建PHP Nginx Mysq內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com