文章目錄
- lamp分離部署
- 1.在控制機上將2、3、4主機的ip寫入清單中,并且ping通
- 2.在2、上安裝httpd
- 3.在3、主機上安裝mysql資料庫
- 4.安裝php及一些常見的組件
- 5.配置apache服務
- 6.配置php檔案
- 7.重啟php服務和apache服務
lamp分離部署
首先是主機分配(4臺)
| 服務 | ip |
|---|---|
| ansible(控制機) | 192.168.100.146(centos8-1) |
| nginx | 192.168.100.147(centos8-2) |
| mysql | 192.168.100.148(centos8-3) |
| php | 192.168.100.149(centos8-4) |
1.在控制機上將2、3、4主機的ip寫入清單中,并且ping通
[root@centos8-1 ansible]# cat shen
[webservers]
192.168.100.147 ansible_user=root ansible_passwd=1
[nginx]
192.168.100.147 ansible_user=root ansible_passwd=1
[mysql]
192.168.100.148 ansible_user=root ansible_passwd=1
[php]
192.168.100.149 ansible_user=root ansible_passwd=1
[root@centos8-1 ansible]#
測驗能否ping通
[root@centos8-1 ansible]# ansible nginx -m ping //nginx可以ping通
192.168.100.147 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@centos8-1 ansible]#
[root@centos8-1 ~]# ansible mysql -m ping //mysql可以ping通
192.168.100.148 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@centos8-1 ~]#
[root@centos8-1 ~]# ansible php -m ping //php能ping通
192.168.100.149 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@centos8-1 ~]#
注意:我們既然使用ansible遠程管理搭建,那我們就要用模塊去遠程搭建,
2.在2、上安裝httpd
在控制機上用ansible模塊命令執行在2主機上安裝apache服務,
[root@centos8-1 ~]# ansible nginx -m yum -a "name=httpd state=present" //安裝成功
192.168.100.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
"Installed: httpd-tools-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64",
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
"Installed: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
"Installed: centos-logos-httpd-85.8-1.el8.noarch",
"Installed: mailcap-2.1.48-3.el8.noarch",
"Installed: httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64",
"Installed: apr-1.6.3-11.el8.x86_64",
"Installed: httpd-filesystem-2.4.37-40.module_el8.5.0+852+0aafc63b.noarch",
"Installed: apr-util-1.6.1-6.el8.x86_64"
]
}
[root@centos8-1 ~]#
設定apache開機自啟 ,并且啟動服務
[root@centos8-1 ~]# ansible nginx -m service -a "name=httpd state=started enabled=yes"
192.168.100.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started", //此處內容省略,,,,,,
在防火墻中放行我們http服務
[root@centos8-1 ~]# ansible nginx -m firewalld -a 'service=http zone=public permanent=yes state=enabled' //在防火墻中放行我們的http服務成功
192.168.100.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "Permanent operation, Changed service http to enabled, (offline operation: only on-disk configs were altered)"
}
[root@centos8-1 ~]#
上面配置完成之后我們在瀏覽器中訪問一下apache是否正常
在這里插入圖片描述

