RBCloud & DevOpsTHE PRACTICAL LEARNING LIBRARY
By Ravindra BagaleResources

CHAPTER 16 / 60

Change document root, port and index

Make the same configuration change in Apache and Nginx and verify it before exposing it.

Concept + practical labBy Ravindra Bagale · ~5 min read · lab time additional

Lab objective

Serve home.html from /var/www/course-site on TCP 8080. Use a dedicated lab configuration and retain an SSH session. Create the root and page first:

bash
sudo mkdir -p /var/www/course-site
printf '<h1>Course site on 8080</h1>\n' | sudo tee /var/www/course-site/home.html

Apache configuration

On AL2023 save as /etc/httpd/conf.d/course-site.conf. On Ubuntu save as /etc/apache2/sites-available/course-site.conf and enable with sudo a2ensite course-site. Add Listen 8080 exactly once; on Ubuntu it can go in ports.conf instead of the site file.

apache
Listen 8080
<VirtualHost *:8080>
    ServerName course.example.com
    DocumentRoot /var/www/course-site
    DirectoryIndex home.html
    <Directory /var/www/course-site>
        Require all granted
        Options -Indexes
        AllowOverride None
    </Directory>
</VirtualHost>

Test with apachectl configtest on AL2023 or apache2ctl configtest on Ubuntu, then reload httpd or apache2. <VirtualHost *:8080> alone does not make Apache listen on 8080; Listen does.

Nginx equivalent

Use this as an alternative, not concurrently on the same 8080 socket. Save under an included conf.d path.

nginx
server {
    listen 8080;
    server_name course.example.com;
    root /var/www/course-site;
    index home.html;
    location / { try_files $uri $uri/ =404; }
}

Run sudo nginx -t then reload. Test:

bash
curl -i -H 'Host: course.example.com' http://127.0.0.1:8080/
sudo ss -lntp

Network and host controls

Allow 8080 only from your lab client's address in the EC2 security group. A host firewall, if enabled, must also permit the traffic. Under SELinux enforcing policy, a nonstandard listener or new directory may need an appropriate port type or file context; inspect getenforce and audit logs. Do not blindly copy policy changes into an unrelated server.

Verify, diagnose, revert

Expected result: HTTP 200 and the custom heading. 404 suggests wrong root/path/index; 403 suggests directory permission or policy; timeout suggests a network boundary. Restore the original config or remove only this lab site, test syntax, reload and remove the temporary security-group rule. Confirm port 8080 is no longer listening if it was lab-only.

Official reference

Apache core directives

Ravindra’s Tip

Root, port और index बदलने के बाद guess मत करो। Config test, reload, local curl और remote test—यही क्रम रखो।

Interview and revision check

Why does an Apache VirtualHost on 8080 need a Listen directive?

VirtualHost defines request handling for that address/port. Listen creates the listener; defining the virtual host alone is insufficient.

Ravindra Bagale · Cloud & DevOps Academy · Handbook and project downloads