Apache Virtual Host Without Domain Name
You can serve PHP projects directly via IP address without configuring a specific domain name, but you need to adjust your Apache configuration to treat those paths as distinct virtual hosts rather than relying on the default catch-all block. The core challenge lies in how Apache resolves requests when no Host header is provided or when the header matches a wildcard. By default, the 000-default.conf file acts as a blanket catch-all, routing any request to /var/www/html. To serve /projecta and /projectb from their respective directories while keeping /html/info.php accessible, you must define specific virtual host blocks that intercept requests based on the URL path.
In your current setup, the 000-default.conf file defines a generic VirtualHost listening on port 80 with DocumentRoot set to /var/www/html. When you visit http://162.243.93.216/info.php, Apache matches this request against the default configuration because no other rule claims it. The <Directory> directive inside this block grants access to /var/www/, which covers your subdirectories, but the routing logic remains tied to the single document root defined in the VirtualHost context.
To separate projecta and projectb into their own environments without domains, you need to create additional virtual host configurations that explicitly map the IP address and port to specific paths. You can achieve this by creating new configuration files in /etc/apache2/sites-available/, such as projecta.conf and projectb.conf.
Here is an example of how to configure the virtual host for projecta:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
# Use a unique ServerName or leave it empty if relying on path-based routing
ServerName 162.243.93.216
# Explicitly set the document root for this specific project
DocumentRoot /var/www/projecta
<Directory /var/www/projecta>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/projecta-error.log
CustomLog ${APACHE_LOG_DIR}/projecta-access.log combined
</VirtualHost>
However, simply adding this block might not be enough if Apache prioritizes the default 000-default.conf for all requests or if your request handler still points to /var/www/html. The most robust approach when avoiding domain names is to use Alias directives or override the DirectoryIndex behavior, but a cleaner method for path-based separation without domains is to ensure the default virtual host only handles the root path (/) and lets specific VirtualHosts handle sub-paths if your web server supports them.
In modern Apache versions (2.4+), you can rely on Location directives within the default block or create separate blocks that take precedence based on the URL structure, though standard VirtualHost matching is primarily Host-header based. A more direct solution for path-based routing without domains involves using Alias in the main config or ensuring the default document root is restricted to /html while aliasing other paths.
Alternatively, you can configure the default virtual host to serve only the /html directory and use an Alias directive to map /projecta and /projectb to their actual directories:
# Inside /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
# Define the root location explicitly
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
# Map projecta to its directory
Alias /projecta /var/www/projecta
<Directory /var/www/projecta>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
# Map projectb to its directory
Alias /projectb /var/www/projectb
<Directory /var/www/projectb>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
After editing the configuration files, you must test the syntax and restart the service to apply changes:
sudo apachectl configtest
sudo systemctl restart apache2
Trade-offs and Considerations
Using IP addresses directly with path-based routing is convenient for internal testing or simple setups but lacks the security and maintainability of domain names. Without a ServerName, Apache might log these requests under the IP address, making debugging harder. Additionally, if you ever decide to implement SSL (HTTPS), you will need certificates that match either the IP address or specific domains, which can be complex for bare IPs.
If you are managing multiple projects on a single VPS and want to keep them isolated, consider using containers or distinct document roots with proper aliasing. If your stack is Laravel-based or requires complex middleware configuration, ensure your public folders are correctly aliased. For instance, if /projecta/public contains the actual entry point, update the Directory permissions accordingly.
For production environments, always prefer domain names to leverage features like automatic SSL provisioning (e.g., Let's Encrypt via Certbot) and better DNS management. If you need further guidance on configuring Laravel-specific directories or optimizing Apache for high-traffic PHP applications, resources from LaravelCompany can provide deeper insights into best practices for your specific stack.