본문 바로가기

리눅스

Nginx 환경에서 WordPress 설치하는 방법

728x90
반응형

Nginx 환경에서 WordPress 설치하는 방법

테스트 환경

OS : Ubuntu 24.04 LTS

Web : Nginx 1.28

PHP : PHP-FPM 8.4

WordPress 경로 : /usr/share/nginx/html

Nginx, PHP-FPM 설정

PHP extensions 체크 확인

php -m | grep -E 'mysqli|gd|curl|mbstring|zip|xml'

PHP-FPM 소켓 확인

ls /run/php/

기본 Nginx 서버블록(HTTP)

더보기

---

### 기본 서버 블록
server {
  listen 80;
  server_name example.com www.example.com;

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

### HTTPS 서버 블록
server {
  listen 443 ssl http2;
  server_name example.com www.example.com;

  root /usr/share/nginx/html;
  index index.php index.html;

  ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

  ssl_protocols TLSv1.2 TLSv1.3;
  ssl_prefer_server_ciphers on;

  access_log /var/log/nginx/wordpress.ssl.access.log;
  error_log /var/log/nginx/wordpress.ssl.error.log;

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

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }

  location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2?)$ {
    expires 30d;
    access_log off;
  }

  location = /xmlrpc.php {
    deny all;
  }

  location ~ /\. {
    deny all;
  }

  client_max_body_size 64M;
}

---

1. WordPress 소스 다운로드

cd /usr/share/nginx
sudo wget https://wordpress.org/latest.tar.gz

2. 압축 해제 및 디렉토리 이동

tar xf latest.tar.gz
sudo mv wordpress /usr/share/nginx/html

3. wp-config.php 생성

cp /usr/share/nginx/html/wp-config-sample.php \
  /usr/share/nginx/html/wp-config.php

4. 보안 키(SALT) 생성

SALT 자동 생성해서 .env에 넣기

더보기

---

curl -s https://api.wordpress.org/secret-key/1.1/salt/

---

curl -s https://api.wordpress.org/secret-key/1.1/salt/ \
  | sed -n "s/^define('\([A-Z_]*\)', *'\([^']*\).*/WP_\1='\2'/p" >> /usr/share/nginx/html/.env

5. wp-config.php 설정

vim /usr/share/nginx/html/wp-config.php
더보기

---

wp-config.php 설정

vim /usr/share/nginx/html/wp-config.php

데이터베이스 정보 설정

define( 'DB_NAME', 'wordpress_database' );
define( 'DB_USER', 'wordpress_user' );
define( 'DB_PASSWORD', 'wordpress_password' );
define( 'DB_HOST', 'localhost' );

인증 키(SALT) 설정

define('AUTH_KEY', '...');
define('SECURE_AUTH_KEY', '...');
define('LOGGED_IN_KEY', '...');
define('NONCE_KEY', '...');
define('AUTH_SALT', '...');
define('SECURE_AUTH_SALT', '...');
define('LOGGED_IN_SALT', '...');
define('NONCE_SALT', '...');

---

$env = parse_ini_file(__DIR__ . '/.env');

define('DB_NAME', $env['WP_DB_NAME']);
define('DB_USER', $env['WP_DB_USER']);
define('DB_PASSWORD', $env['WP_DB_PASS']);
define('DB_HOST', $env['WP_DB_HOST']);

define('AUTH_KEY',         $env['WP_AUTH_KEY']);
define('SECURE_AUTH_KEY',  $env['WP_SECURE_AUTH_KEY']);
define('LOGGED_IN_KEY',    $env['WP_LOGGED_IN_KEY']);
define('NONCE_KEY',        $env['WP_NONCE_KEY']);
define('AUTH_SALT',        $env['WP_AUTH_SALT']);
define('SECURE_AUTH_SALT', $env['WP_SECURE_AUTH_SALT']);
define('LOGGED_IN_SALT',   $env['WP_LOGGED_IN_SALT']);
define('NONCE_SALT',       $env['WP_NONCE_SALT']);
728x90

환경변수 설정

vim /usr/share/nginx/html/.env
# Database
WP_DB_NAME=wordpress_database
WP_DB_USER=wordpress_user
WP_DB_PASS=wordpress_password
WP_DB_HOST=localhost
chmod 600 .env
chown www-data:www-data .env

6. 파일 및 디렉토리 소유권 설정

sudo chown -R www-data:www-data /usr/share/nginx/html

7. 디렉토리 기본 권한 설정

find /usr/share/nginx/html -type d -exec chmod 2755 {} \;

8. wp-content 디렉토리 권한

sudo chmod 775 /usr/share/nginx/html/wp-content
sudo chmod -R 775 /usr/share/nginx/html/wp-content/themes
sudo chmod -R 775 /usr/share/nginx/html/wp-content/plugins

9. 업로드 디렉토리 생성

sudo mkdir -p /usr/share/nginx/html/wp-content/uploads
sudo chown -R www-data:www-data /usr/share/nginx/html/wp-content/uploads
sudo chmod 775 /usr/share/nginx/html/wp-content/uploads

10. (추가 권장) wp-config.php 보안 강화

define('DISALLOW_FILE_EDIT', true);
define('DISALLOW_FILE_MODS', false); // 필요 시 true

define('AUTOSAVE_INTERVAL', 300);
define('WP_POST_REVISIONS', 5);

 

참고URL

- WordPress Developer Resources : Editing wp-config.php

 

정의('DISALLOW_FILE_EDIT', true); 정의('DISALLOW_FILE_MODS', false); // 정말 필요해요 정의('AUTOSAVE_INTERVAL', 300); 정의('WP_POST_REVISIONS', 5);
 
반응형