Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

heroku set document_root

Tags:

php

heroku

If I have a directory structure like this:

/
/libraries/
/other-nonpublic-stuff/
/webroot/

How may I set /webroot/ as the document_root so that only it's content is served under the URL of my herokuapp?

I think that the Procfile might be the way to achieve that, but it's not really documented. Already tried things like

web: php webroot/

or

web: sh boot.sh webroot/

or

web: sh webroot/

in the Procfile, but I always only ended up with Heroku push rejected, no Cedar-supported app detected

When I push a repo with an "index.php" in the root directory it works fine, and a heroku ps just shows one web process running "boot.sh", but even then the Procfile is obviously ignored and heroku serves "/" instead of "/webroot". So not really a hint at how I might setup my Procfile:

Process  State      Command     
-------  ---------  ----------  
web.1    up for 2h  sh boot.sh  

I logged into the console of my "instance" via heroku run bash and did a cat boot.sh. It contains this:

sed -i 's/Listen 80/Listen '$PORT'/' /app/apache/conf/httpd.conf
for var in `env|cut -f1 -d=`; do
  echo "PassEnv $var" >> /app/apache/conf/httpd.conf;
done
touch /app/apache/logs/error_log
touch /app/apache/logs/access_log
tail -F /app/apache/logs/error_log &
tail -F /app/apache/logs/access_log &
export LD_LIBRARY_PATH=/app/php/ext
export PHP_INI_SCAN_DIR=/app/www
echo "Launching apache"
exec /app/apache/bin/httpd -DNO_DETACH

So the problem with the Heroku push rejected, no Cedar-supported app detected error (when no .php file is in "/" is obviously solvable by changing PHP_INI_SCAN_DIR to "/app/www/webroot", but thats a) not possible and b) wouldn't solve the issue of using "/webroot" as the docroot. In order to fix that, I also had to modify "/app/apache/conf/httpd.conf".

Any suggestions? :)

Thanks, Josh

like image 251
Josh Avatar asked Dec 16 '22 06:12

Josh


1 Answers

You'll need to use mod_rewrite to serve content out of "webroot" on Heroku. Try the following in your .htaccess file:

RewriteEngine On

RewriteRule ^\.htaccess$ - [F]

RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /webroot/index.php [NC,L]

RewriteCond %{REQUEST_URI} !^/webroot/.*$
RewriteRule ^(.*)$ /webroot/$1

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]

RewriteRule ^public/.*$ /webroot/index.php [NC,L]

To get around the "no Cedar-supported app detected" error, I just created a new index.php file in the root of my repository. It doesn't matter what this file contains, but I add a few reminders to document these setup details in my own apps.

Hope that helps!

like image 64
fixlr Avatar answered Jan 05 '23 01:01

fixlr