Set up NginX and Varnish with Apache in Sentora

  ·   4 min read

By default, Sentora, as well as Sentora, comes with Apache 2.2 and I feel that it’s a bad solution when there are many good solutions already for using as web server e.g. Litespeed, NginX, Lighttpd etc. Litespeed is a paid service so I never use it at all. If you have the ability to pay for it, you should use it as it is very good as far as I see on Benchmark test results. The person, like me, should go through the most of the benchmark test results. Then you will see that NginX, one of the best web server, has a very good reputation. After Apache, NginX has the most market share about to 14%. But NginX need PHP-FPM to work sound. Sentora comes with PHP 5.3 so I have to do a lot of tasks to make NginX work with Sentora, won’t I? So I have found out the perfect solution for mine. I am using NginX in front of Apache as reverse proxy. Let’s share it guys! :)

At first, we will make Apache default port changed to a non-standard port. We will edit httpd-vhosts.conf file. Just enter the following command and change port 80 to port 8081.

$ vim /etc/Sentora/configs/apache/httpd-vhosts.conf

Then we need to install NginX on our server. I am using CentOS 6.6 (64-bit) so I am telling you the installation process on CentOS server. For more info or other OS based servers, follow NginX Installation Wiki to get it installed. If you can’t or get any error, let me know I will help you as far as I can.

We have to add NginX yum repository. Enter following command:

$ vim /etc/yum.repos.d/nginx.repo

Now paste the following configurations in it:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Use this command to install NginX:

$ yum install nginx

So, NginX has been installed on the server. Now we will edit nginx.conf file. Use the following command:

$ vim /etc/nginx/nginx.conf

Delete all default lines and paste the following lines:

user nginx;
worker_processes 3; #this number should be as same as your CPU core

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
    worker_connections 1024; #this number indicates the connections, may be larger for large server
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log /var/log/nginx/access.log main;
    sendfile on;
    keepalive_timeout 65;
    gzip on;
    gzip_min_length 1100;
    gzip_buffers 4 32k;
    gzip_types text/plain application/x-javascript text/xml text/css;
    gzip_vary on;

    server {
        listen 8083 default_server;

            location / {
                proxy_set_header Host $host;
                proxy_pass http://127.0.0.1:8080;
             }

            location ~ /\.ht {
                deny all;
            }
    }
}

Find the following lines:

server {
    listen 8083 default server;

Here our Nginx will listen to port 8083 as I also tell you how to set up Varnish Cache on your server. But if you want to use only NginX in front of Apache then change the port to 80. Hope you understand!

Let’s try to set up Varnish Cache now. But before setting up it, I like to tell you something about Varnish Cache. Personally I don’t like Apache+NginX+Varnish combination at all because of high RAM usage of Varnish Cache. Actually Varnish Ccahe is a web application accelerator also known as a caching HTTP reverse proxy. It uses RAM for caching as RAM is faster than our hard-disk. So I don’t like it all when NginX alone is showing a better performance. But still if you want to set it, you can at your own risk. Just joking dear!

To install Varnish cache, enter these commands:

$ rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm
$ yum install varnish

We have to change Varnish default backend port to 8083 on default.vcl. Use this command:

$ vim /etc/varnish/default.vcl

Now set** .port=“8083”;**

Then we have to edit /etc/sysconfig/varnish file and change VARNISH_LISTEN_PORT to 80. Have you done this? It’s time we should restart Apache and start NginX & Varnish (if you use) for working together. Enter following commands:

$ service httpd restart
$ service nginx start
$ service varnish start

That’s all. Now you should check your CPU and RAM usage. I hope you will see them in better health. For Apache+NginX+Varnish combination, Varnish works in front of NginX and NginX works in front of Apache, I mean Varnish works in front of all. For Apache+NginX combination, NginX works in front of Apache simply. Hope it makes sense. Still if you face any problem, please don’t hesitate to let me know. I will try my level best to help you. Hope I will be with you in future with any new topic. Till then, Take care Guys!

comments powered by Disqus