• [Ubuntu, Nginx] PHP 연동하기
    백엔드/Linux(Ubuntu) 2023. 3. 4. 21:58
    728x90

    PHP-FPM

    Nginx와 PHP를 연동하기 위해서는 PHP-FPM를 사용한다. PHP-FPM은 PHP FastCGI Process Manager의 약자로, PHP의 FastCGI 지원을 제공하는 프로세스 관리자이다.

     

    일반적으로 PHP 웹 애플리케이션을 실행하는 데 사용되며, PHP를 FastCGI 프로토콜을 지원하는 웹 서버와 함께 사용할 수 있도록 설계되어 상호간 효율적인 통신을 돕는다.

     

    PHP-FPM 설치

    Nginx는 PHP를 직접 지원하지 않으므로, PHP를 먼저 설치해야한다.

    이때 설치되는 php의 버전을 잘 확인해야한다.

    apt-get install php-fpm

     

     

    설치후에 설정도 자동으로 되면 좋겠지만, 아래의 경로로 이동해서 수정해야한다.

    경로 : etc/nginx/sites-available/

    server {
            listen 80 default_server;
            listen [::]:80 default_server;
    
            # SSL configuration
            #
            # listen 443 ssl default_server;
            # listen [::]:443 ssl default_server;
            #
            # Note: You should disable gzip for SSL traffic.
            # See: https://bugs.debian.org/773332
            #
            # Read up on ssl_ciphers to ensure a secure configuration.
            # See: https://bugs.debian.org/765782
            #
            # Self signed certs generated by the ssl-cert package
            # Don't use them in a production server!
            #
            # include snippets/snakeoil.conf;
    
            root /var/www/html;
    
            # Add index.php to the list if you are using PHP
            index index.php;
    
            server_name _;
    
            location / {
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    try_files $uri $uri/ =404;
            }
    
            # pass PHP scripts to FastCGI server
            #
            #location ~ \.php$ {
            #        include snippets/fastcgi-php.conf;
            #
            #       # With php-fpm (or other unix sockets):
            #        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
            #       # With php-cgi (or other tcp sockets):
            #       fastcgi_pass 127.0.0.1:9000;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #       deny all;
            #}
    }

     

    위의 location ~ \.php$ 블록 부분의 주석을 아래와 같이 삭제하고 apt-get으로 설치했던 버전을 기입한다.

     

            # pass PHP scripts to FastCGI server
            #
            location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
            #
            #       # With php-fpm (or other unix sockets):
                    fastcgi_pass unix:/var/run/php/php버전-fpm.sock;
            #       # With php-cgi (or other tcp sockets):
            #       fastcgi_pass 127.0.0.1:9000;
            }

     

    이후 설정 파일을 수정했다면, Nginx를 아래의 명령어를 사용해서 재시작한다.

    service nginx restart

     

    php가 제대로 연동되었는지 확인하려면 /var/www/html 경로에 index.php 파일명으로 아래와 같이 작성한다.

    이후, http://127.0.0.1/index.php에 접속하여 php info가 제대로 출력되는지 확인한다.

    <?php
    phpinfo();
    ?>

    문제 해결

    제대로 출력되지 않는다면, 아래의 명령어로 방화벽에 허용 규칙을 추가한다.

    ufw allow 'Nginx Full'​

    728x90

    댓글

Copyright ⓒ syudal.tistory.com