Como instalar Varnish Cache no CPANEL (WHM) com CentOS
É muito simples fazer essa instalação, só precisa de atenção ao copiar os códigos e nas versões dos programas utilizados.
O CentOS que estou utilizando está na versão 6.9, o CPANEL (WHM) 66.0.24 e o Varnish Cache 5.2.0.
Para você que ainda está se perguntando, mas o que é Varnish Cache? Em poucas palavras ele é um acelerador web ou um proxy reverso http, que aumentará significativamente o desempenho do seu projeto, seja site, ecommerce ou sistema.
Basicamente o Varnish, armazena uma cópia da página solicitada pelo servidor web a primeira vez que o usuário visita essa página. Na próxima vez que o usuário solicitar a mesma página, o Varnish será consultado ao invés do servidor web. Isso significa que seu servidor web, irá lidar com menos tráfego, desempenho e escalabilidade, deixando seu projeto até 300x mais rápido.
Depois de toda explicação do meu ambiente e o que é esse tal de varnish, vamos implementa-ló?
Como instalar o Varnish Cache?
1. Configurar apache para ouvir outra porta;
O primeiro passo é alterar a porta padrão que o servidor web escuta, nesse caso estamos utilizando o apache, vamos alterar para a porta 8080, você pode fazer de duas formas, com o comando abaixo:
vi /etc/httpd/conf/httpd.conf
Ou acessando o painel do WHM, do lado direito, Configuração do Servidor -> Ajustar configurações, no campo de pesquisa, procurar por Apache non-SSL IP/port e alterar para 0.0.0.0:8080
2. Instalar o Varnish Cache;
Depois de alterado vamos iniciar a instalação do Varnish:
yum install varnish
Caso de algum erro iremos precisar instalar manualmente com o comando abaixo:
curl -s https://packagecloud.io/install/repositories/varnishcache/varnish5/script.rpm.sh | sudo bash
Ou com esse código:
#!/bin/bash unknown_os () { echo "Unfortunately, your operating system distribution and version are not supported by this script." echo echo "You can override the OS detection by setting os= and dist= prior to running this script." echo "You can find a list of supported OSes and distributions on our website: https://packagecloud.io/docs#os_distro_version" echo echo "For example, to force CentOS 6: os=el dist=6 ./script.sh" echo echo "Please email [email protected] and let us know if you run into any issues." exit 1 } curl_check () { echo "Checking for curl..." if command -v curl > /dev/null; then echo "Detected curl..." else echo "Installing curl..." yum install -d0 -e0 -y curl fi } detect_os () { if [[ ( -z "${os}" ) && ( -z "${dist}" ) ]]; then if [ -e /etc/os-release ]; then . /etc/os-release os=${ID} if [ "${os}" = "poky" ]; then dist=`echo ${VERSION_ID}` elif [ "${os}" = "sles" ]; then dist=`echo ${VERSION_ID}` elif [ "${os}" = "opensuse" ]; then dist=`echo ${VERSION_ID}` else dist=`echo ${VERSION_ID} | awk -F '.' '{ print $1 }'` fi elif [ `which lsb_release 2>/dev/null` ]; then # get major version (e.g. '5' or '6') dist=`lsb_release -r | cut -f2 | awk -F '.' '{ print $1 }'` # get os (e.g. 'centos', 'redhatenterpriseserver', etc) os=`lsb_release -i | cut -f2 | awk '{ print tolower($1) }'` elif [ -e /etc/oracle-release ]; then dist=`cut -f5 --delimiter=' ' /etc/oracle-release | awk -F '.' '{ print $1 }'` os='ol' elif [ -e /etc/fedora-release ]; then dist=`cut -f3 --delimiter=' ' /etc/fedora-release` os='fedora' elif [ -e /etc/redhat-release ]; then os_hint=`cat /etc/redhat-release | awk '{ print tolower($1) }'` if [ "${os_hint}" = "centos" ]; then dist=`cat /etc/redhat-release | awk '{ print $3 }' | awk -F '.' '{ print $1 }'` os='centos' elif [ "${os_hint}" = "scientific" ]; then dist=`cat /etc/redhat-release | awk '{ print $4 }' | awk -F '.' '{ print $1 }'` os='scientific' else dist=`cat /etc/redhat-release | awk '{ print tolower($7) }' | cut -f1 --delimiter='.'` os='redhatenterpriseserver' fi else aws=`grep -q Amazon /etc/issue` if [ "$?" = "0" ]; then dist='6' os='aws' else unknown_os fi fi fi if [[ ( -z "${os}" ) || ( -z "${dist}" ) ]]; then unknown_os fi # remove whitespace from OS and dist name os="${os// /}" dist="${dist// /}" echo "Detected operating system as ${os}/${dist}." } finalize_yum_repo () { echo "Installing pygpgme to verify GPG signatures..." yum install -y pygpgme --disablerepo='varnishcache_varnish5' pypgpme_check=`rpm -qa | grep -qw pygpgme` if [ "$?" != "0" ]; then echo echo "WARNING: " echo "The pygpgme package could not be installed. This means GPG verification is not possible for any RPM installed on your system. " echo "To fix this, add a repository with pygpgme. Usualy, the EPEL repository for your system will have this. " echo "More information: https://fedoraproject.org/wiki/EPEL#How_can_I_use_these_extra_packages.3F" echo # set the repo_gpgcheck option to 0 sed -i'' 's/repo_gpgcheck=1/repo_gpgcheck=0/' /etc/yum.repos.d/varnishcache_varnish5.repo fi echo "Installing yum-utils..." yum install -y yum-utils --disablerepo='varnishcache_varnish5' yum_utils_check=`rpm -qa | grep -qw yum-utils` if [ "$?" != "0" ]; then echo echo "WARNING: " echo "The yum-utils package could not be installed. This means you may not be able to install source RPMs or use other yum features." echo fi echo "Generating yum cache for varnishcache_varnish5..." yum -q makecache -y --disablerepo='*' --enablerepo='varnishcache_varnish5' } finalize_zypper_repo () { zypper --gpg-auto-import-keys refresh varnishcache_varnish5 } main () { detect_os curl_check yum_repo_config_url="https://packagecloud.io/install/repositories/varnishcache/varnish5/config_file.repo?os=${os}&dist=${dist}&source=script" if [ "${os}" = "sles" ] || [ "${os}" = "opensuse" ]; then yum_repo_path=/etc/zypp/repos.d/varnishcache_varnish5.repo else yum_repo_path=/etc/yum.repos.d/varnishcache_varnish5.repo fi echo "Downloading repository file: ${yum_repo_config_url}" curl -sSf "${yum_repo_config_url}" > $yum_repo_path curl_exit_code=$? if [ "$curl_exit_code" = "22" ]; then echo echo echo -n "Unable to download repo config from: " echo "${yum_repo_config_url}" echo echo "This usually happens if your operating system is not supported by " echo "packagecloud.io, or this script's OS detection failed." echo echo "You can override the OS detection by setting os= and dist= prior to running this script." echo "You can find a list of supported OSes and distributions on our website: https://packagecloud.io/docs#os_distro_version" echo echo "For example, to force CentOS 6: os=el dist=6 ./script.sh" echo echo "If you are running a supported OS, please email [email protected] and report this." [ -e $yum_repo_path ] && rm $yum_repo_path exit 1 elif [ "$curl_exit_code" = "35" -o "$curl_exit_code" = "60" ]; then echo echo "curl is unable to connect to packagecloud.io over TLS when running: " echo " curl ${yum_repo_config_url}" echo echo "This is usually due to one of two things:" echo echo " 1.) Missing CA root certificates (make sure the ca-certificates package is installed)" echo " 2.) An old version of libssl. Try upgrading libssl on your system to a more recent version" echo echo "Contact [email protected] with information about your system for help." [ -e $yum_repo_path ] && rm $yum_repo_path exit 1 elif [ "$curl_exit_code" -gt "0" ]; then echo echo "Unable to run: " echo " curl ${yum_repo_config_url}" echo echo "Double check your curl installation and try again." [ -e $yum_repo_path ] && rm $yum_repo_path exit 1 else echo "done." fi if [ "${os}" = "sles" ] || [ "${os}" = "opensuse" ]; then finalize_zypper_repo else finalize_yum_repo fi echo echo "The repository is setup! You can now install packages." } main
3. Editar as configurações do Varnish Cache;
vi /etc/sysconfig/varnish
Iremos configurar o Varnish para escutar a porta 80, ficando assim: VARNISH_LISTEN_PORT=80
vi /etc/varnish/default.vcl
Com o comando acima, iremos configurar o proxy, com o IP dedicado do seu servidor e a porta do servidor web (apache), deverá ficar da seguinte forma:
backend default { .host = "184.82.165.4"; .port = "8080"; }
4. Iniciar o serviço do Varnish Cache;
chkconfig varnish on service varnish start
5. Fim :D;
Se você chegou até aqui é porque deu tudo certo, ou ao menos esperamos né, hehe…
Para monitorar o seu Varnish Cache utilize o comando:
varnishstat
Dúvidas?
Espero que este tutorial tenha ajudado você a instalar o Varnish Cache no CPANEL (WHM). Como sempre, se você achou esta postagem útil, inscreva-se em nossa newsletter gratuita ou siga-nos no Instagram @douglasvillar, conecte comigo no Linkedin e curta nossa página no Facebook.