How to Enable Gzip Compression for a website on cPanel Server
How to Enable Gzip Compression for a website on cPanel Server
What is Gzip compression ?
When Gzip compression is enabled the data will be compressed by the webserver before sending it to the visitors browser, it will help in better speed of the website. Gzip compression for the website can enabled from cPanel (Frontend). Before enabling Gzip compression you must contact your webhosting provider and ask them whether apache mod_deflate module is enabled. mod_deflate (Earlier known as mod_gzip) apache module is required for Gzip compression.
You must rebuild apache to enable mod_deflate module.
Run the below command to rebuild apache on easyapache 3 servers :
/scripts/easyapache
Advantages of Gzip Compression?
1. It will improve the site speed. Compressing HTML, JS, CSS etc will speed up your website.
Images like JPEG, JPG, PNG are already in a compressed format so further compressing will slow the website. There won’t be much difference if images are further compressed. You can use any online image resizer to reduce image size instead of compressing it.
2. Pages will be compressed by the webservers before sending to site visitor so bandwidth usage will be low. It saves bandwidth.
Steps to enable Gzip compression for a website on cPanel server
Make sure mod_deflate module is enabled in apache before doing the below steps.
1. Login to cPanel using cPanel username and Password
link to access cPanel : https://YourDomain.com:2083
OR
https://YourServerIP:2083
2. In the cPanel Home page search for “Optimize website” like shown in below picture
3. Click on “Optimize Website” under “software”
cPanel >> Software >> Optimize Website
4. In the next page under “Compress Content” you can see the below three options
Disabled
Compress All Content
Compress the specified MIME types.
5. Select the radio button “Compress All Content” and click on “Update Settings”
Now gzip compression is enabled for your website. selecting “Disabled” will disable the gzip compression for your website.
6. Go to website https://checkgzipcompression.com to check whether gzip compression is enabled for your website.
You can use any other sites or commands to check whether webserver is sending compressed headers.
Enable Gzip via .htaccess on Apache server
1. Login to cPanel account of the website
2. Click on “File Manager” under “Files”
3. Click on “New File” in File manager to create a new file
You must create .htaccess in the document root of the website. There is no need to create a new .htaccess file, if you already have one in public_html.
4. Add the below lines in the .htaccess file and save the file.
————————————————————-
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/opentype
# For Olders Browsers That Can’t Handle Compression
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
————————————————————-
Enable Gzip compression on nginx servers
1. Log into linux server via SSH as ‘root’
2. Take a backup of the nginx configuration file before editing
vi /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
3. Open nginx configuration file using vi editor and change the below lines
—————-
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 1000;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
—————-
4. Save the nginx configuration file and exit
5. Run “nginx -t” to check the syntax before restarting the service
[root@linux ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
6. Restart nginx service : service nginx restart
Now go to checkgzipcompression.com website to check whether gzip compression is enabled.