Enter your keyword

post

How to install varnish & nginx on cpanel at vps server

How to install varnish & nginx on cpanel at vps server

Varnish cache is a free and opensource high-performance HTTP accelerator used to speed up the rate at which web servers serve web pages such as sanjibkumardas.com.

The cache saves the webpages in memory such that the web server doesn’t have to serve the webpages afresh every time they are requested by clients.

This ultimately gives your server a speed boost which increases your chances of getting ranked higher by Google. In this article, you will learn how to install Varnish cache on CentOS 7.

Step 1: Install EPEL repository

The first step is to install EPEL, short for Extra Packages for Enterprise Linux. It contains extra packages and dependencies that are required for the smooth running of packages.

sudo yum install -y epel-release

Install EPEL repositoryStep 2: Install Nginx

Next on line is the installation of the Nginx web server.

To install Nginx, run the command

yum install nginx -y

Install NginxStep 3: Start and Enable Nginx

Once you have successfully installed Nginx, be sure to start and enable it on boot using the commands below

systemctl start nginx 

systemctl enable nginx

systemctl status nginx

Start and Enable Nginx

status nginx

By default, Nginx runs on port 80. To verify this, use the netstat command

netstat -pnltu

netstat nginxStep 4: Install Varnish

Next, install varnish using the command below

yum install varnish

install varnishStep 5. Start and Enable Varnish

After the successful installation of the Varnish cache, we need to ensure that it is up and running.

To start Varnish cache execute the command

systemctl start varnish 
systemctl enable varnish
systemctl status varnish

Sample Output:

status varnishAwesome, Varnish is up and running as expected.

You can view more information such as the release, version and install data using the command

rpm -qi varnish

information about varnish cacheStep 6: Configure Varnish as a reverse proxy for Nginx

Since Varnish cache sits in front of the web server to serve HTTP requests, we need to change the default Nginx port to port 8080 and later configure Varnish to listen on port 80.

Open the Nginx configuration file

vi /etc/nginx/nginx.conf

Locate the Server block and make the changes as show

..... server { 
listen 8080 default_server;
listen [::] 8080 default_server;
....
}

Save and exit the text editor.

systemctl restart nginx 

Now Nginx should run on port 8080

Next, head out to the Varnish configuration

vi /etc/varnish/default.vcl

Locate the backend default block and ensure it resembles the lines shown below

backend default {
.host = "127.0.0.1";
.port = "8080";
}

Note:

.host = Refers to the backend web server IP address.
.port = Refers to the backend web server port it is running on.

Save and exit the text editor.

We need to configure Varnish to listen to port 80. Head out to the configuration file below

vi /etc/varnish/varnish.params

Modify the value of 'VARNISH_LISTEN_PORT' line to HTTP port 80.

VARNISH_LISTEN_PORT=80

Save end exit the text editor.

Restart Varnish.

systemctl restart varnish

netstat pnltuFrom the above output, we can clearly see that Nginx is listening on port 8080 while Varnish is listening on port 80, which is exactly what we want.

Now let us configure the firewall

Step 7: Install and configure Firewalld

We need to install firewalld and allow http and https protocols.

To install firewalld run

yum install firewalld -y

install firewalldow start and enable firewalld to start on boot

systemctl start firewalld
systemctl enable firewalld

Now let’s allow ports 80 (http) and 443 (https)

firewall-cmd --add-port=80/tcp --zone=public --permanent
firewall-cmd --add-port=443/tcp --zone=public --permanent

Great! The only remaining item is to test our configuration.

Step 8: Testing Varnish configuration

To test varnish, run the following command

curl -I localhost

localhost test

To check the logs, run the following command

varnishncsa

Open your browser and browse your server’s addres

http://server-ip

This brings us to the end of this tutorial. We have successfully installed Varnish and configured it to act as a reverse proxy to the Nginx & varnish web server.

Note: For https(ssl) port:

listen 443 http2 default_server; 
listen [::]:443 http2 default_server;

Readymade NGINX + VARNISH + APACHE WITH PHP FPM contact leading web hosting provider.

For any further assistance in server management contact me.

No Comments

Leave a Reply

Your email address will not be published.