current position:Home>Docker quickly builds a PHP+Nginx+Mysql environment and steps on the pit diary
Docker quickly builds a PHP+Nginx+Mysql environment and steps on the pit diary
2022-08-06 17:33:33【1 layman】
我正在参与掘金创作者训练营第5期,点击了解活动详情
准备
创建目录
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/phpProgramis the local project directory,/var/www/htmlIs the project storage directory inside the container
配置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 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,则验证码显示正常了
copyright notice
author[1 layman],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/218/202208061702144874.html
The sidebar is recommended
- Understand the location and root nginx
- Alibaba Cloud nginx configuration file does not take effect
- Men "powerless" don't blame electric cars
- java project recommendation - based on springboot+vue car rental system 296
- Shared charging treasure management system based on springboot+vue
- Reprint--Several recommended practices for JavaScript asynchronous code
- Web version face recognition, face login, face comparison, springboot+vue+mysql
- Web version face login, web face login, face recognition, based on Springboot+vue2.X version+mysql
- Nodejs knowledge that the front end needs to know (basic)
- AngularJS uses ng-class, ng-style to achieve dynamic css style
guess what you like
vue drop down menu
Nginx proxy_set_header parsing and stretches add_header instruction [go]
Vue study notes (middle)
Vue study notes
Vue study notes (below)
Escaping and escaping strings json, xml, html escaping and escaping tools StringEscapeUtils
Django website development 01.Web website and front-end HTML tags
Django website development 02. CSS Cascading Style Sheets (Cascading Style Sheets)
(Front-end) Application of "Memo" design pattern in project development
Javascript paging: simulate the method of returning data in the background
Random recommended
- How to use wireless serial communication module to realize long-distance communication between touch screen and PLC?
- Getting Started with Vue2 - Rookie Level 1
- HTTP and HTTPS protocols
- 2022 Autumn Recruitment Front-end Interview Questions (1) (with answers)
- From function computing to serverless architecture
- Before 2022 the autumn recruit face questions (with answers) (2)
- About git's fatal: unable to access 'https://gitee.com/': Could not resolve host: gitee.com; Unknown error
- The sinkhole of Nginx's current limit!!
- Write a progress bar in JavaScript
- Write input box validation in JavaScript
- CSS knowledge points
- Write a random color in JavaScript
- Construct HTTP
- Webstorm opens and browses md files with garbled characters?
- PHP+HTML+MySQL realizes login error
- The comments in the Dygraphs Annotations
- [React] Part VI Life Cycle
- [React] Part VII Dom's diffing algorithm
- To React the React in the setState after data didn't change
- [React] The problem of React project startup stuck in Starting the development server...
- [React] The eighth part of react scaffolding installation and react scaffolding configuration agent
- [React] Part 5 Higher-Order Functions and Currying of Functions
- form and CSS
- In the Vue named Vue 】 【 encountered when component name "index" should always be multi - solution of word
- A pit in Nginx forwarding, operation and maintenance actually let me take the blame
- Easily converts HTML to PDF BFO Publisher
- vue2 typescript style type error?
- The back-end json is transferred to the front-end, but the front-end does not display?
- The sinkhole of Nginx current limit
- nginx service upgrade
- Vue gets the width and height attributes of the file file
- html submit form does not jump to the page
- CSS Tricks You Need to Know: Images
- Huawei FreeBuds Pro 2 review: the strongest TWS is here, this is the ceiling of wireless headphones
- Docker deploys a single page application
- Nginx and CDN
- vue click.stop stops the click event from continuing to propagate (stops the event from bubbling up)
- js array to remove the specified element [function encapsulation] (including object array to remove the specified element)
- CSS Drawing [Summary]
- [Webpack Quick Start] How to manually configure a webpack packaging tool for the project?