[Exchange and share] Linux安装多版本PHP共存,和多版本MySQL共存,和多版本Go共存
Tofloor
poster avatar
kentrl
deepin
2024-09-02 11:10
Author

多版本共存的好处是:在nginx的配置~/goserver/conf/nginx/conf.d/ 目录内加一个demo.conf 就可以直接跑起来项目了。

server {
    listen       80;
    server_name  demo.test;
    index index.php index.html index.htm;

    root /var/www/demo;

    location / {
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php/$1  last;
            break;
        }
    }

    location ~ .php {
        # try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass php74:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        include fastcgi_params;
    }
}

注意:仅修改版本号即可实现版本任意切换

fastcgi_pass php74:9000; 这一行修改指定使用php7.4版本的。

fastcgi_pass php80:9000; 这一行修改指定使用php8.0版本的。

先上图,真实可用可靠。

image.png

Linux安装多版本PHP共存,和多版本MySQL共存,和多版本Go共存

一:先安装Docker,如果不知道可以论坛点击搜索,有其他人帖子

二:使用我给的docker-compose和命令

创建目录:

image.png

  1. 创建docker-compose.yaml
#version: '3'
services:
  redis:
    image: redis:6.0.2
    container_name: redis
    restart: always
    ports:
      - "6379:6379"
    volumes:
      - ~/goserver/data/redis/data:/data
      - ~/goserver/data/redis/conf/:/usr/local/etc/redis/
      - ~/goserver/data/redis/logs:/var/log/redis.log/
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
    environment:
      - TZ=Asia/Shanghai
    networks:
      - lnmp

  mysql57:
    image: mysql:5.7
    container_name: mysql57
    build:
      context: ~/goserver/data/mysql57
      dockerfile: Dockerfile
    restart: always
    ports:
      - "3307:3307"
    privileged: true
    command: ["mysqld", "--default-authentication-plugin=mysql_native_password", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"]
    volumes:
      - ~/goserver/data/mysql57/logs/:/var/log/mysql/
      - ~/goserver/data/mysql57/data/:/var/lib/mysql/
      - ~/goserver/data/mysql57/conf/my.cnf:/etc/mysql/conf.d/my.cnf
    environment:
      - TZ=Asia/Shanghai
      - MYSQL_ROOT_PASSWORD=root
    networks:
      - lnmp

  mysql80:
    image: mysql:8.0
    container_name: mysql80
    build:
      context: ~/goserver/data/mysql80
      dockerfile: Dockerfile
    restart: always
    ports:
      - "3308:3308"
    privileged: true
    volumes:
      - ~/goserver/data/mysql80/logs/:/var/log/mysql/
      - ~/goserver/data/mysql80/data/:/var/lib/mysql/
      - ~/goserver/data/mysql80/conf/my.cnf:/etc/mysql/conf.d/my.cnf
    command: ["mysqld", "--default-authentication-plugin=mysql_native_password", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"]
    environment:
      - TZ=Asia/Shanghai
      - MYSQL_ROOT_PASSWORD=root
    networks:
      - lnmp

  php74:
    image: php:7.4
    container_name: php74
    build:
      context: ~/goserver/conf/php74
      dockerfile: Dockerfile
    restart: always
    cap_add:
      - SYS_PTRACE
    ports:
      - "9074:9000"
    user: "1000:1000"
    privileged: true
    volumes:
      - ~/www:/var/www/
      - ~/goserver/conf/php74/php.ini:/usr/local/etc/php/php.ini-development
      - ~/goserver/conf/php74/php-fpm.conf:/usr/local/etc/php-fpm.d/www.conf
    environment:
      - TZ=Asia/Shanghai
    networks:
      - lnmp

  php80:
    image: php:8.0
    container_name: php80
    build:
      context: ~/goserver/conf/php80
      dockerfile: Dockerfile
    restart: always
    cap_add:
      - SYS_PTRACE
    ports:
      - "9080:9000"
    user: "1000:1000"
    privileged: true
    volumes:
      - ~/www:/var/www/
      - ~/goserver/conf/php80/php.ini:/usr/local/etc/php/php.ini-development
      - ~/goserver/conf/php80/php-fpm.conf:/usr/local/etc/php-fpm.d/www.conf
    environment:
      - TZ=Asia/Shanghai
    networks:
      - lnmp

  nginx:
    image: nginx:latest
    container_name: nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    privileged: true
    volumes:
      - ~/www:/var/www/
      - ~/goserver/conf/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ~/goserver/conf/nginx/conf.d:/etc/nginx/conf.d
      - ~/goserver/conf/nginx/logs:/var/log/nginx
    depends_on:
      - php74
      - php80
    environment:
      - TZ=Asia/Shanghai
    networks:
      - lnmp

networks:
  lnmp:

创建php7.4的Dockerfile

# PHP 7.4 Dockerfile
FROM php:7.4-fpm

# 设置容器的时区为"Asia/Shanghai"-`
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN echo "deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib" > /etc/apt/sources.list \
    && echo "deb-src https://mirrors.aliyun.com/debian/ bullseye main non-free contrib" >> /etc/apt/sources.list \
    && echo "deb https://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list \
    && echo "deb-src https://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list \
    && echo "deb https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list \
    && echo "deb-src https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list \
    && echo "deb https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list \
    && echo "deb-src https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list \
    && rm -f /etc/apt/sources.list.d/*

# 安装依赖包
RUN apt-get update && apt-get install -y \
        build-essential \
        imagemagick \
        libmagickcore-dev \
        libmagickwand-dev \
        libfreetype6-dev \
        libmcrypt-dev \
        libjpeg-dev \
        libpng-dev \
        libzip-dev \
        libwebp-dev \
        libjpeg62-turbo-dev \
        zlib1g-dev \
    # 配置GD扩展
    && docker-php-ext-configure gd --with-webp=/usr/include/webp --with-jpeg=/usr/include --with-freetype=/usr/include/freetype2/ \
    # 安装PHP扩展
    && docker-php-ext-install -j$(nproc) gd iconv pdo_mysql mysqli bcmath calendar exif gettext sockets dba  pcntl  shmop sysvmsg sysvsem sysvshm zip \
    && pecl install imagick \
    && pecl install mcrypt \
    && docker-php-ext-enable imagick mcrypt

#opcache  开发暂时不加

#7.4添加可变版本扩展 -- 单独加
ENV PHPREDIS_VERSION 6.0.2
ENV PHPXDEBUG_VERSION 3.1.6
ENV PHPSWOOLE_VERSION 4.8.13

RUN pecl install redis-$PHPREDIS_VERSION \
   && pecl install xdebug-$PHPXDEBUG_VERSION \
   && pecl install swoole-$PHPSWOOLE_VERSION \
   && docker-php-ext-enable redis xdebug swoole

# 安装 Composer
ENV COMPOSER_HOME /root/composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
ENV PATH $COMPOSER_HOME/vendor/bin:$PATH

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

# 这句很重要,如果和你的nginx用户不同会导致你的服务起不来
RUN groupadd www && useradd -g www www

CMD ["php-fpm"]

WORKDIR /var/www/html

创建php8.0的Dockerfile

# PHP 8.0 Dockerfile
FROM php:8.0-fpm

# 设置容器的时区为"Asia/Shanghai"-`
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN echo "deb http://mirrors.aliyun.com/debian/ buster main non-free contrib" > /etc/apt/sources.list.d/buster.list \
    && echo "deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib" >> /etc/apt/sources.list.d/buster.list \
    && echo "deb http://mirrors.aliyun.com/debian-security buster/updates main non-free contrib" > /etc/apt/sources.list.d/buster-security.list \
    && echo "deb-src http://mirrors.aliyun.com/debian-security buster/updates main non-free contrib" >> /etc/apt/sources.list.d/buster-security.list \
    && echo "deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" > /etc/apt/sources.list.d/buster-updates.list \
    && echo "deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" >> /etc/apt/sources.list.d/buster-updates.list \
    && rm -f /etc/apt/sources.list.d/*

# 依赖库和扩展
RUN apt-get update && apt-get install -y \
        build-essential \
        libmagickcore-dev \
        libmagickwand-dev \
        imagemagick \
        libfreetype6-dev \
        libmcrypt-dev \
        libpng-dev \
        libzip-dev \
        libwebp-dev \
        libjpeg62-turbo-dev \
        zlib1g-dev \
    && docker-php-ext-configure gd --with-webp=/usr/include/webp --with-jpeg=/usr/include --with-freetype=/usr/include/freetype2/ \
    && docker-php-ext-install -j$(nproc) gd iconv pdo_mysql mysqli bcmath calendar exif gettext sockets dba  pcntl  shmop sysvmsg sysvsem sysvshm zip \
    && pecl install imagick \
    && pecl install mcrypt \
    && docker-php-ext-enable imagick mcrypt

#7.4添加可变版本扩展 -- 单独加
ENV PHPREDIS_VERSION 6.0.2
ENV PHPXDEBUG_VERSION 3.3.2
ENV PHPSWOOLE_VERSION 5.1.1

RUN pecl install redis-$PHPREDIS_VERSION \
   && pecl install xdebug-$PHPXDEBUG_VERSION \
   && pecl install swoole-$PHPSWOOLE_VERSION \
   && docker-php-ext-enable redis xdebug swoole

# 安装 Composer
ENV COMPOSER_HOME /root/composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
ENV PATH $COMPOSER_HOME/vendor/bin:$PATH

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

# 这句很重要,如果和你的nginx用户不同会导致你的服务起不来
RUN groupadd www && useradd -g www www

CMD ["php-fpm"]

WORKDIR /var/www/html

创建MySQL5.7的Dockerfile

FROM mysql:5.7
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
#设置环境变量(root密码)
#设置免密登录
ENV MYSQL_ROOT_PASSWORD=root
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
ENV LANG=C.UTF-8
EXPOSE 3307
COPY ./conf/my.cnf /etc/mysql/conf.d/
RUN chmod 644 /etc/mysql/conf.d/my.cnf
RUN chmod 644 /home/kentrl/goserver/data/mysql80/conf/my.cnf

创建MySQL8.0的Dockerfile

FROM mysql:8.0
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

ENV MYSQL_ROOT_PASSWORD=root
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
ENV LANG=C.UTF-8
EXPOSE 3308
COPY ./conf/my.cnf /etc/mysql/conf.d/
RUN chmod 644 /etc/mysql/conf.d/my.cnf
RUN chmod 644 /home/kentrl/goserver/data/mysql80/conf/my.cnf

执行docker compose命令安装服务:

docker-compose up redis
docker-compose up msyql57
docker-compose up msyql80
docker-compose up php74
docker-compose up php80
docker-compose up nginx

有什么问题再问我。

image.png

image.png

测试成功:论坛相当扯淡,这个截图还提示识别到非法,没办法只得打码图片了........擦,辣鸡

image.png

Reply Favorite View the author
All Replies
青稚
deepin
2024-09-02 12:01
#1
It has been deleted!
青稚
deepin
2024-09-02 12:01
#2

个人搭建服务器,可以参考这个教程

Reply View the author
燕子大王来也!
deepin
2024-09-02 18:38
#3

agree

Reply View the author
xysemies
deepin
2024-09-10 16:31
#4

感谢分享.

谢谢

Reply View the author