Setting my project public folder as document root

December 3, 2018 772 views
PHP MySQL Apache Ubuntu 18.04

I am trying to set the public folder in my project as the document root.

I have explained everything in detail at this link with screenshots.

https://www.evernote.com/l/AlnOgLBaJ7lBIYAequqvVe_VRMF7KH5Ue_U/

When I set the root to one folder above public the index files picks up the css file. Otherwise it does not... Not sure what the problem is. Any guidance is appreciated. Thanks.

5 Answers

The CSS and assets in general are looked up by the web server relative to the document root. So when you set the document root to globe_bank/public then this:

<img src="/public/images/gbi_logo.png" />

Gets translated to:

/full/doc/path/globe_bank/public/public/images/gbi_logo.png

You'll need to change the path in your HTML relative to the doc root.

Hi Unixynet,

Thank you for your response.

I am using php to set the URLs relative to the root.

This is the code for setting the stylesheet url:

<link rel="stylesheet" media="all" href="<?php echo url_for('/stylesheets/public.css'); ?>" />

And, here is the url_for function:

// Creates a url by appending the root to the script path sent in
// The root ends at public the script should be everything after that
  function url_for($script_path)
  {
    // add the leading '/' if not present
    if ($script_path[0] != '/') {
      $script_path = "/" . $script_path;
    }
    return WWW_ROOT . $script_path;
  }

And here are the definitions for the WWW_ROOT

// Assign the root URL to a PHP constant
  // * Do not need to include the domain
  // * Use same document root as webserver
  // * Can set a hardcoded value:
  // define("WWW_ROOT", '/~kevinskoglund/globe_bank/public');
//   define("WWW_ROOT", '/public');
  // define("WWW_ROOT", '');
  // * Can dynamically find everything in URL up to "/public"
  $public_end = strpos($_SERVER['SCRIPT_NAME'], '/public') + 7;
  $doc_root = substr($_SERVER['SCRIPT_NAME'], 0, $public_end);
  define("WWW_ROOT", $doc_root);

So this function should find everything till /public and then the $script path argument is appended to it
/stylesheets/public.css

Please see some more things I have tried to get the document root right so that the relative CSS link works...

https://www.evernote.com/l/Alnr0bIh0rVNvas_xmBD59R6VskfjaXLn4M/

Basically, the relative link works if I use something like

/var/www/phpmysqlcms.tk/html/globe_bank

as document root (and then generate the relative link)

But, it does not work when that root is changed to

/var/www/phpmysqlcms.tk/html/globe_bank/public

And I was thinking that having a separate public folder for a site would be for the purpose of setting it as the root so that people cannot type a URL to reach the private folder.

Any further guidance is appreciated.

Thanks.

Okay I found the issue with what I was doing as I was working on a response to your suggestion. In one way or another, you helped me solve this :)

Thank you.

P.S. I have posted at the following link what I was doing wrong:

https://www.evernote.com/l/Alnr0bIh0rVNvas_xmBD59R6VskfjaXLn4M/

If you use /var/www/phpmysqlcms.tk/html/globe_bank/public as your document root, then you don't need any hack or function to build your links. This should work OK:

<img src="/images/gbi_logo.png" />

There's no need to pull that info. Even $_SERVER['DOCUMENT_ROOT'] will give you the apache docroot but you don't need that.

Perhaps I'm not fully understanding what you're setting out to do.

Thank you. I am all set for now. Thanks for your help!

Have another answer? Share your knowledge.