I will be setting up a new server soon and I have few questions.
I will be installing CentOS 7 with LAMP stack, but I also heard that I can install Nginx as reverse proxy.
I am new with nginx and node.js, but I am familiar with DO, ubuntu, LAMP stack and basic beginner stuff.
Thank you for answering.
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.
@Chempo
Honestly, I can’t really think of a situation where NGINX wouldn’t be a good fit :-). While Apache can do some of the same things as NGINX, I find the syntax a bit cluttered and dated.
For me, as a developer/programmer, I find the syntax of NGINX to be far cleaner.
As a basic example, the typical VirtualHost block for Apache looks something like:
Some will argue that’s pretty clean, though with NGINX, the above would look like:
The above is a simple server block to handle WordPress sites. Comparing the two, the syntax of NGINX is much like a programming language, so without any other considerations, I naturally draw to it.
Beyond the programming-like syntax, it’s super simple to setup Proxying. For example, if we wanted to proxy a NodeJS application, our server block would look something like:
Now I know that looks complex, though that’s working configuration. You’d need to change the port in
@backend
at the top to match your NodeJS application port, andserver_name
to match your domain, but that will just work 99% of the time.Beyond that, if you wanted to do something really crazy, you could load balance, proxy an application, serve multiple WordPress sites, and cache all with the same NGINX server. It’s not ideal, but you can. NGINX functions as a reverse proxy, load balancer, PHP-FPM proxy, caching proxy, etc.
The repo that I noted in the above post sets up a fully configured NGINX installation from source with numerous example configurations that work.
The configuration is already tweaked/tuned for production use and works well (I used it on many sites).
…
As far as recommendations, I would start with at least 1GB RAM, though 2GB would be preferred. That’s not because NGINX is RAM-hungry, it’s because MySQL/MariaDB and WordPress + NodeJS applications are from time to time.
You need RAM to run NGINX, PHP-FPM, MySQL/MariaDB, and NodeJS on the same server, and without a good amount, services crash.
The good news is that in those installers, there’s a also installers for MariaDB (a MySQL fork that has better overall performance), PHP-FPM (5.6, 7.0, and 7.1), as well as NodeJS.
So you could run the installers and easily setup each piece of software with relative ease :-).
@hansen @jtittle
Hi,
thank you for quick replies, I am confused what is real advantage of nginx over apache.
I know its faster, but I’ve read somewhere that it is not for dynamic sites. Can you give me example of sites that are good for nginx and type of site that are not for nginx.
Mine will be worpdress site with node.js web-app on a sub-domain.
Also I forgot to ask on a original thread how powerful droplet should I choose? Is $5 enough or $10 is smart choice?
@Chempo @hansen
Completely agree on selecting NGINX over Apache or NGINX + Apache. Unless you’re locked to Apache, for one reason or another (I can’t think of anything specific off the top of my head), then choosing NGINX from the start would be the best option.
You can run a normal PHP site with one server block and proxy requests to a NodeJS application with another server block with relative ease.
If you were using Ubuntu 16.04, I’d recommend using the auto-installer I wrote to compile NGINX from source. It’ll setup a pretty well optimized NGINX configuration and all you need to do is:
That’ll setup the latest version of NGINX complete with Brotli compression support as well as quite a few examples to get you started ranging from NGINX as a Load Balancer, NGINX as a Proxy (for your NodeJS app), PHP-FPM, etc.
I’m actually reworking the script now as the one there needs a little more TLC, in my opinion, but it works well enough as-is.
Of course, if you choose to go Ubuntu and use that, if you have any questions feel free to ask.
Hi @Chempo
Unless you have a specific reason for using Apache, why not start you installation with LEMP stack instead. That’s simply Nginx instead of Apache.
Then you would have one layer less to manage/debug/configure.
And you can run Node.js reversed through Nginx, which is better at handling connections than Node.js by itself.
If you need Apache, then you could skip Nginx and only use Apache as the web server. Nginx is much faster than Apache, but only because there is no dynamic configuration in Nginx. I am bias, since I haven’t used Apache for many years - mainly only using Nginx now.