【问题标题】:Upload files with Apache & PHP-FPM使用 Apache 和 PHP-FPM 上传文件
【发布时间】:2017-11-04 08:45:07
【问题描述】:

我已经安装了一个新的 Ubuntu 16.04 服务器并使用 this 指南启用了 PHP-FPM。我还启用了userdir 模块(这样我就可以在/home/$user 下的public_html 文件夹中运行虚拟站点)和mod_ruid2

根据第一个指南必须做的事情之一是将这三行添加到 000-default.conf 中:

<FilesMatch "\.php$">
  SetHandler "proxy:fcgi://127.0.0.1:9000/"
</FilesMatch>

添加树线后,我的 conf 文件如下所示:

<VirtualHost *:80>
        ServerName jrrtest
        RMode stat
        ServerAdmin webmaster@localhost
        DocumentRoot /home/jrr/public_html
        ErrorLog ${APACHE_LOG_DIR}/jrrtest-error.log
        CustomLog ${APACHE_LOG_DIR}/jrrtest-access.log combined
        <FilesMatch "\.php$">
                SetHandler "proxy:fcgi://127.0.0.1:9000/"
        </FilesMatch>
</VirtualHost>

PHP 现在按预期执行,但是当我尝试使用 this 页面上的示例上传文件时,它无法正常工作。如果我尝试上传文件,我会在 Apache error_log 中收到此错误:

PHP Warning:  move_uploaded_file(uploads/jorara.png): failed to open stream: Permission denied in /home/jrr/public_html/upload.php on line 38\nPHP message: PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpAJsos1' to 'uploads/jorara.png' in /home/jrr/public_html/upload.php on line 38

如果我从 000-default.conf 中删除 FilesMatch 行,则上传功能再次起作用,但 php 不再按照我的意愿使用 FastCGI 执行。

如果我重新启用 FilesMatch 行并将上传文件夹的所有者更改为 www-data:www-data,我也可以使用上传功能。

如何配置 PHP-FPM 以便 php 脚本的所有者可以将文件上传到与脚本所有者相同所有者的文件夹中?

问候, 约根

【问题讨论】:

  • 什么FilesMatch linesit is not working 是什么意思 - 错误消息、日志消息是什么?
  • 我所指的 FilesMatch 行可以在我链接到 (server-world.info/en/note?os=Ubuntu_16.04&p=httpd&f=16) 和 not working 的第一个指南中看到我的意思是当我尝试上传一个错误时文件。 Apache error_log 中的错误是PHP Warning: move_uploaded_file(uploads/jorara.png): failed to open stream: Per mission denied in /home/jrr/public_html/upload.php
  • 编辑您的问题并添加这些详细信息。您提供的所有细节越细心,您将获得更好的响应。没有人会去阅读教程以了解您在做什么。

标签: php apache file-upload


【解决方案1】:

经过几个小时的搜索,我终于找到了一个似乎可行的解决方案。

我最终做的是这个。

用 LAMP 安装了一个标准的 Ubuntu。

安装了这些额外的包:

apt-get install libapache2-mod-fastcgi php7.0-fpm

启用这些 Apache 模块:

a2enmod actions fastcgi

创建了几个目录:

/var/www/testsite/htdocs
/var/www/testsite/phpsessions
/var/www/testsite/logs

在文件/etc/php/7.0/fpm/pool.d/testsite.conf中创建了一个新的php-fpm池:

[testsite]
    user = testsite
    group = testsite
    listen = /run/php/php7.0-fpm.testsite.sock
    listen.owner = www-data
    listen.group = www-data
    listen.mode = 0660

    pm = dynamic
    pm.max_children = 5
    pm.start_servers = 2
    pm.min_spare_servers = 1
    pm.max_spare_servers = 3

    php_admin_value[session.save_path] = /var/www/testsite/phpsessions
    php_admin_value[session.save_handler] = files
    php_admin_value[display_errors] = Off
    php_admin_value[log_errors] = On
    php_admin_value[error_log] = "/var/www/testsite/logs/php_error.log"
    php_admin_value[open_basedir] = "/var/www/testsite/htdocs:/usr/share/php:/tmp:/var/www/testsite/phpsessions"

在我第一次尝试使用listen.ownerlisten.group 时,我将它们设置为testsite。如果我没有将listen.mode 设置为0666,这会给我访问套接字文件/run/php/php7.0-fpm.testsite.sock 带来问题。在我(和许多其他人)看来,这是一个安全风险。相反,我最终将listen.ownerlisten.grouplisten.mode 设置为上面显示的值,这似乎是推荐的解决方案。

在文件 /etc/apache2/sites-available/testsite.conf 中创建了一个包含以下内容的站点定义:

<IfModule mod_fastcgi.c>
    AddHandler php7-fcgi-testsite .php
    Action php7-fcgi-testsite /php7-fcgi-testsite
    Alias /php7-fcgi-testsite /usr/lib/cgi-bin/php7-fcgi-testsite
    FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi-testsite -socket /run/php/php7.0-fpm.testsite.sock -pass-header Authorization

    <Directory "/usr/lib/cgi-bin">
        Require all granted
    </Directory>
</IfModule>

<VirtualHost *:80>
    ServerName testsite

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/testsite/htdocs

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

    <Directory /var/www/testsite/htdocs>
        Options -Indexes
    </Directory>

    <IfModule mod_fastcgi.c>
        <FilesMatch ".+\.ph(p[345]?|t|tml)$">
            SetHandler php7-fcgi-testsite
        </FilesMatch>
    </IfModule>
</VirtualHost>

启用新网站:

a2ensite testsite

在 Apache 中禁用了默认的 php 模块:

a2dismod php7.0

重启 Apache 和 PHP-FPM:

systemctl restart php7.0-fpm apache2

php-fpm 的状态(包括新池)可以用这个命令查看:

systemctl status php7.0-fpm

使用以下内容在 /var/www/testsite/htdocs 中创建了文件 info.php:

<?php
    phpinfo();
?>

在浏览器中访问 testsite/info.php 现在显示 php 正在作为我想要的 FastCGI 运行,并且文件可以毫无问题地上传。

【讨论】:

    猜你喜欢
    • 2017-07-18
    • 2021-04-02
    • 2014-08-17
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    相关资源
    最近更新 更多