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

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.

Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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.