Mastering Nginx: A Beginner's Guide to High-Performance Web Servers

4. Installing Nginx: Compiling from Source (Advanced)

Section 4

Getting Started: Installation and Basic Configuration

Mastering Nginx: A Beginner's Guide to High-Performance Web ServersGetting Started: Installation and Basic Configuration

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-dev

Next, 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.1

Now, 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

The prefix option defines the installation directory. Other options specify paths for binaries, configuration, logs, and the user/group Nginx will run as. --with-http_ssl_module is crucial for HTTPS support, and the other --with- options enable various HTTP and stream modules. If you want to exclude certain modules, you can use --without-module_name.

チャプターへ戻る