How to setup a server with PHP and Node.js?

December 5, 2014 5k views

Hello, I'm new in DO and i'm trying to configure my server for deploy some apps.

I would like to set my DNS in my domain and redirect to my app in DO.
And another thing, an app can run Node or PHP.

For example:
app1.domain.com -> /home/myuser/www/app1 -> PHP
app2.domain.com -> /home/myuser/www/app2 -> NODE
app2.domain.com -> /home/myuser/www/app3 -> NODE

First, I installed PHP, Node and Nginx.

How can I modify the Nginx config file to make the redirects?

My Nginx file until now:

server {
    server_name app1.domain.com;
    root /home/myuser/www/app1;
}

PS: At this time, if i try to enter in my droplet ip (104.XXX.77.XX), i see the app1 html.

I'm thinking of something like this:

server {
    server_name app1.domain.com;
    root /home/myuser/www/app1;
}

server {
    server_name app2.domain.com;
    root /home/myuser/www/app2;
}

server {
    server_name app3.domain.com;
    root /home/myuser/www/app3;
}

Thanks!

3 Answers

you have to use nginx reverse proxy for nodejs app

server {
    listen 80;
    server_name app2.domain.com;
    location / {
        proxy_pass http://localhost:{YOUR_NODEJS_PORT};
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

or something
https://www.digitalocean.com/community/tutorials/how-to-host-multiple-node-js-applications-on-a-single-vps-with-nginx-forever-and-crontab

by Christian Fei
This is our tutorial on hosting multiple Node.js apps on a single VPS with nginx, forever, and crontab.
  • Thanks wiak.

    So, i have to create a config like yours every time that i create a new app with node?
    If i need to create like an app3.domain.com for example, do i need to duplicate de server config?

  • yep, exactly the same for another node app if you want to run it under another domain/server
    there are other ways like doing it on a nameserver level but doing it in nginx is far faster and easier

  • my blog runs ghost nodejs reverse proxy'd with lighttpd
    http://nwgat.ninja/

    and my lftp windows builds directory run on lighttpd+php
    http://lftp.nwgat.ninja/

    same works with nginx, i just like lighttpd more and am more use to that

  • Thanks again wiak, i will try and then, i will post here if i need more help hehe

  • am on irc if you need me ;)

  • are there some channel or room to connect?

  • irc.freenode.net
    #digitalocean
    its unofficial eh ;)
    
Have another answer? Share your knowledge.