• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » Setup Varnish Cache with Apache and TLS/SSL Termination

By Abhishek Ghosh May 12, 2023 8:21 am Updated on May 12, 2023

Setup Varnish Cache with Apache and TLS/SSL Termination

Advertisement

Varnish demands experience and keeping an eye on your server. For that reason, test this guide on a non-production, non-money-making site with SSL. It will cost $5/month or less to host such a WordPress site on a VPS. If you manage the HTTPS site with Varnish for a couple of months, you’ll gain some confidence.

If ever Varnish started to throw errors on your production server, you have to quickly change ports, and settings files and gracefully restart Apache. That will revert the system. The Apache modules are super stable even when configured by a novice. You can always hire an expert to configure your server. But even in such cases, you have to know how to quickly revert to the last working state only with Apache.

 

The Initial Steps Before Setup of Varnish Cache

 

For this guide, your website should be correctly configured and working with LAMP stack and SSL/TLS certificate. We have tried to gracefully restart Apache whenever possible. So, for a production site, the front end will face minimal downtime.

Advertisement

---

Load all the necessary Apache modules:

Vim
1
2
3
4
5
6
7
8
9
10
a2enmod rewrite
a2enmod headers
a2enmod ssl
a2enmod proxy
a2enmod proxy_balancer
a2enmod proxy_http
# configtest and graceful restart
apachectl configtest
sudo /etc/init.d/apache2 reload
# service apache2 restart

We will create two VirtualHost files for this configuration. We will go to /etc/apache2/sites-available/ and start creating the first new configuration, which we can say is ext-https (it is external):

Vim
1
2
/etc/apache2/sites-available/
nano ext-https.conf

The content of this file will be like this, you can add more directives for SSL/TLS:

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<VirtualHost *:443>
        ServerName example.com
        ErrorLog              /var/log/apache2/ext-https_error.log
        CustomLog             /var/log/apache2/ext-https_access.log combined
 
        SSLCertificateFile    /etc/ssl/example.com.crt
        SSLCertificateKeyFile /etc/ssl/example.com.key
        # for wordpress
        RequestHeader set X-Forwarded-Proto expr=%{REQUEST_SCHEME}
 
        ProxyPreserveHost       On
        ProxyPass               / http://127.0.0.1:8080/
        ProxyPassReverse        / http://127.0.0.1:8080/
</VirtualHost>

The above configuration is the main for the frontend header. Next, we will create the second new configuration, which we can say is int-https (it is internal):

Vim
1
2
/etc/apache2/sites-available/
nano int-https.conf

The content of this file will be like this:

Vim
1
2
3
4
5
6
7
8
<VirtualHost 127.0.0.1:8181>
        ServerName    example.com
        DocumentRoot  /var/www/example
        ErrorLog      /var/log/apache2/int-http_error.log
        CustomLog     /var/log/apache2/int-http_access.log combined
        #for wordpress
        SetEnvIf X-Forwarded-Proto https HTTPS=on
</VirtualHost>

Next, we will edit /etc/apache2/ports.conf so that the internal VirtualHost file can listen on localhost on port 8181:

Vim
1
nano /etc/apache2/ports.conf

Find and change the Listen directive:

Vim
1
Listen 127.0.0.1:8181

At this point, Apache2 is working with the old configuration and the site is running fine.

Setup Varnish Cache with Apache and TLS SSL Termination
 

Steps to Setup Varnish Cache

 

Install Varnish from the Ubuntu repo:

Vim
1
2
3
apt-get update
apt-get upgrade
apt-get install varnish apache2

Open /lib/systemd/system/varnish.service :

Vim
1
nano /lib/systemd/system/varnish.service

Find this line:

Vim
1
ExecStart=/usr/sbin/varnishd -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

Change to:

Vim
1
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

We have changed the port.

Open /etc/varnish/default.vcl :

Vim
1
nano /etc/varnish/default.vcl

Find this:

Vim
1
2
3
4
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

Change to:

Vim
1
2
3
4
backend default {
    .host = "127.0.0.1";
    .port = "8181";
}

Everything done. Now activate, and restart all the things:

Warning: Please disable any other virtual host configuration which is/are using port 80 and port 443 with a2dismod command before running these commands.

Vim
1
2
a2ensite external-https
a2ensite internal-http

And finally:

Vim
1
2
3
4
5
systemctl daemon-reload
systemctl restart varnish
apachectl configtest
# if throws no error
sudo /etc/init.d/apache2 reload

The front end should be working fine with Varnish. You can check the logs:

Vim
1
2
3
varnishlog
cd /var/log/apache2
tail -f ext-https_access.log -f int-http_access.log

We can use curl -v https://example.com command to check the header.

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Setup Varnish Cache with Apache and TLS/SSL Termination

  • Join/Merge Multiple Log Files For Big Data Analysis

    Here Are The Ways To Join/Merge Multiple Log Files For Big Data Analysis, Store Them To OpenStack Based Cloud Storage And Delete Old Files.

  • Get Started with Varnish Cache

    With Cloud Servers, it is easier to spin up a separate server to work as front end. Let us get started with Varnish Cache setup with commands.

  • How To Install Apache2 on Ubuntu 18.04 With Let’s Encrypt, HTTP/2, HSTS

    Here is Detailed Guide on How To Install Apache2 on Ubuntu 18.04 With Let’s Encrypt, HTTP/2, HSTS With Commands and Configurations For Most Secured Setup.

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy