dockerfile编译安装php74+nginx1.17+supervisord启动
一些注意事项 注:纯属个人理解
1.使用过镜像生成容器,在commit生成镜像,这样的做法闲的臃肿,切无法在镜像启动时候使用一些命令
2.docker是后台运行,所以docker里的服务不可以后台运行,应设置前台运行,只要保证服务的生命周期和docker声明周期一样即可保持运行
3.dockerfile就是linux下安装过程的命令复制到dockerfile中
好下面直接上dockerfile
1.Dockerfile(注意,Dockerfile名字第一个字母必须为大写)
FROM centos:7
MAINTAINER 915177778@qq.com
#切换系统时间
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#安装wget php74
RUN yum -y install wget \
&& wget https://www.php.net/distributions/php-7.4.0.tar.gz
#安装系统所需扩展
RUN yum -y install gcc \
gcc-c++ \
make \
libxml2-devel \
openssl-devel \
curl-devel \
libjpeg-devel \
libpng-devel \
libicu-devel \
sqlite-devel \
freetype-devel \
openldap-devel \
openldap \
openldap-devel
#解压php
RUN tar -zxvf php-7.4.0.tar.gz
#进入工作目录
WORKDIR /php-7.4.0
#编译安装及php扩展安装
RUN ./configure --prefix=/usr/local/php \
--with-config-file-scan-dir=/usr/local/php/etc/ \
--with-mhash --with-pdo-mysql \
--with-openssl --with-mysqli \
--with-iconv --with-zlib \
--enable-inline-optimization \
--disable-debug --disable-rpath \
--enable-shared --enable-xml \
--enable-bcmath --enable-shmop \
--enable-sysvsem --enable-sysvshm --enable-mbregex \
--enable-ftp \
--enable-pcntl --enable-sockets \
--with-xmlrpc --enable-soap \
--without-pear --with-gettext \
--enable-session --with-curl \
--enable-opcache --enable-fpm \
--without-gdbm --enable-fast-install \
--disable-fileinfo
RUN make && make install
#配置文件修改
RUN cp /php-7.4.0/php.ini-production /usr/local/php/etc/php.ini
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
#添加www组,www可以运行,'www'在www下
RUN groupadd www \
&& useradd -g www www
#解压nginx
RUN wget http://nginx.org/download/nginx-1.17.6.tar.gz
RUN tar -zxvf nginx-1.17.6.tar.gz
#进入nginx
WORKDIR nginx-1.17.6
#编译安装
RUN ./configure \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-pcre
RUN make && make install
#修改配置文件
ADD nginx.conf /usr/local/nginx/conf/
WORKDIR /
#创建nginx的工作目录和配置目录
RUN mkdir www \
&& chmod -R 777 www \
&& cd www \
&& mkdir web \
&& mkdir conf
#进入代码文件目录
WORKDIR /www/web
RUN mkdir default
#进入nginx配置目录
WORKDIR /www/conf
RUN mkdir vhosts
#修改站点配置
ADD default.conf /www/conf/vhosts/
#代码复制到文件目录(demo文件夹下是你的代码文件)
ADD demo /www/web/default/
#切换工作目录
WORKDIR /var
#给/run/php-fpm权限 php-fpm启动时会生产在此目录下
RUN chmod -R 777 run
#WORKDIR /var/run
#RUN mkdir php-fpm
WORKDIR /run
RUN mkdir php-fpm
RUN chmod -R 777 /run/php-fpm
#docker容器里的服务不能后台运行(不能使用sevice),使用supervisord控制开机启动
RUN rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
RUN rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
#安装supervisor
RUN yum -y install supervisor
#修改配置,最后两行是运行php-fpm和nginx
COPY supervisord.conf /etc/
#启动supervisord
CMD /usr/bin/supervisord -c /etc/supervisord.conf
2.文件目录
3.php-fpm.conf
重要的最后一行include
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;
; All relative paths in this configuration file are relative to PHP's install
; prefix (/usr/local/php). This prefix can be dynamically changed by using the
; '-p' argument from the command line.
;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;
[global]
; Pid file
; Note: the default prefix is /usr/local/php/var
; Default Value: none
;pid = run/php-fpm.pid
; Error log file
; If it's set to "syslog", log is sent to syslogd instead of being written
; into a local file.
; Note: the default prefix is /usr/local/php/var
; Default Value: log/php-fpm.log
;error_log = log/php-fpm.log
; syslog_facility is used to specify what type of program is logging the
; message. This lets syslogd specify that messages from different facilities
; will be handled differently.
; See syslog(3) for possible values (ex daemon equiv LOG_DAEMON)
; Default Value: daemon
;syslog.facility = daemon
;这里要设置成no,php-fpm运行到前台
daemonize = no
include=/usr/local/php/etc/php-fpm.d/*.conf
4.www.conf
[www]
user = www
group = www
listen.owner = www
listen.group = www
listen.mode = 0666
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
listen.owner = www listen.group = www listen.mode = 0666是为了生成的php-fpm.sock生成在www下,可以让nginx访问,不设置这三行会无权限。需要chmod去加权限
5.nginx.conf
#user nobody;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include /www/conf/vhosts/*.conf;
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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
root /usr/local/nginx/html;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
6.default.conf
server{
listen 8088;
server_name 127.0.0.1;
root /www/web/default;
location / {
#开启 url 美化
if (!-e $request_filename){
rewrite ^/(.*) /index.php last;
}
index index.html index.php;
}
location ~ \.php$ {
include fastcgi.conf;
#fastcgi_pass 127.0.0.1:9000
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
try_files $uri = 404;
}
}
这里我fastcgi_pass用的sock,代表php监听这个端口,nginx接收到webserver的php文件发送到这个端口
fastcgi_pass要和www.conf里listen的路径一样
7.文件和dockerfile构建好了,开始生产镜像
docker build -t php-nginx7.4 .
最后的’.'不能没有,代表当前目录,
制作镜像成功,如果不成功,会提示你哪步骤的原因的
8.查看镜像,nginx-php7.4就是刚才生成的镜像了
docker images
9运行镜像
docker run -itd -p 8088:8088 nginx-php7.4
10.查看镜像
docker -ps -a
这时候代表镜像已经启动了,default文件夹下就是放你代码的地方,我没放数据,如果测试可以在default下放一个index.html和index.php
然后访问127.0.0.1:8088/index.html或127.0.0.1:8088/index.php就可以看到结果了
11.进入镜像
docker exec -it e0f01f6 /bin/bash
这样也可以进入镜像