Installing nginx on macOS Mojave using brew

Carvia Tech | July 10, 2019 | 2 min read | 4,747 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:

/usr/local/etc/nginx/nginx.conf
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:

http://localhost:9090

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. DevOps interview questions - Basic Concepts, Microservices, Databases, AWS
  2. Install Redis on Ubuntu 20.04 LTS
  3. Install ElasticSearch 7 on Ubuntu 20.04
  4. Install Erlang and Rabbitmq on Ubuntu 18
  5. Install and upgrade Jenkins on Ubuntu 18.04 LTS
  6. Install OpenJDK 11 on Ubuntu 18.04 LTS
  7. Install Artifactory OSS private maven repository


Find more on this topic:
Dev Ops image
Dev Ops

Dev Ops Automation - Ubuntu OS, Centos, MySql, MongoDB, Java, AWS Infrastructure.

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info