ITFragile
[Ansible] 07. php 활성화 본문
1. DB와 WEB을 연결해주기 위해 main 플레이북 하단에 새 파일 연결
# main.yaml
- name: DB WEB Connection
hosts: web
tasks:
- import_tasks: php.yaml
2. php 설치 및 index.php 전달
# php.yaml
- name: yum install python
apt:
name:
- python3-pymysql
- php
- php-mysql
state: latest
- name: copy index.php
template: # 템플릿 파일을 지정된 이름으로 원격 서버에 복사
src: index.php
dest: /var/www/html
- name: restart apache2
service:
name: apache2
state: restarted
3. index.php 작성
# index.php
<?php
$server_addr = "192.168.56.104"; #DB-m01 ip 주소
$user_name = "web_user";
$password = "12345";
$db_name = "webdb";
$connection = mysqli_connect($server_addr, $user_name, $password, $db_name);
$query = "SELECT * FROM web_tb";
$rst = mysqli_query($connection, $query);
if (mysqli_num_rows($rst) > 0) {
while($i = mysqli_fetch_assoc($rst)) {
echo "id : " . $i["id"] . " | name : " . $i["name"] . "<br>" ;
}
}
mysqli_close($connection);
?>
4. 앤서블 실행
5. 접속 확인
# web-01
cat /var/www/html/index.php
'Project' 카테고리의 다른 글
[Kubernetes] 01. 작업 환경 구성 (0) | 2023.03.20 |
---|---|
[Ansible] 08. DB 로드밸런싱 (0) | 2023.03.16 |
[Ansible] 06. mysql 구성하기 (0) | 2023.03.16 |
[Ansible] 05. DB 이중화 (0) | 2023.03.16 |
[Ansible] 04. WEB 로드밸런싱 (0) | 2023.03.16 |