Skip to content

Instantly share code, notes, and snippets.

@ninjarobot
Last active September 6, 2022 05:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ninjarobot/efa4bcb91d558fa25691691602316600 to your computer and use it in GitHub Desktop.
Save ninjarobot/efa4bcb91d558fa25691691602316600 to your computer and use it in GitHub Desktop.
Building nginx for macOS without homebrew
# default target so running 'make' by itself wll download and build nginx
build: nginx
cd nginx && ./configure --with-pcre=../pcre2 && make
nginx: pcre2
curl -O https://nginx.org/download/nginx-1.21.6.tar.gz
tar -xzvf nginx-*.tar.gz
mv nginx-*/ nginx
rm nginx-*.tar.gz
pcre2:
curl -OL https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.40/pcre2-10.40.tar.gz
tar -xzvf pcre2-*.tar.gz
mv pcre2-*/ pcre2
rm pcre2-*.tar.gz
# clean removes files used to build nginx to allow for a fresh build
clean:
rm -rf nginx* *.tar.gz pcre2*
# install requires elevated permissions and installs to the default /usr/local/nginx
install: build
cd nginx && sudo make install
# some handy symlinks
symlinks:
ln -s /usr/local/nginx/logs/access.log access.log
ln -s /usr/local/nginx/conf/nginx.conf nginx.conf
ln -s /usr/local/nginx/logs/error.log error.log
# purge will stop nginx and remove everything created when installing
purge: clean
rm -f *.log nginx.conf
sudo /usr/local/nginx/sbin/nginx -s quit
sudo rm -rf /usr/local/nginx
# backup makes a copy of nginx.conf
backup:
cp /usr/local/nginx/conf/nginx.conf backup.nginx.conf
# restore puts the backup of nginx.conf back into place
restore:
sudo cp backup.nginx.conf /usr/local/nginx/conf/nginx.conf
start:
sudo /usr/local/nginx/sbin/nginx
stop:
sudo /usr/local/nginx/sbin/nginx -s quit
@ninjarobot
Copy link
Author

Since nginx is so lightweight, I hate to depend on having homebrew to install it on macOS (plus it breaks sometimes), so I made a Makefile for it instead (works on both M1 and Intel Macs). Here's a quick guide:

make install
Downloads the source, builds it, and installs it to /usr/local/nginx. This will use sudo to copy files to /usr/local, so you'll be prompted for a password. Don't run it by itself as sudo or the source download and build artifacts will all be created as root.

make purge
Removes the downloaded source and build artifacts, stops nginx, and removes it totally from your system. Requires sudo to remove it, so you'll be prompted.

make clean
Just removes the downloaded source and build artifacts. It doesn't remove the installation.

make start
Starts nginx. This also uses sudo and you'll be prompted for a password.

make stop
Stops nginx. Also uses sudo.

make backup
You're doing local development and probably will want to backup your nginx.conf from time to time. This just copies it to your current directory so you can purge your build and still have your configuration around.

make restore
Restores that backup, if you have one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment