How to Install Nginx on Mac: Simple Steps
To install
nginx on a Mac, use the Homebrew package manager by running brew install nginx in the Terminal. After installation, start the server with brew services start nginx and visit http://localhost:8080 to see the default Nginx page.Syntax
The main commands to install and manage Nginx on Mac using Homebrew are:
brew install nginx: Installs Nginx.brew services start nginx: Starts Nginx as a background service.brew services stop nginx: Stops the Nginx service.brew services restart nginx: Restarts Nginx.nginx -v: Checks the installed Nginx version.
bash
brew install nginx brew services start nginx brew services stop nginx brew services restart nginx nginx -v
Output
==> Downloading https://homebrew.bintray.com/bottles/nginx-1.24.0.catalina.bottle.tar.gz
==> Pouring nginx-1.24.0.catalina.bottle.tar.gz
==> Caveats
To have launchd start nginx now and restart at login:
brew services start nginx
Or, if you don't want/need a background service you can just run:
nginx
nginx version: nginx/1.24.0
Example
This example shows how to install Nginx, start it as a service, and verify it is running by accessing the default web page.
bash
brew install nginx
brew services start nginx
curl http://localhost:8080Output
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and working.</p>
</body>
</html>
Common Pitfalls
Some common mistakes when installing or running Nginx on Mac include:
- Not having Homebrew installed before running
brew install nginx. - Trying to start Nginx without using
brew services, which may cause it to stop when the terminal closes. - Accessing
http://localhostinstead ofhttp://localhost:8080, since Nginx on Mac defaults to port 8080. - Not checking for permission issues if you modify Nginx config files.
bash
Wrong: curl http://localhost Right: curl http://localhost:8080
Output
curl: (7) Failed to connect to localhost port 80: Connection refused
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and working.</p>
</body>
</html>
Quick Reference
Summary tips for installing and running Nginx on Mac:
| Command | Description |
|---|---|
| brew install nginx | Install Nginx using Homebrew |
| brew services start nginx | Start Nginx as a background service |
| brew services stop nginx | Stop the Nginx service |
| brew services restart nginx | Restart Nginx service |
| nginx -v | Check Nginx version |
| http://localhost:8080 | Default URL to verify Nginx is running |
Key Takeaways
Use Homebrew to easily install Nginx on Mac with 'brew install nginx'.
Start Nginx as a service using 'brew services start nginx' to keep it running in background.
Access Nginx default page at 'http://localhost:8080' to confirm it works.
Remember to install Homebrew first if not already installed.
Use 'nginx -v' to verify the installed Nginx version.