Install the latest NginX from source package

Posted on Posted in NginX

As you may know the latest versions of the software packages are not available in the software repositories. It can take few months for any release upgrade to appear in the repository. Unfortunately in the real world the latest releases never appear in the official repositories of RHEL/CentOS Linux.

In my opinion it is extremely risky to install the packages that were pre-configured by someone else because such packages can contain back-doors or malware.

However you can always keep your NginX up to date using the package with the source code. There is nothing to be afraid of with such approach.

The following article describes how to compile the latest NginX using the source package.

I used fresh installation of the CentOS 6.7 to test this tutorial.

First of all make sure that your system has required packages:
yum install aspell-devel bzip2-devel curl-devel freetype-devel gcc gcc-c++ gmp-devel libc-client-devel libicu-devel libjpeg-devel libpng-devel libxml2-devel libxslt-devel make mhash-devel mysql-devel ncurses-devel net-snmp-devel openldap-devel openssl-devel pkgconfig postgresql-devel unixODBC-devel db4-devel pcre-devel sqlite-devel enchant-devel libXpm-devel libedit-devel recode-devel libtidy-devel

I'd recommend to include srcache module into installation.

The complete list of the releases can be found on the following page:

Let's use the default manual and include the module into NginX on the compilation stage with --add-module=
cd /usr/local/src
tar xf v0.30.tar.gz

The final preparation step is to create system user for nginx:
echo "/bin/false" >> /etc/shells
useradd -d /opt/nginx -s /bin/false nginx

Download and uncompress the source package:
cd /usr/local/src
tar xf nginx-1.11.6.tar.gz
cd nginx-1.11.6

Configure and make it. I am using the /opt/nginx as an installation prefix. Without this it will be installed into /usr/local/nginx. You can specify alternative location:
./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_stub_status_module --add-module=/usr/local/src/srcache-nginx-module-0.30
make install

If you used another tutorial and installed the latest OpenSSL according to it than you can compile NginX with support of the latest OpenSSL. Please add the following to configure arguments:

If make ends with an error related to the OpenSSL, than there might be a need to fix the source file. Run the following in the NginX source folder:
sed -i -e 's|\.openssl/||' auto/lib/openssl/conf

After this the NginX will be installed into the /opt/nginx folder.

Next step would be to create the init.d script that will be used to start, stop and restart the daemon. The contents of the working script can be found on one of the following pages:

You will need to edit the definitions of the following variables:

  • NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
  • nginx="/opt/nginx/sbin/nginx"

When the script is created, let's make it executable and enable autostart on boot:
sudo chmod +x /etc/init.d/nginx
chkconfig nginx on

In Ubuntu/Debian, you will need to use the following command to enable upstart for NginX:
sudo /usr/sbin/update-rc.d -f nginx defaults

Edit the configuration file /opt/nginx/conf/nginx.conf with the definition of the user that to run the web server:
user nginx;

After this you can start the NginX by running the following:
/etc/init.d/nginx start

Behind this point you can proceed with the configuration according to the official documentation.

The version can be verified in the responce headers:
Screenshot from 2016-07-27 04:04:35

More resources:

Leave a Reply

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