본문 바로가기

리눅스

우분투에 Nginx와 PHP 8.3을 소스에서 컴파일하여 설치하는 방법

반응형

우분투에 Nginx와 PHP 8.3(php-fpm)을 소스에서 컴파일하여 설치하는 방법

1. 필수 패키지 및 의존성 설치

sudo apt-get update
sudo apt-get install -y \
build-essential pkg-config autoconf make \
wget vim

2. Nginx 소스에서 컴파일 및 설치

Nginx를 소스에서 설치합니다.

sudo apt-get install -y zlib1g-dev libssl-dev libpcre3-dev libzip-dev

Nginx 소스를 다운로드합니다.

cd /usr/local/src
wget https://nginx.org/download/nginx-1.26.1.tar.gz
tar -zxvf nginx-1.26.1.tar.gz
cd nginx-1.26.1

Nginx를 컴파일하고 설치합니다.

./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--sbin-path=/usr/local/nginx/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=www-data \
--group=www-data \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-http_v3_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-http_v2_module
make -j4 && sudo make install

설치된 Nginx 버전을 확인합니다.

/usr/local/nginx/sbin/nginx -v
$ /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.26.1

Nginx 설정 파일

sudo mkdir -p /usr/local/nginx/conf.d
sudo mkdir -p /var/cache/nginx/client_temp
sudo chown www-data:www-data /var/cache/nginx
vim /usr/local/nginx/nginx.conf
더보기

---

user  www-data www-data;
worker_processes  1;

error_log  logs/error.log;

#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 65;

    #gzip on;

    include /usr/local/nginx/conf.d/default.conf;
}

---

vim /usr/local/nginx/conf.d/default.conf
더보기

---

