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. Clear?
Ravindra Bagale's Tip – मराठी
एखादा student मला 403 दाखवतो तेव्हा मी या क्रमाने तीन प्रश्न विचारतो: File अस्तित्वात आहे का आणि पूर्ण path वर 644/755 आहे का? (namei -l), <Directory> / root नेमका याच path कडे point करतो का?, आणि CentOS वर, SELinux label बरोबर आहे का? (ls -Z). जवळजवळ प्रत्येक 403 चं उत्तर या तिघांपैकी एकात मिळतं. समजलं का?
Ravindra Bagale's Tip – हिंदी
जब कोई student मुझे 403 दिखाता है, तो मैं इस क्रम में तीन सवाल पूछता हूँ: क्या file मौजूद है और पूरे path पर 644/755 है? (namei -l), क्या <Directory> / root ठीक इसी path की तरफ़ point करता है?, और CentOS पर, क्या SELinux label सही है? (ls -Z). लगभग हर 403 का जवाब इन तीन में से किसी एक में मिल जाता है. समझ आया?