Building Nginx from Source for Static File Hosting on Ubuntu
To compile Nginx on Ubuntu for serving static content, first install the toolchain and required libraries:
sudo apt update
sudo apt install -y build-essential libpcre3-dev zlib1g-dev libssl-dev
The build environment requires PCRE for regex support in location blocks, zlib for gzip compression modules, and OpenSSL for TLS termination capabilities.
Download the Nginx source distribution and extract the archive. Navigate to the extracted directory and configure the build parameters:
./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_gzip_static_module
make
sudo make install
The installation creates the following hierarchy under /opt/nginx:
conf/- Configuration files including nginx.confhtml/- Default web root for static assetslogs/- Access and error log filessbin/- Binary executables
Launch the server by executing the binary directly:
sudo /opt/nginx/sbin/nginx
Validate the installation by querying the process status:
pgrep -a nginx
For lifecycle management, use these signal-based commnads:
# Graceful shutdown
sudo /opt/nginx/sbin/nginx -s quit
# Immediate termination
sudo /opt/nginx/sbin/nginx -s stop
# Configuration reload without dropping connections
sudo /opt/nginx/sbin/nginx -s reload
Configure static resource delivery by modifying /opt/nginx/conf/nginx.conf. Place assets in the designated document root directory. Ensure the worker process has read permissions on all static files and directories.