[태그:] nextcloud

  • Nextcloud 설치 및 셋팅

    Nextcloud 설치 및 셋팅

    1. 사전 준비

    아래의 설치 과정은 ubuntu 22 환경에서 진행되는 절차입니다. 사전 준비가 필요하면 아래 단계를 참고해주세요

    1. Ubuntu 22.04.1 설치(VirtualBox)
    2. ubuntu update 및 upgrade
    3. Ubuntu 방화벽 iptables 설치 및 활성화
    4. Ubuntu 방화벽 iptables 설정 및 관리
    5. Ubuntu OpenSSH 설치 및 MobaXterm 설치
    6. Ubuntu SSH 포트 설정 및 연결
    7. Nginx 웹서버 설치(ubuntu)
    8. PHP 8 설치(ubuntu)
    9. Nginx + Php8 설정
    10. MariaDB 설치 및 관리(MySQL)
    11. OpenSSL 설치
    12. 무료 HTTPS 설정(Let’s Encrypt, Cloudflare)
    13. 메모리 caching APCu, Redis, Memcached 설치

    2. 설치형 클라우드 Nextcloud 이해

    Nextcloud는 개인 및 기업용 클라우드 저장소 및 협업 플랫폼입니다. Nextcloud는 2016년 Frank Karlitschek이 ownCloud 프로젝트를 떠나 개발한 오픈 소스 소프트웨어로 ownCloud의 커뮤니티 구성원들이 Nextcloud를 만들기 위해 모여 개발을 시작하였습니다.

    1) Nextcloud 특징

    Nextcloud는 개인 및 기업에서 안전하고 효율적인 클라우드 저장소 및 협업 도구로 많이 사용되고 있습니다.

    1. 파일 동기화 및 공유: Nextcloud를 사용하면 여러 디바이스 간에 파일을 동기화하고 공유할 수 있습니다.
    2. 보안: Nextcloud는 데이터 암호화, 2단계 인증 등 다양한 보안 기능을 제공하여 사용자의 데이터 보호를 강화합니다.
    3. 협업 도구: 문서 편집, 캘린더, 주소록 등과 같은 협업 도구를 포함하여 다양한 작업을 팀원들과 함께 수행할 수 있습니다.
    4. 앱 생태계: 다양한 앱과 확장 기능을 제공하여 사용자의 요구에 맞게 Nextcloud를 확장할 수 있습니다.
    5. 자체 호스팅: Nextcloud는 온프레미스나 클라우드에서 사용할 수 있으며, 사용자는 자신만의 서버를 운영하여 데이터를 관리할 수 있습니다.

    2) 장점

    1. 개인정보 보호: Nextcloud는 사용자가 직접 데이터를 관리할 수 있으므로 개인정보 보호에 용이합니다.
    2. 확장성: 다양한 앱과 확장 기능을 통해 사용자의 요구에 맞게 Nextcloud를 확장할 수 있습니다.
    3. 협업 기능: 문서 편집, 캘린더 등 다양한 협업 도구를 제공하여 팀원들과 함께 작업하기 용이합니다.

    3) 단점

    1. 기술적 지식 필요: 서버 설정 및 유지보수에 대한 기술적인 이해가 해야 합니다.
    2. 초기 설정 복잡성: 처음에 Nextcloud를 설정하는 과정은 다소 복잡할 수 있습니다.

    4) 시스템 요구사항

    플랫폼옵션
    운영 체제 (64비트)Ubuntu 22.04 LTS(권장)
    우분투 20.04 LTS
    Red Hat Enterprise Linux 8(권장)
    데비안 12
    리눅스 엔터프라이즈 서버 15
    openSUSE Leap 15.4
    CentOS 스트림
    데이터베이스MySQL 8.0+ 또는 MariaDB 10.3/10.4/10.5/10.6(권장)
    Oracle Database 11g(엔터프라이즈 구독의 일부로만 해당))
    PostgreSQL 10/11/12/13/14/15
    SQLite(테스트 및 최소 인스턴스에만 권장됨))
    웹서버Apache 2.4 및 or (권장) mod_phpphp-fpm
    nginx php-fpm
    PHP 런타임8.0(더 이상 사용되지 않음))
    8.1
    8.2(권장)

    Read in English

    3) 하드웨어 요구사항

    1. 프로세서: 1GHz 이상의 듀얼 코어 프로세서
    2. 메모리: 최소 512MB RAM (추천: 2GB 이상)
    3. 저장 공간: 최소 10GB 이상의 여유 공간
    4. 네트워크: 인터넷 연결을 위한 네트워크 인터페이스

    2. Nextcloud 설치

    nextcloud 설치는 all-in-one VM, all-in-one Docker, 웹 인스톨러 설치 등의 방법이 있습니다. 여기서는 웹 인스톨러를 사용해서 설치를 진행하겠습니다.

    1) 폴더 생성 및 설치 파일 다운로드

    nextcloud를 설치할 폴더를 생성합니다. 하위에 웹 로그를 저장할 logs 폴더와 nextcloud 를 설치할 public 폴더를 생성합니다.

    sudo mkdir  /var/www/nc.skdream.com
    sudo mkdir  /var/www/nc.skdream.com/logs
    sudo mkdir  /var/www/nc.skdream.com/public

    public 폴더에 설치 파일을 다운로드 합니다. https://download.nextcloud.com/server/installer/setup-nextcloud.php

    cd /var/www/nc.skdream.com/public
    sudo wget https://download.nextcloud.com/server/installer/setup-nextcloud.php

    public 폴더 사용자를 www-data로 변경합니다.

    sudo chown -R www-data:www-data /var/www/nc.skdream.com/public

    2) Nginx의 서버 블럭 생성

    도메인 연결을 위한 서버 블럭 파일을 생성합니다.

    sudo nano /etc/nginx/sites-available/nc.skdream.com

    아래 코드를 붙여넣고 도메인 부분을 자신의 도메인으로 변경합니다.

    upstream php-handler {
            server unix:/var/run/php/php8.2-fpm.sock;
    
    }
    # Set the `immutable` cache control options only for assets with a cache busting `v` argument
    map $arg_v $asset_immutable {
            "" "";
            default "immutable";
    }
    server {
            listen 443 ssl http2;
            listen [::]:443 ssl http2;
    
            server_name example.com;
    
            ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
            ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
            ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
            ssl_dhparam /etc/ssl/certs/dhparam.pem;
    
            ssl_protocols TLSv1.2 TLSv1.3;
            ssl_prefer_server_ciphers on;
            ssl_ciphers TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256;
            ssl_ecdh_curve secp384r1;
            ssl_session_timeout 10m;
            ssl_session_cache shared:SSL:10m;
            ssl_session_tickets off;
            ssl_stapling on;
            ssl_stapling_verify on;
            resolver 1.1.1.1 1.0.0.1 valid=300s;
            resolver_timeout 5s;
    
            access_log /var/www/nc.skdream.com/logs/access.log;
            error_log /var/www/nc.skdream.com/logs/error.log;
    
            root /var/www/nc.skdream.com/public/;
            #index index.php index.html;
    
            #Nextcloud
            # Prevent nginx HTTP Server Detection
            server_tokens off;
    
            # HSTS settings
            # WARNING: Only add the preload option once you read about
            # the consequences in https://hstspreload.org/. This option
            # will add the domain to a hardcoded list that is shipped
            # in all major browsers and getting removed from this list
            # could take several months.
            add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload" always;
    
            # set max upload size and increase upload timeout:
            client_max_body_size 512M;
            client_body_timeout 300s;
            fastcgi_buffers 64 4K;
    
            # Enable gzip but do not remove ETag headers
            gzip on;
            gzip_vary on;
            gzip_comp_level 4;
            gzip_min_length 256;
            gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
            gzip_types application/atom+xml text/javascript application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/wasm application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;
    
            # Pagespeed is not supported by Nextcloud, so if your server is built
            # with the `ngx_pagespeed` module, uncomment this line to disable it.
            #pagespeed off;
    
            # The settings allows you to optimize the HTTP2 bandwitdth.
            # See https://blog.cloudflare.com/delivering-http-2-upload-speed-improvements/
            # for tunning hints
            client_body_buffer_size 512k;
    
            # HTTP response headers borrowed from Nextcloud `.htaccess`
            add_header Referrer-Policy                   "no-referrer"       always;
            add_header X-Content-Type-Options            "nosniff"           always;
            #add_header X-Download-Options                "noopen"            always;
            add_header X-Frame-Options                   "SAMEORIGIN"        always;
            add_header X-Permitted-Cross-Domain-Policies "none"              always;
            add_header X-Robots-Tag                      "noindex, nofollow" always;
            add_header X-XSS-Protection                  "1; mode=block"     always;
    
            # Remove X-Powered-By, which is an information leak
            fastcgi_hide_header X-Powered-By;
    
            # Add .mjs as a file extension for javascript
            # Either include it in the default mime.types list
            # or include you can include that list explicitly and add the file extension
            # only for Nextcloud like below:
            include mime.types;
    
            # Specify how to handle directories -- specifying `/index.php$request_uri`
            # here as the fallback means that Nginx always exhibits the desired behaviour
            # when a client requests a path that corresponds to a directory that exists
            # on the server. In particular, if that directory contains an index.php file,
            # that file is correctly served; if it doesn't, then the request is passed to
            # the front-end controller. This consistent behaviour means that we don't need
            # to specify custom rules for certain paths (e.g. images and other assets,
            # `/updater`, `/ocs-provider`), and thus
            # `try_files $uri $uri/ /index.php$request_uri`
            # always provides the desired behaviour.
            index index.php index.html /index.php$request_uri;
            #types {
            #        text/javascript js mjs;
            #}
            # Rule borrowed from `.htaccess` to handle Microsoft DAV clients
            location = / {
                    if ( $http_user_agent ~ ^DavClnt ) {
                            return 302 /remote.php/webdav/$is_args$args;
                    }
            }
    
            location = /robots.txt {
                    allow all;
                    log_not_found off;
                    access_log off;
            }
    
    
            # Make a regex exception for `/.well-known` so that clients can still
            # access it despite the existence of the regex rule
            # `location ~ /(\.|autotest|...)` which would otherwise handle requests
            # for `/.well-known`.
            location ^~ /.well-known {
                    # The rules in this block are an adaptation of the rules
                    # in `.htaccess` that concern `/.well-known`.
    
                    location = /.well-known/carddav { return 301 /remote.php/dav/; }
                    location = /.well-known/caldav  { return 301 /remote.php/dav/; }
    
                    location /.well-known/acme-challenge    { try_files $uri $uri/ =404; }
                    location /.well-known/pki-validation    { try_files $uri $uri/ =404; }
    
                    # Let Nextcloud's API for `/.well-known` URIs handle all other
                    # requests by passing them to the front-end controller.
                    return 301 /index.php$request_uri;
            }
    
    
            # Rules borrowed from `.htaccess` to hide certain paths from clients
            location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/)  { return 404; }
            location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console)                { return 404; }
    
            # Ensure this block, which passes PHP files to the PHP process, is above the blocks
            # which handle static assets (as seen below). If this block is not declared first,
            # then Nginx will encounter an infinite rewriting loop when it prepends `/index.php`
            # to the URI, resulting in a HTTP 500 error response.
    
            # to the URI, resulting in a HTTP 500 error response.
            location ~ \.php(?:$|/) {
                    # Required for legacy support
                    rewrite ^/(?!index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|ocs-provider\/.+|.+\/richdocumentscode\/proxy) /index.php$request_uri;
    
                    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
                    set $path_info $fastcgi_path_info;
    
                    try_files $fastcgi_script_name =404;
    
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    fastcgi_param PATH_INFO $path_info;
                    fastcgi_param HTTPS on;
    
                    fastcgi_param modHeadersAvailable true;         # Avoid sending the security headers twice
                    fastcgi_param front_controller_active true;     # Enable pretty urls
                    fastcgi_pass php-handler;
    
                    fastcgi_intercept_errors on;
                    fastcgi_request_buffering off;
    
                    fastcgi_max_temp_file_size 0;
            }
            # Javascript mimetype fixes for nginx
            # Note: The block below should be removed, and the js|mjs section should be
            # added to the block below this one. This is a temporary fix until Nginx 
            # upstream fixes the js mime-type
            location ~* \.(?:js|mjs)$ {
                types { 
                    text/javascript js mjs;
                } 
                default_type "text/javascript";
                try_files $uri /index.php$request_uri;
                add_header Cache-Control "public, max-age=15778463, $asset_immutable";
                access_log off;
            }
    
            # Serve static files
            location ~ \.(?:css|svg|gif|png|jpg|ico|wasm|tflite|map|ogg|flac)$ {
                try_files $uri /index.php$request_uri;
                add_header Cache-Control "public, max-age=15778463, $asset_immutable";
                access_log off;     # Optional: Don't log access to assets
    
                location ~ \.wasm$ {
                    default_type application/wasm;
                }
            }
            location ~ \.woff2?$ {
                    try_files $uri /index.php$request_uri;
                    expires 7d;         # Cache-Control policy borrowed from `.htaccess`
                    access_log off;     # Optional: Don't log access to assets
            }
    
            # Rule borrowed from `.htaccess`
            location /remote {
                    return 301 /remote.php$request_uri;
            }
    
            location / {
                    try_files $uri $uri/ /index.php$request_uri;
            }
    
    
    }
    
    server {
            listen 80;
            listen [::]:80;
    
            server_name example.com;
            # Prevent nginx HTTP Server Detection
            server_tokens off;
    
            return 301 https://example.com$request_uri;
    }

    심볼릭 링크를 설정하고 nginxt를 재시작합니다.

    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
    sudo systemctl restart nginx

    3) DB 생성 및 권한 부여

    아래 명령어로 MariaDB에 접속합니다.

    mysql -u root -p

    MariaDB를 설치하지 않았다면 다음 글을 참고하세요 MariaDB 설치 및 관리(MySQL) – Thinknote

    Nextcloud에 사용할 데이터베이스를 생성합니다.

    CREATE DATABASE nextcloud;

    새로운 사용자를 생성합니다. 기존 사용자를 연결하려면 생략해도 됩니다.

    CREATE USER '[사용자 이름]'@'localhost' IDENTIFIED BY '[비밀번호]';

    생성한 사용자에게 새로운 데이터베이스의 권한을 부여합니다.

    GRANT ALL PRIVILEGES ON [데이터베이스 이름].* TO '[사용자 이름]'@'localhost';

    변경된 권한 설정을 적용합니다.

    FLUSH PRIVILEGES;
    exit;

    4) php 요구사항

    Nextcloud를 사용하기 위해서는 추가적인 php모듈이 해야 합니다. 아래는 Nextcloud manual 에 제시되어 있는 요구사항입니다.

    • PHP (see System requirements for a list of supported versions)
    • PHP module ctype
    • PHP module curl
    • PHP module dom
    • PHP module fileinfo (included with PHP)
    • PHP module filter (only on Mageia and FreeBSD)
    • PHP module GD
    • PHP module hash (only on FreeBSD)
    • PHP module JSON (included with PHP >= 8.0)
    • PHP module libxml (Linux package libxml2 must be >=2.7.0)
    • PHP module mbstring
    • PHP module openssl (included with PHP >= 8.0)
    • PHP module posix
    • PHP module session
    • PHP module SimpleXML
    • PHP module XMLReader
    • PHP module XMLWriter
    • PHP module zip
    • PHP module zlib

    아래 코드를 입력해서 설치되지 않은 모듈을 설치합니다. PHP8이 설치되지 않았다면 다음 글을 참고하세요. PHP 8 설치(ubuntu) – Thinknote

    sudo apt install php8.2-ctype php8.2-curl php8.2-dom php8.2-gd php8.2-mbstring php8.2-zip php8.2-intl php8.2-gmp php8.2-bcmath libmagickcore-6.q16-6-extra php8.2-apcu php8.2-bz2 php8.2-imagick php8.2-imap php8.2-ldap php8.2-mysql php8.2-smbclient php8.2-xml php8.2-zip

    php를 재시작합니다.

    sudo systemctl restart php8.2-fpm

    3. 웹 인스톨러 통한 설치

    자신의 도메인에서 웹 인스톨러에 접속해서 설치를 진행합니다.

    https://example.com/setup-nextcloud.php

    만약 Fatal error: Uncaught ValueError: Invalid or uninitialized Zip object in 에러가 발생하면 이는 압축 해제 경로가 /var/www/html로 되어있기 때문입니다. 이때는 직접 압축을 풀어서 설치해야 합니다. 압축이 풀려 있다면 도메인 url로 접속하면 됩니다.

    설치가 완료되었습니다.

    4. 보안 및 경고 확인

    관리자로 로그인하고 관리자 설정으로 들어갑니다. 이때 보안 및 설치 경고 메시지가 나온다면 항목별 해결방법을 참고하시기 바랍니다.

    1) 파일 무결성과 관련된 오류

    /etc/php/8.2/fpm/pool.d/www.conf 에서 clear_env = no의 주석을 해제합니다.

    sudo nano /etc/php/8.2/fpm/pool.d/www.conf 
    clear_env = no #주석해제

    2) 시스템 환경변수 getenv(‘path’)오류

    관리자 설정의 무결성과 관련된 파일 리스트를 확인하고 문제가 되는 파일을 삭제합니다. 그리고 아래 코드를 실행하여 무결성 문제가 해결 되었는지 확인합니다.

    sudo -u www-data php occ integrity:check-core

    3) PHP 메모리 제한

    php.ini에서 memory_limit값과 upload_max_filesize 을 512M 이상으로 설정합니다.

    Memory_limit = 1G
    upload_max_filesize = 1G

    4) 국가 전화번호 설정

    nextcloud config.php 파일에 다음 내용을 추가합니다.

    'default_phone_region' => 'kr'

    5) 트렌젝션 성능 향상 – memcached 구성

    memcached 구성 다음 글을 참고하시기 바랍니다. 메모리 caching APCu, Redis, Memcached 설치 – Thinknote

    트렌젝션 파일 잠금과 함께 memcached 를 사용하려면 redis도 설치해야 합니다.

    설치가 완료되면 nextcloud config.php 파일에 다음 내용을 추가합니다.

    'memcache.local' => '\\OC\\Memcache\\Redis',
    'memcache.distributed' => '\\OC\\Memcache\\Redis',
    'memcache.locking' => '\\OC\\Memcache\\Redis',
    'filelocking.enabled' => 'true',
    'redis' =>
        array (
            'host' => 'localhost',
            'port' => 0,
            'timeout' => 0.0,
    ),

    6) 보안 및 배경 작업 확인

    Thinknote

  • 메모리 caching APCu, Redis, Memcached 설치

    1. 메모리 캐싱 방법(APCu, Redis, Memcached)

    ubuntu에서 사용할 수 있는 메모리 캐싱 방법으로 APCu, Redis, Memcached 가 있습니다. 각각의 캐싱 도구는 다양한 용도와 요구 사항에 맞게 선택해야 합니다.

    • 예를 들어, APCu는 PHP 코드의 성능을 향상시킬 수 있지만 단일 서버에서만 사용 가능하므로 분산 환경에는 적합하지 않습니다.
    • Redis는 다양한 데이터 구조와 클러스터링 기능을 제공하여 복잡한 애플리케이션에 적합합니다.
    • Memcached는 분산 환경에서 대규모 처리량을 지원하기 위해 설계되었습니다.

    1) APCu (Alternative PHP Cache user caching)

    APCu는 PHP 확장 기능으로서 로컬 서버 내에서 데이터를 캐시하는 데 사용되며 단일 서버 환경에서 사용하기 적합합니다. APCu는 프로세스 간 데이터 공유가 불가능하며, 데이터는 해당 프로세스에서만 유지됩니다.

    1. PHP용 메모리 캐싱 시스템입니다.
    2. PHP 코드에 직접 접근하여 데이터를 캐싱할 수 있습니다.
    3. 단일 서버에서만 사용 가능하며 분산 환경에는 적합하지 않습니다.
    4. APCu는 기본적으로 메모리에 데이터를 저장하므로 빠른 읽기/쓰기 속도를 제공합니다.
    5. 하지만 APCu는 데이터 유지 및 복제를 지원하지 않으므로 서버 장애 시 데이터 손실 수 있습니다.

    2) Redis

    Redis는 오픈 소스 인메모리 데이터 구조 저장소로 다중 서버 환경에서 스케일 아웃이 가능하며, 데이터를 메모리에 저장하고 디스크에 지속적으로 저장할 수 있습니다. 다양한 데이터 구조 (문자열, 해시, 리스트, 세트 등)를 지원하며 Pub/Sub 메커니즘을 통해 메시지 브로커로도 사용할 수 있습니다.

    1. 단일 스레드로 작동하며, 이벤트 기반 아키텍처를 사용하여 동시성 문제를 해결합니다.
    2. 문자열, 해시, 목록, 세트, 정렬된 세트 등 다양한 데이터 구조를 지원합니다..
    3. Publish/Subscribe (Pub/Sub) 메커니즘을 지원하여 메시지 기반 아키텍처를 구축할 수 있습니다
    4. 마스터-슬레이브 복제 및 Redis 클러스터와 같은 분산 시스템을 지원합니다.
    5. 다양한 성능 모니터링 및 관리 도구를 제공하여 데이터베이스 인스턴스의 상태를 모니터링하고 성능을 최적화할 수 있습니다.

    3) Memcached

    Memcached는 분산 메모리 객체 캐시 시스템으로 다중 서버 환경에서 스케일 아웃이 가능하며, 데이터를 메모리에 저장합니다. 데이터는 키-값 형태로 저장되며, 복잡한 데이터 구조를 지원하지 않습니다.

    1. 분산 객체 캐싱 시스템입니다.
    2. 키-값 형태의 데이터를 저장하고 검색하는 데 사용됩니다.
    3. 분산 환경에서 사용하기 적합하며, 여러 서버 간에 데이터를 공유할 수 있습니다.
    4. Memcached는 단순한 키-값 저장소로서 복잡한 데이터 구조를 지원하지 않습니다.
    5. 메모리 사용량이 크고, 디스크에 데이터를 저장하지 않으므로 서버 장애 시 데이터 손실 수 있습니다.

    일반적으로 APCu, Redis 및 Memcached 모두 설치하고 운영할 수 있습니다.

    모든 시스템을 동시에 운영할 때는 서버 리소스 (메모리 및 CPU)를 고려해야 하며, 각 캐시 시스템의 설정과 운영 방법을 이해하고 최적화해야 합니다. 또한 데이터 일관성과 동기화 문제도 고려해야 합니다.

    2. APCu 캐싱

    1) APCu 설치

    APCu를 설치하기 위해 다음 명령어를 실행합니다.

    sudo apt install php8.2-apcu

    2) APCu 활성화(acpu.ini)

    PHP 설정 파일을 열기 위해 다음 명령어를 실행합니다. php 버전은 서버에 설치된 php 버전을 입력합니다.

    Apache 웹 서버를 사용한다면 /etc/php/8.2/apache2/php.ini을 수정해야 합니다.

    sudo nano /etc/php/8.2/apache2/php.ini

    PHP-FPM (PHP FastCGI Process Manager)에서 php를 사용한다면 sudo nano /etc/php/8.2/fpm/php.ini를 수정하거나 acpu.ini 파일을 수정합니다.
    만약 acpu.ini 파일이 없다면 /etc/php/8.2/mods-available/apcu.ini 파일을 생성하고 다음 내용을 붙여 넣습니다.

    sudo nano /etc/php/8.2/mods-available/apcu.ini
    extension = apcu.so
    apc.enabled = 1

    Read in English

    다음 명령어로 apcu 모듈을 활성화합니다.

     sudo phpenmod -v 8.2 apcu

    nginx 서버를 재시작하여 변경 사항이 적용되도록 합니다.

    sudo systemctl restart nginx

    3) APCu 실행 확인(acpu.ini)

    아래 명령어를 실행해서 phpinfo() 함수의 결과를 출력하고 APCu 관련 설정, 버전 정보, 디렉토리 경로 등을 확인할 수 있습니다.

    php -i | grep apcu

    1. Redis 캐싱

    1) Redis 설치

    Redis를 설치하기 위해 다음 명령어를 실행합니다. 설치가 완료되면 Redis 서버가 자동으로 시작됩니다

    sudo apt install redis-server

    2) Redis 상태 확인

    서비스 상태를 확인하기 위해 다음 명령어를 실행합니다.
    Redis 서버가 정상적으로 실행 중인지 확인한 후, 필요에 따라 Redis 구성 파일을 수정할 수 있습니다. 구성 파일은 /etc/redis/redis.conf 경로에 위치해 있습니다

    sudo systemctl status redis-server
    sudo usermod -a -G redis www-data
    

    3) 방화벽 설정(iptables)

    redis-server가 사용하는 6379 port 를 개방하고 iptable을 저장 및 리로드 합니다.

    sudo iptables -A INPUT -p tcp --dport 6379 -j ACCEPT
    sudo netfilter-persistent save
    sudo netfilter-persistent reload

    4) Redis와 php 연동

    Redis PHP 확장 모듈을 설치합니다.

    sudo apt install php8.2-redis

    nginx 서버를 재시작하여 변경 사항이 적용되도록 합니다.

    sudo systemctl restart nginx

    5) Redis 활성화(redis.ini)

    sudo nano /etc/php/8.2/mods-available/redis.ini에서 아래 코드를 추가합니다.

    sudo nano /etc/php/8.2/mods-available/redis.iniextension = redis.so

    PHP-FPM 서버를 재시작합니다.

    sudo systemctl restart php8.2-fpm

    새로운 PHP 파일을 생성하고 다음과 같은 코드를 작성하여 Redis와의 연결 및 작업을 확인할 수 있습니다(value 출력)

    <?php
    $redis = new Redis();
    $redis->connect('localhost', 6379);
    
    // 문자열 저장 및 조회 예제
    $redis->set("key", "value");
    echo $redis->get("key");
    
    // 해시 저장 및 조회 예제
    $redis->hSet("hash", "field", "value");
    echo $redis->hGet("hash", "field");
    
    // 연결 종료
    $redis->close();
    ?>

    2. Memcached 캐싱

    1) Memcached 설치

    Memcache를 설치하기 위해 다음 명령어를 실행합니다.

    sudo apt install memcached

    2) Memcached 서비스 시작

    다음 명령어를 실행해서 Memcached 서비스를 시작합니다.

    sudo systemctl start memcached
    sudo systemctl status memcached

    Memcached 서비스가 부팅시 자동으로 시작되도록 아래 코드를 실행합니다.

    sudo systemctl enable memcached

    3) 방화벽 설정(iptables)

    Memcached는 localhost(127.0.0.1)의 11211 포트에서 실행됩니다.
    Memcached가 실행되도록 iptables 11211 포트를 개방합니다.

    sudo iptables -A INPUT -p tcp --dport 11211 -j ACCEPT
    sudo netfilter-persistent save
    sudo netfilter-persistent reload

    추가로, Memcached를 사용하는 애플리케이션 구성을 위해 다음과 같은 설정 파일을 편집할 수 있습니다:

    • 메모리 할당량 수정: /etc/memcached.conf 파일에서 -m 옵션 값을 수정하여 할당량을 조정할 수 있습니다.
    • 바인딩 주소 수정: /etc/memcached.conf 파일에서 -l 옵션 값을 변경하여 다른 IP 주소로 바인딩할 수 있습니다.
    • 포트 수정: /etc/memcached.conf 파일에서 -p 옵션 값을 변경하여 다른 포트 번호로 설정할 수 있습니다.

    4) PHP 연동(php.ini)

    아래 명령어를 실행해서 패키지를 설치합니다.

    sudo apt install php8.2-memcached

    /etc/php/8.2/fpm/php.ini 파일 또는 /etc/php/8.2/mods-avaiable/memcached.ini 에서 extension=memcached.so 라인을 찾아 주석 해제하고 저장합니다.

    sudo nano /etc/php/8.2/mods-available/memcached.ini

    5) Memcached 작동 확인

    다음 코드를 사용하여 Memcached 서버에 연결하고 값을 저장하고 검색하며 테스트 할 수 있습니다.(value 출력 정상)

    <?php
    $memcached = new Memcached();
    $memcached->addServer('localhost', 11211);
    
    $memcached->set('key', 'value', 60); // 60초 동안 값 저장
    
    $value = $memcached->get('key');
    echo $value; // 저장된 값 출력
    ?>

    4. 추가 학습 자료

    APCu, Redis, Memcached와 관련된 공식 문서 및 참고 자료 링크입니다

    1) APCu

    2) Redis

    3) Memcached

    Thinknote

    함께 읽으면 좋은 글