1. 이미지를 업로드시, s3에 저장

- 아래 코드를 /var/www/html/wp-content/themes/sample/functions.php 에 추가

// After media uploaded
add_filter( 'wp_handle_upload', function( $upload, $context ){
    require_once $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';

	define( 'S3_KEY', 'S3_KEY');
	define( 'S3_SECRET', 'S3_SECRET');
	define( 'S3_BUCKET', 'S3_BUCKET');
	define( 'S3_REGION', 'ap-northeast-2');

    $s3 = new Aws\S3\S3Client([
        'version' => 'latest',
        'region' => S3_REGION,
        'credentials' => [
            'key' => S3_KEY,
            'secret' => S3_SECRET,
        ]
    ]);

    $file_data = file_get_contents( $upload['file'] );
	$file_url = explode('/wp-content/',$upload['url']);
    $s3_key = $file_url[1];

    $result = $s3->putObject([
        'Bucket' => S3_BUCKET,
        'Key' => $s3_key,
        'Body' => $file_data,
        'ACL' => 'public-read'
    ]);


    return $upload;
}, 10, 2);

 

※ 위 코드는 s3에도 저장만 하는 방법입니다.

아직 작업중이니, 추후에 업데이트 하도록 하겠습니다.

워드프레스는 기본적으로 모든 이미지가 'wp-content/uploads' 경로로 저장이 됩니다.

그래서 ELB로 여러대의 서버를 분산시키거나, 서버 교체시 이미지가 404나는 경우가 발생하곤 합니다.

이런 상황을 방지하고자 워드프레스와 AWS S3와 연동하는 법을 찾아보겠습니다.

※ 사전에 꼭 필요한 것들

- composer

https://aljshal.tistory.com/54

 

- php-xml

$ sudo yum install -y php-xml

 

- S3 Key

https://aljshal.tistory.com/55

 

1. AWS SDK for PHP 설치

- 아래 코드 실행 후, /var/www/html 위치에 vendor 폴더 및 관련 파일이 설치되었는지 확인

$ cd /var/www/html
$ composer require aws/aws-sdk-php

 

2. 샘플 코드 테스트

- 아래 코드를 test.php 로 저장하여 웹에서 실행

- s3 버킷에 이미지가 저장되고, 그 주소가 출력되면 성공

<?php

ini_set( 'display_errors', 1 );

require_once 'vendor/autoload.php';


use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;

echo '<pre>';

define( 'S3_KEY', 'S3_KEY');
define( 'S3_SECRET', 'S3_SECRET');
define( 'S3_BUCKET', 'S3_BUCKET');
define( 'S3_REGION', 'ap-northeast-2');

$s3 = new S3Client([
    'version' => 'latest',
    'region' => S3_REGION,
    'credentials' => [
        'key' => S3_KEY,
        'secret' => S3_SECRET,
    ]
]);

$file_url = 'https://www.example.com/sample-logo.jpg';
$s3_path = 'uploads/sample-logo.jpg';
$file_data = file_get_contents( $file_url );

try{
    $result = $s3->putObject([
        'Bucket' => S3_BUCKET,
        'Key' => $s3_path,
        'Body' => $file_data,
        'ACL' => 'public-read'
    ]);
    echo $result['ObjectURL']. PHP_EOL;
} catch ( S3Exception $e ) {
    echo $e->getMessage();
}

외부 업체에 맡기고 있는, 워드프레스 사이트를 AWS로 이관하는 작업을 진행하였는데,

HTTPS 에 문제가 생겨 제대로 작동하지 않았다.

AWS에서 ALB와 ROUTE 53, ACM을 이용하였는데, 근본적인 원인은 파악하지 못했다...

일단 해결방법은 아래 코드를 wp-config.php 에 추가하면 된다.

 

if(strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
	$_SERVER['HTTPS'] = 'on';

Warning: Declaration of selective_Walker::start_el(&$output, $item, $depth = 0, $args = Array) should be compatible with Walker_Nav_Menu::start_el(&$output, $item, $depth = 0, $args = Array, $id = 0)

워드프레스에서 해당 에러가 발생했다.

이 문장을

function start_el(&$output, $item, $depth=0, $args=array()) {

 

이렇게 수정해주면 끝!

function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

 

WAMP를 이용하여 서버에 올라가 있는 프로젝트를 로컬에서 실행시켜보자.

1. 프로젝트 다운로드

2. WAMP 다운로드

www.wampsercer.com/en/

3. DB 가져오기

라이브 DB 를 export하여 'phpmyadmin' 에 import

경로

4. DB 값 바꾸기

UPDATE wp_options SET option_value = replace(option_value, 'http://www.yoursitename.com', 'http://localhost');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.yoursitename.com', 'http://localhost');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.yoursitename.com','http://localhost');

 

5. httpd-vhost.conf 설정 바꾸기

경로 : WAMP - Apache - httpd-vhosts.conf

<VirtualHost localhost:80>
  DocumentRoot "Local Project Path"
  ServerAdmin localhost
  <Directory "Local Project Path">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
  </Directory>
</VirtualHost>

 

 

6. wp-config.php 값 바꾸기

define('DB_NAME', 'DB name');

/** MySQL database username */
define('DB_USER', 'root');

/** MySQL database password */
define('DB_PASSWORD', '');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8mb4');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

 

끝!

+ Recent posts