Chempo
By:
Chempo

Setting up a new server with CentOS 7 and LAMP, but I have few questions.

April 6, 2017 633 views
Apache Node.js LAMP Stack Nginx CentOS

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.

  1. Is this possible and if it is, is it smart to do that?
  2. Later I will be installing Node.js app (or probably MEAN stack), I just want to make sure it will function correctly with setup above?
  3. And lastly will I be able to install other applications normally without configuring them additionally due to 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.

4 Answers

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.

@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:

cd /opt \
&& git clone https://github.com/serveradminsh/installers.git \
&& cd installers/nginx \
&& chmod +x installer.sh \
&& ./installer.sh

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.

@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?

  • Hi @Chempo

    You can use Nginx for anything. It's perfect for WordPress and the like. Both @jtittle and I have many sites running just like that.

    I cannot even come up with something, where Nginx wouldn't be a good fit, but if you're using a system that has massive amount of .htaccess files (that's one of Apache features) and maybe dynamically changed, then it would probably be easier to use Apache.

    It's difficult to host WordPress on anything below 1GB RAM - unless you really tweak every single service (specially the database).
    And then it depends on your Node.js apps - if you expect them to use anywhere near 500MB RAM in total, then I would recommend 2GB.
    But start with $10 - you can always upgrade to $20 or more later on. And it takes less than a minute to upgrade if you don't change the disk space.

@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:

<VirtualHost *:80>
    ServerAdmin you@domain.com
    ServerName domain.com
    ServerAlias www.domain.com

    DocumentRoot /var/www/domain.com/public_html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /path/to/dir>
        ...
        ...
        ...
     </Directory>

    ...
    ...
    ...
</VirtualHost>

Some will argue that's pretty clean, though with NGINX, the above would look like:

server {
    listen 80;
    listen [::]:80;
    server_name domain.com www.domain.com;

    access_log /path/to/domain.com.access.log;
    error_log /path/to/domain.com.error.log;

    root /path/to/home/dir;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;

        include fastcgi_params;
    }
}

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:

upstream @backend {
    server 127.0.0.1:2356;
}

server
{
    listen 80;
    listen [::]:80;
    server_name _;

    location /
    {
        proxy_pass http://@backend;

        proxy_buffers 16 32k;
        proxy_buffer_size 64k;
        proxy_busy_buffers_size 128k;
        proxy_cache_bypass $http_pragma $http_authorization;
        proxy_connect_timeout 59s;
        proxy_hide_header X-Powered-By;
        proxy_http_version 1.1;
        proxy_ignore_headers Cache-Control Expires;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
        proxy_no_cache $http_pragma $http_authorization;
        proxy_pass_header Set-Cookie;
        proxy_read_timeout 600;
        proxy_redirect off;
        proxy_send_timeout 600;
        proxy_set_header Accept-Encoding '';
        proxy_set_header Cookie $http_cookie;
        proxy_set_header Host $host;
        proxy_set_header Referer $http_referer;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_temp_file_write_size 64k;
        proxy_set_header X-Original-Request $request_uri;
    }
}

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, and server_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 :-).

Have another answer? Share your knowledge.