GCP: install mod_headers - wordpress

It seems my server setup does not support headers in .htaccess
When I try to add this below code in .htaccess file, My site breaks.
<filesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|svg|js|css|swf)$">
Header set Cache-Control "max-age=84600, public"
</filesMatch>
So my question is how do I install mod_headers in the GCP where my apache server is.
For the httpd.conf file: following is the tutorial
https://www.onepagezen.com/add-expires-headers-wordpress-bitnami/
But my server uses apache2.conf file instead of the httpd.conf
Can't find the solution, any inputs?

Man, I've had the same issue.
First off, next time you setup a wordpress via GCP, try out KUSANAGI OS, incredibly fast.
You need to make sure you put the FilesMatch config in the right spot.
httpd/apache2 and the way they load conf seems to be rather complex, so I can't provide a specific reason as why it doesn't work when you place it.
BUT!
according to: https://serverfault.com/questions/648262/filesmatch-configuration-to-restrict-file-extensions-served
you might have a chance by trying to place it in v_host.conf, as per:
The FilesMatch directive is applied against the "directory" hit prior to the mod-dir
DirectoryIndex directive assignment to "index.htm." As a result, a "null" condition
must pass the test in order for the request to be handled. With that, this set up
(all the same except for this change to the V_HOST.conf file) works:...

Related

Publishing or uploading failed. Error message: "The response is not a valid JSON response"

