OS : Ubuntu 20.04

nginx : 

 

1. 우분투 모든 패키지 최신화하기

sudo apt update
sudo apt upgrade

 

2. 우분투 20.04에 최신 Nginx 설치

2-1. 기존에 Nginx가 설치 되어있다면, 먼저 삭제 합니다.

sudo systemctl stop nginx
sudo apt-get purge nginx -y && sudo apt autoremove nginx -y

2.2 최신 Nginx PPA를 추가합니다.

sudo add-apt-repository ppa:ondrej/nginx-mainline
sudo apt update

2.3 Nginx 설치

sudo apt install nginx-core nginx-common nginx nginx-full

 

3. 저장소에 Nginx 소스코드 추가

(Modsecurity를 컴파일 하기 위해서)

3-1. apt 구성 파일을 엽니다.

sudo nano /etc/apt/sources.list.d/ondrej-ubuntu-nginx-mainline-*.list

3-2. #주석처리 되어있는 라인 해제

# deb-src http://ppa.launchpad.net/ondrej/nginx-mainline/ubuntu/ focal main

** 아래와 같이 형태로 만들면 됩니다 **
deb http://ppa.launchpad.net/ondrej/nginx-mainline/ubuntu focal main
deb-src http://ppa.launchpad.net/ondrej/nginx-mainline/ubuntu focal main

3-3. apt update

sudo apt update

 

4. Nginx 소스 다운로드

- Modsecurity를 컴파일 하려면 Nginx 소스코드를 다운로드 해야 합니다.

 

4-1. 디렉토리 생성 및 구성

sudo mkdir /usr/local/src/nginx
cd /usr/local/src/nginx

#필요한 경우, 생성한 디렉터리에 권한을 할당합니다.
sudo chown username:username /usr/local/src -R

4-2. 종속성 설치 및 소스코드 다운로드

sudo apt install dpkg-dev
sudo apt source nginx


## 아래와 비슷한 에러메시지가 나타날 수 있지만, 무시해도 됩니다 ##
dpkg-source: info: extracting nginx in nginx-1.21.1
dpkg-source: info: unpacking nginx_1.21.1.orig.tar.gz
dpkg-source: info: unpacking nginx_1.21.1-1+ubuntu20.04.1+deb.sury.org+1.debian.tar.xz
dpkg-source: info: using patch list from debian/patches/series
dpkg-source: info: applying 0001-Make-sure-signature-stays-the-same-in-all-nginx-buil.patch
dpkg-source: info: applying 0002-define_gnu_source-on-other-glibc-based-platforms.patch
W: Download is performed unsandboxed as root as file 'nginx_1.21.1.orig.tar.gz' couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)

4.3 다운로드 받은 Nginx 버전과 현재 설치되어있는 Nginx 버전이 같은지 확인합니다.

- 같은 버전으로 다운받아야 컴파일 할 수 있습니다.

ls

## 결과 예시 ##
onsemiro@ubuntu:/usr/local/src/nginx$ ls
nginx-1.21.1
nginx_1.21.1-1+ubuntu20.04.1+deb.sury.org+1.debian.tar.xz
nginx_1.21.1-1+ubuntu20.04.1+deb.sury.org+1.dsc
nginx_1.21.1.orig.tar.gz
nginx_1.21.1.orig.tar.gz.asc


nginx -v

## 결과 예시 ##
onsemiro@ubuntu:/usr/local/src/nginx$ nginx -v
nginx version: nginx/1.21.1

 

5. ModSecurity용 libmodsecurity3 설치

5.1 GIthub에서 ModSecurity 리포 복제

sudo apt install git

git clone --depth 1 -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity /usr/local/src/ModSecurity/
cd /usr/local/src/ModSecurity/

5-2. libmodsecurity3 종속성 설치

sudo apt install gcc make build-essential autoconf automake libtool libcurl4-openssl-dev liblua5.3-dev libfuzzy-dev ssdeep gettext pkg-config libpcre3 libpcre3-dev libxml2 libxml2-dev libcurl4 libgeoip-dev libyajl-dev doxygen -y

5-3. 하위 모듈 설치 및 업데이트

git submodule init
git submodule update

 

6. ModSecurity 환경 구축

6-1. build & configure

./build.sh
./configure

