Like the Debian build process, first we'll need to install the package build tools and the additional Extra Packages For Enterprise Linux (EPEL) repository:
sudo yum install yum-utils epel-release mock
Next, update /etc/yum.repos.d/nginx.repo and add the additional source repository:
[nginx-source]
name=nginx source repo
baseurl=http://nginx.org/packages/mainline/centos/7/SRPMS/
gpgcheck=0
enabled=1
In this example, we'll be using a CentOS 7-based release. Refer to the Packages – RHEL/CentOS section for how to modify it for other CentOS versions.
With the updated repository, we then create a directory for the build, and download the Source RPM (SRPM):
mkdir ~/nginxbuild
cd ~/nginxbuild
yumdownloader --source nginx
Next, download the required packages to complete the build:
yum-builddep nginx
Once all of the development packages have been downloaded, we can now extract the files from the SRPM:
rpm2cpio nginx-1.9.10-1.el7.ngx.src.rpm | cpio -idmv
Note that the name of your directory may vary based on the version of NGINX you have installed. For instance, here it is nginx-1.9.10 as I have installed NGINX 1.9.10.
You should see an output of the source files similar to this:
If we want to update the configuration and apply a patch or change one of the defaults, then this can simply be done by editing the files.
We can now rebuild these files from source using mock, which is a tool for building packages. The advantage of mock is that all of the development dependencies are contained within a chrooted environment, so it doesn't clutter your main installation. This chrooted environment can be cleaned and removed without any impact on the host system, which is great if you want repeatable builds.
To build, we run the following command:
mock --buildsrpm --spec ~/nginxbuild/nginx.spec --sources ~/nginxbuild
This generates the SRPMs, and they will be located in the /var/lib/mock/epel-7-x86_64/result directory, along with the associated log files. Now that we have a rebuilt SRPM, we can now compile it. Again, we're going to use mock so that everything is neatly contained:
mock --no-clean --rebuild var/lib/mock/epel-7-x86_64/result/nginx-1.9.11-1.el7.ngx.src.rpm
Depending on your processing power, this may take five minutes or more to complete. Once the build is complete, you should see the resultant binary RPM as well as a debug RPM in the /var/lib/mock/epel-7-x86_64 directory. Here's an example:
-rw-rw-r-- 1 demo mock 159K Feb 10 20:59 build.log
-rw-r--r-- 1 demo mock 889K Feb 10 20:57 nginx-1.9.11-1.el7.ngx.src.rpm
-rw-r--r-- 1 demo mock 803K Feb 10 20:59 nginx-1.9.11-1.el7.ngx.x86_64.rpm
-rw-r--r-- 1 demo mock 3.1M Feb 10 20:59 nginx-debuginfo-1.9.11-1.el7.ngx.x86_64.rpm
-rw-rw-r-- 1 demo mock 45K Feb 10 20:59 root.log
-rw-rw-r-- 1 demo mock 1000 Feb 10 20:59 state.log
Now that we have the new binary file, we can install it via yum:
sudo yum install /var/lib/mock/epel-7-x86_64/result/nginx-1.9.11-1. ngx.x86_64.rpm
It's preferable to use yum over rpm to install the packages, as it can also install any dependencies.
You should now have a fully installed NGINX installation, which you compiled from source.