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

Posted September 20, 2016 3.5k views
NginxApachePHPAPIApplicationsUbuntu 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.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
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.

by Justin Ellingwood
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...