溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

docker完整配置nginx+php+mysql的方法步驟

發(fā)布時間:2020-08-21 21:29:04 來源:腳本之家 閱讀:509 作者:冒雨ing 欄目:服務(wù)器

首先了解一個方法:

使用docker exec進(jìn)入Docker容器

docker在1.3.X版本之后還提供了一個新的命令exec用于進(jìn)入容器,這種方式相對更簡單一些,下面我們來看一下該命令的使用:

sudo docker exec --help  

docker完整配置nginx+php+mysql的方法步驟

接下來我們使用該命令進(jìn)入一個已經(jīng)在運行的容器

$ sudo docker ps 
$ sudo docker exec -it 775c7c9ee1e1 /bin/bash

一. 配置nginx

查找Docker Hub 上的 nginx 鏡像

runoob@runoob:~/nginx$ docker search nginx
NAME           DESCRIPTION                   STARS   OFFICIAL  AUTOMATED
nginx           Official build of Nginx.            3260   [OK]    
jwilder/nginx-proxy    Automated Nginx reverse proxy for docker c...  674         [OK]
richarvey/nginx-php-fpm  Container running Nginx + PHP-FPM capable ...  207         [OK]
million12/nginx-php    Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS...  67          [OK]
maxexcloo/nginx-php    Docker framework container with Nginx and ...  57          [OK]
webdevops/php-nginx    Nginx with PHP-FPM               39          [OK]
h4nrik/nginx-ldap     NGINX web server with LDAP/AD, SSL and pro...  27          [OK]
bitnami/nginx       Bitnami nginx Docker Image           19          [OK]
maxexcloo/nginx      Docker framework container with Nginx inst...  7          [OK]
...

這里我們拉取官方的鏡像

runoob@runoob:~/nginx$ docker pull nginx

等待下載完成后,我們就可以在本地鏡像列表里查到 REPOSITORY 為 nginx 的鏡像。

runoob@runoob:~/nginx$ docker images nginx
REPOSITORY     TAG         IMAGE ID      CREATED       SIZE
nginx        latest       555bbd91e13c    3 days ago     182.8 MB

創(chuàng)建并運行容器:

docker run --name mynginx -p 80:80 -v /var/www:/var/www -v /usr/local/nginx/conf/conf.d:/etc/nginx/conf.d -d nginx

注意:

-v 添加文件映射關(guān)系,這樣在宿主機(jī)上更改的文件可以直接映射到容器中。這里的目錄根據(jù)自己實際情況進(jìn)行映射。

創(chuàng)建并運行容器后,docker內(nèi)的nginx即啟動成功,無需進(jìn)入docker內(nèi)部再次啟動nginx, 否則會提示80等端口被占用,因為nginx已經(jīng)啟動。

這時候便可以訪問nginx配置的域名驗證了。

我這里映射的conf.d主要包含nginx的配置文件,php的配置信息為:

# php
server {
  charset utf-8;
  client_max_body_size 128M;

  listen 80; ## listen for ipv4
  #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

  server_name www.baidu.com;
  root    /var/www;
  index    index.php;

  location / {
    #-e表示只要filename存在,則為真
    if (!-e $request_filename){
      rewrite ^(.*)$ /index.php?s=$1 last;
      break;
    }
    # Redirect everything that isn't a real file to index.php
    try_files $uri $uri/ /index.php$is_args$args;
  }

  # uncomment to avoid processing of calls to non-existing static files by Yii
  #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
  #  try_files $uri =404;
  #}
  #error_page 404 /404.html;

  # deny accessing php files for the /assets directory
  location ~ ^/assets/.*\.php$ {
    deny all;
  }

  location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass 172.17.0.3:9000;
    #fastcgi_pass unix:/var/run/php5-fpm.sock;
    try_files $uri =404;
  }

  location ~* /\. {
    deny all;
  }
}

注意最后面的fastcgi_pass的ip地址,在php配置常見問題有詳細(xì)介紹。

二. php配置

查找Docker Hub上的php鏡像

runoob@runoob:~/php-fpm$ docker search php
NAME           DESCRIPTION                   STARS   OFFICIAL  AUTOMATED
php            While designed for web development, the PH...  1232   [OK]    
richarvey/nginx-php-fpm  Container running Nginx + PHP-FPM capable ...  207         [OK]
phpmyadmin/phpmyadmin   A web interface for MySQL and MariaDB.     123         [OK]
eboraas/apache-php    PHP5 on Apache (with SSL support), built o...  69          [OK]
php-zendserver      Zend Server - the integrated PHP applicati...  69    [OK]    
million12/nginx-php    Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS...  67          [OK]
webdevops/php-nginx    Nginx with PHP-FPM               39          [OK]
webdevops/php-apache   Apache with PHP-FPM (based on webdevops/php)  14          [OK]
phpunit/phpunit      PHPUnit is a programmer-oriented testing f...  14          [OK]
tetraweb/php       PHP 5.3, 5.4, 5.5, 5.6, 7.0 for CI and run...  12          [OK]
webdevops/php       PHP (FPM and CLI) service container       10          [OK]
...

這里我們拉取官方的鏡像,標(biāo)簽為5.6-fpm

runoob@runoob:~/php-fpm$ docker pull php:5.6-fpm

等待下載完成后,我們就可以在本地鏡像列表里查到REPOSITORY為php,標(biāo)簽為5.6-fpm的鏡像。

runoob@runoob:~/php-fpm$ docker images
REPOSITORY     TAG         IMAGE ID      CREATED       SIZE
php         5.6-fpm       025041cd3aa5    6 days ago     456.3 MB

