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_moduleThe 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.
After configuration, you compile and install Nginx using make and make install.
make
sudo make installIt's also a good practice to create the log directory and set appropriate permissions if they don't exist.
sudo mkdir -p /var/log/nginx
sudo chown -R nginx:nginx /var/log/nginx
sudo mkdir -p /var/run/nginx
sudo chown -R nginx:nginx /var/run/nginxFinally, you can start Nginx. The executable will be located in the sbin directory specified by --sbin-path during configuration.
sudo /usr/local/nginx/sbin/nginxTo stop or reload Nginx, you'll use the same executable and appropriate signals:
sudo /usr/local/nginx/sbin/nginx -s stop
sudo /usr/local/nginx/sbin/nginx -s reloadCompiling from source gives you granular control, but it also means you'll need to manually recompile and reinstall for updates and security patches. This is a trade-off between flexibility and ease of maintenance.
graph TD
A[Download Source Code] --> B(Extract Archive)
B --> C(Configure Build Options)
C --> D(Compile Nginx)
D --> E(Install Nginx)
E --> F{Start Nginx}