In this tutorial, we will show you how to install Nginx with ngx_pagespeed on CentOS. For those of you who didn’t know, PageSpeed (ngx_pagespeed) is an Nginx module created by Google to help Make the Web Faster by rewriting web pages to reduce latency and bandwidth. For the installation, we will need to compile Nginx from the source with the PageSpeed module, as Nginx doesn’t support Dynamic module loading (DSO) unless you want to build your own rpm or deb files.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of Nginx With ngx_pagespeed on the CentOS server.
Prerequisites
- A server running one of the following operating systems: CentOS or RHEL-based.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for Nginx.
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Nginx With ngx_pagespeed on CentOS
Step 1. The first step is to update your system to the latest version of the package list. To do so, run the following commands:
sudo dnf check-update
Step 2. Installing Nginx Web Server.
By default, Nginx is not available on the CentOS base repository. Now run the following command below to add Nginx stable repository to your system:
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Once the repository is added, you can install Nginx by running the following command:
sudo dnf install nginx
To verify that the latest version of Nginx has been installed, run:
nginx -v
Step 3. Installing ngx_pagespeed.
To install ngx_pagespeed, you need to add the ngx_pagespeed repository to your system. You can do this by running the following command below:
sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
Once the repository is added, you can install ngx_pagespeed by running the following command:
sudo dnf install ngx_pagespeed
Step 4. Configuring Nginx.
After Nginx and ngx_pagespeed are installed, you need to configure Nginx to use ngx_pagespeed. You can do this by editing the Nginx configuration file located at /etc/nginx/nginx.conf
.
nano /etc/nginx/nginx.conf
Add the following lines at the beginning of the file:
load_module "/usr/lib64/nginx/modules/ngx_pagespeed.so"; pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache;
Save and close the file, then you can start Nginx by running the following command:
sudo systemctl start nginx sudo systemctl enable nginx
Congratulations! You have successfully installed Nginx with ngx_pagespeed. Thanks for using this tutorial for installing Nginx with ngx_pagespeed on the CentOS system. For additional help or useful information, we recommend you check the official Nginx website.