linux centos + nginx +多版本php安装 实战

2022/1/23 7:04:36

本文主要是介绍linux centos + nginx +多版本php安装 实战,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

项目需要,有些旧的项目使用的是php5.6,有些使用php7.2 ,作为本地开发环境需要多套php环境,之前是开启多个虚拟机,但是虚拟机占用资源大,在多个虚拟机之前切换也不方便,故打算使用docker来搭建一套高性能的多版本php环境

一、docker 安装

1、先卸载旧版docker

# yum remove docker \
           docker-client \
           docker-client-latest \
           docker-common \
           docker-latest \
           docker-latest-logrotate \
           docker-logrotate \
           docker-engine

2、安装 yum-utils并把docker存仓库到yum配置

# yum install -y yum-utils
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

3、安装docker 引擎

# yum install docker-ce docker-ce-cli containerd.io

4、启动docker 

# systemctl start dockera 

二、拉取 nginx 镜像

# docker pull nginx

三、使用dockerfile 制作php-fpm镜像

因为默认的php-fpm镜像是没有数据库,redis等常用的扩容的,所有用到项目的时候需要构建自己的镜像

1、创建dockerfile

# cd ~
# mkdir dockerfiles 
# mkdir PHP5.6
# mkdir PHP7.2
# cd PHP5.6
# vi Dockerfile

2、把一些内容保存到Dockerfile文件下

FROM daocloud.io/php:7.2-fpm-alpine

LABEL maintainer="lestat@lestat.me"

# 修改镜像源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 安装依赖,核心扩展,pecl扩展,git,composer,npm工具
RUN apk update && apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS \
        curl-dev \
        imagemagick-dev \
        libtool \
        libxml2-dev \
        postgresql-dev \
        sqlite-dev \
        libmcrypt-dev \
        freetype-dev \
        libjpeg-turbo-dev \
        libpng-dev \
    && apk add --no-cache \
        curl \
        git \
        imagemagick \
        mysql-client \
        postgresql-libs \
        nodejs \
        nodejs-npm \
    # 配置npm中国镜像
    && npm config set registry https://registry.npm.taobao.org \
    && pecl install imagick \
    && pecl install mcrypt-1.0.1 \
    && docker-php-ext-enable mcrypt \
    && docker-php-ext-enable imagick \
    && docker-php-ext-install \
        curl \
        mbstring \
        pdo \
        pdo_mysql \
        pdo_pgsql \
        pdo_sqlite \
        pcntl \
        tokenizer \
        xml \
        zip \
        && docker-php-ext-install -j"$(getconf _NPROCESSORS_ONLN)" iconv \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j"$(getconf _NPROCESSORS_ONLN)" gd \
    && pecl install -o -f redis \
    && rm -rf /tmp/pear \
    && docker-php-ext-enable redis

# 安装composer并允许root用户运行
ENV COMPOSER_ALLOW_SUPERUSER=1
ENV COMPOSER_NO_INTERACTION=1

3、运行 构建包含常用扩容的的php-fpm

# docker build -t php:my7.2 .

运行需要半个小时以上,运行完后,查看是否创建成功

[root@localhost php7.2]# docker images
REPOSITORY        TAG              IMAGE ID       CREATED         SIZE
php               my7.2            96e302a03e7f   2 minutes ago   759MB
nginx             latest           605c77e624dd   3 weeks ago     141MB

 

同理去到~/Dockerfiles/php5.6 目录下保存5.6的dockefile,并构建包含常用扩容的php5.6镜像

# cd ~/Dockerfiles/php5.6
# vi Dockerfile

把以下内容保存到 Dockerfile 文件上

 

FROM php:5.6-fpm

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
libmemcached-dev \
zlib1g-dev \
libbz2-dev \
libgmp-dev \
libedit-dev \
libxml2-dev \
libxslt-dev \
openssl \
libssl-dev \
libpq-dev \
&& docker-php-ext-install -j$(nproc) iconv \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd  \
bcmath \
mysqli \
pdo_mysql \
bz2 \
calendar \
exif \
gettext \
intl \
pcntl \
readline \
pgsql \
pdo_pgsql \
shmop \
sockets \
wddx \
xsl \
zip \
opcache \
mcrypt \
&& pecl install igbinary-1.2.1 && docker-php-ext-enable igbinary \
&& echo "\n" | pecl install redis && docker-php-ext-enable redis \
&& echo "\n" | pecl install memcache && docker-php-ext-enable memcache \
&& echo "\n" | pecl install memcached-2.2.0  && docker-php-ext-enable memcached \
&& pecl install mongodb && docker-php-ext-enable mongodb \
&& echo "\n" | pecl install mongo && docker-php-ext-enable mongo \
&& docker-php-source delete \
&& apt-get clean; rm -rf /var/lib/apt/lists/

 

