Report this

What is the reason for this report?

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

Posted on September 20, 2016

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.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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:

/etc/nginx/sites-available/default
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 developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.