Installing Varnish and Nginx on a cPanel Server with Elasticsearch

Categories

Installing Varnish and Nginx on a cPanel Server with Elasticsearch

You are here:

Introduction

This guide will detail howto install nginx and varnish onto a cPanel/WHM server (in this instance running Almalinux 8, however the guide can be adopted for CentOS and Ubuntu servers if required.

In this instance we will configure nginx to listen on Ports 80 and 443 to proxy connections to Varnish running on Port 81 finally forwarding connections to Apache running on port 8080 and 8443.

 

Prerequisites

Ensure that the following are present prior to setup:

  • ea-ruby27-mod_passenger — Install this package if your system runs CentOS 7 or AlmaLinux 8.
  • ea-apache24-mod-passenger — Install this package if your system runs Ubuntu

These can be installed with:

yum install ea-ruby27-mod_passenger    -- Almalinux/CentOS
apt-get install ea-apache24-mod-passenger -- Ubuntu

 

Compatibility

Please be aware that this guide will proxy all web connections through nginx and Varnish.

Limitations

  • For security reasons, NGINX will not serve any file with a name starting with .ht.
  • NGINX redirects any non-SSL IPv6 requests to use SSL. This ensures that any IPv6-only service subdomains will work correctly. If your client will not accept the hostname’s security certificate, we recommend that you use the subdomain’s fully-qualified domain name instead.

Installing Nginx and Varnish

To do this first open the start menu, search for notepad then right click and select run as admin.

1) Install WHMs Nginx Manager:

yum install ea-nginx

 

2) Edit the ports that Nginx forwards to within the nginx configuration:

nano /etc/nginx/conf.d/ ea-nginx.conf

Change:

map $host $CPANEL_APACHE_PROXY_SSL_PORT {
default 444;
}

To:

map $host $CPANEL_APACHE_PROXY_SSL_PORT {
default 81;
}

 

3) Disable Caching within WHM for Nginx Manager:

 

4) Install Varnish:

yum install varnish
systemctl start varnish
systemctl enable varnish
systemctl status varnish

 

5) Edit the ports that Varnish forwards connections to:

nano /etc/varnish/default.vcl

Within this file change the .port setting which will be the port that we move Apache to listen on.

backend default {
.host = “127.0.0.1”;
.port = “8443”;
}

 

6) Change the port on which varnish listens:

nano /usr/lib/systemd/system/varnish.service

Within this file change:

ExecStart=/usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m

to

to ExecStart=/usr/sbin/varnishd -a :81 -f /etc/varnish/default.vcl -s malloc,256m

 

7) Run the following commands:

systemctl daemon-reload
systemctl restart varnish.service
systemctl status varnish.service

 

8) Change the Apache Port to 8080 and 8443 within WHM under Tweak Settings -> System

 

 

 

 

 

Installing Java and ElasticSearch

Elasticsearch is often used when hosting e-commerce platofrm such as Magento to get a fast search working on your website.

 

1) The first step to installing ElasticSearch will be to get Java running on your server.

yum install java

This will get you the latest version of OpenJDK from the default repositories, and will work fine with ElasticSearch 7.

2) Next we will need to setup the repository for ElasticSearch.

Download the public signing key:

rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

3) Then create /etc/yum.repos.d/elasticsearch.repo and add the following:

[elasticsearch]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

4) This will then allow you to install and update ElasticSearch using yum

yum install elasticsearch

 

Before you start…

There are a couple of settings that will need to be changed in order to get Elasticsearch working properly on a cPanel install.

The first is to configure your Java temp directory. This can be done by editing the file

nano /etc/elasticsearch/jvm.options

And change the line that includes tmpdir to the following:

-Djava.io.tmpdir=/var/log/elasticsearch

It is also highly recommended to reduce swap usage to encourage Elasticsearch to use RAM rather than excessively using swap space.

sysctl -w vm.swappiness=1 >> /etc/sysctl.conf
sysctl -p

That should be it! You are now ready to begin using Elasticsearch, simply enable the service so it begins on reboots and start it.

systemctl enable elasticsearch
systemctl start elasticsearch

 

Verify it is working

You can check your Elasticsearch install has started correctly by viewing the logs at /var/log/elasticsearch/elasticsearch.log or by querying it

curl -X GET “localhost:9200/?pretty”

This should give the following output:

{
"name" : "cp-yourhostname.net",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "WlokuA-8SMiUAM9AUeqhTw",
"version" : {
"number" : "7.17.4",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "79878662c54c886ae89206c685d9f1051a9d6411",
"build_date" : "2022-05-18T18:04:20.964345128Z",
"build_snapshot" : false,
"lucene_version" : "8.11.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"

 

Conclusion

That is it! Your domains on the server will now be proxied via Nginx → Varnish -> Apache. It is recommended that further work is carried out with regards to configuring Varnish caching to improve performance and this will be covered in later knoweldgebase articles.  If you are using an e-commerce platform such as Magento you will need to ensure this is configured to use Varnish and Elastisearch.

Table of Contents