●▲■ 개발일기

[리눅스] M1맥북에서 NGINX + PHP 설치, 연동하기 본문

👉 Linux

[리눅스] M1맥북에서 NGINX + PHP 설치, 연동하기

●▲■ PRINT 2022. 5. 10. 12:15

맥북 M1에서 웹서버 NGINX, PHP를 설치하고 연동을 해본다.

phpinfo까지 출력해보기.

 

패키지 관리자는 homebrew을 사용함.

먼저 nginx를 먼저 설치해본다. 보통 인텔맥은 brew install nginx라고 명령하면 설치되는데 M1에서는 에러가 날 수도 있음.

(로제타를 사용해서 열기 체크)

arch -arm64 brew install nginx

 

nginx가 설치되었다면 다음 php를 설치한다. (2022년 5월 9일 현재 8.0 버젼 설치됨)

arch -arm64 brew install php

 

 

 

둘다 설치가 되었다면 nginx -v, php -v를 명령하여 설치를 확인한다.

아래 그림처럼 나왔다면 제대로 설치된거임.

 

 

 

설치 이후에는 nginx, php를 구동시켜줘야함.

brew services 명령하면 아래 그림처럼 초록색 started가 나오면 제대로 둘다 작동중인거임.

brew services start nginx
brew services start php
// 서비스 시작 이후 제대로 실행되고 있는지 확인
brew services

 

 

 

nginx는 설치 이후 기본 8080포트로 페이지를 확인할 수 있음.

아래 명령으로 'It works!'페이지가 뜨는것을 확인한다.

localhost:8080

페이지가 위치하는 경로는 

opt/homebrew/var/www 에 위치하고 있었다.

위 경로로 들어가보면 index.html이 생성되어 있다.

 

 

 

이제 nginx와 php를 연동해줘야함.

nginx.conf 파일을 수정해줘야함. 파일 경로는 opt/homebrew/etc/nginx에 있다.

두가지 영역을 변경해줘야 하는데

첫번째는 location / 부분에 index의 php확장자 추가.

두번째는 location ~ \.php$ 부분의 fastcgi_param SCRIPT_FILENAME 의 경로 수정. ($document_root$...)

요렇게 해주고 brew services restart nginx, brew services restart php@8.0 해주고 브라우저에서 PHP가 제대로

뜨는지 확인한다. <?php phpinfo(); ?>

 

//root경로를 변경해주려면 45줄을 변경해주면된다.

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm index.php; //index.php 추가
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        
        // 아래 부분 주석 다 풀어줌. 그리고 73번줄에 SCRIPT_FILENAME 뒤에
        // /scripts를 $document_root로 바꿔준다
        
        #location ~ \.php$ {	
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}