Docker部署php運(yùn)行環(huán)境(php-fpm+nginx)
前言
如果使用docker去部署一套php的運(yùn)行環(huán)境,我們需要構(gòu)建出nginx、php-fpm兩個(gè)容器,nginx通過(guò)fast_cgi協(xié)議去轉(zhuǎn)發(fā)php-fpm中的端口,從而實(shí)現(xiàn)web server的搭建,接下來(lái)以php的laravel框架為演示例子。
部署php-fpm
第一步 編寫(xiě)php-fpm鏡像的Dockerfile:
./Dockerfile
#根據(jù)你自身業(yè)務(wù)需求來(lái)選擇官方的php基礎(chǔ)鏡像 FROM php:7.4-fpm-alpine # 設(shè)置時(shí)區(qū) ENV TZ Asia/Shanghai # 創(chuàng)建supervisor進(jìn)程管理器相關(guān)數(shù)據(jù)存在的文件夾 RUN mkdir -p "/var/log/supervisor" && mkdir -p "/var/run" # 設(shè)置源,提高下載效率 RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories # 安裝系統(tǒng)依賴(lài) RUN apk update && apk --no-cache add \ autoconf \ g++ \ make \ openssl-dev \ libzip-dev \ unzip \ tzdata \ supervisor # 安裝Redis擴(kuò)展 RUN pecl install redis && docker-php-ext-enable redis # 安裝PDO MySQL擴(kuò)展 RUN docker-php-ext-install pdo_mysql && docker-php-ext-enable pdo_mysql # 安裝opcache擴(kuò)展 RUN docker-php-ext-install opcache && docker-php-ext-enable opcache # 安裝Composer RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer #工作目錄 WORKDIR /app # 定義容器啟動(dòng)時(shí)運(yùn)行的命令 CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
第二步 配置Crontab定時(shí)任務(wù):
./cronJob
* * * * * /usr/local/bin/php /app/www/laravel8/artisan schedule:run >> /var/log/laravel8-crontab-task.log 2>&1
第三步 配置supervisor進(jìn)程管理器:
./supervisord/supervisord.conf
; supervisor config file [unix_http_server] file=/var/run/supervisor.sock ; (the path to the socket file) chmod=0700 ; sockef file mode (default 0700) [supervisord] logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP) ; the below section must remain in the config file for RPC ; (supervisorctl/web interface) to work, additional interfaces may be ; added by defining them in separate rpcinterface: sections [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket ; The [include] section can just contain the "files" setting. This ; setting can list multiple files (separated by whitespace or ; newlines). It can also contain wildcards. The filenames are ; interpreted as relative to this file. Included files *cannot* ; include files themselves. [include] files = /etc/supervisor/conf.d/*.conf
supervisord/conf.d/supervisord.conf
[supervisord] nodaemon=true [program:php-fpm] command=/usr/local/sbin/php-fpm -F autostart=true autorestart=true startretries=3 priority=1 stdout_logfile=/var/log/php-fpm.log redirect_stderr=true [program:crond] command=/usr/sbin/crond -f autostart=true autorestart=true stdout_logfile=/var/log/crond.log redirect_stderr=true priority=2
第四步 編寫(xiě)docker-compose.yml:
docker-compose.yml
version: '3.8' services: php7.4fpm: build: dockerfile: Dockerfile image: php7.4fpm container_name: php7.4fpm restart: always volumes: # 映射應(yīng)用程序目錄 - /Users/king/Code/laravel8:/app/www/laravel8 # 映射Crontab定時(shí)任務(wù)配置 - ./cronJob:/etc/crontabs/root # 映射supervisor配置文件 - ./supervisord:/etc/supervisor # 映射php擴(kuò)展配置 ps:首次構(gòu)建時(shí)需要注釋?zhuān)駝t容器內(nèi)該目錄會(huì)為空 #- ./extensions:/usr/local/etc/php/conf.d # 映射fpm配置文件 ps:首次構(gòu)建時(shí)需要注釋?zhuān)駝t容器內(nèi)該目錄會(huì)為空 #- ./fpm-conf:/usr/local/etc/php-fpm.d networks: default: external: true name: frontend
第五步 構(gòu)建鏡像和容器:
- 拉去基礎(chǔ)鏡像
docker pull php:7.4-fpm-alpine
- 創(chuàng)建網(wǎng)絡(luò)
docker network create frontend
- 容器編排
docker-compose up -d --build
- 查看容器狀態(tài)
同步文件
#同步php擴(kuò)展配置文件夾,后續(xù)可以直接在宿主機(jī)變更相關(guān)參數(shù)配置 docker cp php7.4fpm:/usr/local/etc/php/conf.d ./extensions #同步fpm配置文件夾,后續(xù)可以直接在宿主機(jī)變更相關(guān)參數(shù)配置 docker cp php7.4fpm:/usr/local/etc/php-fpm.d ./fpm-conf
查看當(dāng)前目錄結(jié)構(gòu)
部署nginx
第一步 編寫(xiě)Dockerfile:
./Dockerfile
FROM nginx:alpine # 安裝時(shí)區(qū)工具 RUN set -ex \ && sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \ && apk --update add tzdata \ && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime EXPOSE 80 443 # 定義容器啟動(dòng)時(shí)運(yùn)行的命令 CMD ["nginx", "-g", "daemon off;"]
第二步 編寫(xiě)配置文件以及站點(diǎn)vhost:
./nginx.conf
user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; #壓縮配置 gzip on; gzip_comp_level 5; gzip_min_length 1k; gzip_buffers 4 16k; gzip_proxied any; gzip_vary on; gzip_types application/javascript application/x-javascript text/javascript text/css text/xml application/xhtml+xml application/xml application/atom+xml application/rdf+xml application/rss+xml application/geo+json application/json application/ld+json application/manifest+json application/x-web-app-manifest+json image/svg+xml text/x-cross-domain-policy; gzip_static on; gzip_disable "MSIE [1-6]\."; include /etc/nginx/conf.d/*.conf; }
./conf.d/default.conf
server { listen 80; server_name localhost; root /app/www/laravel8/public; index index.php index.html; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass php7.4fpm:9000; # PHP-FPM 容器的地址和端口 fastcgi_index index.php; } location ~ /\.ht { deny all; } error_log /var/log/nginx/error-php7.4fpm.log; access_log /var/log/nginx/access-php7.4fpm.log; }
第三步 編寫(xiě)docker-compose.yml:
./docker-compose.yml
version: '3.8' services: nginx: build: dockerfile: Dockerfile image: nginx container_name: nginx volumes: - ./nginx.conf:/etc/nginx/nginx.conf - ./conf.d:/etc/nginx/conf.d - ./log:/var/log/nginx restart: always ports: - "80:80" networks: default: external: true name: frontend
第四步 構(gòu)建鏡像和容器:
- 拉去基礎(chǔ)鏡像
docker pull nginx:alpine
- 容器編排
docker-compose up -d --build
- 查看容器狀態(tài)
- 目錄結(jié)構(gòu)
驗(yàn)證
如果以上步驟順利操作,瀏覽器訪(fǎng)問(wèn) http://127.0.0.1或http://localhost看響應(yīng)結(jié)果。
到此這篇關(guān)于Docker部署php運(yùn)行環(huán)境(php-fpm+nginx)的文章就介紹到這了,更多相關(guān)Docker部署php運(yùn)行環(huán)境內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- docker運(yùn)行Nginx及配置方法
- docker運(yùn)行nginx不生效的解決辦法
- docker運(yùn)行nginx綁定配置文件失敗原因以及問(wèn)題解決
- Docker容器下運(yùn)行Nginx并實(shí)現(xiàn)反向代理
- 淺談docker運(yùn)行nginx為什么要使用daemon off
- Docker中運(yùn)行nginx并掛載本地目錄到鏡像中的方法
- docker nginx 運(yùn)行后無(wú)法訪(fǎng)問(wèn)的問(wèn)題解決
- 利用docker搭建php7和nginx運(yùn)行環(huán)境全過(guò)程(官方鏡像)
- 在 Docker 環(huán)境中部署和運(yùn)行 Nginx 的方法
相關(guān)文章
Dockerfile如何使用alpine系統(tǒng)制作haproxy鏡像
這篇文章主要介紹了Dockerfile如何使用alpine系統(tǒng)制作haproxy鏡像問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Docker Desktop啟動(dòng)失敗的解決(Docker failed to i
本文主要介紹了Docker Desktop啟動(dòng)失敗的解決(Docker failed to initialize Docker Desktop is shutting down),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Docker 容器互聯(lián)互通的實(shí)現(xiàn)方法
這篇文章主要介紹了Docker 容器互聯(lián)互通,本文講解不同網(wǎng)絡(luò)下的容器可以通過(guò)加入同一個(gè)docker網(wǎng)絡(luò),來(lái)訪(fǎng)問(wèn)該docker網(wǎng)絡(luò)下的容器,并且既可以通過(guò)容器ip也可以通過(guò)容器名連接,非常方便,需要的朋友可以參考下2022-10-10使用Grafana 展示Docker容器的監(jiān)控圖表并設(shè)置郵件報(bào)警規(guī)則(圖解)
這篇文章主要介紹了使用Grafana 展示Docker容器的監(jiān)控圖表并設(shè)置郵件報(bào)警規(guī)則的圖文教程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01使用docker?compose快速配置一組容器服務(wù)詳解
這篇文章主要為大家介紹了使用docker-?compose快速配置一組容器服務(wù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11解決Docker x509 insecure registry的問(wèn)題
這篇文章主要介紹了解決Docker x509 insecure registry的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03Docker查看及修改Redis容器密碼的實(shí)用指南
在使用 Docker 部署 Redis 容器時(shí),有時(shí)我們需要查看或修改 Redis 的密碼,本文將詳細(xì)介紹如何在 Docker 中查看和修改 Redis 容器的密碼,幫助你更好地管理和維護(hù)你的 Redis 實(shí)例,需要的朋友可以參考下2024-07-07Docker容器訪(fǎng)問(wèn)掛載文件權(quán)限問(wèn)題小結(jié)
在使用docker-compose部署項(xiàng)目時(shí),因?yàn)镾ELinux策略導(dǎo)致容器無(wú)法訪(fǎng)問(wèn)宿主機(jī)上掛載的文件,解決方案包括禁用SELinux、修改文件類(lèi)型為svirt_sandbox_file_t或使用:Z選項(xiàng)掛載文件,本文介紹Docker容器訪(fǎng)問(wèn)掛載文件權(quán)限問(wèn)題,感興趣的朋友一起看看吧2025-02-02