Install Nginx with Pagespeed in CentOs or RedHat

Installation & Configuration steps of Nginx with Pagespeed in Centos

How Nginx is Different from Apache

In its quest to be lightweight and fast, Nginx takes a different approach to modules than you’re probably familiar with in Apache. In Apache you can dynamically load various features using modules. You just add something like  LoadModule module.so to your Apache config files and just like that Apache loads the alias module.

Unlike Apache, Nginx can not dynamically load modules. Nginx has available what it has available when you install it.

That means if you really want to customize and tweak it, it’s best to install Nginx from source. You don’t have to install it from source. But if you really want a screaming fast server, I suggest compiling Nginx yourself, enabling and disabling exactly the modules you need. Installing Nginx from source allows you to add some third-party tools, most notably Google’s PageSpeed module, which has some fantastic tools for speeding up your site.

Luckily, installing Nginx from source isn’t too difficult. Even if you’ve never compiled any software from source, you can install Nginx. The remainder of this post will show you exactly how.

Here are the step-by-step process to installing Nginx on a Debian/Ubuntu server.The first step is to make sure you’re installing the latest release of Nginx. To do that check the Nginx download page for the latest version of Nginx.

Okay, Login via shell access (SSH) to your server and let’s get started.

While these instructions will work on just about any server, the one thing that will be different is how you install the various prerequisites needed to compile Nginx.

First install some dependency packages which would require to compile nginx from source

 yum install gcc-c++ pcre-dev pcre-devel zlib-devel make

After you have the prerequisites installed it’s time to grab the latest version of Google’s Pagespeed module. Google’s Nginx PageSpeed installation instructions are pretty good, so I’ll reproduce them here.

First grab the latest version of PageSpeed, which is currently 1.9.32.2, but check the sources since it updates frequently and change this first cariable to match the latest version.

NPS_VERSION=1.9.32.2
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip
unzip release-${NPS_VERSION}-beta.zip

Now, before we compile pagespeed we need to grab psol, which PageSpeed needs to function properly.

cd ngx_pagespeed-release-${NPS_VERSION}-beta/
wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz
tar -xzvf ${NPS_VERSION}.tar.gz
cd ../

Alright, so the ngx_pagespeed module is all setup and ready to install. All we have to do at this point is tell Nginx where to find it.

Now let’s grab the Headers More and Naxsi modules as well. Again, check the Headers More and Naxsi pages to see what the latest stable version is and adjust the version numbers in the following accordingly.

HM_VERSION =v0.25 
wget https://github.com/agentzh/headers-more-nginx-module/archive/${HM_VERSION}.tar.gz
tar -xvzf ${HM_VERSION}.tar.gz
NAX_VERSION=0.53-2
wget https://github.com/nbs-system/naxsi/archive/${NAX_VERSION}.tar.gz
tar -xvzf ${NAX_VERSION}.tar.gz

Now we have all three third-party modules ready to go, the last thing we’ll grab is a copy of Nginx itself:

NGINX_VERSION=1.7.7
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
tar -xvzf nginx-${NGINX_VERSION}.tar.gz
cd nginx-${NGINX_VERSION}/

So now we’re inside the Nginx folder, let’s configure our installation. We’ll add in all our extras and turn off a few things we don’t need. Or at least they’re things I don’t need, if you need the mail modules, then delete those lines. If you don’t need SSL, you might want to skip that as well. Here are config files which added mostly uses modules to compile for nginx.

This will install nginx on /etc/nginx/nginx.conf path which you can change it also.

./configure \
        —add-module=$HOME/naxsi-${NAX_VERSION}/naxsi_src \
        —prefix=/usr/share/nginx \
        —sbin-path=/usr/sbin/nginx \
        —conf-path=/etc/nginx/nginx.conf \
        —pid-path=/var/run/nginx.pid \
        —lock-path=/var/lock/nginx.lock \
        —error-log-path=/var/log/nginx/error.log \
        —http-log-path=/var/log/access.log \
        —user=www-data \
        —group=www-data \
        —with-http_stub_status_module \
        —with-http_ssl_module \
        —with-http_spdy_module \
        —with-http_gzip_static_module \
        —add-module=$HOME/ngx_pagespeed-release-${NPS_VERSION}-beta \
        —add-module=$HOME/headers-more-nginx-module-${HM_VERSION}\

 