## 위 2개 명령 실행 시, 아래의 오류가 표시될 수 있지만 무시해도 됩니다 ##
fatal: No names found, cannot describe anything.

6-2. 코드 컴파일

make

sudo make install

 

7. ModSecurity-nginx 커넥터 설치

7-1 ModSecurity-nginx 리포 복제

sudo git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git /usr/local/src/ModSecurity-nginx/

7-2. ModSecurity-nginx 종속성 설치

sudo apt build-dep nginx
sudo apt install uuid-dev

7-3. 컴파일

sudo ./configure --with-compat --add-dynamic-module=/usr/local/src/ModSecurity-nginx
sudo make modules

7-4. 동적모듈 복사

sudo cp objs/ngx_http_modsecurity_module.so /usr/share/nginx/modules/

 

8. Nginx conf파일 구성 (ModSeucrity-nginx 커넥터 로드 및 구성)

8-1. nginx.conf 에서 ModSecurity 활성화

sudo nano /etc/nginx/nginx.conf

## 파일 상단에 추가 ##
load_module modules/ngx_http_modsecurity_module.so;

## http {} 섹션에 추가 ##
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/modsec-config.conf;

8-2. ModSecurity용 디렉토리 및 파일 생성 및 구성

sudo mkdir /etc/nginx/modsec/
sudo cp /usr/local/src/ModSecurity/modsecurity.conf-recommended /etc/nginx/modsec/modsecurity.conf
sudo nano /etc/nginx/modsec/modsecurity.conf

## line 7
SecRuleEngine DetectionOnly -> SecRuleEngine On

## line 224
# Log everything we know about a transaction.
SecAuditLogParts ABIJDEFHZCopied! -> SecAuditLogParts ABCEFHJKZCopied!

8-3. conf 파일 생성

sudo nano /etc/nginx/modsec/modsec-config.conf

8-4. 생성한 파일에, 다음 라인 추가

Include /etc/nginx/modsec/modsecurity.conf

8-5. ModSecurity 유니코드 매핑

sudo cp /usr/local/src/ModSecurity/unicode.mapping /etc/nginx/modsec/

8-6. nginx 테스트

sudo nginx -t

## 결과 ##
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

## nginx 재시작 ##
sudo systemctl restart nginx

 

9. ModSecurity용 OWASP 핵심 규칙 세트 설치

9-1.  OWASP CRS 3.3.2 다운로드

wget https://github.com/coreruleset/coreruleset/archive/refs/tags/v3.3.2.zip

9-2. 압축 풀기

## unzip 설치 ##
sudo apt install unzip

## 압축 풀기 ##
sudo unzip v3.3.2.zip -d /etc/nginx/modsec

9-3. Core Rule Set 복사

sudo cp /etc/nginx/modsec/coreruleset-3.3.2/crs-setup.conf.example /etc/nginx/modsec/coreruleset-3.3.2/crs-setup.conf

9-4. Core Rule Set 추가

sudo nano /etc/nginx/modsec/modsec-config.conf

