Apache中php运行的两种模式

Apache 2 Handler (mod_php)

Server APIApache 2 Handler (mod_php)

  1. .htaccess 配置会生效
  2. 原因:在这种模式下,PHP 作为 Apache 的一个模块运行。Apache 服务器自己会直接读取并解析 .htaccess 文件中的指令(包括 php_value 和 php_flag),然后在自己进程内部应用这些配置到 PHP 模块上。
  3. 结论:这是 .htaccess 文件原本设计的工作方式,所以修改会直接反映在 phpinfo() 的 Local Value 中。

站点配置

vim /etc/apache2/sites-available/mghu.demo.com-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 mghu.demo.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/ayd/Magento2_IE/pub
        <Directory "/var/www/html/ayd/Magento2_IE/pub">
            # 启用重写引擎
            RewriteEngine On
            # 允许 .htaccess 文件覆盖配置
            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>

FPM/FastCGI

Server APIFPM/FastCGI

  • .htaccess 中的 PHP 配置通常不会生效
  • 原因
    • PHP 此时是一个独立的进程(PHP-FPM),Apache 只是作为一个“代理”,将 PHP 请求转发给它。两者通过 FastCGI 协议通信。
    • Apache 不负责解析和传递 .htaccess 中的 PHP 配置指令给后端的 PHP-FPM 进程。
    • 负责与 PHP-FPM 通信的 Apache 模块(如 mod_proxy_fcgi)会忽略 .htaccess 中的 php_value 等指令。
  • 结论:在这种架构下,.htaccess 文件中的 php_value memory_limit 756M 这行代码会被静默忽略,完全不起作用。

站点配置

vim /etc/apache2/sites-available/mghu.demo.com-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 mghu.demo.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/ayd/Magento2_IE/pub
        <Directory "/var/www/html/ayd/Magento2_IE/pub">
            # 启用重写引擎
            RewriteEngine On
            # 允许 .htaccess 文件覆盖配置
            AllowOverride All
            # 设置目录访问控制
            Require all granted


           <FilesMatch "\.php$">
                SetHandler "proxy:unix:/var/run/php/php8.4-fpm.sock|fcgi://127.0.0.1"
        </FilesMatch>



       </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>

创建站点配置软连接(注意需要进入到/etc/apache2/sites-enabled )

yang@DESKTOP-C6K4RMT:/etc/apache2/sites-enabled$  sudo ln -s ../sites-available/mg248.demo.com-defaut.conf

重启Apache

重启之前检查配置语法是否正确:

sudo apachectl configtest

如果输出 Syntax OK,就可以继续。

然后重启

sudo systemctl restart apache2

修改php内存的大小

sudo vim /etc/php/8.4/fpm/pool.d/www.conf

在文件末尾增加 :php_admin_value[memory_limit]=2G

重启 PHP-FPM 的正确方法

在重启之前,最好先检查配置文件语法是否正确,避免因配置错误导致服务无法启动:

bash

sudo php-fpm8.4 -t

请将 8.1 替换为您实际使用的 PHP 版本(如 7.4、8.0、8.2 等)。如果显示 "Syntax OK",则表示配置正确。
sudo php-fpm8.4 -t
[17-Sep-2025 09:01:48] NOTICE: configuration file /etc/php/8.4/fpm/php-fpm.conf test is successful

重启 PHP-FPM 服务

根据您的操作系统和 PHP 版本,使用适当的命令:

对于 Ubuntu/Debian 系统:

# 确认您的PHP版本
php -v

# 根据版本重启相应的PHP-FPM服务
sudo systemctl restart php8.1-fpm    # PHP 8.1
sudo systemctl restart php8.0-fpm    # PHP 8.0
sudo systemctl restart php7.4-fpm    # PHP 7.4
yang@DESKTOP-C6K4RMT:/etc/php/8.4/fpm/pool.d$ sudo systemctl restart php8.4-fpm
yang@DESKTOP-C6K4RMT:/etc/php/8.4/fpm/pool.d$ 

检查服务状态

yang@DESKTOP-C6K4RMT:/etc/php/8.4/fpm/pool.d$ sudo systemctl status php8.4-fpm
● php8.4-fpm.service - The PHP 8.4 FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php8.4-fpm.service; enabled; preset: enabled)
     Active: active (running) since Wed 2025-09-17 09:02:09 CST; 15min ago
       Docs: man:php-fpm8.4(8)
    Process: 99723 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.4/fpm/pool.d/www.conf 84 (code=exited,>
   Main PID: 99720 (php-fpm8.4)
     Status: "Processes active: 0, idle: 3, Requests: 139, slow: 0, Traffic: 0.00req/sec"
      Tasks: 4 (limit: 18990)
     Memory: 221.6M (peak: 276.9M)
        CPU: 6.681s
     CGroup: /system.slice/php8.4-fpm.service
             ├─ 99720 "php-fpm: master process (/etc/php/8.4/fpm/php-fpm.conf)"
             ├─ 99721 "php-fpm: pool www"
             ├─ 99722 "php-fpm: pool www"
             └─100601 "php-fpm: pool www"

Sep 17 09:02:09 DESKTOP-C6K4RMT systemd[1]: Starting php8.4-fpm.service - The PHP 8.4 FastCGI Process Manager...
Sep 17 09:02:09 DESKTOP-C6K4RMT systemd[1]: Started php8.4-fpm.service - The PHP 8.4 FastCGI Process Manager.

执行逻辑图

总结

通过phpinfo 和echo “当前内存限制: ” . ini_get(‘memory_limit’); 可以获取当前php的内存大小

.user.ini

在本地站点代码里面修改php的内存

检查本地是否存在文件.user.ini

memory_limit = 2048M
max_execution_time = 18000
session.auto_start = off
suhosin.session.cryptua = off

修改之后立即生效。

发表评论