server {
    listen       80;
    server_name  _;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location = /basic_status {
        stub_status;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}

---

ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

Nginx 서비스 파일 설정

sudo vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx -c /usr/local/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target

Nginx 서비스를 시작합니다.

sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx

3. PHP 소스에서 컴파일 및 설치

PHP를 컴파일하기 위한 필요한 라이브러리를 설치합니다.

sudo apt-get install -y \
libxml2-dev libsqlite3-dev libcurl4-openssl-dev libonig-dev \
libreadline-dev libargon2-dev libjpeg-dev libpng-dev libfreetype6-dev \
libmcrypt-dev libxslt1-dev libffi-dev libsodium-dev libexpat1-dev \
libpcre2-dev imagemagick bison re2c

PHP 소스를 다운로드합니다.

cd /usr/local/src
wget https://www.php.net/distributions/php-8.3.8.tar.gz
tar -zxvf php-8.3.8.tar.gz
cd php-8.3.8

PHP를 컴파일하고 설치합니다.

./configure \
--prefix=/usr/local/php/8.3 \
--sysconfdir=/usr/local/php/8.3/fpm \
--enable-fpm \
--enable-bcmath \
--enable-calendar \
--enable-exif \
--enable-ftp \
--enable-gd \
--enable-intl \
--enable-mbstring \
--enable-mysqlnd \
--enable-pcntl \
--enable-shmop \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-cgi \
--with-fpm-user=www-data \
--with-fpm-group=www-data \
--with-curl \
--with-ffi \
--with-gettext \
--with-mysqli \
--with-openssl \
--with-sodium \
--with-xsl \
--with-zip \
--with-zlib \
--with-pdo-mysql \
--with-readline \
--with-expat \
--with-external-pcre
make -j4 && sudo make install

설치된 PHP 버전을 합니다.

/usr/local/php/8.3/bin/php -v
$ /usr/local/php/8.3/bin/php -v
PHP 8.3.8 (cli) (built: Jun 12 2024 23:59:21) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.8, Copyright (c) Zend Technologies

PHP 심볼릭 링크 설정

sudo ln -s /usr/local/php/8.3/bin/php /usr/bin/php8.3
sudo ln -s /usr/local/php/8.3/bin/php-config /usr/bin/php-config8.3
sudo ln -s /usr/local/php/8.3/bin/phpize /usr/bin/phpize8.3
sudo ln -s /usr/local/php/8.3/sbin/php-fpm /usr/sbin/php-fpm8.3

PHP-FPM 디렉토리 생성 및 소유권 설정합니다.

sudo mkdir -p /var/log/php-fpm
sudo chown www-data.root -R /var/log/php-fpm
sudo mkdir -p /var/run/php
sudo chown www-data.www-data -R /var/run/php

PHP 설정 파일을 복사합니다.

sudo cp /usr/local/src/php-8.3.8/php.ini-production /usr/local/php/8.3/fpm/php.ini
sudo cp /usr/local/php/8.3/fpm/php-fpm.conf.default /usr/local/php/8.3/fpm/php-fpm.conf
sudo vim /usr/local/php/8.3/fpm/php-fpm.conf
더보기

---

include = /usr/local/php/8.3/fpm/php-fpm.d/www.conf

[global]
pid = /var/run/php/php8.3-fpm.pid

error_log = /var/log/php-fpm/error-8.3.log

daemonize = yes

---

sudo cp /usr/local/php/8.3/fpm/php-fpm.d/www.conf.default /usr/local/php/8.3/fpm/php-fpm.d/www.conf
sudo vim /usr/local/php/8.3/fpm/php-fpm.d/www.conf
더보기

---

[www]
user = www-data
group = www-data

listen = /var/run/php/php8.3-fpm.sock

; Set listen(2) backlog.
; Default Value: 511 (-1 on Linux, FreeBSD and OpenBSD)
;listen.backlog = 511

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
;pm.max_spawn_rate = 32
;pm.process_idle_timeout = 10s;
;pm.max_requests = 500

pm.status_path = /status
;pm.status_listen = 127.0.0.1:9001
;ping.path = /ping
;ping.response = pong

access.log = /var/log/php-fpm/$pool-access.log
access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{milli}d %{kilo}M %C%%"
slowlog = /var/log/php-fpm/log/$pool-slow.log
;request_slowlog_timeout = 0
;request_slowlog_trace_depth = 20
;request_terminate_timeout = 0
;request_terminate_timeout_track_finished = no

;rlimit_files = 1024
;rlimit_core = 0

;security.limit_extensions = .php .php3 .php4 .php5 .php7

; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp

php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/fpm-php/$pool-error.log
php_admin_flag[log_errors] = on
php_admin_value[memory_limit] = 32M

---

PHP-FPM 서비스 파일 작성

sudo vim /usr/lib/systemd/system/php8.3-fpm.service
[Unit]
Description=The PHP 8.3 FastCGI Process Manager
Documentation=man:php-fpm8.3(8)
After=network.target

[Service]
Type=simple
PIDFile=/var/run/php/php8.3-fpm.pid
ExecStart=/usr/sbin/php-fpm8.3 --nodaemonize --fpm-config /usr/local/php/8.3/fpm/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -WINCH $MAINPID

[Install]
WantedBy=multi-user.target

PHP-FPM 서비스를 시작합니다.

sudo systemctl daemon-reload
sudo systemctl enable php8.3-fpm.service
sudo systemctl start php8.3-fpm.service

PHP 모듈 설치

igbinary

wget https://pecl.php.net/get/igbinary-3.2.15.tgz

tar xvfz igbinary-3.2.15.tgz

cd igbinary-3.2.15

/usr/local/php/bin/phpize 

./configure --with-php-config=/usr/local/php/bin/php-config

make && sudo make install

imagick

sudo apt-get install -y libmagickwand-dev

sudo apt-get install -y imagemagick

wget https://pecl.php.net/get/imagick-3.7.0.tgz

/usr/local/php/bin/phpize 

./configure --with-php-config=/usr/local/php/bin/php-config

make && sudo make install

mongodb

wget https://pecl.php.net/get/mongodb-1.19.2.tgz

/usr/local/php/bin/phpize 

./configure --with-php-config=/usr/local/php/bin/php-config

make && sudo make install

rdkafka

sudo apt-get install -y librdkafka-dev

wget https://pecl.php.net/get/rdkafka-6.0.3.tgz

/usr/local/php/bin/phpize 

./configure --with-php-config=/usr/local/php/bin/php-config

make && sudo make install

redis

wget https://pecl.php.net/get/redis-6.0.2.tgz

/usr/local/php/bin/phpize 

./configure --with-php-config=/usr/local/php/bin/php-config

make && sudo make install

xmlrpc

wget https://pecl.php.net/get/xmlrpc-1.0.0RC3.tgz

/usr/local/php/bin/phpize 

./configure --with-php-config=/usr/local/php/bin/php-config

make && sudo make install

screwim

git clone https://github.com/OOPS-ORG-PHP/mod_screwim.git

/usr/local/php/bin/phpize 

./configure --with-php-config=/usr/local/php/bin/php-config

make && sudo make install

Nginx와 PHP-FPM 연동

sudo mkdir -p /usr/local/nginx/html
sudo vim /usr/local/nginx/conf.d/default.conf
server {
    listen 80;
    server_name  _;
    root /usr/local/nginx/html;
    index index.html index.htm index.php;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        fastcgi_pass   unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_index  index.php;
	    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }

    location = /basic_status {
        stub_status;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
 
    location /status {
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}

PHP 설정 확인

sudo vim /usr/local/nginx/html/info.php
<?php phpinfo(); ?>

브라우저에서 http://your_server_ip/info.php를 열어 PHP 정보 페이지를 확인합니다.

http://localhost/info.php

 

참고URL

- PECL : Package Statistics

 

728x90
반응형