Today I got an issue in WordPress. When I try to create a new page and uploading a new image in the WordPress admin section, I try to find out the solution, but I didn't get it... So after sanding an hour I got a solution...
Error
"Publishing failed. Error message: The response is not a valid JSON response."
Go to settings>permalinks. Select "Post name" and save.
Try updating your posts/pages. If it doesnot work, try selecting another option in the settings>permalinks.
Right now, you can use the Classic Editor plugin for fixing this issue.
The answer for the error is the editor I didn't know, but there is a new editor issue. If you are getting the same issue, then please use the below plugin. For fixing these issues, I'm doing R&D on this issue. If I get an exit solution, then will I make an update soon...
I had the same problem on a local dev environment and found the issue was due to rewrite permissions. Make sure your .htaccess file has the proper permission.
sudo chmod 755 .htaccess
After permissions are set, save your permalink settings. If the problem still exists, make sure mod_rewrite is enabled. The following will work for apache2 on Ubuntu.
sudo a2enmod rewrite
sudo systemctl apache2 restart
If still not working, your apache config is probably too strict. The following should do the trick for apache2 on Ubuntu. Edit /etc/apache2/apache2.conf and look for the root directory block. It's normally the one with /var/www like below. You will probably see AllowOverride None. Just change it to All like below for your local, but you probably want to do some research and be more secure on a production server.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
After the change is made do another restart on apache and all should be good.
sudo systemctl apache2 restart
Changing the permalink settings as mentioned previously fixed the problem for me. If you keep "Post name" as the permalink setting, then the .htaccess file needs to be writable by wordpress.
Alternatively the below can be pasted at the bottom of .htaccess file. Mod_rewrite changes are executed from the bottom of the file first.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Maybe you have a security issue with mixed content (HTTP and https URLs).
Step-1
1. 1. Check your Wordpress URL. Go to Settings -> permalink then click plain and save a page. then remove error
Step-2
Check your Wordpress URL. Go to Settings -> General
Change Wordpress Adress and Site Adress to https://
This solved the problem for me
Navigate to Settings > Permalinks. Select the permalink structure to Post name and save. Now try saving your post/page.
Here I have shared the screen-shot for your reference.
The problem should have been resolved.
Once your issue is resolved then you can select an old option in Permalinks if you want.
Note: If you have already selected Post Name then you need to select other option for reset permalink.
Go to /etc/nginx/sites-available/
open default file --> sudo vi default or sudo nano default
Add Below line to the location:
Add the comment :
#try_files $uri $uri/ =404;
Add this line : try_files $uri $uri/ /index.php?$args;
See the screenshot below described with red line :
I just installed classic editor plugin and it sorted out the “The response is not a valid JSON response” problem.
I had the same "not a valid JSON response" error when trying to publish my content. WordPress seems to do a JSON post when publishing a new post/page so I checked the network tab in my Developer Tools. If you check the "response" tab for this JSON call you might see some more details about this invalid JSON response.
In my case (yours might be different) some deprecated debug message was outputted before the actual JSON data and messed up the response. After fixing the deprecated message publishing worked again.
<br />
<b>Deprecated</b>: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in <b>Unknown</b> on line <b>0</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent in <b>Unknown</b> on line <b>0</b><br />
{__NORMAL_JSON_DATA_HERE__}
Now, here there are two server blocks, first one is running on port 80.
for second server block there is a port 443 where you should implement the below code.
server{
#ssl configuration
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404; // comment this block
try_files $uri $uri/ /index.php$is_args$args;
}
}
Now exit and restart nginx server.
~$ sudo service nginx restart
you are done. enjoy.
In My case, it was a Firewall issue. A ModSecurity firewall rule was triggered for the website. I contacted the hosting provider (NameCheap), they identified the issue and then whitelisted the triggered rule on the server. Then the problem was resolved.
Looks like this still has not been fixed.
A workaround that worked for me (posting in case it works for someone else too) - in my case I was using an HTML block which was causing the issue. What I did was to add a Paragraph block > Edit as HTML. It resolved the issue for now. It's a shame that WP didn't look into it yet, seems like it's been happening for a while.
I had this same error and the cause in my case was a shortcode used in the page and the function that defined the shortcode was using 'echo' to output the data rather than using 'return'
I'd been getting the same error message when adding and/or updating pages, in addition to various other random errors. I was getting the errors on both my local development server and my webhost. When changing to one of the default themes (e.g. twenty nineteen), the errors would go away. For me, the problem turned out to be in my page template files. I'd noticed that when choosing a template for a newly created page, I'd have multiple choices with the same name.
I have several page template files (e.g. about-page.php, contact-page.php, services-page.php, etc.). When creating these files, since most of these pages were similar, I would just copy/paste from an existing file to make a new xxxx-page.php file. However, in some of those copied files (not all), I'd forgotten to modify the 'Template Name' (at the top of the file).
After I'd gone through and made sure all of my template files had unique names, the JSON error went away. All of my other errors disappeared as well. I'm using Wordpress 5.4.
My issue was .htaccess related. I got the hint from LucasBr's comment about a ".htaccess configuration problem". Since I had copied the site to my local dev environment. I needed to update the custom mod_rewrite settings to match my setup.
The important lines were:
RewriteBase /
and
RewriteRule . /index.php [L]
They had previously been set to access a sub-directory site.
I have some echo in some of my custom plugins, so this was also causing this issue.
I had the persisting issues. Before I could find the solution, I replaced PHP 8, as this was custom PHP Installation.
But nothing worked.
The solution was found during Drupal Installation.
Enable Opcache(only for Drupal though)
Allow Mod rewrite, Allow all(in my httpd.ini file there were three occurrences of Mod Rewrite,
And they were changed to None to All again none all this was small letter, and final one None to All
Restart httpd with httpd -k restart (CMD as Administrator)
This resolved.
I encountered the same kind of problem for a different reason with one of my client and never saw it as a potential solution.
If you enabled a W.A.F (like cloudflare or similar), one of the side effects can be this error, so be sure to disable to disable you waf if none of the other solutions are working.
If you use mod security 3 with Wordpress, you might get this message:
Publishing failed. The response is not a valid JSON response.
To remove it, you would need to add this rule to your exclusions file called
REQUEST-900-EXCLUSION-RULES-BEFORE-CRS
as follows:
# Remove Publishing failed. The response is not a valid JSON response.
SecRule REQUEST_HEADERS:Host "example.com" \
"id:1025,\
phase:2,\
pass,\
nolog,\
chain"
SecRule REQUEST_URI "#beginsWith /wp-json/wp/v2" \
" ctl:ruleRemoveById=933210,ctl:ruleRemoveById=949110"
You might need to change the rule id and change example.com to your domain name. After that, do this:
sudo nginx -t
if no problems were detected, restart nginx:
sudo service nginx restart
sudo systemctl restart nginx
This is a permalinks error.
Do these steps to fix it (depending upon the cause in your scenario) as explained here: https://wordpress786.com/updating-failed-error-message-the-response-is-not-a-valid-json-response/ :
go to settings > permalinks and save them once again
if you're on nginx, go to site nginx conf file and make sure that it contains the rules for wordpress permalinks (usual nginx configurationn for wordpress, ask me if confused)
restart nginx if you make any edits to conf file
If you face issue on image upload follow this: https://navinrao.com/the-response-is-not-a-valid-json-response/
I was getting this issue while updating normal text too.
The issue I face was due to having javascript:void(0) keyword inside my link () .
Please be sure if your content has keyword containing javascript
before updating.
<i class="fa fa-linkedin" aria-hidden="true"></i>
After I remove javascript:void(0) ; it works fine.
Have a nice time :)
Try removing any redirects to the WordPress site including subdomains.

What happens if no mime.types is included in nginx.conf?

