Installing nginx on macOS Mojave using brew

Upasana | July 10, 2019 | 2 min read | 5,623 views

Installing nginx with homebrew is very easy on macOS X/Mojave. In this article we will cover nginx installation, configuration and start/stop.

Install nginx on macOS

Use brew to install nginx with below command:
$ brew install nginx

This command will install nginx and set default port in /usr/local/etc/nginx/nginx.conf to 8080 (so that you can run nginx without sudo), so you can access nginx at http://localhost:8080

There are two ways you can start nginx on your mac:

using brew services

this option will launch nginx now and restart at login in background mode:

Start nginx
$ brew services start nginx
Stop nginx
$ brew services stop nginx
without service

If you don’t want to start nginx in background mode, then you can can just run the below command to start nginx

Start nginx
$ sudo nginx
Stop nginx
$ sudo nginx -s stop

Configure nginx

The default place of nginx.conf on macOS after installing with brew is:

$ vi /usr/local/etc/nginx/nginx.conf

You can change the default port of server to something else, for example 9090, as shown below:

server {
    listen       9090;
    server_name  localhost;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;

now restart the server for changes to take effect:

Restart nginx server from terminal
$ sudo nginx -s stop
$ sudo nginx

Now browse the new url:


Upgrade brew

If you just want to upgrade the existing nginx installation using brew, you can simply run the below command:

Update nginx using brew
$ brew update
$ brew upgrade nginx

The above two commands will update the application metadata and then upgrade only nginx installation.

Or you can even upgrade all the existing applications using brew:

Upgrade all applications
$ brew upgrade
This command will update all the existing applications installed through brew.

We can cleanup older installation artifacts using cleanup command.

Cleanup brew
$ brew cleanup

Uninstall nginx

Below brew command can be used to uninstall nginx completely.

$ brew uninstall nginx

Top articles in this category:
  1. Install & configure Redis on Ubuntu
  2. Install ElasticSearch 7 on Ubuntu 20.04
  3. Install Cassandra 4 on Ubuntu 20.04
  4. Install RabbitMQ and Erlang 23 on Ubuntu 20
  5. Install OpenJDK 11 on Ubuntu 18.04 LTS
  6. MySql 8 installation on Ubuntu 20
  7. Upgrade Jenkins on Ubuntu 18.04 LTS

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.