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/
相关推荐
ServBay2 天前
告别面条代码,PSL 5.0 重构 PHP 性能与安全天花板
后端·php
JaguarJack4 天前
FrankenPHP 原生支持 Windows 了
后端·php·服务端
BingoGo4 天前
FrankenPHP 原生支持 Windows 了
后端·php
JaguarJack5 天前
PHP 的异步编程 该怎么选择
后端·php·服务端
BingoGo5 天前
PHP 的异步编程 该怎么选择
后端·php
JaguarJack6 天前
为什么 PHP 闭包要加 static?
后端·php·服务端
ServBay7 天前
垃圾堆里编码?真的不要怪 PHP 不行
后端·php
用户962377954487 天前
CTF 伪协议
php
BingoGo9 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack9 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端