Ravindra BagaleCourses & study guides

Chapter 8: Configuring Nginx and Apache

8.5 Task: change the Apache DocumentRoot

Option A: edit the virtual host (recommended). Change DocumentRoot and the matching <Directory> path together:

# Ubuntu: default site
sudo sed -i 's#/var/www/html#/var/www/portfolio#' /etc/apache2/sites-available/000-default.conf
sudo tee /etc/apache2/conf-available/portfolio-dir.conf > /dev/null <<'__EOCONF__'
<Directory /var/www/portfolio>
    Options -Indexes +FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
__EOCONF__
sudo a2enconf portfolio-dir
sudo apache2ctl configtest && sudo service apache2 reload
# Amazon Linux 2023 / CentOS Stream 9: own virtual host
sudo tee /etc/httpd/conf.d/portfolio.conf > /dev/null <<'__EOCONF__'
<VirtualHost *:80>
    ServerName portfolio.local
    DocumentRoot /var/www/portfolio
    <Directory /var/www/portfolio>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
__EOCONF__
sudo apachectl configtest && sudo service httpd reload

Option B: change the global default in /etc/httpd/conf/httpd.conf, which has both DocumentRoot "/var/www/html" and <Directory "/var/www/html">. Change both, then test and reload. It works, but Option A is cleaner.

Ubuntu security default

/etc/apache2/apache2.conf denies access to / and allows only /var/www and /usr/share. A DocumentRoot outside these (e.g. /srv/site) needs its own <Directory> block with Require all granted. Otherwise you get 403 Forbidden even with perfect file permissions.

Ravindra Bagale's Tip

When a student shows me a 403, I ask three questions in this order: Does the file exist and have 644/755 along the whole path? (namei -l), Does a <Directory> / root point to exactly this path?, and on CentOS, Is the SELinux label right? (ls -Z). Almost every 403 is answered by one of these three. Samjla ka?