How to install Nginx on MacOS

Categories: Code, Technology

0. Install Homebrew

To check if Homebrew’s already installed, open up your Terminal and type:

brew -v

If it isn’t installed, install Homebrew with the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

After installing homebrew, update it:

 brew update

1. Install Nginx and Start it

Depending on your setup, you may or may not need to start Nginx as root (sudo)

brew install nginx
nginx

2. Check Nginx Installation

Open your web browser and navigate to:

http://localhost:8080

You will see the following message confirming that the installation was successful:

3. Nginx Config File (default server configuration) and Default HTML Directory locations

Unlike in Ubuntu, where the config file is nested in /etc/nginx/sites-enabled/, and default html directory is in /var/www/, when installing Nginx via Homebrew on MacOS, these location will slightly differ.

Config File: 
/opt/homebrew/etc/nginx/nginx.conf
Default html Directory:
/opt/homebrew/var/www/

4. Optional – Change Nginx Port from 8080 to 80

Optionally, you can change Nginx server to listen to port 80 instead of 8080, edit the nginx.conf file using the following command:

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

You will be presented with the default configuration file (as illustrated below), change the value of `listen` from 8080 to 80.

 server {
        listen       8080; # change this here to 80
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

Restart nginx for the changes to take effect using the following commands.
As mentioned, depending on your setup, you may have to run the command as `root` using `sudo nginx`:

nginx -s stop
nginx




«
»

    Leave a Reply

    Your email address will not be published. Required fields are marked *