brew-nginx 실행 문제 해결하기
mac에서 brew로 설치한 nginx가 잘 실행되지 않는 경우의 몇가지 해결책이다. 뭔가 잘 안되면 sudo brew restart nginx
등으로 해결해버리는데 이는 나중에 유지보수를 어렵게 만든다.
brew 관련 명령어 몇가지
- 설치한것 확인
brew list
- 실행중인것 확인
brew services list
- 시작
brew services start nginx
- 종료 stop, 재시작은 restart를 하면 된다.
nginx의 설정이 정확한지 확인하기
nginx -t
결과는 아래와 같을 수 있다.
davelee@macmini2020 live % nginx -t
nginx: [emerg] cannot load certificate key "/etc/letsencrypt/live/chatup.benjaminlee.kr/privkey.pem": BIO_new_file() failed (SSL: error:0200100D:system library:fopen:Permission denied:fopen('/etc/letsencrypt/live/chatup.benjaminlee.kr/privkey.pem','r') error:2006D002:BIO routines:BIO_new_file:system lib)
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed
davelee@macmini2020 live % nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/usr/local/var/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed
davelee@macmini2020 live % nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
인증서 폴더 권한 오류 해결하기
brew가 /etc/letsencrypt 폴더에 접근하지 못하면서 nginx가 작동하지 않는 문제가 있었다. 해당 폴더에 권한을 others에 대해 풀어버리려고 했는데 chatgpt가 말렸다. 그래서 아래에 제시된 방법대로 진행했다.
Create a new group. Substitute 'ssl-cert' with the name of the group you want to create:
sudo dscl . -create /Groups/ssl-cert
sudo dscl . -create /Groups/ssl-cert RealName "SSL Certificates Group"
sudo dscl . -create /Groups/ssl-cert gid 600
Add a user to the new group. Substitute 'davelee' with the name of the user you want to add to the group:
sudo dscl . -append /Groups/ssl-cert GroupMembership davelee
Next, change the group of the certificates directory and grant the group read access: (이렇게 안되면 /etc/letsencrypt 도 동일하게 진행)
sudo chown -R root:ssl-cert /etc/letsencrypt/live
sudo chown -R root:ssl-cert /etc/letsencrypt/archive
sudo chmod 750 /etc/letsencrypt/live
sudo chmod 750 /etc/letsencrypt/archive
live는 archive의 한 파일을 가리키는 심볼릭 링크만 들어있다. pem파일의 권한도 변경하자.
sudo chmod 640 /etc/letsencrypt/archive/chatup.benjaminlee.kr/privkey*.pem
nginx.pid permission 해결
위의 두번째 오류인데, 예전에 brew를 sudo로 한번 실행해서 생긴 문제인 것 같다. 삭제해준다.
sudo rm /usr/local/var/run/nginx.pid
테스트도 성공하고 재실행도 했는데 안될 때
davelee@macmini2020 live % nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
davelee@macmini2020 live % brew services restart nginx
Stopping `nginx`... (might take a while)
==> Successfully stopped `nginx` (label: homebrew.mxcl.nginx)
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)
davelee@macmini2020 live % brew services list
256
Name Status User File
jenkins started davelee ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist
mysql started davelee ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
nginx error davelee
이럴 경우 nginx/error.log 를 확인해야 한다.
cat /usr/local/var/log/nginx/error.log
443을 누군가 이미 사용하고 있다는 오류가 있었다. mac에서 특정 포트 사용프로세스는 sudo lsof -i :443
로 찾을 수 있다. 여게서 pid를 알아낸 다음 sudo kill -9 {id}
로 해당 프로세스를 종료시킨다.
마지막으로 brew services restart nginx
를 해주면 실행된다.