Report this

What is the reason for this report?

Installing LEMP and MEAN stacks on single droplet?

Posted on March 8, 2015

Howdy all,

I’m switching from a shared host to Digital Ocean, but want to try the waters with the 5$ plan for a bit first.

I will just be porting over my personal portfolio/blog (PHP, AnchorCMS) so I will need the LEMP stack.

I also hav a couple of small side projects I am working on using Node.JS / MEAN stack, which I would love to host with the same 5$ droplet.

Is it possible/simple to configure both stacks to operate on the same droplet without breaking anything? Are there any guides or tutorials for this?

Thanks for any help!



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.

It should be fine to have both set ups on a single droplet. We have tutorials going through both setups:

You can either start with a fresh Ubuntu base install and follow those tutorials or start with one of the “applications” and install the other along side it.

The one thing to keep in mind is that only one process can bind to port 80 at a time. So in order to run both sites, you’ll need to set up your MEAN app to run behind an Nginx reverse proxy.

You Nginx configuration might look something like this:

server {
	listen 80 default_server;
	listen [::]:80 default_server ipv6only=on;

	root /var/www/html;
	index index.php index.html index.htm;

	server_name example.com;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}

	error_page 404 /404.html;
	error_page 500 502 503 504 /50x.html;
	location = /50x.html {
		root /usr/share/nginx/html;
	}

	location ~ \.php$ {
		try_files $uri =404;
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_index index.php;
		include fastcgi_params;
	}
}

upstream mean_app{
  server 127.0.0.1:3000;
}

server{
	listen 80;
	listen [::]:80 ipv6only=on;

    server_name meanapp.example.com;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://mean_app/;
      proxy_redirect off;
    }
}

A couple things to note here. This assumes the MEAN app is running on port 3000. Update that as needed. Also, the server_name directive is important here. It’s how Nginx know how to route requests to the correct app.

Let us know how it goes!

Heya,

Yes, it is indeed possible to configure both a LEMP stack (Linux, Nginx, MySQL, PHP) and a MEAN stack (MongoDB, Express.js, Angular, Node.js) to operate on the same DigitalOcean droplet, especially if your traffic is low and your applications are not resource-intensive.

Here are the high-level steps you would follow to set this up:

  1. Install the LEMP Stack: Start by installing the LEMP stack. DigitalOcean has an excellent tutorial for this:

  2. Install Node.js and NPM: After setting up LEMP, you can install Node.js and npm (Node Package Manager), which you’ll need for your MEAN stack applications.

    • You can use the NodeSource binary distributions to install Node.js and npm. This can be done via a PPA if you’re using Ubuntu.
  3. Configure Nginx for Node.js: You’ll need to configure Nginx as a reverse proxy for your Node.js applications. You’ll set up server blocks (similar to virtual hosts in Apache) for each Node.js app. Here is a general guide on how to do this:

  4. Install MongoDB: Install MongoDB only if you need it for the MEAN stack. Again, DigitalOcean provides an excellent guide:

  5. Configure Server Blocks: Set up different server blocks for each project. You can have different locations within your Nginx config file where you define where to find the site files for your PHP application and how to proxy requests to your Node.js application.

  6. Manage Services: Ensure that all your services are set to start on boot. Use systemctl to enable and manage your services.

  7. Security: Secure your MongoDB installation (especially if you are exposing it to the public) and ensure you have a firewall setup, such as ufw (Uncomplicated Firewall), to allow only specific ports.

  8. Test: Test your configuration extensively to ensure that both stacks are working as expected. Start with one stack, and once you confirm it’s working, move on to set up the second stack.

When running multiple applications on a single droplet, remember to monitor the resource usage closely. If you find that your droplet is running out of memory or CPU, you might need to optimize your applications or upgrade your droplet.

As you move forward, keep regular backups of your droplet or, at the very least, of your databases and site content, so that you can recover quickly in case anything goes wrong during the setup or in the future.

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.