## 아래 명령문 추가 ##
Include /etc/nginx/modsec/coreruleset-3.3.2/crs-setup.conf
Include /etc/nginx/modsec/coreruleset-3.3.2/rules/*.conf

## nginx 테스트 ##
sudo nginx -t

## nginx 재시작 ##
sudo systemctl restart nginx

 

 

참고: https://ko.linuxcapable.com/how-to-install-modsecurity-with-nginx-on-ubuntu-20-04/

sronsemiro.tistory.com/9

 

Ubuntu 18.04.4 LTS, Nginx에 Let's Encrypt 설치 및 HTTPS적용(SSL)

1. Certbot 설치 // add repo $ sudo add-apt-repository ppa:certbot/certbot // install certbot $ sudo apt install python-certbot-nginx 2. HTTPS(SSL)을 설정할, nginx 설정 체크 // check server_name $ su..

sronsemiro.tistory.com

기존 시스템을 유지하고 있었으므로,

Ubuntu 18.04.4 / nginx 로 운영중이다.

 

예전에 작성해두었던, 위 글을 참조해서 오랜만에 SSL을 적용하려고 아래 명령어를 입력했다.

certbot --nginx -d AAA.co.kr -d www.AAA.co.kr

 

그랬더니, 생각보다 오랜 시간이 걸린 후, 아래와 같은 에러 메시지가 나왔다.

Failed authorization procedure. AAA.co.kr (http-01): urn:ietf:params:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://AAA.co.kr/.well-known/acme-challenge/96Ot7ZF9cVMwcycFhodd96TXi97sFQ9OR7Ln2sPpwN4 [183.111.125.112]: "\n<!DOCTYPE html>\n<html class=\"html\" lang=\"ko-KR\" itemscope itemtype=\"http://schema.org/WebPage\">\n<head>\n\t<meta charset=\"UTF-8\">\n", www.AAA.co.kr (http-01): urn:ietf:params:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://AAA.co.kr/.well-known/acme-challenge/YTz6-bfcu9UWltNrFoqkYxrURdasHSTSePse7oH6zyI [183.111.125.112]: "<html>\r\n<head><title>504 Gateway Time-out</title></head>\r\n<body>\r\n<center><h1>504 Gateway Time-out</h1></center>\r\n<hr><center>ng"

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: AAA.co.kr
   Type:   unauthorized
   Detail: Invalid response from
   http://AAA.co.kr/.well-known/acme-challenge/96Ot7ZF9cVMwcycFhodd96TXi97sFQ9OR7Ln2sPpwN4
   [183.111.125.112]: "\n<!DOCTYPE html>\n<html class=\"html\"
   lang=\"ko-KR\" itemscope
   itemtype=\"http://schema.org/WebPage\">\n<head>\n\t<meta
   charset=\"UTF-8\">\n"

   Domain: www.AAA.co.kr
   Type:   unauthorized
   Detail: Invalid response from
   http://AAA.co.kr/.well-known/acme-challenge/YTz6-bfcu9UWltNrFoqkYxrURdasHSTSePse7oH6zyI
   [183.111.125.112]: "<html>\r\n<head><title>504 Gateway
   Time-out</title></head>\r\n<body>\r\n<center><h1>504 Gateway
   Time-out</h1></center>\r\n<hr><center>ng"

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address.

이전이랑 똑같이 했는데,.. 왜 안되지..?

 

이것저것 찾아보니, 발생할 수 있는 이유는 여러가지가 있었다.

1. 해당 주소가 https로 리다이렉트 되고 있는 경우.

2. 해당 URL의 .well-known/acme-challenge/ <=== 이 경로로 접근이 안되는경우

3. 위 에러코드에도 나오듯이 제대로 된 IP주소가 아니거나 DNS 설정이 잘못된 경우 

등등..

 

근데 나는 아무것도 해당되지 않았다.

그래서 어떻게 해야 할까 고민하다가.

 

인증서만 발급받고 나머지 설정은 수동으로 하는 방법을 찾았고, 시도해봤다.

 

letsencrypt certonly --webroot --webroot-path=/var/www/AAA -d AAA.co.kr -d www.AAA.co.kr

certbot은 인증서를 발급받고, 설정까지 자동으로 해주는 반면

letsencrypt 명령어를 사용하면, 수동으로 설정해줄 수 있다. 

(이게 맞나? 정확히는 모르겠다..)

 

--webroot --webroot-path

여기서 --webroot는 인증서를 발급받을 홈페이지 소스가 있는 폴더의 경로를 적어주면 된다.

 

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for AAA.co.kr
http-01 challenge for www.AAA.co.kr
Using the webroot path /var/www/1800_7058_co_kr for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/AAA.co.kr/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/AAA.co.kr/privkey.pem
   Your cert will expire on 2020-12-18. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

 

명령어를 실행하면, 위와 같이 잘 되었다고 나온다.

그러면, /etc/letsencrypt/archive/AAA.co.kr 경로아래에 인증서들이 발급된 것을 확인할 수 있다.

 

그럼 이제, 이 인증서들을 이용해서, nginx에 설정해주면 된다.

server {
 ## 이 부분은 http로 접속하면, https로 리다이렉트 시켜주는 부분이다 ##
 listen 80;
 root /var/www/AAA;
 server_name AAA.co.kr www.AAA.co.kr;

 return       301 https://$server_name$request_uri;
}

server {
 listen 443 ssl http2;
 root /var/www/sbcompany_co_kr;
 server_name AAA.co.kr www.AAA.co.kr;
 index index.php;

 ## 아래 4줄이 아까 발급받은 인증서를 설정 하는 부분이다. ##
 ssl_certificate /etc/letsencrypt/live/AAA.co.kr/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/AAA.co.kr/privkey.pem;
 include /etc/letsencrypt/options-ssl-nginx.conf;
 ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

 modsecurity on;
 modsecurity_rules_file /etc/nginx/modsec/modsec_includes.conf;
 #modsecurity_rules_file /etc/nginx/modsec/modsecurity.conf;

 location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
     access_log        off;
     log_not_found     off;
     expires           360d;
 }

 access_log /var/log/nginx/sbcompany_co_kr/access.log;

 location / {
 try_files $uri $uri/ /index.php?q=uri&$args;
 }

 location ~ \.php$ {
 include snippets/fastcgi-php.conf;
 fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
 }

 location ~ /xmlrpc.php {
  deny all;
  return 444;
 }
}

nginx에 해당 사이트의 site설정에 위 4줄을 추가해주면 된다.

끝~~

 

서버환경

Ubuntu 18.04.4 LTS

Nginx 1.19.1

 

기존 운영 도메인

1. AAA.com

2. BBB.Com (SSL 적용완료)

3. a.CCC.com

4 b.CCC.com (SSL 적용완료)

..등등 약 30개 정도 사이트

 

위 환경의 서버에서, Letsencrypt를 이용해서, 추가로 x.CCC.com 사이트에 SSL를 설정하고 있었다.

그런데, 자꾸 인증과정에서 에러가 발생했고,

추가적으로, https://asdf.CCC.com 과 같이 현재 서버에는 설정되어 있지 않은, 주소로 접속을 시도하면, 

기존에 SSL이 설정되어 있던, https://BBB.com 으로 접속이 되는 현상이 있었다.

 

이게, 기존부터 이렇게 되었던 건지,

아니면, 어떤 이유로 발생 했는지는 모르겠다.

 

여기저기 찾아보다가, 

qastack.kr/server/578648/properly-setting-up-a-default-nginx-server-for-https

 

위의 링크를 찾게 되었고, 해결했다.

 

이유는 바로, default 파일이 없어서 발생했던거다.

또한, default 파일도, http로 접속했을때와, https로 접속했을때 둘다를 설정해주어야 한다.

 

나는 그래서 아래와 같이 설정했다.

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  listen 443 default_server;
  listen [::]:443 default_server;

  root /var/www/html;
  index index.html;

  server_name _;

  ssl_certificate /etc/nginx/ssl/nginx.crt;
  ssl_certificate_key /etc/nginx/ssl/nginx.key;

  location / {
    try_files $uri $uri/ =404;
    }
}

 

이렇게 설정후, nginx를 재시작 또는 reload를 하면,

위의 설명했던 현상이 발생하지 않게 된다.

 

또, default를 설정하지 않았을 때,

없는 주소로, https 를 이용해서 접속하게 되면,

https://BBB.com이 접속되는데, 

아마 그 이유는 sites-available 안에 파일 중,

SSL이 설정된 가장 첫번째 파일의 주소로 접속되는 것 같다. (알파벳 순)

 

이상하게, 서버가 느려지는 것 같은 느낌이 들어서,

nginx 서버 튜닝을 알아보다 보니, log에 관련된 이야기가 나왔다.

그래서, 어떻게 되어있나 살펴보니.. 헉..

80만 라인이 넘어가고 있었다..

 

아아..

 

어떻게 해야 하나 찾아보다가

logrotate를 알게 되었다.

 

일단 logrotate를 설치하자.

sudo apt-get install logrotate

 

그리고, /etc/logrotate.d/nginx 파일을 만들고

아래 코드를 추가하자.

## 보통은 이 부분만 만든다.
/var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}

## 나는 로그를 서브도메인별로 따로따로 저장하게 설정이 되어있어서,
## 이 부분까지 설정해 주었다.
/var/log/nginx/*/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}

