How to Configure a Subdomain to Point to a Directory in LAMP Stack (Ubuntu/VPS)


31 views

When you want to serve the contents of /var/www/john through john.mysite.com instead of mysite.com/john, you'll need to configure both DNS and Apache settings. Here's the complete workflow:

First, create a DNS A record pointing to your server's IP:

john.mysite.com.   IN   A   192.0.2.1

If you're using a control panel like cPanel or Plesk, you can add this through their interface. For manual DNS management, edit your zone file accordingly.

Create a new virtual host configuration file:

sudo nano /etc/apache2/sites-available/john.mysite.com.conf

Add the following configuration:

<VirtualHost *:80>
    ServerName john.mysite.com
    DocumentRoot /var/www/john
    ErrorLog ${APACHE_LOG_DIR}/john-error.log
    CustomLog ${APACHE_LOG_DIR}/john-access.log combined

    <Directory /var/www/john>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Enable the site and reload Apache:

sudo a2ensite john.mysite.com.conf
sudo systemctl reload apache2

Check for syntax errors:

sudo apache2ctl configtest

If everything is correct, you should see "Syntax OK". Then test the subdomain in your browser or using curl:

curl -I http://john.mysite.com

To redirect old URLs from mysite.com/john to john.mysite.com, create or modify /var/www/john/.htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^john/(.*)$ http://john.mysite.com/$1 [L,R=301]

If the subdomain isn't working:

  • Check DNS propagation using dig john.mysite.com
  • Verify Apache is listening to the subdomain with sudo apache2ctl -S
  • Ensure directory permissions are correct: sudo chown -R www-data:www-data /var/www/john

When working with LAMP stack on Ubuntu, you might want to elevate a subdirectory to a proper subdomain for better URL structure or organizational purposes. This requires both DNS and Apache configuration changes.

- Ubuntu server with LAMP stack
- Root or sudo access
- Ability to modify DNS records
- Apache's mod_vhost_alias enabled (usually is by default)

First, add a CNAME or A record for your subdomain in your DNS management panel:

john.mysite.com.   300   IN   CNAME   mysite.com.

Or if using A record:

john.mysite.com.   300   IN   A   192.0.2.1

Create a new configuration file for your subdomain:

sudo nano /etc/apache2/sites-available/john.mysite.com.conf

Add this configuration:

<VirtualHost *:80>
    ServerName john.mysite.com
    DocumentRoot /var/www/john
    
    <Directory /var/www/john>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/john-error.log
    CustomLog ${APACHE_LOG_DIR}/john-access.log combined
</VirtualHost>

Run these commands to enable the configuration:

sudo a2ensite john.mysite.com.conf
sudo systemctl reload apache2

To redirect mysite.com/john to john.mysite.com, add this to your main site's .htaccess:

RewriteEngine On
RewriteRule ^john(.*)$ http://john.mysite.com$1 [R=301,L]

Check your configuration with:

sudo apache2ctl configtest

If everything is OK, test by accessing john.mysite.com in your browser.

If you need HTTPS, generate a certificate with Let's Encrypt:

sudo certbot --apache -d john.mysite.com
  • DNS changes may take up to 48 hours to propagate
  • Check Apache error logs if the subdomain doesn't work
  • Ensure proper permissions on the /var/www/john directory
  • Clear your browser cache when testing