How To Point a Subdomain To a Specific Port on DigitalOcean

Nowadays, it is a common practice to host several applications on Virtual Private Servers. VPSs are cheap, easy to manage and comes pre-installed with most packages. DigitalOcean is where we host all our applications. DO is very reliable and you can start your own VPS instance in less 30 seconds. DigitalOcean also offers extensive documentation issues commonly faced by developers. You can use this link to get an extra $10 credit when you sign up for a VPS with DigitalOcean.

One of the advantages of using a VPS is that you can host several applications on a single VPS even though they use different technologies. For example, a PHP application may run side by side a Node.js application on a single VPS. This is a major difference between shared hosting and VPS, apart from greater control and package management on VPSs.

In this post we are going to discuss about how we can point a subdomain to a specific port on DigitalOcean (and most other VPS for that matter). To give you an example, let us suppose we have a PHP application running on port 80 on domain The domain is attached to DigitalOcean and the DocumentRoot is /var/www/ Now, suppose we have built a Node.js application using Express and the app runs on port 8080. So, we can access our app from ip_of_our_vps:8080. But we want to the subdomain to point to ip_of_our_vps:8080.

To achieve this with Apache, we can follow this simple method:

  1. Add a domain from Networking tab in DigitalOcean control panel.
  2. Next, create a file inside the directory /etc/apache2/sites-enabled. This file is going to hold the VirtualHost directives to point the subdomain to the port.
  3. Put the following lines inside
    <VirtualHost *:80>
       ProxyPreserveHost On
       ProxyPass /
       ProxyPassReverse /
       ErrorLog ${APACHE_LOG_DIR}/error.log
       CustomLog ${APACHE_LOG_DIR}/access.log combined

    This directives tell Apache to point subdomain to port 8080 on the VPS.

  4. Lastly, we need to restart Apache in order for this new change to take its effect. We can restart Apache with: sudo service apache2 reload