How to Enable Nginx Startup Service on Amazon Linux (Alternative to update-rc.d)


2 views

Amazon Linux, based on RHEL/CentOS, uses chkconfig instead of Debian's update-rc.d for managing service startup. This trips up many admins familiar with Ubuntu/Debian systems. Here's how to properly set up Nginx as a service.

First, ensure your init script has proper LSB headers (required for chkconfig):

#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx web server

The numbers represent:

  • 2345: Runlevels where service should start
  • 99: Start priority
  • 20: Stop priority

Before registering the service:

sudo chmod +x /etc/init.d/nginx

Instead of update-rc.d, Amazon Linux provides these alternatives:

# Add service to startup
sudo chkconfig --add nginx

# Enable for default runlevels
sudo chkconfig nginx on

# Verify the configuration
sudo chkconfig --list nginx

For AMIs using systemd:

sudo systemctl enable nginx
sudo systemctl start nginx

If you encounter problems:

  • Check script permissions (must be executable)
  • Verify LSB headers exist in the init script
  • Test manual start: sudo service nginx start
  • Examine logs: tail -f /var/log/nginx/error.log

To make your configuration survive instance replacement:

  1. Package your init script in a custom AMI
  2. Use EC2 User Data to reapply configuration
  3. Consider AWS Systems Manager for state management

Amazon Linux, being derived from RHEL/CentOS, uses the chkconfig system instead of Debian's update-rc.d. This catches many admins off guard when migrating from Ubuntu/Debian environments.

First, ensure your nginx init script has proper LSB headers for chkconfig compatibility:

#!/bin/sh
# chkconfig: 2345 99 20
# description: Nginx web server

The numbers represent:

  • 2345: Runlevels where service should start
  • 99: Startup priority (higher number = later start)
  • 20: Shutdown priority

Use these commands to enable autostart:

sudo chmod +x /etc/init.d/nginx
sudo chkconfig --add nginx
sudo chkconfig nginx on

For Amazon Linux 2 instances using systemd:

sudo systemctl enable nginx.service
sudo systemctl start nginx.service

Check your configuration with:

sudo chkconfig --list nginx
# Or for systemd:
sudo systemctl is-enabled nginx

If nginx fails to start at boot:

  • Check /var/log/nginx/error.log
  • Test manual start with sudo service nginx start
  • Verify dependencies are available early in boot