각각이 뜻하는 바를 알아보면,

  • daily : 하루치를 따로따로 보관하자.
  • missingok : 로그 파일이 없어도 에러를 내지말자.
  • rotate 52 : 로그 파일을 52개까지만 보관하자. 위에 daily로 설정되어 있으니, 52일치만 보관하고, 53일째 되는 로그는 삭제.
  • compress : 로그를 압축해서 보관하자.
  • notifempty: 로그파일이 비어있으면, 로테이트 하지 않습니다.
  • create 640 nginx adm : 로그파일은 새로 생성시에, 파일 권한을 640으로 생성하고, 소유자/그룹은 nginx로 하자.
  • postrotate : 로테이트 작업이 끝난후에, 실행할 스크립트를 입력합니다.

 

위 설정 후, 정상적으로 설정되었는지, 테스트하기 위해서 아래의 명령어를 실행합니다.

logrotate -d -f /etc/logrotate.d/nginx

 

정상적으로 설정되었다면, /etc/cron.daily/logrotate 파일로 인해서, 매일 자동 실행됩니다.

 

혹시, /etc/cron.daily/logrotate 파일이 없다면,

아래와 같이 만들어 주자.

 

#!/bin/sh

# Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
do
    [ -e "$logfile" ] && echo "\"$logfile\" $date"
