Linux

Linux_ wordpress , mysql(8.0) , php(8.0) 연동

우당탕탕인생기 2023. 8. 26. 23:42

이번에는 2Tier 구성인 worpress와 php 그리고 db인 mysql과 연동하는 방법에 대해 기록해보도록 하겠다.

 

시뮬레이션은 아래와 같다고 가정하고 해보겠다.

 

1번 server 2번 server
Mysql-client 8.0 Mysql-server 8.0
wordpress
php
http 

 

 

대략적인 순서는 다음과 같다.

순서
1번서버
http , wordpress , php mysql 패키지 설치
wordpress 설정파일 -> html 파일로 이동
wp-config.php 파일 만든 이후 db 연동 설정
http.conf 에서 index.html이 아닌 index.php 파일 읽도록 설정 -> 방화벽설정
2번서버
mysql-server 패키지 설치
mysql 보안설정을 통해 원격제어 허용 및 방화벽설정
원격제어 가능한 유저을 생성하고 권한부여
1번서버
원격접속 후 wp-config.php 설정파일에서 지정한 db생성

1번 Server 

 

(1) http , wordpress , php, mysql 패키지 설치

yum intsall -y httpd tar wget # http ,tar , wget 패키치 설치
wget https://ko.wordpress.org/wordpress-5.7.8-ko_KR.tar.gz  # wordpress tar 파일 설치

tar xvfz wordpress-5.7.8-ko_KR.tar.gz  # wordpress tar 파일 압축해제

yun install -y php php-common php-opcache php-cli php-gd php-curl php-mysqlnd mysql # php ,mysql 설치

그러면 이렇게 wordpress 디렉토리가 생성이 된다.

 

(2)  웹에서 wordpress을 실행시키기 위해 wordpress 디렉토리 내 파일을 html 파일로 복사한다

cp -ar wordpress/* /var/www/html/

(3) httpd.conf 에서 html 파일 뿐만아니라 php 파일도 읽을 수 있도록 설정한다.

 -우선 wp-config-sample.php 파일을 wp-config.php 파일로 변경해야 한다.

cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php

sed -i 's/DirectoryIndex index.html/ DirectoryIndex index.php/g' /etc/httpd/conf/httpd.conf
## httpd.conf에 DirectoryIndex index.html 내용을 DirectoryIndex index.php로 변경

잘 변경이 되었다.

(4) wp-config.php 파일 설정

 

vi /var/www/html/wp-config.php

### 편집모드 진입

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );

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

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

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

### 위 내용 변경

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );

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

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

/** MySQL hostname */
define( 'DB_HOST', '10.0.0.12' ); # mysql 서버 ip 주소 입력 이번상황의 경우 mysql이 설치된 2번 server ip 입력

주의 해야할 건 여기에 입력한 유저이름과 비밀번호 모두가 일치해야 한다!

 

 

(5) 방화벽 열기 

 

httpd 이기때문에 80번 포트를 열어야 한다.

이후 1번 server의 웹에 접속해보면 아래와 같은 창이 나온다


 

 

2번서버

 

(1) mysql-server 패키지 설치 및 mysql 보안설정

- mysql 설치 이후 원격접속을 위해 보안설정을 해야한다.

 

yum install -y mysql-server

systemclt start mysqld
mysql_secure_installation
비밀번호 설정

Disallow root login remotely? (Press y|Y for Yes, any other key for No) #원격제어를 거부하겠다는 내용인데 여기서 n을 해야 한다.

 

 

(2) myslq 접속 후 원격접속을 위한 권한설정

mysql -uroot -p 
- 비밀번호 입력

데이터베이스를 확인해보면 이렇게 잘 보인다.

 

이제 유저를 생성하고 권한을 부여하자.

create user 'root'@'%' identified 'Test1234!'; # 모든 원격지에서 접속하는하도록 유저 생성

grant all privileges on *.* to root'@'%'; # root 라는 사용자에게 모든테이블접속권한부여

flush privileges; # 변경된 내용을 메모리에 반영

 

(3) mysql 3306 포트 열기

firewall-cmd --permanent --add-prt=3306/tcp
firewall-cmd --reload

 


1번서버

 

(1) Mysql 원격접속

 

mysql -uroot -p -h 10.0.0.12

 

(2) wordpress 데이터베이스 생성

 

create database wordpress ;

 

다시 systemctl restart httpd 이후 웹에 접속하면

이렇게 화면이 나오면서 연결이 잘 된것을 볼 수 있다.