How to setup two droplets with Angular app (Nginx) on one and PHP API (Apache) on another?

September 20, 2016 137 views
Apache Nginx API PHP Applications Ubuntu 16.04

I would like to build Angular app which will be on one Nginx (Ubuntu server) droplet and PHP api on another droplet. How I can setup my droplets in order to use Ajax calls from Angular app to my api.

1 Answer

You have two options.
Most easiest will be making single droplet. You will need Nginx + PHP.

Next option would be what you want -> Two droplets. One would require Nginx, and on second you will need Nginx/Apache + PHP.
How to install LEMP on Ubuntu 16.04. On first droplet you will only need step 1, yet on second you will need step 1, 3, 4 and 5.

Be careful about Ajax limitations. Research about it, you can have problem if you want to access API on another server.

This can be solved on multiple ways and I can suggest Nginx proxy_pass. I will give idea how it can be done.
This is easy way, you need to add location for API into your nginx server block (default block is located at /etc/nginx/sites-available/default).
Example would be:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    location /api {
        proxy_pass http://ip_of_api_droplet;

Result of this would be that when you access localhost/api, you will use API on other droplet. This will help you solve SOP.

Also, if both API and Client droplet are in same datacenter, you can look into Private Networking.

The LEMP software stack is a group of software that can be used to serve dynamic web pages and web applications. This is an acronym that describes a Linux operating system, with an Nginx web server. The backend data is stored in the MySQL database and the dynamic processing...
Have another answer? Share your knowledge.