Prerequisites and Build Dependencies
Before compiling Nginx from source, ensure the necessary development toolchain and libraries are present on the operating system. Execute the following package installation command to resolve dependencies for OpenSSL, zlib, GCC, and other utilities required during the compilation phase: ```
yum -y install gcc-c++ libtool openssl-devel zlib-devel make zlib openssl
</div>Building the PCRE Library
-------------------------
Nginx requires the PCRE library to enable regular expression-based URL rewriting. 1. **Retrieve the Source:** Navigate to your source directory and download the latest stable release.
<div>```
<span class="path"><span class="prompt">cd</span></span> /opt/src
<span class="cmd"><span class="prompt">wget</span></span> https://ftp.pcre.org/pub/pcre/pcre-10.42.tar.gz
tar zxvf pcre-10.42.tar.gz
</div>- **Compile and Install:**
<div>```
<span class="cmd"><span class="prompt">cd</span></span> pcre-10.42
<span class="cmd"><span class="prompt">./configure</span></span>
<span class="cmd"><span class="prompt">make && make</span> install</span>
pcre-config --version
</div>Compiling and Installing Nginx
------------------------------
Once dependencies are met, proceed with downloading and building the Nginx daemon. 1. **Setup Source Directory:**
<div>```
<span class="cmd"><span class="prompt">cd</span></span> /opt/src
<span class="cmd"><span class="prompt">wget</span></span> https://nginx.org/download/nginx-1.24.0.tar.gz
<span class="cmd"><span class="prompt">tar</span> zxvf</span> nginx-1.24.0.tar.gz
cd nginx-1.24.0
./configure
--prefix=/opt/webroot/nginx
--with-http_stub_status_module
--with-http_ssl_module
--with-pcre=/opt/src/pcre-10.42
</div>- **Execution:** Compile the binary and install it.
<div>```
<span class="cmd"><span class="prompt">make</span></span>
<span class="cmd"><span class="prompt">make</span> install</span>
/opt/webroot/nginx/sbin/nginx -v
</div>System User Configuration
-------------------------
For security best practices, Nginx should operate under a dedicated non-root user account. <div>```
<span class="cmd"><span class="prompt">/usr/sbin/groupadd</span> webapp
<span class="cmd"><span class="prompt">/usr/sbin/useradd</span> -g webapp webapp</span>
</span>
Create the configuration file at /opt/webroot/nginx/conf/nginx.conf. Below is a robust configuration template adjusting workers, logging, timeouts, and virtual hosts. Insure backups are made before overwriting existing files. ```
vim /opt/webroot/nginx/conf/nginx.conf
</div><div>```
<span class="kw">user</span> webapp webapp;
<span class="kw">worker_processes</span> 2;
<span class="kw">error_log</span> /opt/webroot/nginx/logs/error.log crit;
<span class="kw">pid</span> /opt/webroot/nginx/nginx.pid;
<span class="kw">worker_rlimit_nofile</span> 65535;
<span class="kw">events</span> {
<span class="kw">use</span> epoll;
<span class="kw">worker_connections</span> 65535;
}
<span class="kw">http</span> {
<span class="kw">include</span> mime.types;
<span class="co"># Default MIME type fallback</span>
<span class="kw">default_type</span> application/octet-stream;
<span class="co"># Custom Log Format</span>
<span class="kw">log_format</span> main '<span class="var">$remote_addr</span> - <span class="var">$remote_user</span> [<span class="var">$time_local</span>] "<span class="var">$request</span>"
'<span class="var">$status</span> <span class="var">$body_bytes_sent</span> "<span class="var">$http_referer</span>"
"<span class="var">$http_user_agent</span>" <span class="var">$http_x_forwarded_for</span>';
<span class="co"># Performance Tuning</span>
<span class="kw">sendfile</span> on;
<span class="kw">tcp_nopush</span> on;
<span class="kw">keepalive_timeout</span> 60;
<span class="kw">tcp_nodelay</span> on;
<span class="kw">client_max_body_size</span> 8m;
<span class="kw">gzip</span> on;
<span class="kw">gzip_min_length</span> 1k;
<span class="kw">gzip_buffers</span> 4 16k;
<span class="kw">gzip_http_version</span> 1.0;
<span class="kw">gzip_comp_level</span> 2;
<span class="kw">gzip_types</span> text/plain application/x-javascript text/css application/xml;
<span class="co"># Example Virtual Host Pattern</span>
<span class="kw">server</span> {
<span class="kw">listen</span> 80;
<span class="kw">server_name</span> example.com www.example.com;
<span class="kw">index</span> index.html index.php;
<span class="kw">root</span> /var/www/html;
<span class="co"># PHP-FPM Proxy Settings</span>
<span class="kw">location</span> ~ .*\.(php|php5)?$ {
<span class="kw">fastcgi_pass</span> 127.0.0.1:9000;
<span class="kw">fastcgi_index</span> index.php;
<span class="kw">fastcgi_split_path_info</span> ^((?U).+\.php)(/?.+)$;
<span class="kw">fastcgi_param</span> PATH_INFO $fastcgi_path_info;
<span class="kw">fastcgi_param</span> SCRIPT_FILENAME $document_root$fastcgi_script_name;
<span class="kw">include</span> fastcgi_params;
}
<span class="co"># Static Assets Caching</span>
<span class="kw">location</span> ~ .*\.(gif|jpg|png|css|js)$ {
<span class="kw">expires</span> 30d;
}
<span class="co"># Rewrite Logic for Front Controller</span>
<span class="kw">location</span> / {
<span class="kw">if</span> (!-e $request_filename) {
<span class="kw">rewrite</span> ^/(.*)$ /index.php?s=$1 last;
}
}
}
}
Before restarting services, validate the syntax of the configuraton file to prevent startup errors: ```
/opt/webroot/nginx/sbin/nginx -t
</div>Environment Path Adjustment
---------------------------
To allow executing `nginx` commands from any terminal session, append the binary directory to the system path. <div>```
<span class="cmd"><span class="prompt">vim</span> /etc/profile
</span>
PATH=$PATH:/opt/webroot/nginx/sbin export PATH
</div>Reload the environment configuration: <div>```
<span class="cmd"><span class="prompt">source</span> /etc/profile</span>
Integrate Nginx into the OS initialization system using a service control script. Save the following shell script to /etc/init.d/nginx: ```
vim /etc/init.d/nginx
</div><div>```
<span class="csh">#!/bin/sh</span>
# NGINX Service Control Script
# chkconfig: - 85 15
# description: NGINX HTTP Server Control
NGINX_BIN="/opt/webroot/nginx/sbin/nginx"
PID_FILE="/var/run/nginx.pid"
CONF_FILE="/opt/webroot/nginx/conf/nginx.conf"
case "$1" in
start)
echo -n "Starting nginx: "
daemon $NGINX_BIN -c $CONF_FILE
echo
;;
stop)
echo -n "Stopping nginx: "
killproc $NGINX_BIN -QUIT
rm -f /var/lock/subsys/nginx
echo
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
killproc $NGINX_BIN -HUP
;;
status)
status $NGINX_BIN
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status}"
exit 1
esac
chmod 755 /etc/init.d/nginx chkconfig --add nginx chkconfig nginx on
</div>Operational Commands
--------------------
Control the running service and apply changes dynamically: <div>```
<span class="cmd"><span class="prompt">service</span> nginx start
<span class="cmd"><span class="prompt">nginx</span> -s reload</span>
</span>
Implement a security rule to reject requests targeting unknown server names (IP address access only): ```
server { listen 80 default_server; server_name _; return 403; }
</div>