宿主机环境
系统:MAC、Windows10
Docker版本:Docker version 23.0.5
Docker Desktop:Dockerdesktop官方地址
前言
这篇主要介绍如何在Mac、Windows10使用docker搭建LNMP集成开发环境。下面我会写Dockerfile编译安装Nginx+php基础环境。mysql、redis基于官方Docker hub,分别构建我们自己的镜像。
目录结构
- code目录存放工程代码,会映射到docker容器web目录
- docker-data目录存放容器数据
- fileinfo是php的扩展
- fricc2是php源码加密扩展
构建PHP+Nginx镜像
Dockerfile文件
# 从官方基础版本构建
FROM centos:7
MAINTAINER ityun
ENV TIME_ZONE Asia/Shanghai
#基础依赖
RUN yum install -y install epel-release && \
yum -y install gcc gcc-c++ autoconf wget libxml2 libxml2-devel openssl openssl-devel curl curl-devel libjpeg-turbo libjpeg-turbo-devel libpng-devel libpng freetype-devel freetype icu libicu-devel libicu libmcrypt libmcrypt-devel libxslt libxslt-devel php-mysql oniguruma oniguruma-devel libtidy-devel openldap openldap-devel sqlite-devel zlib-static zlib-devel && \
yum clean all && \
groupadd www && \
useradd -g www www
RUN cat .pwd.txt | chpasswd
#拷贝源码包到环境中
ADD php-8.0.10.tar.gz /usr/local/src/
ADD redis-5.3.4.tgz /usr/local/src/
ADD nginx-1.21.1.tar.gz /usr/local/src/
ADD libzip-1.2.0.tar.gz /usr/local/src/
COPY fricc2 /usr/local/src/php-fricc2/
COPY nginx.conf /usr/local/src/nginx.conf
COPY cas.ityun.com_80.conf /usr/local/src/cas.ityun.com_80.conf
COPY php.ini /usr/local/src/php.ini
COPY fileinfo /usr/local/src/fileinfo/
#编译安装nginx及php
RUN cd /usr/local/src/nginx-1.21.1 && \
./configure --prefix=/usr/local/nginx --user=www --group=www --with-stream --with-http_ssl_module --with-http_stub_status_module && \
make -j 4 && \
make install && \
mkdir -p /web && \
cd /usr/local/nginx/conf/ && \
mkdir vhosts && \
cp /usr/local/src/cas.ityun.com_80.conf /usr/local/nginx/conf/vhosts/ &&\
cp /usr/local/src/nginx.conf /usr/local/nginx/conf/ &&\
cp -frp /usr/lib64/libldap* /usr/lib/ &&\
#安装libzip依赖,方便gd开启
cd /usr/local/src/libzip-1.2.0 && \
./configure && make && make install && \
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/" &&\
#安装php
cd /usr/local/src/php-8.0.10 && \
./configure --prefix=/usr/local/php8 --with-config-file-path=/usr/local/php8/etc \
--with-config-file-scan-dir=/usr/local/php8/conf.d \
--enable-fpm --with-fpm-user=www \
--with-fpm-group=www --enable-static --enable-sockets \
--enable-mysqlnd --enable-opcache --enable-pcntl --enable-gd \
--enable-mbstring --enable-soap --enable-calendar \
--enable-bcmath --enable-exif --enable-ftp --enable-intl --with-mysqli --with-pdo-mysql --without-sqlite3 \
--with-openssl --with-curl --with-gettext --with-zip --with-zlib \
--with-mhash --with-tidy \
--disable-fileinfo \
--with-freetype \
--with-jpeg && \
make && \
make install && \
ln -s /usr/local/php8/bin/php /usr/bin/php && \
#配置相关
cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf && \
cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf && \
sed -i "90a \daemonize = no" /usr/local/php8/etc/php-fpm.conf && \
cp /usr/local/src/php-8.0.10/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \
chmod +x /etc/init.d/php-fpm && \
echo "${TIME_ZONE}" > /etc/timezone && \
ln -sf /usr/share/zoneinfo/${TIME_ZONE} /etc/localtime && \
#安装redis扩展
cd /usr/local/src/redis-5.3.4 && \
/usr/local/php8/bin/phpize &&\
./configure --with-php-config=/usr/local/php8/bin/php-config &&\
make -j 4 && make install &&\
#安装ldap扩展
cd /usr/local/src/php-8.0.10/ext/ldap/ &&\
/usr/local/php8/bin/phpize && ./configure --with-php-config=/usr/local/php8/bin/php-config --with-ldap &&\
make && make install &&\
#安装fileinfo扩展#
cd /usr/local/src/php-8.0.10/ext/fileinfo/ &&\
/usr/local/php8/bin/phpize && ./configure --with-php-config=/usr/local/php8/bin/php-config &&\
cp -rf /usr/local/src/fileinfo/Makefile /usr/local/src/php-8.0.10/ext/fileinfo/ &&\
make && make install &&\
#安装fricc2扩展
cd /usr/local/src/php-fricc2/fricc2load/ &&\
/usr/local/php8/bin/phpize && ./configure --with-php-config=/usr/local/php8/bin/php-config &&\
make && make install &&\
cd /usr/local/src/php-fricc2/fricc2/ && make &&\
cp fricc2 /usr/bin &&\
cp /usr/local/src/php.ini /usr/local/php8/etc/php.ini &&\
rm -rf /usr/local/src/*
WORKDIR /usr/local/nginx
EXPOSE 9000
EXPOSE 80
EXPOSE 443
CMD /etc/init.d/php-fpm start && /usr/local/nginx/sbin/nginx -g "daemon off;"
nginx配置文件
user www;
worker_processes 4;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#keepalive_timeout 0;
keepalive_timeout 200;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php application/javascript application/json;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
include vhosts/*.conf;
}
cas.ityun.com_80.conf配置文件
server {
listen 80;
server_name cas.ityun.com;
client_max_body_size 2048M;
root "/web/cas.ityun.com/public/";
location / {
index index.php index.html;
#主要是这一段一定要确保存在
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.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;
}
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Frame-Options SAMEORIGIN;
error_page 403 =404 /404.html;
}
开始构建
进入Dockefile所在目录,执行以下命令。PS:构建过程比较慢,大概需要20分钟左右。
docker build -t ityun/centos7-php8 .
拉取Mysql、Redis镜像
docker pull redis
docker pull mysql:5.7
执行完成后执行docker images命令,可以看到本地镜像列表。
运行镜像
# 启动Docker容器
## 启动redis容器
docker run -it -d -p 8379:6379 --name redis redis:latest
## 启动mysql容器
docker run -it -d -p 8306:3306 -e MYSQL_ROOT_PASSWORD=root --name mysqldb -v ~/Documents/mysql57:/var/lib/mysql mysql:5.7
## 通过上面步骤下面这个对应的link名称自己改一下
docker run -it -p 8472:80 -p 8473:443 --name ityun --link mysqldb:mysqldb --link redis:redis --link -d -v ~/Documents/code:/web ityun/centos7-php8
### ps:以下是windows系统的启动 可忽略
docker run -it -d -p 8379:6379 --name redis redis:latest
docker run -it -d -p 8306:3306 -e MYSQL_ROOT_PASSWORD=root --name mysqldb -v E://project/docker/docker-data/mysql57:/var/lib/mysql mysql:5.7
docker run -it -p 8472:80 -p 8473:443 --name ityun --link mysqldb:mysqldb --link redis:redis -d -v E://project/docker/code:/web ityun/centos7-php8
运行完成后 通过docker ps可以查看正在运行的docker容器。
运行thinkphp5
在code目录创建一个文件夹cas.ityun.com,目录中放置thinkphp5框架代码
在浏览器访问一下http://cas.ityun.com:8472内容显示正常即可:
文章来源:https://www.toymoban.com/news/detail-610984.html
构建时需要的源文件:PS:期待给点个星。。。lnmp: 使用Dockerfile部署LNMP集成环境。centos+php+nginx+mysql+redis.文章来源地址https://www.toymoban.com/news/detail-610984.html
到了这里,关于使用Docker Dockerfile构建php LNMP集成开发环境,并运行Thinkphp5的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!