3.在3、主機上安裝mysql資料庫
在控制機中對mysql主機進行資料庫安裝
[root@centos8-1 ~]# ansible mysql -m yum -a "name=mariadb state=present" //yum下載mariadb成功
192.168.100.148 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: perl-Term-Cap-1.17-395.el8.noarch",
此處省略,,,,,,
[root@centos8-1 ~]#
[root@centos8-1 ~]# ansible mysql -m yum -a 'name=mariadb-server state=present' //安裝mariadb-server成功
192.168.100.148 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: perl-Math-Complex-1.59-420.el8.noarch",
"Installed: mariadb-server-utils-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: perl-DBD-MySQL-4.046-3.module_el8.3.0+419+c2dec72b.x86_64",
此處省略,,,,,,
設定mariadb開機自啟,并啟動服務,
[root@centos8-1 ~]# ansible mysql -m service -a "name=mariadb enabled=yes" //成功啟動mariadb服務
192.168.100.148 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "mariadb",
"status": {
"ActiveState": "inactive",
此處省略,,,,,,
4.安裝php及一些常見的組件
[root@centos8-1 ~]# ansible php -m yum -a 'name=php state=present' //成功安裝php服務,
192.168.100.149 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: php-fpm-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64",
此處省略,,,,,,
安裝php相關的組件
[root@centos8-1 ~]#
[root@centos8-1 ~]# ansible php -m yum -a "name=php-* state=present" //php組件安裝成功
192.168.100.149 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: php-pecl-apcu-devel-5.1.12-2.module_el8.2.0+313+b04d0a66.x86_64",
此處省略,,,,,,
[root@centos8-1 ~]# ansible php -m yum -a "name=curl state=present"
192.168.100.149 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"msg": "Nothing to do",
"rc": 0,
"results": []
}
[root@centos8-1 ~]#
[root@centos8-1 ~]# ansible php -m yum -a "name=curl-devel state=present"
192.168.100.149 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: libcurl-7.61.1-18.el8.x86_64",
"Installed: libcurl-devel-7.61.1-18.el8.x86_64",
"Removed: libcurl-7.61.1-17.el8.x86_64"
]
}
[root@centos8-1 ~]#
5.配置apache服務
首先是httpd服務器配置
[root@centos8-1 ~]# ansible nginx -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf line="<VirtualHost 192.168.100.147:80>\nDocumentRoot "/var/www/html/www1"\nServerName www.node2.com\nProxyRequests off\nProxyPassMatch ^/(.*\.php)$ fcgi://192.168.100.149:9000/var/www/html/www1/$1\n<Directory "/var/www/html/www1">\nOptions None\nAllowOverride None\nOrder allow,deny\nAllow from all\n</Directory>\n</VirtualHost>"'
192.168.100.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@centos8-1 ~]#
[root@centos8-1 ~]# ansible nginx -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^AddType " insertafter="^AddType application/x-" line="AddType application/x-httpd-php .php"'
192.168.100.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@centos8-1 ~]#
[root@centos8-1 ~]# ansible nginx -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^AddType " insertafter="^AddType application/x-" line="AddType application/x-httpd-php-source .phps"'
192.168.100.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@centos8-1 ~]#
[root@centos8-1 ~]# ansible nginx -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^DirectoryIndex" line="DirectoryIndex index.html index.php"'
192.168.100.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@centos8-1 ~]#
6.配置php檔案
[root@centos8-1 ~]# ansible php -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="^listen =" line="listen = 192.168.100.149:9000"'
192.168.100.149 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@centos8-1 ~]#
[root@centos8-1 ~]# ansible php -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="^listen.allowed_clients =" line="listen.allowed_clients = 192.168.100.147"'
192.168.100.149 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@centos8-1 ~]#
[root@centos8-1 ~]# ansible nginx -a 'mkdir /var/www/html/www1' //創建跟目錄成功
[WARNING]: Consider using the file module with state=directory rather than running 'mkdir'. If you need to use
command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False'
in ansible.cfg to get rid of this message.
192.168.100.147 | CHANGED | rc=0 >>
[root@centos8-1 ~]#
[root@centos8-1 ~]# ansible nginx -a 'cp /var/www/html/index.php /var/www/html/www1/'
192.168.100.147 | CHANGED | rc=0 >>
[root@centos8-1 ~]#
7.重啟php服務和apache服務
[root@centos8-1 ~]# ansible nginx -m service -a 'name=httpd state=restarted' //重啟apache服務
192.168.100.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "started",
"status": {
此處省略,,,,,,
[root@centos8-1 ~]# ansible php -m service -a 'name=php-fpm state=restarted' //重啟php服務
192.168.100.149 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "php-fpm",
"state": "started",
"status": {
此處省略,,,,,,
訪問ip地址,看看是否能通

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/289231.html
標籤:其他
