Over a million developers have joined DZone.

Hosting Multiple Applications on the Same Server

DZone's Guide to

Hosting Multiple Applications on the Same Server

These days, many developers work on a project on the same server at the same time — but we still need to be able to schedule app updates the server whenever we want to.

· Integration Zone ·
Free Resource

WSO2 is the only open source vendor to be named a leader in The Forrester Wave™: API Management Solutions, Q4 2018 Report. Download the report now or try out our product for free.

In this guide, we will talk about how to host multiple applications built on various backend technologies such as Java, PHP, and nodes on the same Ubuntu server, along with APIs and clients running under multiple domains or sub-domains on the same server.

Most modern developers use collaboration servers like GitHub. With multiple developers working on the same app, what if we could schedule the build and automate updates of the app installed on the server we are configuring whenever we want? With Maven, you build your apps from the updated source cloned from your Git repository. And we can schedule and automate those builds and executions with the support of cron (crontab/cron jobs).

As Apache is running on port 80, we can easily point our domains to apps running on it. If you have some other apps running on different ports, you can use Apache's virtual host configuration to map those apps under different ports to domains or subdomains and omit the ugly port numbers. This means that you can use domains without mentioning port numbers to point several apps installed on same saver computer and running under different application servers such as Apache, Tomcat, node, etc.

When you install the SSH server and enable access, you can control it all from anywhere via the internet — even from your mobile phone or tablet.

It doesn't matter whether you are going to use the same general internet connection that you use for other internet needs. You just have to do some port forwarding configurations to direct all requests on port 80 to the server that we are configuring. If you need the server's SSH to be accessible via the internet, you have to forward port 22, as well — but you should map it to another port number, just to be safe.

There is a risk of going all of it offline if something happens to your internet connection. To prevent that, we can have multiple internet connections from different ISPs (also in different technologies such as fiber optics, WiMAX, and maybe old ADSL) and use a load balance router to mix them up.

We all need backup, mostly in different locations. For that, we can schedule a local backup and upload it via FTP to another server. Also, we can permanently mount a folder shared in a local network on a Windows computer and schedule synchronization of the backup files with that Windows shared folder.

If we use MySQL as our DBMS, we can use phpMyAdmin to manage it via the web. You can configure a subdomain to point to your phpMyAdmin instance.

You can do the same thing and install multiple WordPress sites and configure to work on multiple domains. By that, you can host many websites on your own server.

By installing an FTP server, you can manage files on your server.

Spring Boot apps can be launched by just executing the JAR file as it has an embed server within the jar, but what if you have multiple spring boot app? You may need to set different ports in configurations to run all together on different ports; it looks like a waste of resources. If we use separate Tomcat server, we can deploy them all in that one Tomcat. We can schedule the deployment of apps as we update things via Git.

You may need to see Tomcat logs occasionally. If you create hard links to that log file from within the Tomcat container, you can read them via your browser.

I'd like to share my list of commands and templates with you so that if you are interested, you can give it a try.

You can find many articles about Ubuntu as well as basic installation guides on the internet — so I am not going to explain it all in detail here. 

Look at the BIOS configuration of your computer to confirm if the auto start on power failure feature is turned on.

In the Ubuntu installation wizard, you should have selected LAMP and Open SSH. If you haven't, don't worry; we can install them manually.

Install open SSH server:

sudo apt install openssh-server

See the status of SSH server:

sudo service ssh status

Now you, should be able to connect it via the network.

To install MySQL server:

sudo apt install mysql-server

To install Java:

sudo apt install default-jdk

To install Maven:

sudo apt install maven

To install Git:

sudo apt install git

Clone Git repositories (replace URLs with repos that you need to clone):

mkdir git
cd git
git clone https://github.com/GIT_USER/REPO_ONE.git
git clone https://github.com/GIT_USER/REPO_TWO.git

Install and enable Apache 2:

sudo apt install apache2
enable  apache2

Install phpMyAdmin:

sudo apt install phpmyadmin

Create a folder for hosting the site (replace SITE_NAME with your actual site name).

cd /var/www
sudo mkdir SITE_NAME

Let's assume that you need the site to be a cloned Git repo.

Add a link to the cloned repo folder as your site's public_html folder (replace UBUNTU_USERNAME, CLONED_REPO_FOLDER, and SITE_NAME to match yours).

sudo ln -s /home/UBUNTU_USERNAME/git/CLONED_REPO_FOLDER/var/www/SITE_NAME/public_html

Create and edit virtual host file to manage requests to your site (replace SITE_NAME). 

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/SITE_NAME.conf
sudo nano /etc/apache2/sites-available/SITE_NAME.conf

Enable the site, then disable the default site. Reload Apache 2 to make the change effective.

sudo a2ensite SITE_NAME.conf
sudo a2dissite 000-default.conf
sudo service apache2 reload

At the moment inside one of my servers I have a spring based API app running inside tomcat and an angular based REST client app running inside apache. My virtual host file for this looks like below. (Real domains replaced with "yourdomain.com" evetrything else remains unchanged)

<VirtualHost *:*>
        ProxyPreserveHost On
        ServerAdmin r.thilina@gmail.com
        ServerName yourdomain.com
        ServerAlias app.yourdomain.com
        DocumentRoot /var/www/app.yourdomain.com/public_html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

<VirtualHost *:*>
    ProxyPreserveHost On
    ServerAdmin r.thilina@gmail.com
    ServerName yourdomain.com
    ServerAlias api.yourdomain.com
    # setup the proxy
    <Proxy *>
        Order allow,deny
        Allow from all
    ProxyPass / http://localhost:8080/your-api/
    ProxyPassReverse / http://localhost:8080/your-api /

Now, let's assume that you need to update the site's files from Git. Change the directory to the repository folder then Pull the changes from Git.

cd ~/git/REPO_NAME
git pull

There are more things to come! I will update this continuously and add more as time goes on.

If you feel this helps you in any way, please like and share this article. You can find me on LinkedIn (and I really love it when you upvote my skills there!).

I am happy to help you if you are going to practice these things. Also, please give feedback and suggestions. I will improve this article with your support.

IAM is now more than a security project. It’s an enabler for an integration agile enterprise. If you’re currently evaluating an identity solution or exploring IAM, join this webinar.

hosting ,integration ,tutorial ,applications

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}