Chapter 8: Configuring Nginx and Apache
8.5 Task: change the Apache DocumentRoot
Friends, when you change the root in Apache you must change it in two places – DocumentRoot and its <Directory>. Change only one and a 403 is certain. Come on, let's look at both options.
मित्रांनो, Apache मध्ये रूट बदलला की दोन जागी बदल करायचा – DocumentRoot आणि त्याचा <Directory>. एकच बदलला तर 403 नक्की. चला, दोन्ही options बघू.
दोस्तों, Apache में रूट बदला तो दो जगह बदलाव करना है – DocumentRoot और उसका <Directory>. एक ही बदला तो 403 पक्का. चलो, दोनों options देखते हैं.
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 is the whole path 644/755? (namei -l), Does <Directory> / root point to exactly this path?, and on CentOS, Is the SELinux label correct? (ls -Z). Almost every 403 is answered by one of these three. Got it?
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 का जवाब इन तीनों में से एक में मिलता है. समझ आया?
Lab
Chala, Option A ne Apache cha DocumentRoot /var/www/portfolio kara aani test karun reload kara. Mag muddam <Directory> block kadhun 403 bagha, sudo tail -n 5 ne error log vacha, aani block parat taka.