Solved Apache2 - Document root and directory structures

Solved issue

Bagheera

Member
Joined
Sep 2, 2024
Messages
91
Reaction score
22
Credits
870
If my memory serves me correctly, many years ago I created and published a website
on an Apache web server, whereby the public_html directory was at the same level as
the supporting directories for CSS, images, data and Javascript. Thus:-

cgi-bin
css
data
images
javascript
public_html

But as I understand it, on Apache2 if I quote example/public_html as the document root
then references to CSS etc directory entries would be outside the doc root and wouldn't
be available.

If I just have example as the document root, then the call to www.example.com isn't going to
find public_html/index.html

Is there a way to achieve what I had before or is it considered bad practice now not to have
the support directories underneath public_html?
 
Last edited:


Normally everything goes under /var/www/html (this can vary from distro to distro)
If you are supporting multiple sites this may not be true.
If you are the web admin, this is where you put everything, css included.

If you are NOT the system web admin, the /home/user/public_html directories
are a way to share web content. These are treated, kind of like private sites. Each user has their own site.
But again, all of YOUR content can run under these (as you wouldn't have permission to the system web directories)
that includes CSS and everything else.

Your /etc/httpd/httpd.conf should have all this already configured.
All of the directories you listed above are optional. You can put everything in one directory if you want to.
You wouldn't normally have a public_html in the web root.
 
Last edited:
Thanks dos2unix. I am developing multiple sites and would be the web admin on my own computer's server and would have admin privileges on the eventual web-server (i.e Hostinger) once it's published ... but only for the website, not the server itself of course.

My own apache2 (ver 2.4) does not have an /etc/httpd directory
 
Last edited:
Mint does things a little different.
It's likely /etc/apache2
 
On my Mint at least, httpd.conf is in /usr/share/doc/apache2/examples/apache2/original
A bit more convoluted.

So, if I have /var/www/example as my document root, and I place all other directories underneath that,
will a modification somewhere (goodness only knows where & what) within httpd.conf, direct a browser request from
www.example.com to /var/www/example/public_html/index.html ?
 
Last edited:
This was too much to type. I had Ai summarize it for me.

To direct a browser request from www.example.com to /var/www/example/public_html/index.html, you'll need to modify the httpd.conf file for Apache. Here are the steps to achieve this:

  1. Locate the httpd.conf file:Since you mentioned that the httpd.conf file is located at /usr/share/doc/apache2/examples/apache2/original, you might want to copy it to a more standard location, such as /etc/apache2/, for easier management.
  2. Edit the httpd.conf file:Open the httpd.conf file with a text editor (e.g., nano, vim).
  3. Set the DocumentRoot:Find the DocumentRoot directive and set it to /var/www/example/public_html.
Code:
DocumentRoot "/var/www/example/public_html"
  1. Configure the Directory:Add a <Directory> block to specify permissions and settings for the document root directory
Code:
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
  1. Set up the Virtual Host:If you are using virtual hosts, you need to configure a virtual host for www.example.com.
Code:
    ServerName www.example.com
    DocumentRoot "/var/www/example/public_html"
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
  1. Restart Apache:After making these changes, restart the Apache service to apply the new configuration.
Code:
sudo systemctl restart apache2

Summary​

By setting the DocumentRoot to /var/www/example/public_html and configuring the appropriate <Directory> and <VirtualHost> blocks, you can direct requests from www.example.com to /var/www/example/public_html/index.html.
 
Thank you again for your suggestions dos2unix. If the document root though includes public_html, then the html pages in there referencing support directories which are located at the same level as public_html (so within html pages, referenced prefixed with ../), would be outside the document root and therefore unavailable to the html page. So no stylesheets or images etc.
 
(so within html pages, referenced prefixed with ../), would be outside the document root and therefore unavailable to the html page. So no stylesheets or images etc.

That is correct, typically you cannot go "above" the documentroot.
 
the document root though includes public_html,

public_html is a special directory name. You might be able to remove it from your httpd.conf files, but it might also
partly be coded in the binary (it was about 15 years ago). It could cause some permissions issues.
 
Would a DirectoryIndex definition within the site's virtual host config help?

i.e
DocumentRoot /var/www/example
DirectoryIndex public_html/index.html

I was reading through this StackExchange thread
 
DocumentRoot /var/www/example

This is typically how it's done.

/var/www/cgi-bin
/var/www/html/site1
/var/www/html/site2
/var/www/html/site3
/home/bagjeera/public_html
/home/joesmith/public_html

Then beneath each site directory
you could have a flat file structure like
index.html mydata.php mystyle.css picture1.jpg picture2.jpg
Or if you have many files of each type, you can have subdirectories
/var/www/html/site1/index.html
/var/www/html/site1/another.html
/var/www/html/site1/pix/picture1.jpg
/var/www/html/site1/pix/picture2.jpg
/var/www/html/site1/css/style1.css
/var/www/html/site1/css/style2.css
 
Thanks again dos2unix. I'll need time to digest this and try out.
 
Actually dos2unix I've discovered that a simple DirectoryIndex public_html/index.html entry in example.conf
virtual host section does the trick perfectly. I keep the document root at /var/www/example/, keep the public_html
directory underneath it and at the same level as cgi-bin, data, css, graphics, javascript directories underneath example/

/var/www/example (document root)
/var/www/example/cgi-bin
/var/www/example/css
/var/www/example/data
/var/www/example/graphics
/var/www/example/javascript
/var/www/example/public_html (directory index)

The browser call to www.example.net displays index.html within public_html whilst at the same time all the
other the html pages within public_html (incl index.html) containing ../ prefixes to the support directories
are all still within the document root.

So for one site at least, problem solved :)
 
Last edited:
I spoke too soon and should have known apache would find a new way to screw it up!
I wouldn't mind but it's not as though I'm trying to achieve anything novel here.

So, the DirectoryIndex definition allows call to example.net to display public_html/index.html
and the javascript, images and stylesheets are included successfully from their own directories.

But ... clicking to another page link within public_html/index.html such as <a href="news.html">, for some
weird reason apache2 looks in the directory above public_html for that page, namely
www.example.net/news.html rather than www.example.net/public_html/news.html ... doh!
 
Last edited:


Follow Linux.org

Members online


Top