創(chuàng)建并運行php容器:

docker run -p 9000:9000 --name phpfpm -v /var/www:/var/www -d php:5.6-fpm

注意這里一定要創(chuàng)建文件映射,或者php容器內(nèi)有對應(yīng)的php代碼。上一步nginx的文件映射,在這里是找不到的。所以如果沒有文件映射,127.0.0.1:9000 在此容器內(nèi)就找不到文件 。

常見問題:

啟動php容器后,如果訪問nginx為:502 Bad Gateway

嘗試以下方法:

查看php鏡像的ip地址

docker inspect --format='{{.NetworkSettings.IPAddress}}' phpfpm

如:192.168.4.202

那么修改nginx的conf配置文件,使fastcgi_pass的值為 192.168.4.202:9000

vim /docker/nginx/conf.d/default.conf

fastcgi_pass 192.168.4.202:9000;

重啟nginx容器后,就可以正常訪問。

三. mysql配置

查找Docker Hub上的mysql鏡像

runoob@runoob:/mysql$ docker search mysql
NAME           DESCRIPTION                   STARS   OFFICIAL  AUTOMATED
mysql          MySQL is a widely used, open-source relati...  2529   [OK]    
mysql/mysql-server    Optimized MySQL Server Docker images. Crea...  161         [OK]
centurylink/mysql    Image containing mysql. Optimized to be li...  45          [OK]
sameersbn/mysql                             36          [OK]
google/mysql       MySQL server for Google Compute Engine     16          [OK]
appcontainers/mysql   Centos/Debian Based Customizable MySQL Con...  8          [OK]
marvambass/mysql     MySQL Server based on Ubuntu 14.04       6          [OK]
drupaldocker/mysql    MySQL for Drupal                2          [OK]
azukiapp/mysql      Docker image to run MySQL by Azuki - http:...  2          [OK]
...

這里我們拉取官方的鏡像,標(biāo)簽為5.6

runoob@runoob:~/mysql$ docker pull mysql:5.6

等待下載完成后,我們就可以在本地鏡像列表里查到REPOSITORY為mysql,標(biāo)簽為5.6的鏡像。

runoob@runoob:~/mysql$ docker images |grep mysql
mysql        5.6         2c0964ec182a    3 weeks ago     329 MB

創(chuàng)建并運行MySQL容器:

docker run -p 3306:3306 --name mysql -v /usr/local/mysql:/etc/mysql/sqlinit -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6

這里的文件映射主要目的是把宿主機(jī)的sql數(shù)據(jù)庫數(shù)據(jù)文件映射到docker mysql容器,方便導(dǎo)入,注意這里mysql容器的目錄不能是已有的目錄,否則會覆蓋。

注意:

這里創(chuàng)建容易已經(jīng)有了my.cnf,無需自己添加。

拓展

使用外部工具navicat連接docker 內(nèi)mysql

mysql的host 填寫docker內(nèi)的IP,獲取方式為:

docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql

docker完整配置nginx+php+mysql的方法步驟

填寫ssh連接信息:

docker完整配置nginx+php+mysql的方法步驟

即可連接成功!

注意:

docker的容器啟動順序問題會導(dǎo)致容器的IP地址不一致,如果在連接數(shù)據(jù)庫和fastcgi處有用到容器的IP,要注意容器的啟動順序。

重啟容器:docker restart 容器名/容器ID

關(guān)閉容器:docker stop xxx

開啟容器:docker start xxx

查看正在運行的容器:docker ps

查看所有容器(包括未運行的容器): docker ps -a

創(chuàng)建并運行容器: docker run

---------------------------------------

常見報錯:

1.  thinkphp報錯 Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'

缺少pdo_mysql擴(kuò)展,鏈接數(shù)據(jù)庫失敗

找到php.ini,docker中在/usr/local/etc/php中,復(fù)制一份php.ini,增加 extension=pdo_mysql.so ,重啟phpfpm。

如果還不行,訪問phpinfo頁面,查看是否有pdo_mysql

docker完整配置nginx+php+mysql的方法步驟

如果沒有,說名擴(kuò)展不存在,需要編譯。

編譯方法如下:

可以通過兩種方式實現(xiàn)

方式一(未驗證):

pecl pdo_msql

方式二(已驗證可行):

到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目錄和對應(yīng)的docker-php-ext-pdo_msql.ini文件

其中docker-php-ext-pdo_msql.ini的內(nèi)容為:

extension=pdo_mysql.so

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

是因為服務(wù)器緩存文件夾的操作權(quán)限不夠,即Runtime沒有權(quán)限,把緩存文件全部刪除,再給Runtime777權(quán)限就行了

sudo chmod 777 Runtime 或者直接對代碼庫最外層設(shè)置777權(quán)限

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

docker完整配置nginx+php+mysql的方法步驟

缺少gd擴(kuò)展,安裝:

docker-php-ext-install pdo pdo_mysql

可能以下報錯:

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

再次執(zhí)行:

// 增加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

docker完整配置nginx+php+mysql的方法步驟

phpinfo中顯示gd庫

docker完整配置nginx+php+mysql的方法步驟

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

Call to undefined function Think\imagettftext()

如果gd庫中沒有freeType,則按照以下步驟進(jìn)行:

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 ,然后再繼續(xù)運行上面的命令。 

gd庫中有了freetype,則驗證碼顯示正常了:

docker完整配置nginx+php+mysql的方法步驟

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI