如何在DigitalOcean Ubuntu16.04已搭建的Nginx上安装ngx_pagespeed
Alright, let's wrap up the ngx_pagespeed installation now that you've got the combined configure flags sorted. Here's the step-by-step breakdown:
1. Compile and Install the Dynamic Module
Assuming you ran the combined configure command without errors (in the Nginx source directory set up by the PageSpeed auto-install script), follow these steps to build and install the module:
- Compile only the dynamic modules (no need to recompile the entire Nginx binary):
make modules - Install the compiled module to your specified Nginx modules directory:
sudo make install
This will automatically place the ngx_pagespeed.so file into /usr/lib/nginx/modules.
2. Configure Nginx to Load PageSpeed
Next, you need to tell Nginx to load the module and enable its core functionality:
- Open the main Nginx configuration file in your preferred editor:
sudo nano /etc/nginx/nginx.conf - At the very top of the file (before the
httpblock), add this line to load the module:load_module modules/ngx_pagespeed.so; - Inside the
httpblock, add basic PageSpeed settings to enable it and set up a cache directory:pagespeed on; # Path to store cached PageSpeed optimization files pagespeed FileCachePath /var/cache/ngx_pagespeed/; - Save and close the file (in nano: press
Ctrl+O, hitEnter, thenCtrl+X).
3. Set Up the PageSpeed Cache Directory
Create the cache directory we referenced and set proper permissions so Nginx can write to it:
sudo mkdir -p /var/cache/ngx_pagespeed/ sudo chown www-data:www-data /var/cache/ngx_pagespeed/
4. Test Configuration and Restart Nginx
- First, validate that your Nginx configuration has no syntax errors:
sudo nginx -t - If you see
nginx: configuration file /etc/nginx/nginx.conf test is successful, restart Nginx to apply the changes:sudo systemctl restart nginx # Alternatively, for Ubuntu 16.04's older init system: # sudo service nginx restart
5. Verify PageSpeed is Working
To confirm the module is active and processing requests:
- Use
curlto check your site's response headers:curl -I http://your-domain-or-server-ip
Look for a header like X-Page-Speed: 1.x.x (the version number will vary) — this confirms PageSpeed is operational.
- For a detailed admin view, add this to your
httpblock (restrict access to your IP for security):pagespeed AdminPath /pagespeed_admin; allow 127.0.0.1; allow your-public-ip-address; deny all;
After restarting Nginx, visit http://your-domain/pagespeed_admin in your browser to access the dashboard.
Troubleshooting Tips
- If you hit errors during
make modules, ensure all required dependencies are installed:sudo apt-get install build-essential libpcre3-dev zlib1g-dev libssl-dev - If Nginx fails to start, check the error log for details:
sudo tail -f /var/log/nginx/error.log
内容的提问来源于stack exchange,提问作者Romel Gomez