There are a few things worth noting here. First off make sure that Naxsi is first. Here’s what the Naxsi wiki page has to say on that score: “Nginx will decide the order of modules according the order of the module’s directive in Nginx’s ./configure. So, no matter what (except if you really know what you are doing) put Naxsi first in your ./configure. If you don’t do so, you might run into various problems, from random/unpredictable behaviors to non-effective WAF.” The last thing you want is to think you have a web application firewall running when in fact you don’t, so stick with Naxsi first.

Okay, we’ve told Nginx what to do, now let’s actually install it:

make
sudo make install

Once above command finishes doing its thing you’ll have Nginx all set up.

Congrats! You made it.

The next step is to add Nginx to the list of things your server starts up automatically whenever it reboots. Since we installed Nginx from scratch we need to tell the underlying system what we did.

Make Nginx Autostart

Since we compiled from source rather than using Debian/Ubuntu’s package management tools, the underlying stystem isn’t aware of Nginx’s existence. That means it won’t automatically start it up when the system boots. In order to ensure that Nginx does start on boot we’ll have to manually add Nginx to our server’s list of startup services. That way, should we need to reboot, Nginx will automatically restart when the server does.

wget https://raw.githubusercontent.com/MovLib/www/develop/etc/init.d/nginx.sh 
# I had to edit the DAEMON var to point to nginx 
# change line 63 in the file to: DAEMON=/usr/sbin/nginx 
# then move it to /etc/init.d/nginx mv nginx.sh /etc/init.d/nginx 
# make it executable: chmod +x /etc/init.d/nginx 
# then just: service nginx start #also restart, reload, stop etc

Okay so we now have the initialization script all set up, now let’s make Nginx start up on reboot. In theory this should do it:

chkconfig —add nginx 
chkconfig nginx on

So there we have it, everything you need to get Nginx installed with SPDY, PageSpeed, Headers More and Naxsi. A blazing fast server for static files.

After that it’s just a matter of configuring Nginx, which is entirely dependent on how you’re using it. For static setups like this my configuration is pretty minimal.

Before we get to that though, there’s the first thing I do: edit nginx.conf down to something pretty simple.

user  www-data;
events {
    worker_connections  1024;
}
http {
    include mime.types;
    include /etc/nginx/naxsi_core.rules;
    default_type  application/octet-stream;
    types_hash_bucket_size 64;
    server_names_hash_bucket_size 128;
    log_format  main  ‘$remote_addr - $remote_user [$time_local] “$request” ‘
                      ‘$status $body_bytes_sent “$http_referer” ‘
                      ‘”$http_user_agent” “$http_x_forwarded_for”’;

    access_log  logs/access.log  main;
    more_set_headers “Server: My Custom Server”;
    keepalive_timeout  65;
    gzip  on;
    pagespeed on;
    pagespeed FileCachePath /var/ngx_pagespeed_cache;
    include /etc/nginx/sites-enabled/*.conf;
}

A few things to note. I’ve include the core rules file from the Naxsi source. To make sure that file exists, we need to copy it over to /etc/nginx/.

sudo cp naxsi-0.53-2/naxci_config/naxsi_core.rule /etc/nginx

Now let’s restart the server so it picks up these changes:

sudo service nginx restart

With this configuration we have a good basic setup and any .conf files you add to the folder /etc/nginx/sites-enabled/ files to be included automatically.

 

Balvinder Singh

Hello, I am Balvinder Singh - DevOps Engineer with 2.5+ year of working experience with different server environments. Tag Line:-Linux | AWS| WHM |Monitoring | Virtualization | Optimization | Performance | Security | Release & Deployment. I love helping companies / clients to deploy their code / applicateions to well managed, optimized, secure server and can go extra mile to satisfy.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *