Learn How To Compile Nginx From Source on Fedora 25

August 24, 2019

Table of Contents

If you are using a different system, please check our other tutorials.

NGINX can be used as an HTTP/HTTPS server, reverse proxy server, mail proxy server, load balancer, TLS terminator, or caching server. It is quite modular by design. It has native modules and third-party modules created by the community. Written in the C programming language, it’s a very fast and lightweight piece of software.

NOTE: NGINX has two version streams that run in parallel – stable and mainline. Both versions can be used on a production server. It is recommended to use the mainline version in production.

Installing NGINX from source code is relatively “easy” – download the latest version of the NGINX source code, configure, build and install it.

In this tutorial I will use the mainline version, which is 1.13.3 at the time of writing. Update version numbers accordingly when newer versions become available.

Requirements for building NGINX from source

Mandatory requirements:

  • OpenSSL library version between 1.0.2 – 1.1.0
  • zlib library version between 1.1.3 – 1.2.11
  • PCRE library version between 4.4 – 8.41
  • GCC Compiler

Optional requirements:

Before you begin

https://www.itweb.services/tutorials/linux-guides/how-to-use-sudo-on-debian-centos-and-freebsd”>regular user with sudo access:

  • Switch to the new user:

    su - <username>
  • Update system:

    sudo dnf check-update || sudo dnf upgrade -y
  • Build NGINX from source

    1. Install “Development Tools”, Vim editor, wget, and gcc-c++:

      sudo dnf install -y @development-tools && sudo dnf install -y vim wget gcc-c++
    2. Download the latest mainline version of NGINX source code and untar it:

      wget https://nginx.org/download/nginx-1.13.3.tar.gz && tar zxvf nginx-1.13.3.tar.gz
    3. Download the NGINX dependencies’ source code and extract them:

      NGINX depends on 3 libraries: PCRE, zlib and OpenSSL:

      # PCRE version 8.41
      wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz && tar xzvf pcre-8.41.tar.gz
      # zlib version 1.2.11
      wget https://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz
      # OpenSSL version 1.1.0f
      wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz && tar xzvf openssl-1.1.0f.tar.gz
    4. Download and install optional NGINX dependencies:

      # perl
      sudo dnf install -y perl perl-devel perl-ExtUtils-Embed
      # libxslt
      sudo dnf install -y libxslt libxslt-devel
      # libxml2
      sudo dnf install -y libxml2 libxml2-devel
      # libgd
      sudo dnf install -y gd gd-devel
      # GeoIP
      sudo dnf install -y GeoIP GeoIP-devel
      # Libatomic_Ops
      sudo dnf install -y libatomic_ops libatomic_ops-devel
    5. Remove all .tar.gz files. We don’t need them anymore:

      rm -rf *.tar.gz
    6. Go to the NGINX source directory:

      cd ~/nginx-1.13.3
    7. For good measure, list NGINX source code files and directories:

      # auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
    8. Copy NGINX manual page to /usr/share/man/man8/:

      sudo cp ~/nginx-1.13.3/man/nginx.8 /usr/share/man/man8/
      sudo gzip /usr/share/man/man8/nginx.8
      # Check that Man page for NGINX is working
      man nginx
    9. For help, you can list available configuration switches by running:

      ./configure --help
      # To see want core modules can be build as dynamic run:
      ./configure --help | grep -F =dynamic
    10. Configure, compile, and install NGINX:

      ./configure --prefix=/etc/nginx 
      sudo make install
    11. Print the NGINX version, compiler version, and configure script parameters:

      nginx -V
      # nginx version: nginx/1.13.3 (Fedora)
      # built by gcc 6.3.1 20161221 (Red Hat 6.3.1-1) (GCC)
      # built with OpenSSL 1.1.0f  25 May 2017
      # TLS SNI support enabled
      # configure arguments: --prefix=/etc/nginx . . .
      # . . .
    12. Create the NGINX system user and group:

      sudo useradd --system --home /var/cache/nginx --shell /sbin/nologin --comment "nginx user" --user-group nginx
    13. Check syntax and potential errors:

      sudo nginx -t
      # Will throw this error: nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)
      # Just create directory
      sudo mkdir -p /var/cache/nginx/ && sudo nginx -t
    14. Create a systemd unit file for NGINX:

      sudo vim /etc/systemd/system/nginx.service
    15. Copy/paste the following content:

      NOTE: The location of the PID file and the NGINX binary may be different depending on how NGINX was compiled.

      Description=Nginx - A high performance web server and a reverse proxy server
      ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
      ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
      ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
      ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /var/run/nginx.pid
    16. Start NGINX:

      sudo systemctl start nginx.service
    17. Enable NGINX to start automatically on boot:

      sudo systemctl enable nginx.service
    18. Check if NGINX will startup after a reboot:

      sudo systemctl is-enabled nginx.service
      # enabled
    19. Check if NGINX is running:

      sudo systemctl status nginx.service
      ps aux | grep nginx
      curl -I
    20. Reboot your VPS to verify that NGINX starts up automatically:

      sudo shutdown -r now
    21. Remove archaic files from the /etc/nginx/ directory:

      sudo rm /etc/nginx/koi-utf /etc/nginx/koi-win /etc/nginx/win-utf
    22. Place syntax highlighting files of NGINX configuration for vim into ~/.vim/. You will be presented with nice syntax highlighting when editing NGINX configuration file:

      mkdir ~/.vim/
      cp -r ~/nginx-1.13.3/contrib/vim/* ~/.vim/
    23. Make a conf.d/ directory in the /etc/nginx/ directory. In this directory, you can place virtual servers and upstreams:

      sudo mkdir /etc/nginx/conf.d/
    24. Remove extracted directories and files from your home directory:

      rm -rf nginx-1.13.3/ openssl-1.1.0f/ pcre-8.41/ zlib-1.2.11/


    That’s it. You now have newest version of NGINX installed. It is compiled statically against some important libraries like OpenSSL. Often, the system OpenSSL version is outdated. By using this method of installing with a newer version of OpenSSL, you can take advantage of new ciphers like CHACHA20_POLY1305 and protocols like TLS 1.3 that will be available in OpenSSL 1.1.1 (which has not been released at the time of writing).

    Need help?

    Do you need help setting up this on your own service?
    Please contact us and we’ll provide you the best possible quote!