Setting Up Varnish on CentOS 7 with Nginx, PHP-FPM, and SSL
As an experienced developer, I understand the challenges of setting up complex caching systems like Varnish, especially when dealing with multiple components such as CentOS 7, Nginx, PHP-FPM, and SSL. In this post, we’ll walk through the process step-by-step to ensure your Magento site runs smoothly and efficiently.
Prerequisites
Before starting, make sure you have:
- A clean CentOS 7 server
- Access via SSH or console
- The necessary permissions (sudo access)
Step 1: Install Varnish on CentOS 7
First, update your package lists and install Varnish:
# Update the packages
yum -y update
# Install Varnish
yum -y install varnish
Check the status to ensure it’s running:
systemctl status varnish
Step 2: Configure Varnish for Nginx and PHP-FPM
Edit the Varnish configuration file located at /etc/varnish/varnish.default.vcl.
Replace the contents with the following, making sure to adjust the backend address to match your PHP-FPM server:
backend default {
.host = "127.0.0.1";
.port = "9000";
# This is the secret for the cache control
.secret = "";
}
sub vcl_recv {
if (req.url ~ "\.(js|css|png|jpg|jpeg|ico|gif)$") {
set beresp.ttl = 3600s;
return deliver;
}
# Set the backend host to get the content
set req.backend = default;
# Pass anything with a cookie to the backends
if (req.http.cookie ~ "PHPSESSID") {
return pass;
}
# If no cookies are present, cache it for an hour
set beresp.ttl = 3600s;
return deliver;
}
Save and exit. Now, restart Varnish:
systemctl restart varnish
Step 3: Configure Nginx to Handle SSL
Edit your Nginx configuration file located at /etc/nginx/conf.d/default.conf.
Add or modify the server block to handle SSL termination:
server {
listen 80;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl default_server;
# SSL Configuration goes here
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Replace the SSL certificate paths with your own.
Step 4: Configure PHP-FPM to Listen on Port 9000
Edit the PHP-FPM configuration file located at /etc/php-fpm.d/www.conf.
Change the listen directive to listen on port 9000:
listen = /var/run/php-fpm/php-fpm.sock
;listen = 127.0.0.1:9000
Save and restart PHP-FPM:
systemctl restart php-fpm
Step 5: Test the Setup
Now, access your website using the SSL URL.
Varnish should cache static content for an hour while passing dynamic content to the PHP-FPM server. The Nginx reverse proxy handles SSL termination and forwards requests to Varnish on port 8080.
Remember that for a multi-store Magento site, you may need to configure the Vhosts accordingly in your Nginx configuration file.
Conclusion
Setting up Varnish with CentOS 7, Nginx, PHP-FPM, and SSL can be complex, but following these steps ensures a robust and efficient caching system. If you find yourself needing more advanced configurations or facing specific issues, feel free to reach out to the experts at Laravel Company. We specialize in Magento performance optimization and would be happy to assist you further.
Happy optimizing!