done >> status.clean
mv status.clean status

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

 

 

참고 : https://www.lesstif.com/system-admin/nginx-log-rotate-logrotate-75956229.html

* Ubuntu 18.04.4 LTS / Nginx 1.19.0 버전에서 테스트 했습니다.

* Ubuntu에 Nginx가 설치되어있다고 가정합니다.

 

필요 라이브러리 설치

apt-get install autotools-dev automake libtool m4 pkgconf libcurl4-openssl-dev libxml2 libxml2-dev libgeoip-dev libpcre3 libpcre3-dev zlib1g-dev g++

 

libmodsecurity 다운 및 컴파일


// Clone 
$ git clone --depth 1 -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity

// Compile
$ cd ModSecurity
$ git submodule init && git submodule update && ./build.sh && ./configure && make && make install

 

Nginx connector 다운 및 컴파일

// Clone
$ git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git

// Check Nginx version
$ nginx -v

nginx version: nginx/1.19.0

// Nginx source code download
$ wget http://nginx.org/download/nginx-1.19.0.tar.gz

// Compile
$ cd nginx-1.19.0
$ ./configure --with-compat --add-dynamic-module=../ModSecurity-nginx
$ make modules

// copy object file
$ cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules

 

Nginx 모듈 로드

// /etc/nginx/nginx.conf

user nginx;

worker_processes auto;


# nginx module load -->
load_module "modules/ngx_http_modsecurity_module.so";



error_log /var/log/nginx/error.log  notice;

pid /var/run/nginx.pid;

 

ModSecurity 설정 -> 아래  업데이트 된, ModSecurity 설정 참고

// 1.추천 설정 다운로드
$ mkdir /etc/nginx/modsec

$ wget -P /etc/nginx/modsec/ https://raw.githubusercontent.com/SpiderLabs/ModSecurity/master/modsecurity.conf-recommended

$ mv /etc/nginx/modsec/modsecurity.conf-recommended /etc/nginx/modsec/modsecurity.conf

// 2.“detection only” 모드에서 actively dropping traffic 로 변경
$ sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/nginx/modsec/modsecurity.conf

// 3. SecRequestBodyInMemoryLimit 줄 제거

 

Nginx 설정

// active ModSeucrity in Nginx each site conf

server {
 ...
 
 modsecurity on;
 modsecurity_rules_file /etc/nginx/modsec/modsec_includes.conf;

 ...
}

// reload nginx
$ nginx -s reload 

 

참고 : https://itzone.tistory.com/707

 

--- 2020.07.24 업데이트 ---

위글 중, ModSecurity설정에서, 

https://raw.githubusercontent.com/SpiderLabs/ModSecurity/master/modsecurity.conf-recommended

위 주소가 404 NotFound로 나온다..

 

그래서 다른 방법을 찾아서 추가한다.

 

OWASP에서 제공하는 ModSecurity Core Rule Set을 이용한다.

https://owasp.org/www-project-modsecurity-core-rule-set/

 

ModSeucrity 설정

// OWASP Core Rule Set 사용
$ mkdir /etc/nginx/modsec

$ cd /etc/nginx/modsec

$ wget https://github.com/SpiderLabs/owasp-modsecurity-crs/archive/v3.1.0.tar.gz
$ tar -xvzf tar -xvzf v3.1.0.tar.gz
$ rm v3.1.0.tar.gz
$ cd v3.1.0.tar.gz/rules
$ cp REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
$ cp RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf

