While installing Nginx from your distribution's package manager is the easiest and recommended approach for most users, compiling from source offers unparalleled flexibility and control. This allows you to enable specific modules, optimize for your server's architecture, and stay on the bleeding edge of Nginx development. However, it's a more involved process and requires a good understanding of your system and the build process. If you're new to Nginx, we strongly advise starting with the package manager installation before diving into compilation.
Before you begin, ensure you have the necessary build tools installed on your system. This typically includes a C compiler (like GCC), make, and other development libraries. The exact packages will vary depending on your Linux distribution.
sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-devNext, download the Nginx source code. You can find the latest stable and mainline releases on the official Nginx website. For this guide, we'll assume you're downloading the latest stable version.
wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar -zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1Now, it's time to configure the build. The ./configure script prepares the source code for compilation. This is where you specify which features and modules you want to include. The configure script has numerous options. You can see all available options by running ./configure --help. Here's a common set of options for a robust installation:
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-http_slice_module