Skip to content

Instantly share code, notes, and snippets.

@cee-dub
Created January 15, 2012 01:15
Show Gist options
  • Save cee-dub/1613746 to your computer and use it in GitHub Desktop.
Save cee-dub/1613746 to your computer and use it in GitHub Desktop.
Homebrew formula to load Nginx listening on standard ports with convenient user-land configuration.
user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
gzip on;
sendfile on;
keepalive_timeout 65;
# Magic userland configuration
include #{user_config_path};
}
require 'formula'
class NginxPlist < GithubGistFormula
url 'https://raw.github.com/gist/1613847/org.nginx.nginx.plist'
md5 '4b6b72d33cef83829017f2e51a5b9471'
end
class NginxDaemon < GithubGistFormula
homepage "https://gist.github.com/1587643"
url "https://raw.github.com/gist/1613746/nginx.conf"
md5 "8a5b17f45f01fa73af7a4ff7fbf282d5"
depends_on 'nginx'
keg_only "This formula only installs nginx configuration, there's nothing to symlink."
def ld; Pathname.new("/Library/LaunchDaemons"); end
def plist; "org.nginx.nginx.plist"; end
def user_config_path; "/Users/*/.nginx/http/*.conf"; end
def nginx_prefix; @nginx_prefix ||= Pathname.new(`brew --prefix nginx`.strip); end
def install
prefix.install 'nginx.conf'
inreplace prefix+'nginx.conf', '#{user_config_path}', user_config_path
(etc+'nginx').install prefix+'nginx.conf'
NginxPlist.new.brew do |f|
prefix.install plist
inreplace prefix+plist, '#{HOMEBREW_PREFIX}', HOMEBREW_PREFIX
system "sudo mkdir -p #{ld}"
system "sudo cp #{prefix+plist} #{ld}"
system "sudo launchctl unload -w #{ld+plist}"
end
begin
safe_system "sudo nginx -t"
system "sudo launchctl load -w #{ld+plist}"
sleep 2
system "sudo chmod 664 #{nginx_prefix+"logs/*.log"}" unless Dir.glob("#{nginx_prefix+"logs/*.log"}").empty?
system "sudo chmod 775 #{nginx_prefix+"*_temp"}" unless Dir.glob("#{nginx_prefix+"*_temp"}").empty?
rescue ErrorDuringExecution
onoe "Nginx found a problem with its configuration."
puts "Check the output of `#{Tty.blue}sudo nginx -t#{Tty.reset}' for errors."
puts <<-INSTRUCTIONS.undent
After you resolve them, run `#{Tty.blue}sudo launchctl load -w #{ld+plist}#{Tty.reset}'
to install the system LaunchDaemon and then run both
`#{Tty.blue}sudo chmod 664 #{nginx_prefix+"logs/*.log"}#{Tty.reset}' and
`#{Tty.blue}sudo chmod 775 #{nginx_prefix+"*_temp"}#{Tty.reset}' to fix file permissions.
INSTRUCTIONS
end
end
def caveats
return <<-FUN.undent
#{Tty.white}Place app-specific nginx config files in #{Tty.blue}#{user_config_path.sub(%r{/\*/}, "/#{ENV['USER']}/")}#{Tty.white}
and reload configuration with `#{Tty.blue}sudo nginx -s reload'#{Tty.reset}
FUN
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment