We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

404 public not found inside userdir on CentOS 7

I've used Phalcon for a while under Debian/Ubuntu and it has always worked great. Now I'm trying to get it running under CentOS 7 and I can't get it to work inside userdirs (public_html), even though mod_rewrite seems to work correctly.

Versions:

  • CentOS 7 with SELinux disabled
  • Apache 2.4
  • PHP 7.3
  • Phalcon 3.4

Starting with a fresh clean CentOS 7 installation in VirtualBox, this is what I do. First update and install httpd:

# yum check-update
# yum update
# setenforce 0 // Disable SELinux, so that we are sure this doesn't cause my problem.
# yum install httpd
# firewall-cmd --add-service=http
# yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
# yum install yum-utils
# yum-config-manager --enable remi-php73
# yum install php
# vi /etc/httpd/conf.d/userdir.conf // Enable userdirs, see contents below
# systemctl restart httpd

The userdir.conf now contains (without all the comment lines):

<IfModule mod_userdir.c>
    UserDir public_html
</IfModule>

<Directory "/home/*/public_html">
    AllowOverride FileInfo AuthConfig Limit Indexes
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    Require method GET POST OPTIONS
</Directory>

Now put something in my userdir and check if it works:

$ mkdir ~/public_html
$ echo Hello > ~/public_html/index.html
$ sudo chmod go+rx ~

This works, I can see 'Hello' when I visit https://192.168.178.28/~gerben/

Now I add Phalcon:

$ curl -s https://packagecloud.io/install/repositories/phalcon/stable/script.rpm.sh | sudo bash
# yum install pcre-devel php-gd php-mbstring php-phalcon3
# systemctl restart httpd

Also add the webtools:

# yum install git
# git clone https://github.com/phalcon/phalcon-devtools.git /usr/local/lib/phalcon-devtools
# ln -s /usr/local/lib/phalcon-devtools/phalcon.php /usr/local/bin/phalcon
# ln -s /usr/local/lib/phalcon-devtools/phalcon.php /usr/local/sbin/phalcon
# chmod +x /usr/local/bin/phalcon
# chmod +x /usr/local/sbin/phalcon

Now create a project in /var/www/html:

# cd /var/www/html
# phalcon project foo

And I add 'AllowOverride FileInfo' to the Directory config of /var/www/html in /etc/httpd/conf/httpd.conf to enable the rewrite module. This works, I see the Congratulations!-page when I visit https://192.168.178.28/foo

Now do the same in my userdir:

$ cd ~/public_html
$ phalcon project foo

This doesn't work. When I visit https://192.168.178.28/~gerben/foo, I see:

The requested URL /home/gerben/public_html/foo/public/ was not found on this server.

Note that the error references foo/public! The rewrite module works; when I open .htaccess and replace 'public' with 'public1234', the error changes accordingly:

The requested URL /home/gerben/public_html/foo/public1234/ was not found on this server.

It's the default htaccess that phalcon-webtools installs:

<IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule  ^$ public/    [L]
        RewriteRule  (.*) public/$1 [L]
</IfModule>

The apache access_log shows:

192.168.178.17 - - [16/Jun/2019:21:57:51 -0400] "GET /~gerben/foo/ HTTP/1.1" 404 233 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36"

Disabling MultiViews (and restarting httpd) didn't help. Setting AllowOverride and Options to 'All' doesn't help. I don't get it, why does it work in /var/www/html but not in public_html? Why can Apache not find the public dir when mod_rewrite forwards the request? The directory permissions are sufficient; when I visit https://192.168.178.28/~gerben/foo/public I do see the Congratulations-page! But it should also work without 'public'.



1.7k
Accepted
answer
edited Jun '19

I found the solution. I need to add a RewriteBase directive. This was not necessary on Ubuntu server.

CentOS 7 ships with Apache 2.4.6. The Apache documentation says about RewriteBase:

In Apache HTTP Server 2.4.16 and later, this directive may be omitted when the request is mapped via Alias or mod_userdir.

So because CentOS ships with an Apache version older than 2.4.16 it is necessary to include a RewriteBase directive when using mod_userdir:

<IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteBase /~username/foo/
        RewriteRule  ^$   public/   [L]
        RewriteRule  (.*) public/$1 [L]
</IfModule>