构建包含常用扩展的镜像

# docker build -t php:my5.6 .

 

 

 4、构建php-fpm和nginx 容器

# docker run --name  myphp72-fpm  -v /mnt/hgfs/pj/web-collect:/usr/share/nginx/html  -d php:my7.2
# docker run --name  myphp56-fpm  -v /mnt/hgfs/pj/web-collect:/usr/share/nginx/html  -d php:my5.6

注:-v /mnt/hgfs/pj/web-collect:/usr/share/nginx/html ,是把本地的网站根目录(/mnt/hgfs/pj/web-collect)挂载到容器的根目录下(/usr/share/nginx/html )

在服务器外增加nginx配置文件,这样每次启动容器的时候不需要进入容器手工加nginx配置

# cd ~
# mkdir nginx
# cd nginx
# mkdir conf
# cd conf
# mkdir conf.d
# cd conf.d
# vi web-collect.conf

把自己的nginx配置保存,以下面的项目为例,和直接使用虚拟机 的nginx 配置相比,需要把 fastcgi_pass 127.0.0.1:9000;  改为 fastcgi_pass myphp56-fpm:9000; 其中 myphp56-fpm为刚刚创建的php5.6容器,这样改后,该站点 的php请求就会发到 myphp56-fpm容器处理。

server {
        listen        80;
        server_name  web-abc.com www.web-abc.com static.web-abc.com order.web-abc.com cart.web-abc.com user.web-abc.com;
        root   "/usr/share/nginx/html/web/htdocs";
        
        add_header Access-Control-Allow-Origin * always;
        add_header Access-Control-Allow-Headers Content-Type;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        #add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'; 
        add_header Access-Control-Allow-Headers Authorization;
        add_header Access-Control-Max-Age 86400;
        
        location / {
        try_files $uri $uri/ /index.php?$query_string;
               index index.php
                autoindex  off;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   myphp56-fpm: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;
        index index.php;
        }
}

server {

    listen       80;
    server_name  admin.web-abc.com;
    root /usr/share/nginx/html/web-admin/htdocs;

    location / {
                index   index.htmi index.php index.html;
                try_files $uri $uri/ /index.php?$args;
        }
    location ~ \.php$ {

                fastcgi_pass   myphp72-fpm:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
                fastcgi_param  ENV 'dev';
                fastcgi_param  site-env 'dev';


        }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|woff2)$
     {
        root /usr/share/nginx/html/web-admin/htdocs;
     }
    location ~ .*(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)$
      {
        deny               all;
      }
    location ^~ /runtime/log/ {
        deny all;
    }

}

 

5、创建nginx容器

# docker run --name mynginx -p 8090:80 -d -v /mnt/hgfs/pj/web-collect/:/usr/share/nginx/html:ro -v /root/nginx/conf/conf.d:/etc/nginx/conf.d:ro --link myphp72-fpm --link myphp56-fpm nginx

注:-p 8090:80  , 是把服务器的8090端口映射到容器的80端口上,如果服务器80端口没有被占用可以不加该参数   ;--link myphp72-fpm --link myphp56-fpm nginx  ,是把上面的php容器 连接到nginx上,这样nginx容器里的/etc/hosts 会增加一个 host记录,从而让nginx容器通过php 容器名或ID找到对应的容器

可以通过以下命名进入容器,并查看hosts

[root@localhost conf.d]# docker exec -it mynginx /bin/bash
root@48cf3f125c74:/# cat /etc/hosts
127.0.0.1    localhost172.17.0.3    myphp72-fpm 06fbeea92bbc
172.17.0.2    myphp56-fpm 332eeba22546

 

查看运行的容器

[root@localhost conf.d]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED             STATUS             PORTS                                   NAMES
48cf3f125c74   nginx       "/docker-entrypoint.…"   10 minutes ago      Up 10 minutes      0.0.0.0:8090->80/tcp, :::8090->80/tcp   mynginx
06fbeea92bbc   php:my7.2   "docker-php-entrypoi…"   About an hour ago   Up About an hour   9000/tcp                                myphp72-fpm
332eeba22546   php:my5.6   "docker-php-entrypoi…"   About an hour ago   Up About an hour   9000/tcp                                myphp56-fpm

 

6、用浏览器访问

 

 



这篇关于linux centos + nginx +多版本php安装 实战的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程