We are happy to announce that Vesta is back under active development as of 25 February 2024. We are working on v1 candidate and expect to engage more with the community over the coming months. We are committed to open source, and we encourage contributors to help us build the future of Vesta.
[HOWTO] FASTCGI CACHE WORDPRESS Topic is solved
[HOWTO] FASTCGI CACHE WORDPRESS
NGINX + PHP-FPM + WORDPRESS + NGINX CACHE SNIPER
I found a good solution to cache wordpress.
Go to /usr/local/vesta/data/templates/web/php-fpm and make copy socket.tpl to www-data-socket.tpl
Open terminal:
Find:
and change with:
Go to /usr/local/vesta/data/templates/web/nginx/php-fpm and make copy wordpress2_rewrite.tpl, wordpress2_rewrite.stpl to wordpress2_rewrite_nginx_sniper.tpl, wordpress2_rewrite_nginx_sniper.stpl
and after
At the beginning add
Next, move the location directive that passes PHP requests to php-fpm. Inside "location ~ .php$ { }" add the following lines.
Some dynamic content such as authentication required pages shouldn't be cached.
Such content can be excluded from being cached based on server variables.
Here is a configuration which must be used in the server{ } context.
To apply the "$no_cache" variable to the appropriate directives, place the following lines inside location ~ .php$ { }
Make your cache directory
Open terminal:
and change owner to youruser web folder
Open terminal: save, update and restart
Open Wordpress Plugins (yourdomain.php/wp-admin/plugins.php)
Add and activate Cache Sniper for Nginx
And set your fastcgi_cache_path to plugin
Cache Path settings /var/cache/nginx/fastcgi_cache/yourdomain.com
Cache Levels settings 1:2
You can controll if you cache or not, and how long you cache page by page
Homepage cache already controlled by plugin last update.
I found a good solution to cache wordpress.
Cache Sniper for Nginx works as it should only if script have permissions to cache files and folders. To make it works you have to change the web user (%user%) to www-data which is the default user that nginx uses. Then your PHP script run through php-fpm will have the appropriate permissions and will be able to delete the cache.Nginx includes a FastCGI module which has directives for caching dynamic content that are served from the PHP backend. Setting this up removes the need for additional page caching solutions like reverse proxies (think Varnish) or application specific plugins (Super Cache). Content can also be excluded from caching based on the request method, URL, cookies, or any other server variable.
Go to /usr/local/vesta/data/templates/web/php-fpm and make copy socket.tpl to www-data-socket.tpl
Open terminal:
Code: Select all
cd /usr/local/vesta/data/templates/web/php-fpm/
cp socket.tpl www-data-socket.tpl
sudo chmod 664 www-data-socket.tpl
nano www-data-socket.tpl
Code: Select all
user = %user%
group = %user%
listen.owner = %user%
listen.group = www-data
and change with:
Code: Select all
user = www-data
group = www-data
listen.owner = %user%
listen.group = www-data
listen.mode = 0660
Code: Select all
cd /usr/local/vesta/data/templates/web/nginx/php-fpm/
cp wordpress2_rewrite.tpl wordpress2_rewrite_nginx_sniper.tpl
cp wordpress2_rewrite.stpl wordpress2_rewrite_nginx_sniper.stpl
sudo chmod 664 wordpress2_rewrite_nginx_sniper.*
Code: Select all
nano wordpress2_rewrite_nginx_sniper.tpl
Code: Select all
nano wordpress2_rewrite_nginx_sniper.stpl
Code: Select all
fastcgi_cache_path /var/cache/nginx/fastcgi_cache/%domain%/ levels=1:2 keys_zone=%domain%:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
add_header X-Cache $upstream_cache_status;
Code: Select all
fastcgi_cache_valid 200 301 302 60m;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_cache %domain%;
Such content can be excluded from being cached based on server variables.
Here is a configuration which must be used in the server{ } context.
Code: Select all
#Cache everything by default
set $skip_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
#Don't cache if the URL contains a query string
if ($query_string != "") {
set $skip_cache 1;
}
# Don’t cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
# Don’t use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
# Skip cache on WooCommerce pages
if ($request_uri ~* "/store.*|/cart.*|/my-account.*|/checkout.*|/addons.*") {
set $skip_cache 1;
}
Code: Select all
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
All together looks like:Show
fastcgi_cache_path /var/cache/nginx/fastcgi_cache/%domain%/ levels=1:2 keys_zone=%domain%:200m inactive=120m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
add_header X-Cache $upstream_cache_status;
server {
listen %ip%:%web_port%;
server_name %domain_idn% %alias_idn%;
root %docroot%;
index index.php index.html index.htm;
access_log /var/log/nginx/domains/%domain%.log combined;
access_log /var/log/nginx/domains/%domain%.bytes bytes;
error_log /var/log/nginx/domains/%domain%.error.log error;
location = /favicon.ico {
log_not_found off;
access_log off;
}
#Cache everything by default
set $skip_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
#Don't cache if the URL contains a query string
if ($query_string != "") {
set $skip_cache 1;
}
# Don’t cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
# Don’t use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
# Skip cache on WooCommerce pages
if ($request_uri ~* "/store.*|/cart.*|/my-account.*|/checkout.*|/addons.*") {
set $skip_cache 1;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_pass %backend_lsnr%;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_cache_valid 200 301 302 60m;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_cache %domain%;
}
}
error_page 403 /error/404.html;
error_page 404 /error/404.html;
error_page 500 502 503 504 /error/50x.html;
location /error/ {
alias %home%/%user%/web/%domain%/document_errors/;
}
location ~* "/\.(htaccess|htpasswd)$" {
deny all;
return 404;
}
location /vstats/ {
alias %home%/%user%/web/%domain%/stats/;
include %home%/%user%/web/%domain%/stats/auth.conf*;
}
include /etc/nginx/conf.d/phpmyadmin.inc*;
include /etc/nginx/conf.d/phppgadmin.inc*;
include /etc/nginx/conf.d/webmail.inc*;
include %home%/%user%/conf/web/nginx.%domain_idn%.conf*;
}
fastcgi_cache_key "$scheme$request_method$host$request_uri";
add_header X-Cache $upstream_cache_status;
server {
listen %ip%:%web_port%;
server_name %domain_idn% %alias_idn%;
root %docroot%;
index index.php index.html index.htm;
access_log /var/log/nginx/domains/%domain%.log combined;
access_log /var/log/nginx/domains/%domain%.bytes bytes;
error_log /var/log/nginx/domains/%domain%.error.log error;
location = /favicon.ico {
log_not_found off;
access_log off;
}
#Cache everything by default
set $skip_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
#Don't cache if the URL contains a query string
if ($query_string != "") {
set $skip_cache 1;
}
# Don’t cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
# Don’t use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
# Skip cache on WooCommerce pages
if ($request_uri ~* "/store.*|/cart.*|/my-account.*|/checkout.*|/addons.*") {
set $skip_cache 1;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_pass %backend_lsnr%;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_cache_valid 200 301 302 60m;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_cache %domain%;
}
}
error_page 403 /error/404.html;
error_page 404 /error/404.html;
error_page 500 502 503 504 /error/50x.html;
location /error/ {
alias %home%/%user%/web/%domain%/document_errors/;
}
location ~* "/\.(htaccess|htpasswd)$" {
deny all;
return 404;
}
location /vstats/ {
alias %home%/%user%/web/%domain%/stats/;
include %home%/%user%/web/%domain%/stats/auth.conf*;
}
include /etc/nginx/conf.d/phpmyadmin.inc*;
include /etc/nginx/conf.d/phppgadmin.inc*;
include /etc/nginx/conf.d/webmail.inc*;
include %home%/%user%/conf/web/nginx.%domain_idn%.conf*;
}
Open terminal:
Code: Select all
mkdir -p /var/cache/nginx/fastcgi_cache
chown -R www-data /var/cache/nginx/fastcgi_cache
chmod 700 /var/cache/nginx/fastcgi_cache
Code: Select all
chown -R www-data:www-data /home/youruser
Code: Select all
v-update-web-templates
service nginx configtest
service nginx restart
systemctl restart php7.2-fpm
Add and activate Cache Sniper for Nginx
And set your fastcgi_cache_path to plugin
Cache Path settings /var/cache/nginx/fastcgi_cache/yourdomain.com
Cache Levels settings 1:2
You can controll if you cache or not, and how long you cache page by page
Homepage cache already controlled by plugin last update.
Good luck with everything!So, open index.pxp and put in the beginning to be sure that your main page cache renew every 10 min.
Code: Select all
<?php if (is_front_page() ) { header("X-Accel-Expires: 600"); // cache main page for a while the big 10 minutes } ?> <html> <body> ... ...
Last edited by valentine on Thu Sep 20, 2018 10:48 am, edited 14 times in total.
Re: [HOWTO] FASTCGI CACHE WORDPRESS
Can you explain this line
Code: Select all
Next, move the location directive that passes PHP requests to php5-fpm. Inside "location ~ .php$ { }" add the following lines.
Re: [HOWTO] FASTCGI CACHE WORDPRESS
Of course, you can check spoiler, where all lines together. But I have php 7.2 onboard.
Last edited by valentine on Mon Sep 10, 2018 11:22 am, edited 1 time in total.
Re: [HOWTO] FASTCGI CACHE WORDPRESS
I am sorry for my short question before what i wanted to ask was in which file do we need to put this code?
Re: [HOWTO] FASTCGI CACHE WORDPRESS
vesta php-fpm templates: wordpress2_rewrite_nginx_sniper.tpl and wordpress2_rewrite_nginx_sniper.stpl
Code: Select all
cd /usr/local/vesta/data/templates/web/nginx/php-fpm/
cp wordpress2_rewrite.tpl wordpress2_rewrite_nginx_sniper.tpl
cp wordpress2_rewrite.stpl wordpress2_rewrite_nginx_sniper.stpl
sudo chmod 664 wordpress2_rewrite_nginx_sniper.*
Re: [HOWTO] FASTCGI CACHE WORDPRESS
UPDATED. U MAKE MISTAKE IN MANUAL. $skip_cache/$no_cache
Re: [HOWTO] FASTCGI CACHE WORDPRESS
UPDATE. ADD BECAUSE viewtopic.php?f=29&t=15764&p=64967&hili ... che#p64967
Code: Select all
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
Re: [HOWTO] FASTCGI CACHE WORDPRESS
Also use this for browser cache:
Code: Select all
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js|eot|otf|woff|woff2|ttf|ogg)$ {
expires max;
fastcgi_hide_header "Set-Cookie";
}
Re: [HOWTO] FASTCGI CACHE WORDPRESS
Cache Sniper for Nginx UPDATE
So, last step no need anymore.1.0.4.1
Fixed a bug that caused wp_insert_comment to fail.
1.0.4
Automatically clearing the homepage cache on content create/update/delete.
open index.pxp and put in the beginning to be sure that your main page cache renew every 10 min.
Code: Select all
<?php if (is_front_page() ) { header("X-Accel-Expires: 600"); // cache main page for a while the big 10 minutes } ?> <html> <body> ... ...
Re: [HOWTO] FASTCGI CACHE WORDPRESS
You want your site to be fast, so you added caching… but now you have a new challenge that you have content that shouldn’t be caching such as ads, feeds, rotating ads, videos, shopping carts and more..
With No Cache AJAX Widgets, you simply drag and drop the new AJAX powered widgets to any widget area/s, add your content and you are done. No more messing with your theme files, functions.php, fragmented caching, mfunc and others or excluding an entire page from caching.