Ubuntu24.04安装Docker下的Ubuntu16.04+Apache2+PHP7+MySQL5.7环境

Title Ubuntu24.04安装Docker下的Ubuntu16.04+Apache2+PHP7+MySQL5.7环境
Framework Ubuntu
User wy8817399@vip.qq.com
Id 13
Created 1/12/26, 9:08 AM
Modified 1/22/26, 10:37 AM
Published Yes
Content

1、设置yml文件

docker-compose.yml

version: '3.8'

services:
  web:
    build: 
      context: .
      dockerfile: Dockerfile
    container_name: ubuntu-apache-php
    ports:
      - "8080:80"
    volumes:
      - /Projects/PHP/qmjt_docker:/var/www/html
      - /docker/qmjt/apache2/logs:/var/log/apache2
    restart: unless-stopped
    networks:
      - timeless-network
    depends_on:
      - mysql  # 确保 MySQL 服务先启动

  mysql:
    image: mysql:5.7  # 使用官方 MySQL 5.7 镜像
    container_name: mysql57
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: pass  # 设置 root 用户密码
      MYSQL_DATABASE: database        # 容器启动时创建的默认数据库
      MYSQL_USER: user1      # (可选)创建新用户
      MYSQL_PASSWORD: pass1  # (可选)新用户的密码
    ports:
      - "3307:3306"  # 将宿主机的 3307 端口映射到容器的 3306 端口,避免与宿主机上可能已运行的 MySQL 服务冲突
    volumes:
      - /docker/mysql57/mysql_data:/var/lib/mysql  # 持久化数据卷
      - /docker/mysql57/my.cnf:/etc/mysql/conf.d/my.cnf  # 自定义配置文件(可选)
    networks:
      - timeless-network
    command: 
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
      - --default-authentication-plugin=mysql_native_password  # 确保使用 PHP 7.2 兼容的认证插件

networks:
  timeless-network:
    name: timeless
    driver: bridge

volumes:
  mysql_data:  # 命名卷,确保数据库数据持久化

 

2、设置Dockerfile文件

Dockerfile

FROM ubuntu:16.04

# 设置非交互式前端,避免安装过程中提示
ENV DEBIAN_FRONTEND=noninteractive
ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8

# 安装Apache、PHP及常用扩展
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    software-properties-common \
    apache2 \
    && add-apt-repository ppa:ondrej/php -y \
    && apt-get update && \
    apt-get install -y \
    php \
    php-cli \
    php-common \
    php-intl \
    php-curl \
    php-gd \
    php-json \
    php-mbstring \
    php-xml \
    php-zip \
    php-mysql \
    libapache2-mod-php \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# 启用Apache的rewrite模块,并配置DocumentRoot
RUN a2enmod rewrite
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf

# 将Apache设置为前台运行,这是容器保持运行的关键
CMD ["apache2ctl", "-D", "FOREGROUND"]

# 设置工作目录
WORKDIR /var/www/html
RUN chmod -R 777 /var/www/html

 

3、对应的000-default.conf文件,主要是要在默认的文件里面加上<Directory /var/www/html>这段

000-default.conf

<VirtualHost *:80>
	# The ServerName directive sets the request scheme, hostname and port that
	# the server uses to identify itself. This is used when creating
	# redirection URLs. In the context of virtual hosts, the ServerName
	# specifies what hostname must appear in the request's Host: header to
	# match this virtual host. For the default virtual host (this file) this
	# value is not decisive as it is used as a last resort host regardless.
	# However, you must set it for any further virtual host explicitly.
	#ServerName www.example.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html

	<Directory /var/www/html>
        	Options FollowSymLinks
        	AllowOverride All
        	Order allow,deny
        	allow from all
        	Require all granted
	</Directory>

	# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
	# error, crit, alert, emerg.
	# It is also possible to configure the loglevel for particular
	# modules, e.g.
	#LogLevel info ssl:warn

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

	# For most configuration files from conf-available/, which are
	# enabled or disabled at a global level, it is possible to
	# include a line for only one particular virtual host. For example the
	# following line enables the CGI configuration for this host only
	# after it has been globally disabled with "a2disconf".
	#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

 

4、都设置好放在同一目录下后过后,用以下命令启动

docker compose up

 

5、该环境数据库的host要用mysql