How to Locate Website Source Files on an Apache/Ubuntu EC2 Instance: PHP/HTML Code Discovery Guide


1 views

html

When dealing with Apache on Ubuntu, the first place to check is the DocumentRoot defined in virtual host configurations. Run this command to find active configurations:

sudo apache2ctl -S

Example output might show:

VirtualHost configuration:
*:80                   www.example.org (/etc/apache2/sites-enabled/000-default.conf:1)
    DocumentRoot "/var/www/html"

If the virtual host isn't clearly defined, check these common locations:

ls -la /var/www/
ls -la /var/www/html/
ls -la /home/*/public_html/

The access logs can reveal the exact path being accessed:

sudo tail -f /var/log/apache2/access.log

Then visit the website and watch for new entries showing the file paths.

For PHP sites, create a temporary info file:

echo "<?php phpinfo(); ?>" > /var/www/html/info.php

Access this file via browser (www.example.org/info.php) and check:

  • 'DOCUMENT_ROOT' in PHP Variables section
  • 'Configuration File (php.ini) Path'

When all else fails, search the entire system:

sudo find / -type f -name "index.php" -o -name "index.html" 2>/dev/null

Sometimes the actual files are symlinked from unusual locations:

ls -la /var/www/html | grep "\->"
find /var/www/ -type l -ls

Rewrite rules might point to the actual location:

cat /var/www/html/.htaccess

Look for RewriteBase or RewriteRule directives that might indicate alternate paths.

See what files Apache actually has open:

sudo lsof -i :80
sudo lsof -p $(pgrep apache2 | head -1) | grep '/var/www'
# 1. Check virtual hosts
apache2ctl -S

# 2. Verify document root
grep -r "DocumentRoot" /etc/apache2/

# 3. Check default locations
ls -la /var/www/html/

# 4. Create PHP info file
echo "<?php phpinfo(); ?>" > /var/www/html/info.php

# 5. Search entire system
sudo find / -type f -name "index.php" 2>/dev/null

When dealing with an Apache server on Ubuntu, the first place to check is the DocumentRoot defined in Apache's configuration. Run this command to find the active configuration:

apache2ctl -S | grep "Main DocumentRoot"

For virtual hosts (common in messy setups), examine all enabled sites:

grep -r "DocumentRoot" /etc/apache2/sites-enabled/

Ubuntu Apache installations typically use these directories:

  • /var/www/html (default DocumentRoot)
  • /var/www/example.org (common for virtual hosts)
  • /home/user/public_html (if using user directories)

Use this command to search for index files:

sudo find / -type f $-name "index.php" -o -name "index.html"$ 2>/dev/null

The access logs show which files are being served:

tail -n 50 /var/log/apache2/access.log

Look for entries like:

GET /index.php HTTP/1.1

Discover what files Apache currently has open:

sudo lsof -c apache2 | grep -E '\.php|\.html'

Messy servers often use symlinks. Find them with:

find /var/www/ -type l -ls

Some sites use auto_prepend_file directives:

php -i | grep auto_prepend_file

Here's a complete search script you can run:

#!/bin/bash
echo "Checking Apache config..."
apache2ctl -S | grep "DocumentRoot"

echo -e "\nSearching for index files..."
sudo find /var/www/ /home/ /opt/ -type f $-name "index.php" -o -name "index.html"$ 2>/dev/null

echo -e "\nChecking active connections..."
sudo lsof -i :80

echo -e "\nExamining open files..."
sudo lsof -c apache2 | head -n 20

For complex setups with multiple sites:

apache2ctl -S

Sample output:

VirtualHost configuration:
*:80                   example.org (/etc/apache2/sites-enabled/example.org.conf:1)

Then examine the specific config file:

cat /etc/apache2/sites-enabled/example.org.conf