Docker搭建Apache2.4 PHP5.6环境部署应用网站

1.前言

项目组遗留的PHP项目,应用运行时环境是PHP5.6,更高版本不适用。近期在新服务器重新部署该项目,中间遇到了不少坑,特此记录,以飨读者。

2.创建容器

docker 容器:Ubuntu 20.04、apache2.4.59、PHP5.6

操作系统选择ubuntu,直接从dockerhub拉取集成apache的镜像,安装起来更省事。

如果采用基于debian或alpine的其他镜像,如php:5.6-apache,会踩不少坑,折腾了三天,放弃。

拉取镜像并创建容器。

宿主机nginx采用8090端口转发请求到容器apache 80端口。

shell 复制代码
docker pull ubuntu/apache2:2.4-20.04_beta
# 创建容器 映射目录 
docker run -d --restart=always -v /var/www/html/:/var/www/html/ --name website -p 8090:80 ubuntu/apache2:2.4-20.04_beta

dockerhub的ubuntu/apache2:2.4-20.04_beta镜像,apache版本是2.4.41,存在安全漏洞,部署应用后需要单独升级到Apache最新版本。

也可使用docker-compose.yml文件创建容器

yaml 复制代码
version: '3.8'
services:
  website:
    container_name: website
    image: ubuntu/apache2:2.4-20.04_beta
    volumes:
      - /etc/timezone:/etc/timezone
      - /etc/localtime:/etc/localtime
      - /var/www/html:/var/www/html
    ports:
      - "8090:80"
    working_dir: /var/www/html
    restart: always
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 1024M
        reservations:
          memory: 256M
复制代码
docker-compose up -d website
3.配置容器
shell 复制代码
apt update
apt install -y software-properties-common
add-apt-repository ppa:ondrej/php
# 这一步时可能会遇到 http://ppa.launchpad.net访问不到或者访问慢的情况,多试几次
apt install -y  php5.6 php5.6-cli php5.6-mysql php5.6-curl php5.6-json php5.6-cgi libapache2-mod-php5.6 php5.6-mbstring php5.6-mcrypt php5.6-gd php5.6-soap php5.6-redis php5.6-xml php5.6-apcu

apt install -y php5.6-fpm

# 安装vim
apt install vim
4.升级Apache
shell 复制代码
add-apt-repository ppa:ondrej/apache2 -y
apt update
apt upgrade apache2 -y
# 查看apache2 最新版本
apache2 -v
# Server version: Apache/2.4.59 (Ubuntu)
5.配置PHP

应用网站开启伪静态,需要确保应用网站根目录下有.htaccess文件存在

shell 复制代码
#启用 php5.6
a2enmod php5.6
#开启伪静态 能够访问html页面 还要在sites-enabled 的配置文件中配置AllowOverride All
a2enmod rewrite

配置Apache 2.4与PHP 5.6的关联

默认情况下,Apache 2.4会将.php文件关联到application/x-httpd-php MIME类型。但为了确保与PHP 5.6的兼容性,可以在Apache的/etc/apache2/apache2.conf 末尾添加以下行:

shell 复制代码
vi /etc/apache2/apache2.conf
# 末尾增加一行
AddType application/x-httpd-php .php
6.配置应用网站
shell 复制代码
vi /etc/apache2/sites-enabled/000-default.conf
xml 复制代码
<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
        DirectoryIndex index.php index.html index.htm
        <Directory "/var/www/html">
             Options Indexes FollowSymLinks
             AllowOverride 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>
7.重启容器
8.提高访问安全性

为了提高应用网站的安全性,需要严格设置目录权限

www-data是Debian及其衍生系统(如Ubuntu)中预定义的一个系统用户和用户组,专门用于运行Web服务器服务,例如Apache和Nginx。这个用户账户的设计目的是为了提高系统的安全性,通过限制Web服务器的权限来减少潜在的安全风险。默认情况下,www-data用户没有登录shell的权限,这意味着它不能用于交互式登录,只能用于运行Web服务。

在Web服务器的配置中,通常会指定www-data作为服务运行的用户,这样即使Web服务器遭受攻击,攻击者也很难获得更高权限的访问权限,因为www-data用户的权限被严格限制在Web服务相关的文件和操作上。此外,通过将Web服务文件的所有权和管理权限分配给www-data用户和组,可以确保Web服务器能够正确地读取、写入和管理网站文件,同时避免不必要的权限过度开放。

总的来说,www-data用户是为了Web服务的安全运行而特别创建的,它有助于在多用户系统中实施最小权限原则,从而提高整个系统的安全性。

shell 复制代码
# 递归设置网站目录的所有者
chown -R www-data:www-data /var/www/html/
# 普遍分配750权限即可
chmod -R 750 /var/www/html/
相关推荐
m0_495562781 天前
Swift的逃逸闭包
服务器·php·swift
Elastic 中国社区官方博客1 天前
Observability:适用于 PHP 的 OpenTelemetry:EDOT PHP 加入 OpenTelemetry 项目
大数据·开发语言·人工智能·elasticsearch·搜索引擎·全文检索·php
catchadmin2 天前
用 LaraDumps 高效调试 PHP 和 Laravel
php·laravel
刘恒1234567892 天前
PHP中对于(并发/并行)相关概念
php
思茂信息2 天前
CST License(Flexnet)设置与问题处理方法
服务器·网络·单片机·3d·php·1024程序员节·cst
m0_738120722 天前
内网横向靶场——记录一次横向渗透(三)
开发语言·网络·安全·web安全·网络安全·php
无心水2 天前
【中间件:Redis】2、单线程Redis高并发原理:I/O多路复用+3大优化点(附多线程对比)
redis·中间件·php·后端面试·i/o多路复用·redis原理·redis高并发
万岳软件开发小城2 天前
在线教育系统源码架构设计指南:高并发场景下的性能优化与数据安全
php·在线教育系统源码·教育平台搭建·教育app开发·教育软件开发
wxin_VXbishe2 天前
springboot在线课堂教学辅助系统-计算机毕业设计源码07741
java·c++·spring boot·python·spring·django·php
苏琢玉3 天前
被问性能后,我封装了这个 PHP 错误上报工具
php·composer