Nginx installation with Homebrew

Ye Min Ko
2 min readJul 24, 2024

--

Photo by Manki Kim on Unsplash

Prerequisites

Make sure you have Homebrew installed on your machine.

Nginx Installation

Run this command. For more information see this.

brew install nginx

Nginx Locations

After installation, you can check the default locations of Nginx as follows:

Nginx path

Root folder of the Nginx.

/opt/homebrew/etc/nginx

Document root path

Root folder for website’s files, such as HTML, CSS, JavaScript, and images.

/opt/homebrew/var/www

Main config file

Main config file for Nginx.

/opt/homebrew/etc/nginx/nginx.conf

Multiple config files

If you want to add multiple site configurations, you can place the config files in this folder.

/opt/homebrew/etc/nginx/servers/

Commands

Start Nginx

brew services start nginx

Stop Nginx

brew services stop nginx

Restart Nginx

brew services restart nginx

Check Nginx configuration for syntax errors

nginx -t

Nginx Status

brew services list

Nginx Version

nginx -v

Setup multiple config files (Optional)

If you want to setup multiple config files, open the main config file with any text editor, such as nano or vim. For a more convenient option, you can use micro as follows:

micro /opt/homebrew/etc/nginx/nginx.conf

Clean the file and replace with follows:

worker_processes  1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

sendfile on;
keepalive_timeout 65;
include servers/*;
}

Then go to servers folder. If you don’t have servers folder create under /nginx root folder.

cd /opt/homebrew/etc/nginx/servers

Create a config file.

touch your-server-name.conf

Open the config file and add your configuration. For example:

server {
listen 80;
server_name localhost;

root /opt/homebrew/var/www/your-server-folder;

index index.html;

location / {
try_files $uri $uri/ /index.html =404;
}
}

--

--

Ye Min Ko

🌐 Web Developer | 👀 Self-Learner | 👨‍💻 An Introvert