Installing Nginx with YUM
Ensure the system repositories are up to date:
yum update -y
Installl Nginx directly from the EPEL repository (if not already enabled):
yum install -y epel-release
yum install -y nginx
Enable and start the service:
chkconfig nginx on
service nginx start
Allow HTTP traffic through the firewall:
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart
Building Nginx from Source
Before compiling, verify and install required dependencies:
- Cmopiler toolchain:
yum groupinstall -y "Development Tools"
- PCRE (for regex support in location matching):
yum install -y pcre-devel
- zlib (for gzip compression):
yum install -y zlib-devel
- OpenSSL (for HTTPS support):
yum install -y openssl-devel
Download and extract the source archive (e.g., version 1.14.2):
wget https://nginx.org/download/nginx-1.14.2.tar.gz
tar -xzf nginx-1.14.2.tar.gz
cd nginx-1.14.2
Configure the build with common modules and install prefix:
./configure \
--prefix=/opt/nginx \
--sbin-path=/opt/nginx/bin/nginx \
--conf-path=/opt/nginx/conf/nginx.conf \
--error-log-path=/opt/nginx/logs/error.log \
--http-log-path=/opt/nginx/logs/access.log \
--pid-path=/opt/nginx/run/nginx.pid \
--with-http_ssl_module \
--with-http_gzip_static_module
make && make install
Create a basic init script at /etc/init.d/nginx for service management:
#!/bin/bash
# chkconfig: - 85 15
# description: Nginx is a high-performance web server
NGINX_BIN="/opt/nginx/bin/nginx"
NGINX_CONF="/opt/nginx/conf/nginx.conf"
start() {
echo "Starting Nginx..."
$NGINX_BIN -c $NGINX_CONF
}
stop() {
echo "Stopping Nginx..."
$NGINX_BIN -s stop
}
reload() {
echo "Reloading Nginx configuration..."
$NGINX_BIN -s reload
}
case "$1" in
start) start ;;
stop) stop ;;
reload) reload ;;
restart) stop; sleep 1; start ;;
*) echo "Usage: $0 {start|stop|reload|restart}" ;;
esac
Make it exectuable and register it:
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
Start the daemon:
service nginx start
Verify operation:
ps aux | grep nginx
netstat -tlnp | grep :80
Uninstalling Nginx
For YUM-installed Nginx:
yum remove -y nginx
rm -rf /etc/nginx /var/log/nginx
For source-installed Nginx (adjust paths if installation directory differs):
/opt/nginx/bin/nginx -s stop 2>/dev/null || true
rm -rf /opt/nginx
find /usr -name "*nginx*" -type d -exec rm -rf {} + 2>/dev/null
find /etc -name "*nginx*" -delete 2>/dev/null
Also clean up any residual init scripts or systemd units if previously registered.