I have a custom nginx.conf file that I start nginx with using the cli, for example nginx -c /my/path/nginx.conf.
I have found that if I take the include /my/path/mime.types from the custom nginx.conf file, that the server still starts up fine, and webpages seem to load normally with no apparent errors.
I have been researching nginx directive priority, but I cannot see any reason that the default mime.types might be getting included. Is it safe to remove the custom mime.types include?
(I should clarify that there is nothing special about the contents of /my/path/mime.types. For the purposes of this question, consider it to be effectively the same as the contents of the default file.)
Actually i did some tests and concluded that if you comment out the mime.types line in the /etc/nginx/nginx.conf file
# include /etc/nginx/mime.types
And restart nginx
sudo service nginx restart
And you clear the browser cache before accessing again your page, you will notice that nginx will not take advantage of mime.types configuration (of course), and will NOT render correctly the media type of your content.
The following example, shows how styles.css is rendered (Content-Type: application/octet-stream) instead of text/css
To sum up, yes mime.types is required for nginx to render the right media type of content.

Setting up an Apache VirtualHost for Symfony

Admittedly I am utterly new to Symfony and I am irritated.
I am using the docu here: [0]. After setting everything up with "symfony/apache-pack" I get the ".htaccess" file with heaps of stuff in the "/public" directory. Now I wanted to be smart and moved all the rewrite rules to a proper VirtualHost and deleted the "htaccess" file. BANG, the required script files are not being loaded from the vendor directory and not style appear on the website. After killing all rewrite rules from the VirtualHost we website is up and running again.
Because of this sentence "A performance improvement can be achieved by moving the rewrite rules from the .htaccess file into the VirtualHost block of your Apache configuration and then changing AllowOverride All to AllowOverride None in your VirtualHost block." I am confused now since it suggests that I imperatively need rewrite rules for Symfony.
Note that I changed to AllowOverride None.
Can anyone shed some light onto this. I somehow feel that I am doing something dead wrong.
[0] https://symfony.com/doc/current/setup/web_server_configuration.html#web-server-apache-mod-php

Wordpress Apache2 mod_speling not working

I am in the process of migrating my site to Wordpress from IIS.
I have run into an issue:
I have URL's that are a mix of upper and lower case that reference images. These URL's are getting a 404 because the case of the URL does not match the case of the file location.
For example, in my article I have a URL:
https://aaa.bbb.com/wp-content/migrate/ABC/abc.png
The file this is referencing is: /var/www/aaa/wp-content/migrate/abc/abc.png
When I change the URL to the same case as the file, it works, however how can I make it ignore the case of the folder on disk?
I tired the mod_speling module by putting this into /etc/apache2/apache2.conf but this just does not work for me. Also tried putting this into .htaccess in the root of my web directory but that made no difference.
<IfModule mod_speling.c>
CheckSpelling On
CheckCaseOnly On
</IfModule>
I have read various threads on here but I'm completely stuck.
I managed to find a work around to this.
I ended up writing a rewrite rule in the VirtualHost file to rewrite everything to lowercase apart from anything within the directory of assets imported from Windows.

Expires headers on an Amazon EC2 with Bitnami Wordpress

I'm working with a client who has a Wordpress installation (via Bitnami) set up on an Amazon EC2. I didn't put this together, and for background I am a Front-end Developer who is used to cPanel and has limited knowledge for the command line.
I'm trying to modify the expires headers to improve the speed of the client's site. Following this tutorial (http://www.theitbaby.com/wordpress/2013/10/14/optimizing-bitnami-for-pagespeed-insights-and-gtmetrix/) I was looking for the htaccess.conf file in my opt/bitnami/apps/wordpress/conf/ folder, however none existed. Do I make my changes in the httpd-app.conf file?
Alternatively I saw a few other resources out there with a line to include the htaccess.conf file added to httpd-app.conf - something like
Include /opt/bitnami/apps/wordpress/conf/htaccess.conf
Do I add this to the httpd-app.conf file and then create htaccess.conf and work there? Sorry for the confusion, just looking for some direction as I try to do my typical performance boost work.
So the option what you are describing: creating and htaccess.conf file and then adding it as an Include statement in the httpd-app.conf file under /opt/bitnami/apps/wordpress/conf/ is correct.
Make sure your Include statement is within the Directory directive and in the htaccess.conf file you don't need to have the Directory directive:
<Directory "/opt/bitnami/apps/wordpress/htdocs">
...
Include /opt/bitnami/apps/wordpress/conf/htaccess.conf
...
</Directory>
You can also add the pagespeed configs directly to the http-app.conf file if you'd like to inside the Directory directive too.
There's another option, but not necessarily popular if you have access to the main apache configuration files. You can create a .htaccess file under /opt/bitnami/apps/wordpress/htdocs and put your pagespeed statements there. In this care you need the following statement preconfigured by the sysadmin in the http-app.conf file.
<Directory "/opt/bitnami/apps/wordpress/htdocs">
...
AllowOverride All
...
</Directory>
You can read more about .htaccess here: http://httpd.apache.org/docs/current/howto/htaccess.html
Hope this helps.

Resources