4 Ways to Install and Configure NGINX on MacOS

Updated: January 19, 2024 By: Guest Contributor Post a comment

Introduction

NGINX is a powerful and versatile web server that’s widely used in the DevOps world for serving websites and applications. For Mac users, setting up NGINX might be for development or testing purposes. No matter the reason, this guide offers multiple solutions for getting NGINX up and running on your MacOS.

Homebrew Installation

Homebrew is a package manager for MacOS which simplifies the installation of software on MacOS. Installing NGINX via Homebrew is straightforward and the easiest among the methods.

Steps:

  1. Install Homebrew (if not installed).
  2. Run the install command for NGINX: brew install nginx
  3. Verify NGINX installation and start the service by running nginx -v then brew services start nginx
  4. Configure NGINX as needed.

TL;DR:

brew install nginx
nginx -v
brew services start nginx

Notes: Installing via Homebrew installs the binaries in a managed path, ensuring it doesn’t conflict with the system.

Building from Source

For users needing specific modules or the latest version, building NGINX from source is the answer. This allows for a customized installation but requires more steps and understanding of compiler options.

Steps:

  1. Download the latest NGINX source code.
  2. Install required build libraries and tools.
  3. Compile and build NGINX with the desired options.
  4. Start NGINX using the custom build.

Commands:

# Download Nginx source code
curl -OL http://nginx.org/download/nginx-1.21.3.tar.gz

# Extract the downloaded archive
tar zxvf nginx-1.21.3.tar.gz

# Change to the Nginx source code directory
cd nginx-1.21.3

# Configure Nginx build options
./configure

# Compile Nginx
make

# Install Nginx (requires sudo privileges)
sudo make install

# Start Nginx server
/usr/local/nginx/sbin/nginx

Notes: You must manage updates manually, and more technical knowledge is required for troubleshooting.

MacPorts Installation

Similar to Homebrew, MacPorts is another package management system that can be used to install NGINX on MacOS. This method is an alternative for those who prefer MacPorts over Homebrew.

Steps:

  1. Install MacPorts (if not installed).
  2. Install NGINX using the MacPorts install command.
  3. Start NGINX and configure as required.

Commands to run:

# Install Nginx using the package manager (assuming macOS/macOS-like system with MacPorts)
sudo port install nginx

# Start the Nginx server
sudo nginx

Notes: The process closely resembles Homebrew, and the choice often comes down to personal preference.

Docker NGINX Container

Docker allows you to run applications inside containers. Setting up NGINX using Docker on MacOS allows isolation and is ideal for development environments where the host system remains unmodified.

  1. Install Docker on MacOS.
  2. Pull the official NGINX image from Docker Hub.
  3. Run a container with the NGINX image.
  4. Configure NGINX by attaching volumes and custom configuration.

Example:

# Pull the official Nginx Docker image from Docker Hub
docker pull nginx

# Run a Docker container with the name "my-nginx", mapping port 8080 on the host to port 80 in the container, and run it in detached mode
docker run --name my-nginx -p 8080:80 -d nginx

Notes: Docker containers are ephemeral, and all changes will be lost after the container is removed unless volumes are used.

Conclusion

Installing NGINX on MacOS can be achieved through various methods, each with its own set of advantages. Users looking for straightforward installations would benefit from Homebrew or MacPorts. Advanced users needing specific configurations can opt for building from source. Docker offers an isolated environment perfect for developers. Choosing the right method depends on your requirements and technical comfort level.