userdir中的PHP无法正常工作

时间:2016-03-10 22:00:31

标签: php apache ubuntu

我的 /etc/apache2/mods-enabled/userdir.conf 文件如下所示:

<IfModule mod_userdir.c>
        UserDir /var/zpanel/hostdata/*/public_html/
        UserDir disabled root

        <Directory /var/zpanel/hostdata/*/public_html/*>
                AllowOverride All
                Options MultiViews Indexes SymLinksIfOwnerMatch
                <Limit GET POST OPTIONS>
                        # Apache <= 2.2:
                        Order allow,deny
                        Allow from all

                        # Apache >= 2.4:
                        # Require all granted
                </Limit>
                <LimitExcept GET POST OPTIONS>
                        # Apache <= 2.2:
                        Order deny,allow
                        Deny from all

                        # Apache >= 2.4:
                        #Require all denied
                </LimitExcept>
        </Directory>
</IfModule>

并提交 /etc/apache2/mods-enabled/php5.conf

<FilesMatch ".+\.ph(p[345]?|t|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch ".+\.phps$">
    SetHandler application/x-httpd-php-source
    # Deny access to raw php sources by default
    # To re-enable it's recommended to enable access to the files
    # only in specific virtual host or directory
   Require all denied
</FilesMatch>
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(p[345]?|t|tml|ps)$">
    Require all denied
</FilesMatch>

# Running PHP scripts in user directories is disabled by default
#
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.


#<IfModule mod_userdir.c>
#    <Directory /var/zpanel/hostdata/*/public_html/*>
#        php_admin_value engine On
#    </Directory>
#</IfModule>

但是在通过userdir访问网站时,php无法正常工作。 的 www.example.com/~admin 当我通过域访问然后它工作。我安装了zPanel

服务器版本:Apache / 2.4.18(Ubuntu)

3 个答案:

答案 0 :(得分:1)

请尝试以下操作:

  • 确保mod_userdir已启用a2enmod userdir
  • $HOMEpublic_html目录具有正确的读取权限。

    sudo -Hu SOMEUSER sh -c 'chmod 755 $HOME $HOME/public_html'
    
  • 确保php_admin_value engineOn

    sudo vim /etc/apache2/mods-enabled/php*.conf
    

    然后找到包含php_admin_flag的行并将其更改为:

    php_admin_flag engine On
    

    这是因为Apache 2的用户目录默认禁用PHP。

在您的特定情况下,您必须取消注释以下行:

<IfModule mod_userdir.c>
    <Directory /var/zpanel/hostdata/*/public_html/*>
        php_admin_value engine On
    </Directory>
</IfModule>

答案 1 :(得分:1)

在我/etc/apache2/mods-available/php7.0.conf我有本节

# Running PHP scripts in user directories is disabled by default   
#                                                                  
# To re-enable PHP in user directories comment the following lines 
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it  
# prevents .htaccess files from disabling it.                      
<IfModule mod_userdir.c>                                           
    <Directory /home/*/public_html>                                
        php_admin_flag engine Off                                  
    </Directory>                                                   
</IfModule>                                                        

我想现在你知道该怎么做了。

答案 2 :(得分:0)

如@kenorb所述,这是因为默认情况下,Apache2的userdir中php_admin_value engine Off禁用了PHP。

但是,php_admin_value engine文件本身建议不要将On的行从/etc/apache2/mods-enabled/php*.conf更改为{{ {1}}被注释掉,以便.conf可以单独处理设置。可以如下所示:

<IfModule>