$ cp /etc/nginx/ModSecurity/modsecurity.conf-recommended /etc/nginx/modsec/modsecurity.conf
$ cp /etc/nginx/ModSecurity/unicode.mapping /etc/nginx/modsec/unicode.mapping

// make modsec conf and rules file
$ touch modsec_includes.conf

// write below in modsec_includes.conf
include modsecurity.conf
include owasp-modsecurity-crs/crs-setup.conf
include owasp-modsecurity-crs/rules/*.conf

// “detection only” 모드에서 actively dropping traffic 로 변경
$ sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/nginx/modsec/modsecurity.conf

// SecRequestBodyInMemoryLimit 줄 제거

이전 OS X 버전에서의 설치방법은 많은데, 10.15 버전 설치방법은 별로 없어서, 기록해 놓는다.

Homebrew를 이용해서 설치했기 때문에, Homebrew를 먼저 설치한다.

 

1. Homebrew 설치

아래 코드를 터미널에 붙여넣기 한다.

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

 

2. PHP 설치

brew install php

 

php 설치 후, php서비스를 시작해 준다.

brew services start php

 

그리고, 아마 기존에 있던 php 버전보다, 지금 설치한 php 버전이 더 높을테니, PHP FPM 버전을 지금 설치한 버전으로 변경해준다.

echo 'export PATH="/usr/local/opt/php/sbin:$PATH"' >> ~/.zshrc
source ~/.zshrc

 

2-1. 기존 php 삭제

brew를 이용해서, 삽질을 하다보면 여러 php가 설치될 수 있는데, 깔끔하게 지우고 시작하는게 좋다.

아래의 코드를 이용하자.

 

# Will show you any php packages you've got. make not of that!
brew list | grep php

# Will uninstall any php packages you may have
brew list | grep php | while read x; do brew uninstall --force $x; done

# You may need to run this too
rm -rf /usr/local/Cellar/php

# Clean up Launch Agents
rm ~/Library/LaunchAgents/homebrew.mxcl.php*
sudo rm /Library/LaunchDaemons/homebrew.mxcl.php*

brew untap homebrew/php
brew cleanup
brew update
brew doctor # just to make sure you're all clean

ps ax | grep php
# if some PHP daemons are still runing, reboot.

참고 URL : https://medium.com/@romaninsh/install-php-7-2-xdebug-on-macos-high-sierra-with-homebrew-july-2018-d7968fe7e8b8

 

3. Mariadb 설치

brew install mariadb

 

mariadb 설치 후, mariadb 서비스를 시작해준다.

brew services start mariadb

 

그리고, mariadb의 패스워드등을 설치하기 위해 아래 명령어를 실행시킨다.

sudo /usr/local/bin/mysql_secure_installation

 

4. nginx 설치

brew install nginx

 

nginx 설치 후, nginx 서비스를 시작한다.

brew services start nginx

 

nginx conf 파일을 열어준다.

vi /usr/local/etc/nginx/nginx.conf

 

nginx.conf 파일안에 server {} 부분은 모두 주석처리 해준다. 

nginx.conf
0.00MB

혹시 몰라서, 파일 업로드 해놓는다.

 

mkdir /usr/local/etc/nginx/servers/
cd /usr/local/etc/nginx/servers/
vi test.conf

 

servers 디렉토리를 만들고, test.conf 파일을 만든다.

#test.conf 파일
server {
    listen 8099;
    server_name localhost;
    root /Users/onsemiro/www/test;
    rewrite . /index.php;
    location / {
    index index.php index.html index.htm;
    autoindex on;
    }
    #proxy the php scripts to php-fpm
    location ~ \.php$ {
        include /usr/local/etc/nginx/fastcgi.conf;
        fastcgi_intercept_errors on;
        fastcgi_pass 127.0.0.1:9000;
    }
}

 

php 테스트 파일을 만들다.

vi /Users/onsemiro/www/test/index.php

#index.php 내용
<?php
	phpinfo();
?>

 

nginx를 재시작한다.

brew services restart nginx

 

http://localhost:8099/

에 접속해서 php 정보가 제대로 나오는지 확인해본다.

 

제대로 나오면 성공.

+ Recent posts