docker完整配置nginx+php+mysql的方法步驟
首先了解一個方法:
使用docker exec進入Docker容器
docker在1.3.X版本之後還提供了一個新的命令exec用於進入容器,這種方式相對更簡單一些,下面我們來看一下該命令的使用:
sudo docker exec --help
接下來我們使用該命令進入一個已經在執行的容器
$ sudo docker ps $ sudo docker exec -it 775c7c9ee1e1 /bin/bash
一. 配置nginx
查詢DockerHub 上的 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] h3nrik/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
建立並執行容器:
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 新增檔案對映關係,這樣在宿主機上更改的檔案可以直接對映到容器中。這裡的目錄根據自己實際情況進行對映。
建立並執行容器後,docker內的nginx即啟動成功,無需進入docker內部再次啟動nginx,否則會提示80等埠被佔用,因為nginx已經啟動。
這時候便可以訪問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配置常見問題有詳細介紹。
二. 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,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,7.0 for CI and run... 12 [OK] webdevops/php PHP (FPM and CLI) service container 10 [OK] ...
這裡我們拉取官方的映象,標籤為5.6-fpm
runoob@runoob:~/php-fpm$ docker pull php:5.6-fpm
等待下載完成後,我們就可以在本地映象列表裡查到REPOSITORY為php,標籤為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
建立並執行php容器:
docker run -p 9000:9000 --name phpfpm -v /var/www:/var/www -d php:5.6-fpm
注意這裡一定要建立檔案對映,或者php容器內有對應的php程式碼。上一步nginx的檔案對映,在這裡是找不到的。所以如果沒有檔案對映,127.0.0.1:9000 在此容器內就找不到檔案 。
常見問題:
啟動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] ...
這裡我們拉取官方的映象,標籤為5.6
runoob@runoob:~/mysql$ docker pull mysql:5.6
等待下載完成後,我們就可以在本地映象列表裡查到REPOSITORY為mysql,標籤為5.6的映象。
runoob@runoob:~/mysql$ docker images |grep mysql mysql 5.6 2c0964ec182a 3 weeks ago 329 MB
建立並執行MySQL容器:
docker run -p 3306:3306 --name mysql -v /usr/local/mysql:/etc/mysql/sqlinit -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
這裡的檔案對映主要目的是把宿主機的sql資料庫資料檔案對映到docker mysql容器,方便匯入,注意這裡mysql容器的目錄不能是已有的目錄,否則會覆蓋。
注意:
這裡建立容易已經有了my.cnf,無需自己新增。
拓展
使用外部工具navicat連線docker 內mysql
mysql的host 填寫docker內的IP,獲取方式為:
docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql
填寫ssh連線資訊:
即可連線成功!
注意:
docker的容器啟動順序問題會導致容器的IP地址不一致,如果在連線資料庫和fastcgi處有用到容器的IP,要注意容器的啟動順序。
重啟容器:docker restart 容器名/容器ID
關閉容器:docker stop xxx
開啟容器:docker start xxx
檢視正在執行的容器:docker ps
檢視所有容器(包括未執行的容器): docker ps -a
建立並執行容器: docker run
---------------------------------------
常見報錯:
1. thinkphp報錯 Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'
缺少pdo_mysql擴充套件,連結資料庫失敗
找到php.ini,docker中在/usr/local/etc/php中,複製一份php.ini,增加 extension=pdo_mysql.so ,重啟phpfpm。
如果還不行,訪問phpinfo頁面,檢視是否有pdo_mysql
如果沒有,說名擴充套件不存在,需要編譯。
編譯方法如下:
可以通過兩種方式實現
方式一(未驗證):
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目錄和對應的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 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
再次執行:
// 增加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 Think\imagettftext()
如果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,則驗證碼顯示正常了:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。