Docker配置PHP+Nginx
阿新 • • 發佈:2020-12-29
安裝Nginx
-
對映目錄結構
docker_www # docker_www 主目錄 -mysql # mysql -nginx # nginx --conf.d # 擴充套件配置 --dq.conf # 新增配置檔案 --logs # 日誌 --nginx.conf # nginx主配置檔案 -www # www目錄->/usr/share/html --index.html # 測試html檔案,寫入 <p>hello docker nginx</p> --info.php # 寫入 phpinfo
-
獲取映象
docker pull nginx # 獲取nginx基礎映象
-
建立測試容器
# ae2feff98a0c nginx的映象ID # PWD 當前目錄, 我這裡進入的是docker_www目錄 docker run -d -p 8080:80 -v $PWD/www:/usr/share/html --name nginx_test ae2feff98a0c # 測試訪問 curl 127.0.0.1:8080 #輸出 <p>hello docker nginx</p>
-
建立可掛載目錄Nginx容器
# ae2feff98a0c nginx的映象ID # PWD 當前目錄, 我這裡進入的是docker_www目錄 # nginx.conf 可以從上一個測試容器中複製出來,操作命令如下: # docker cp 容器名稱:/etc/nginx/nginx.conf 宿主機目錄 # docker cp new_nginx_web_1:/etc/nginx/nginx.conf . docker run -d -p 8080:80 --name new_nginx_web_1 / -v $PWD/nginx/nginx.conf:/etc/nginx/nginx.conf / -v $PWD/nginx/conf.d:/etc/nginx/conf.d / -v $PWD/www:/usr/share/nginx/html / -v $PWD/nginx/logs:/var/log/nginx / ae2feff98a0c
安裝PHP
-
獲取php映象
docker pull php:7.1-fpm # 獲取php基礎映象
-
建立測試PHP映象
# 建立容器 # 404422fc039e php映象ID docker run -d -v $PWD/www:/var/www/html -p 9000:9000 --name phpfpm_test 404422fc039e
-
建立正式PHP映象
# 先從上一個測試映象中將配置檔案都複製出來 # 當前處於php目錄下 docker cp phpfpm_test:/usr/local/var/log log # 複製日誌 docker cp phpfpm_test:/usr/local/etc conf # 複製配置檔案 # 建立 docker run -d -p 9000:9000 \ -v $PWD/php/conf:/usr/local/etc \ -v $PWD/php/log:/usr/local/var/log \ -v $PWD/www:/var/www/html \ --name phpfpm_7.1 404422fc039e
映象互聯
-
Nginx 連結 PHP
# ae2feff98a0c nginx的映象ID # PWD 當前目錄, 我這裡進入的是docker_www目錄 # php7.1設定的連結別名,在nginx容器中可以使用 docker run -d -p 8080:80 --name new_nginx_web_1 \ -v $PWD/nginx/nginx.conf:/etc/nginx/nginx.conf \ -v $PWD/www:/usr/share/nginx/html \ -v $PWD/nginx/logs:/var/log/nginx \ --link phpfpm_test:php7.1 ae2feff98a0c
-
conf.d/dq.conf檔案配置
# php7.1 就是在建立的容器時 `--link phpfpm_test:php7.1` # 在容器中的/etc/hosts檔案中,172.17.0.3 php7.1 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 mine.nginx.com; root /var/www/html; index index index.html 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$ { root /var/www/html; fastcgi_pass php7.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; #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; } }
-
測試請求
curl 127.0.0.1:8080/info.php