{"id":5193,"date":"2018-06-29T20:15:32","date_gmt":"2018-06-29T16:15:32","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/how-to-build-nginx-from-source-on-ubuntu-1804-lts\/"},"modified":"2018-06-29T20:15:32","modified_gmt":"2018-06-29T16:15:32","slug":"how-to-build-nginx-from-source-on-ubuntu-18-04-lts","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/how-to-build-nginx-from-source-on-ubuntu-18-04-lts\/","title":{"rendered":"How to Build NGINX from source on Ubuntu 18.04 LTS"},"content":{"rendered":"<p><a href=\"https:\/\/nginx.org\/\" rel=\"nofollow noopener\" target=\"_blank\">NGINX<\/a><span>\u00a0<\/span>(pronounced<span>\u00a0<\/span><em>&#8220;engine x&#8221;<\/em>) is an open source web server software designed with high concurrency in mind, that can be used as<span>\u00a0<\/span><em>HTTP\/HTTPS server<\/em>,<span>\u00a0<\/span><em>reverse proxy server<\/em>,<span>\u00a0<\/span><em>mail proxy server<\/em>,<span>\u00a0<\/span><em>software load balancer<\/em>,<span>\u00a0<\/span><em>TLS terminator<\/em>,<span>\u00a0<\/span><em>caching server<\/em>&#8230;<\/p>\n<p>It is an extremely modular piece of software. Even some of the seemingly &#8220;built-in&#8221; pieces of the software, such as GZIP or SSL, are actually built as modules that can be enabled and disabled during the build time.<\/p>\n<p>It has<span>\u00a0<\/span><em>core<\/em><span>\u00a0<\/span>(<em>native<\/em>)<span>\u00a0<\/span><em>modules<\/em><span>\u00a0<\/span>and<span>\u00a0<\/span><a href=\"https:\/\/www.nginx.com\/resources\/wiki\/modules\/index.html\" rel=\"nofollow noopener\" target=\"_blank\"><em>third-party (external) modules<\/em><\/a><span>\u00a0<\/span>created by the community. Right now, there are over a hundred third-party modules that we can utilize.<\/p>\n<p>Written in<span>\u00a0<\/span><strong>C<\/strong><span>\u00a0<\/span>language, it&#8217;s very fast and lightweight piece of software.<\/p>\n<p>Installing NGINX from source code is relatively &#8220;easy&#8221; &#8211; download latest version of NGINX source code, configure, build and install it.<\/p>\n<p>You\u2019ll need to choose whether to download the<span>\u00a0<\/span><em>mainline<\/em><span>\u00a0<\/span>or<span>\u00a0<\/span><em>stable<\/em><span>\u00a0<\/span>version, but building them is exactly the same.<\/p>\n<p><a class=\"fancybox\" id=\"img-built-nginx\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_build_nginx_from_source_on_ubuntu_1804_lts\/big\/built-nginx.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-build-nginx-from-source-on-ubuntu-18-04-lts.png\" alt=\"\" title=\"\"><\/a><\/p>\n<p>In this tutorial, we will build NGINX with all available modules in open source version of NGINX and we will use<span>\u00a0<\/span><em>mainline\u00a0<\/em>version which is at<span>\u00a0<\/span><strong>1.15.0<\/strong><span>\u00a0<\/span>at the time of this writing. Update version numbers when newer versions become available.<\/p>\n<h2 id=\"stable-vs-mainline-version\">Stable vs. mainline version<\/h2>\n<p>NGINX Open Source is available in two versions:<\/p>\n<ul>\n<li><em>Mainline<\/em><span>\u00a0<\/span>\u2013 Includes the latest features and bug fixes and is always up to date. It is reliable, but it may include some experimental modules, and it may also have some number of new bugs.<\/li>\n<li><em>Stable<\/em><span>\u00a0<\/span>\u2013 Doesn\u2019t include all of the latest features, but has critical bug fixes that are always backported to the mainline version.<\/li>\n<\/ul>\n<h2 id=\"core-modules-vs-thirdparty-modules\">Core modules vs. third-party modules<\/h2>\n<p>NGINX has two types of modules that you can utilize:<span>\u00a0<\/span><em>core modules<\/em><span>\u00a0<\/span>and<span>\u00a0<\/span><em>third-party modules<\/em>.<\/p>\n<p>Core modules are built by the core NGINX developers and they are part of the software itself.<\/p>\n<p>Third party modules are built by the community and you can use them to extend NGINX functionality. There are a lot of helpful third-party modules, the most famous among them are: PageSpeed, ModSecurity, RTMP, Lua etc&#8230;<\/p>\n<h2 id=\"static-modules-vs-dynamic-modules\">Static modules vs. dynamic modules<\/h2>\n<p>Static modules exist in NGINX from the very first version. Dynamic modules were introduced with NGINX 1.9.11+ in February 2016.<\/p>\n<p>With static modules, set of modules that constitute an NGINX binary is fixed at compile time by the<span>\u00a0<\/span><code>.\/configure<\/code><span>\u00a0<\/span>script. Static modules use<span>\u00a0<\/span><code>--with-foo_bar_module<\/code><span>\u00a0<\/span>or<span>\u00a0<\/span><code>--add-module=PATH<\/code><span>\u00a0<\/span>syntax.<\/p>\n<p>To compile core (standard) module as dynamic we add<span>\u00a0<\/span><code>=dynamic<\/code>, for example<span>\u00a0<\/span><code>--with-http_image_filter_module=dynamic<\/code>.<\/p>\n<p>To compile third-party module as dynamic we use<span>\u00a0<\/span><code>--add-dynamic-module=\/path\/to\/module<\/code><span>\u00a0<\/span>syntax and then we load them by using<span>\u00a0<\/span><code>load_module<\/code><span>\u00a0<\/span>directive in the global context of the<span>\u00a0<\/span><code>nginx.conf<\/code><span>\u00a0<\/span>file.<\/p>\n<h2 id=\"requirements-for-building-nginx-from-source\">Requirements for building NGINX from source<\/h2>\n<p>In comparison with some other UNIX\/Linux software, NGINX is pretty lightweight and doesn\u2019t have many library dependencies. The default build configuration depends on only 3 libraries to be installed:<span>\u00a0<\/span><strong>OpenSSL<\/strong>\/<strong>LibreSSL<\/strong>\/<strong>BoringSSL<\/strong>,<span>\u00a0<\/span><strong>Zlib<\/strong><span>\u00a0<\/span>and<span>\u00a0<\/span><strong>PCRE<\/strong>.<\/p>\n<ul>\n<li>Mandatory requirements: <\/li>\n<li>Optional requirements: <\/li>\n<\/ul>\n<p><span><strong>NOTE<\/strong>: NGINX can also be compiled against<span>\u00a0<\/span><\/span><strong>LibreSSL<\/strong><span><span>\u00a0<\/span>and<span>\u00a0<\/span><\/span><strong>BoringSSL<\/strong><span><span>\u00a0<\/span>crypto libraries instead of<span>\u00a0<\/span><\/span><strong>OpenSSL<\/strong><span>.<\/span><\/p>\n<h2 id=\"requirements\">Requirements<\/h2>\n<ul>\n<li>A server running Ubuntu 18.04 LTS.<\/li>\n<li>A non-root user with sudo privileges.<\/li>\n<\/ul>\n<h2 id=\"initial-steps\">Initial Steps<\/h2>\n<p><span>Check Ubuntu version:<\/span><\/p>\n<p class=\"command\">lsb_release -ds <br \/><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> Ubuntu 18.04 LTS<br \/><\/span><\/p>\n<p><span>Set up the timezone:<\/span><\/p>\n<p class=\"command\"><span><span>timedatectl list-timezones<\/span><br \/><span>sudo timedatectl set-timezone &#8216;Region\/City&#8217;<br \/><\/span><\/span><\/p>\n<p><span><span><span>Update your operating system\u2019s packages:<\/span><\/span><\/span><\/p>\n<p class=\"command\"><span>sudo apt update<span>\u00a0<\/span><\/span><span class=\"pl-k\">&amp;&amp;<\/span><span><span>\u00a0<\/span>sudo apt upgrade -y<br \/><\/span><\/p>\n<h2 id=\"build-nginx-from-source\">Build NGINX from source<\/h2>\n<p><span><span>NGINX is a program written in<span>\u00a0<\/span><\/span><strong>C<\/strong><span>, so you will first need to install a compiler tools. Install<span>\u00a0<\/span><\/span><code>build-essential<\/code><span>,<span>\u00a0<\/span><\/span><code>git<\/code><span><span>\u00a0<\/span>and\u00a0<\/span><code>tree<\/code><span><span>\u00a0<\/span>packages:<\/span><\/span><\/p>\n<p class=\"command\">sudo apt install -y build-essential git tree<\/p>\n<p><span>Download latest mainline version of NGINX source code and extract it. NGINX source code is distributed as compressed archive (gzipped tarball), as most Unix and Linux software:<\/span><\/p>\n<p class=\"command\">wget https:\/\/nginx.org\/download\/nginx-1.15.0.tar.gz <span class=\"pl-k\">&amp;&amp;<\/span> tar zxvf nginx-1.15.0.tar.gz<\/p>\n<p><span>Download the mandatory NGINX dependencies&#8217; source code and extract them:<\/span><\/p>\n<p class=\"command\"><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> PCRE version 8.42<br \/><\/span>wget https:\/\/ftp.pcre.org\/pub\/pcre\/pcre-8.42.tar.gz <span class=\"pl-k\">&amp;&amp;<\/span> tar xzvf pcre-8.42.tar.gz<\/p>\n<p><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> zlib version 1.2.11<br \/><\/span>wget https:\/\/www.zlib.net\/zlib-1.2.11.tar.gz <span class=\"pl-k\">&amp;&amp;<\/span> tar xzvf zlib-1.2.11.tar.gz<\/p>\n<p><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> OpenSSL version 1.1.0h<br \/><\/span>wget https:\/\/www.openssl.org\/source\/openssl-1.1.0h.tar.gz <span class=\"pl-k\">&amp;&amp;<\/span> tar xzvf openssl-1.1.0h.tar.gz<\/p>\n<p>Install optional NGINX dependencies:<br \/><span\/><\/p>\n<p class=\"command\">sudo add-apt-repository -y ppa:maxmind\/ppa<br \/>sudo apt update <span class=\"pl-k\">&amp;&amp;<\/span> sudo apt upgrade -y <br \/>sudo apt install -y perl libperl-dev libgd3 libgd-dev libgeoip1 libgeoip-dev geoip-bin libxml2 libxml2-dev libxslt1.1 libxslt1-dev<\/p>\n<p><span>Clean up all<span>\u00a0<\/span><\/span><code>.tar.gz<\/code><span><span>\u00a0<\/span>files. We don&#8217;t need them anymore:<\/span><\/p>\n<p class=\"command\">rm -rf <span class=\"pl-k\">*<\/span>.tar.gz<\/p>\n<p><span>Enter the NGINX source directory:<\/span><\/p>\n<p class=\"command\"><span class=\"pl-c1\">cd<\/span> <span class=\"pl-k\">~<\/span>\/nginx-1.15.0<\/p>\n<p><span>For good measure list directories and files that compose NGINX source code with<span>\u00a0<\/span><\/span><code>tree<\/code><span><span>\u00a0<\/span>utility:<\/span><\/p>\n<p class=\"command\">tree -L 2 <span class=\"pl-c1\">.<\/span><\/p>\n<p><span class=\"pl-c1\"><span>Copy NGINX manual page to<span>\u00a0<\/span><\/span><code>\/usr\/share\/man\/man8\/<\/code><span><span>\u00a0<\/span>directory:<\/span><br \/><\/span><\/p>\n<p class=\"command\">sudo cp <span class=\"pl-k\">~<\/span>\/nginx-1.15.0\/man\/nginx.8 \/usr\/share\/man\/man8<br \/>sudo gzip \/usr\/share\/man\/man8\/nginx.8<br \/>ls \/usr\/share\/man\/man8\/ <span class=\"pl-k\">|<\/span> grep nginx.8.gz<br \/><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> Check that Man page for NGINX is working:<br \/><\/span>man nginx<\/p>\n<p><span><a class=\"fancybox\" id=\"img-nginx-man-page\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_build_nginx_from_source_on_ubuntu_1804_lts\/big\/nginx-man-page.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-build-nginx-from-source-on-ubuntu-18-04-lts-1.png\" alt=\"Nginx manpage\" width=\"550\" height=\"317\" title=\"\"><\/a><\/span><\/p>\n<p><span>For help, you can see full list of up-to-date NGINX compile time options by running:<\/span><\/p>\n<p class=\"command\">.\/configure &#8211;help<br \/><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> To see want core modules can be build as dynamic run:<br \/><\/span>.\/configure &#8211;help <span class=\"pl-k\">|<\/span> grep -F =dynamic<\/p>\n<p><span>Configure, compile and install NGINX:<\/span><\/p>\n<p class=\"command\">.\/configure &#8211;prefix=\/etc\/nginx \\ <br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;sbin-path=\/usr\/sbin\/nginx \\ <br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;modules-path=\/usr\/lib\/nginx\/modules \\ <br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;conf-path=\/etc\/nginx\/nginx.conf \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;error-log-path=\/var\/log\/nginx\/error.log \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;pid-path=\/var\/run\/nginx.pid \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;lock-path=\/var\/run\/nginx.lock \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;user=nginx \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;group=nginx \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;build=Ubuntu \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;builddir=nginx-1.15.0 \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-select_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-poll_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-threads \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-file-aio \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_ssl_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_v2_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_realip_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_addition_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_xslt_module=dynamic \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_image_filter_module=dynamic \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_geoip_module=dynamic \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_sub_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_dav_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_flv_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_mp4_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_gunzip_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_gzip_static_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_auth_request_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_random_index_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_secure_link_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_degradation_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_slice_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_stub_status_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-http_perl_module=dynamic \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-perl_modules_path=\/usr\/share\/perl\/5.26.1 \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-perl=\/usr\/bin\/perl \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;http-log-path=\/var\/log\/nginx\/access.log \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;http-client-body-temp-path=\/var\/cache\/nginx\/client_temp \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;http-proxy-temp-path=\/var\/cache\/nginx\/proxy_temp \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;http-fastcgi-temp-path=\/var\/cache\/nginx\/fastcgi_temp \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;http-uwsgi-temp-path=\/var\/cache\/nginx\/uwsgi_temp \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;http-scgi-temp-path=\/var\/cache\/nginx\/scgi_temp \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-mail=dynamic \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-mail_ssl_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-stream=dynamic \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-stream_ssl_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-stream_realip_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-stream_geoip_module=dynamic \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-stream_ssl_preread_module \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-compat \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-pcre=..\/pcre-8.42 \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-pcre-jit \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-zlib=..\/zlib-1.2.11 \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-openssl=..\/openssl-1.1.0h \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-openssl-opt=no-nextprotoneg \\<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8211;with-debug<\/p>\n<p>make<br \/>sudo make install<\/p>\n<p><span>After building NGINX, navigate to home (<\/span><code>~<\/code><span>) directory:<\/span><\/p>\n<p class=\"command\"><span class=\"pl-c1\">cd<\/span> <span class=\"pl-k\">~<\/span><\/p>\n<p><span class=\"pl-k\"><span>Symlink<span>\u00a0<\/span><\/span><code>\/usr\/lib\/nginx\/modules<\/code><span><span>\u00a0<\/span>to<span>\u00a0<\/span><\/span><code>\/etc\/nginx\/modules<\/code><span><span>\u00a0<\/span>directory.<span>\u00a0<\/span><\/span><code>etc\/nginx\/modules<\/code><span><span>\u00a0<\/span>is a standard place for NGINX modules:<\/span><br \/><\/span><\/p>\n<p class=\"command\">sudo ln -s \/usr\/lib\/nginx\/modules \/etc\/nginx\/modules<\/p>\n<p><span>Print the NGINX version, compiler version, and configure script parameters:<\/span><\/p>\n<p class=\"command\">sudo nginx -V<br \/><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> nginx version: nginx\/1.15.0 (Ubuntu)<br \/><\/span><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> built by gcc 7.3.0 (Ubuntu 7.3.0-16ubuntu3)<br \/><\/span><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> built with OpenSSL\u00a01.1.0h\u00a0 27 Mar 2018<br \/><\/span><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> TLS SNI support enabled<br \/><\/span><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> configure arguments: &#8211;prefix=\/etc\/nginx &#8211;sbin-path=\/usr\/sbin\/nginx &#8211;modules-path=\/usr\/lib\/nginx\/modules<br \/># . . .<br \/># . . .<\/span><\/p>\n<p><span class=\"pl-c\"><span>Create NGINX system group and user:<\/span><\/span><\/p>\n<p class=\"command\">sudo adduser &#8211;system &#8211;home \/nonexistent &#8211;shell \/bin\/false &#8211;no-create-home &#8211;disabled-login &#8211;disabled-password &#8211;gecos <span class=\"pl-s\"><span class=\"pl-pds\">&#8220;<\/span>nginx user<span class=\"pl-pds\">&#8220;<\/span><\/span> &#8211;group nginx<\/p>\n<p><span>Check NGINX syntax and potential errors:<\/span><\/p>\n<p class=\"command\">sudo nginx -t<br \/><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> Will throw this error -&gt; nginx: [emerg] mkdir() &#8220;\/var\/cache\/nginx\/client_temp&#8221; failed (2: No such file or directory)<\/span><\/p>\n<p><span class=\"pl-c\">#<\/span> Create NGINX cache directories and set proper permissions<br \/>sudo mkdir -p \/var\/cache\/nginx\/client_temp \/var\/cache\/nginx\/fastcgi_temp \/var\/cache\/nginx\/proxy_temp \/var\/cache\/nginx\/scgi_temp \/var\/cache\/nginx\/uwsgi_temp<br \/>sudo chmod 700 \/var\/cache\/nginx\/<span class=\"pl-k\">*<br \/><\/span>sudo chown nginx:root \/var\/cache\/nginx\/<span class=\"pl-k\">*<br \/><\/span><span class=\"pl-c\"><br \/>#<\/span> Re-check syntax and potential errors.<br \/>sudo nginx -t<\/p>\n<p><span>Create NGINX systemd unit file:<\/span><\/p>\n<p class=\"command\">sudo vim \/etc\/systemd\/system\/nginx.service<\/p>\n<p><span>Copy\/paste the below content into<span>\u00a0<\/span><\/span><code>\/etc\/systemd\/system\/nginx.service<\/code><span><span>\u00a0<\/span>file:<\/span><\/p>\n<pre><span class=\"pl-en\">[Unit]<\/span>&#13;\n<span class=\"pl-k\">Description<\/span>=nginx - high performance web server&#13;\n<span class=\"pl-k\">Documentation<\/span>=https:\/\/nginx.org\/en\/docs\/&#13;\n<span class=\"pl-k\">After<\/span>=network-online.target remote-fs.target nss-lookup.target&#13;\n<span class=\"pl-k\">Wants<\/span>=network-online.target&#13;\n&#13;\n<span class=\"pl-en\">[Service]<\/span>&#13;\n<span class=\"pl-k\">Type<\/span>=forking&#13;\n<span class=\"pl-k\">PIDFile<\/span>=\/var\/run\/nginx.pid&#13;\n<span class=\"pl-k\">ExecStartPre<\/span>=\/usr\/sbin\/nginx -t -c \/etc\/nginx\/nginx.conf&#13;\n<span class=\"pl-k\">ExecStart<\/span>=\/usr\/sbin\/nginx -c \/etc\/nginx\/nginx.conf&#13;\n<span class=\"pl-k\">ExecReload<\/span>=\/bin\/kill -s HUP $MAINPID&#13;\n<span class=\"pl-k\">ExecStop<\/span>=\/bin\/kill -s TERM $MAINPID&#13;\n&#13;\n<span class=\"pl-en\">[Install]<\/span>&#13;\n<span class=\"pl-k\">WantedBy<\/span>=multi-user.target<\/pre>\n<p><span>Enable NGINX to start on boot and start NGINX immediately:<\/span><\/p>\n<p class=\"command\">sudo systemctl <span class=\"pl-c1\">enable<\/span> nginx.service<br \/>sudo systemctl start nginx.service<\/p>\n<p><span>Check if NGINX will automatically initiate after a reboot:<\/span><\/p>\n<p class=\"command\">sudo systemctl is-enabled nginx.service<br \/><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> enabled<\/span><\/p>\n<p><span class=\"pl-c\"><span>Check if NGINX is running by running one of the following commands:<\/span><\/span><\/p>\n<p class=\"command\">sudo systemctl status nginx.service<br \/><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> or<br \/><\/span>ps aux <span class=\"pl-k\">|<\/span> grep nginx<br \/>#<span class=\"pl-c\">\u00a0or<br \/><\/span>curl -I 127.0.0.1<\/p>\n<p><span>You can also open your browser and navigate to your domain\/IP address to see default NGINX page. That is an indicator that NGINX is up and running.<\/span><\/p>\n<p><span><a class=\"fancybox\" id=\"img-nginx-page\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_build_nginx_from_source_on_ubuntu_1804_lts\/big\/nginx-page.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-build-nginx-from-source-on-ubuntu-18-04-lts-2.png\" alt=\"Nginx welcome page\" width=\"550\" height=\"315\" title=\"\"><\/a><\/span><\/p>\n<p><span>Create Uncomplicated Firewall (<\/span><strong>UFW<\/strong><span>) NGINX application profile:<\/span><\/p>\n<p class=\"command\">sudo vim \/etc\/ufw\/applications.d\/nginx<\/p>\n<p><span>Copy\/paste the below content into<span>\u00a0<\/span><\/span><code>\/etc\/ufw\/applications.d\/nginx<\/code><span><span>\u00a0<\/span>file:<\/span><\/p>\n<pre><span class=\"pl-en\">[Nginx HTTP]<\/span>&#13;\n<span class=\"pl-k\">title<\/span>=Web Server (Nginx, HTTP)&#13;\n<span class=\"pl-k\">description<\/span>=Small, but very powerful and efficient web server&#13;\n<span class=\"pl-k\">ports<\/span>=80\/tcp&#13;\n&#13;\n<span class=\"pl-en\">[Nginx HTTPS]<\/span>&#13;\n<span class=\"pl-k\">title<\/span>=Web Server (Nginx, HTTPS)&#13;\n<span class=\"pl-k\">description<\/span>=Small, but very powerful and efficient web server&#13;\n<span class=\"pl-k\">ports<\/span>=443\/tcp&#13;\n&#13;\n<span class=\"pl-en\">[Nginx Full]<\/span>&#13;\n<span class=\"pl-k\">title<\/span>=Web Server (Nginx, HTTP + HTTPS)&#13;\n<span class=\"pl-k\">description<\/span>=Small, but very powerful and efficient web server&#13;\n<span class=\"pl-k\">ports<\/span>=80,443\/tcp<\/pre>\n<p><span>Verify that UFW application profiles are created and recognized:<\/span><\/p>\n<p class=\"command\">sudo ufw app list<\/p>\n<p><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> Available applications:<br \/><\/span><span class=\"pl-c\"><span class=\"pl-c\">\u00a0#<\/span> Nginx Full<br \/><\/span><span class=\"pl-c\"><span class=\"pl-c\">\u00a0#<\/span> Nginx HTTP<br \/><\/span><span class=\"pl-c\"><span class=\"pl-c\">\u00a0#<\/span> Nginx HTTPS<br \/><\/span><span class=\"pl-c\"><span class=\"pl-c\">\u00a0#<\/span> OpenSSH<\/span><\/p>\n<p><span class=\"pl-c\"><span>NGINX by default, generates backup<span>\u00a0<\/span><\/span><code>.default<\/code><span><span>\u00a0<\/span>files in<span>\u00a0<\/span><\/span><code>\/etc\/nginx<\/code><span>. Remove<span>\u00a0<\/span><\/span><code>.default<\/code><span><span>\u00a0<\/span>files from<span>\u00a0<\/span><\/span><code>\/etc\/nginx<\/code><span>directory:<\/span><\/span><\/p>\n<p class=\"command\">sudo rm \/etc\/nginx\/<span class=\"pl-k\">*<\/span>.default<\/p>\n<p><span>Place syntax highlighting of NGINX configuration for Vim editor into<span>\u00a0<\/span><\/span><code>~\/.vim<\/code><span>:<\/span><\/p>\n<pre><span class=\"pl-c\"><span class=\"pl-c\">#<\/span> For regular non-root user<\/span>&#13;\nmkdir <span class=\"pl-k\">~<\/span>\/.vim\/&#13;\ncp -r <span class=\"pl-k\">~<\/span>\/nginx-1.15.0\/contrib\/vim\/<span class=\"pl-k\">*<\/span> <span class=\"pl-k\">~<\/span>\/.vim\/&#13;\n<span class=\"pl-c\"><span class=\"pl-c\">#<\/span> For root user<\/span>&#13;\nsudo mkdir \/root\/.vim\/&#13;\nsudo cp -r <span class=\"pl-k\">~<\/span>\/nginx-1.15.0\/contrib\/vim\/<span class=\"pl-k\">*<\/span> \/root\/.vim\/<\/pre>\n<p><strong>NOTE<\/strong><span>: By doing the above step, you will get a nice syntax highlighting when editing NGINX configuration files in Vim editor.<\/span><\/p>\n<p><a class=\"fancybox\" id=\"img-nginx-syntax-hgl\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_build_nginx_from_source_on_ubuntu_1804_lts\/big\/nginx-syntax-hgl.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-build-nginx-from-source-on-ubuntu-18-04-lts-3.png\" alt=\"Nginx Syntax highlighting in Vim\" width=\"550\" height=\"317\" title=\"\"><\/a><\/p>\n<p><span>Create<span>\u00a0<\/span><\/span><code>conf.d<\/code><span>,<span>\u00a0<\/span><\/span><code>snippets<\/code><span>,<span>\u00a0<\/span><\/span><code>sites-available<\/code><span><span>\u00a0<\/span>and<span>\u00a0<\/span><\/span><code>sites-enabled<\/code><span><span>\u00a0<\/span>directories in<span>\u00a0<\/span><\/span><code>\/etc\/nginx<\/code><span><span>\u00a0<\/span>directory:<\/span><\/p>\n<p class=\"command\">sudo mkdir \/etc\/nginx\/{conf.d,snippets,sites-available,sites-enabled}<\/p>\n<p><span>Change permissions and group ownership of NGINX log files:<\/span><\/p>\n<p class=\"command\">sudo chmod 640 \/var\/log\/nginx\/<span class=\"pl-k\">*<br \/><\/span>sudo chown nginx:adm \/var\/log\/nginx\/access.log \/var\/log\/nginx\/error.log<\/p>\n<p><span>Create logrotation config for NGINX.<\/span><\/p>\n<p class=\"command\">sudo vim \/etc\/logrotate.d\/nginx<\/p>\n<p><span>Populate the file with the below text, then save and exit:<\/span><\/p>\n<pre>\/var\/log\/nginx\/<span class=\"pl-k\">*<\/span>.log {&#13; daily&#13; missingok&#13; rotate 52&#13; compress&#13; delaycompress&#13; notifempty&#13; create 640 nginx adm&#13; sharedscripts&#13; postrotate&#13; <span class=\"pl-k\">if<\/span> [ <span class=\"pl-k\">-f<\/span> \/var\/run\/nginx.pid ]<span class=\"pl-k\">;<\/span> <span class=\"pl-k\">then<\/span>&#13; <span class=\"pl-c1\">kill<\/span> -USR1 <span class=\"pl-s\"><span class=\"pl-pds\">`<\/span>cat \/var\/run\/nginx.pid<span class=\"pl-pds\">`<\/span><\/span>&#13; <span class=\"pl-k\">fi<\/span>&#13; endscript&#13;\n}<\/pre>\n<p><span>Remove all downloaded files from home directory:<\/span><\/p>\n<p class=\"command\"><span class=\"pl-c1\">cd<\/span> <span class=\"pl-k\">~<br \/><\/span>rm -rf nginx-1.15.0\/ openssl-1.1.0h\/ pcre-8.42\/ zlib-1.2.11\/<\/p>\n<p><span>That&#8217;s it. Now, you have the latest version of NGINX installed by building it from source code. It is compiled statically against some important libraries like OpenSSL. Often, system-supplied version of OpenSSL is outdated. By using this method of installing with a newer version of OpenSSL, you can take advantage of new ciphers like<span>\u00a0<\/span><\/span><code>CHACHA20_POLY1305<\/code><span><span>\u00a0<\/span>and protocols like<span>\u00a0<\/span><\/span><strong>TLS 1.3<\/strong><span><span>\u00a0<\/span>that will be available in OpenSSL<span>\u00a0<\/span><\/span><code>1.1.1.<\/code><span>\u00a0Also, by compiling your own binary, you are able to tailor what functionality your NGINX will provide, which is much more flexible than installing a pre-built binary.<\/span><\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fhow-to-build-nginx-from-source-on-ubuntu-1804-lts%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-build-nginx-from-source-on-ubuntu-18-04-lts-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fhow-to-build-nginx-from-source-on-ubuntu-1804-lts%2F&amp;text=How+to+Build+NGINX+from+source+on+Ubuntu+18.04+LTS&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-build-nginx-from-source-on-ubuntu-18-04-lts-5.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-build-nginx-from-source-on-ubuntu-18-04-lts-6.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fhow-to-build-nginx-from-source-on-ubuntu-1804-lts%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-build-nginx-from-source-on-ubuntu-18-04-lts-7.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>NGINX\u00a0(pronounced\u00a0&#8220;engine x&#8221;) is an open source web server software designed with high concurrency in mind, that can be used as\u00a0HTTP\/HTTPS server,\u00a0reverse proxy server,\u00a0mail proxy server,\u00a0software load balancer,\u00a0TLS terminator,\u00a0caching server&#8230; It is an extremely modular piece of software. Even some of the seemingly &#8220;built-in&#8221; pieces of the software, such as GZIP or SSL, are actually built &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-5193","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/5193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=5193"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/5193\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=5193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=5193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=5193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}