角色部署lamp架构
部署Apache
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# ls
ansible.cfg hosts roles
[root@ansible ansible]# vim hosts
node1
[root@ansible ansible]# cd roles/
[root@ansible roles]# ls
[root@ansible roles]# ansible-galaxy init apache
- Role apache was created successfully
[root@ansible roles]# ansible-galaxy init mysql
- Role mysql was created successfully
[root@ansible roles]# ansible-galaxy init php
- Role php was created successfully
[root@ansible roles]# ls
apache mysql php
[root@ansible roles]# cd apache/
[root@ansible apache]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@server apache]# vim tasks/main.yml
---
# tasks file for apache
- name: set yum
script: yum.sh
- name: install packages
yum:
name: "{{ httpdpkgs }}"
state: present
- name: unzip apr
unarchive:
src: apr-1.6.5.tar.bz2
dest: /usr/src/
- name: unzip apr-util
unarchive:
src: apr-util-1.6.1.tar.bz2
dest: /usr/src/
- name: unzip httpd
unarchive:
src: httpd-2.4.54.tar.bz2
dest: /usr/src/
- name: install httpd
script: httpd.sh
- name: apache.sh
script: apache.sh
- name: create user
user:
name: apache
system: yes
create_home: no
shell: /sbin/nologin
state: present
- name: set httpd service
template:
src: httpd.service.j2
dest: /usr/lib/systemd/system/httpd.service
- name: refresh
shell:
cmd: systemctl daemon-reload
- name: start httpd service
service:
name: httpd
state: started
enabled: yes
- name: stop firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=disabled
[root@ansible apache]# cd files/
[root@ansible files]# ls
apache.sh apr-1.6.5.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.54.tar.bz2 httpd.sh yum.sh
[root@ansible files]# vim yum.sh
#!/bin/bash
/usr/bin/curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum reinstall -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
/usr/bin/sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
/usr/bin/sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@ansible apache]# vim vars/main.yml
---
# vars file for apache
httpdpkgs:
- bzip2
- make
- wget
- openssl-devel
- pcre-devel
- expat-devel
- libtool
- gcc
- gcc-c++
- libxml2-devel
[root@ansible files]# vim httpd.sh
#/bin/bash
cd /opt/apr-1.6.5
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr
make
make install
cd /opt/apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make install
cd /opt/httpd-2.4.54
./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
make
make install
[root@ansible files]# vim apache.sh
export PATH=/usr/local/apache/bin/:$PATH
[root@ansible apache]# vim templates/httpd.service.j2
[Unit]
Description=httpd server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@ansible ansible]# vim httpd.yml
---
- name: use apache role
hosts: node1
roles:
- apache
[root@ansible ansible]# ansible-playbook httpd.yml
PLAY [use apache role] *************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [node1]
TASK [apache : set yum] ************************************************************************************************************************************************
changed: [node1]
TASK [apache : install packages] ***************************************************************************************************************************************
ok: [node1]
TASK [apache : unzip apr] **********************************************************************************************************************************************
changed: [node1]
TASK [apache : unzip apr-util] *****************************************************************************************************************************************
ok: [node1]
TASK [apache : unzip httpd] ********************************************************************************************************************************************
changed: [node1]
TASK [apache : install httpd] ******************************************************************************************************************************************
changed: [node1]
TASK [apache.sh] *******************************************************************************************************************************************************
changed: [node1]
TASK [apache : create user] ********************************************************************************************************************************************
ok: [node1]
TASK [apache : set httpd service] **************************************************************************************************************************************
ok: [node1]
TASK [apache : refresh] ************************************************************************************************************************************************
changed: [node1]
TASK [apache : start httpd service] ************************************************************************************************************************************
ok: [node1]
TASK [apache : stop firewalld] *****************************************************************************************************************************************
ok: [node1]
TASK [apache : stop selinux] *******************************************************************************************************************************************
ok: [node1]
PLAY RECAP *************************************************************************************************************************************************************
node1 : ok=14 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
部署mysql
[root@ansible roles]# cd mysql/
[root@ansible mysql]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@ansible mysql]# cd tasks/
[root@ansible tasks]# ls
main.yml
[root@ansible tasks]# vim main.yml
---
# tasks file for mysql
- name: create user mysql
user:
name: mysql
system: yes
shell: /sbin/nologin
create_home: no
state: present
- name: install pkgs
yum:
name: libncurses*
state: present
- name: unzip
unarchive:
src: mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
dest: /usr/local/
- name: create link
file:
src: /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64
dest: /usr/local/mysql
owner: mysql
group: mysql
state: link
- name: create directory
file:
path: /opt/data
owner: mysql
group: mysql
state: directory
- name: mysql initialization.sh
script: initialization.sh
- name: copy config
template:
src: my.cnf.j2
dest: /etc/my.cnf
- name: replace file1
replace:
path: /usr/local/mysql/support-files/mysql.server
regexp: "#^(basedir=).*"
replace: "basedir=/usr/local/mysql"
- name: replace file2
replace:
path: /usr/local/mysql/support-files/mysql.server
regexp: "#^(datadir=).*"
replace: "datadir=/opt/data"
- name: copy mysql.service
template:
src: mysqld.service.j2
dest: /usr/lib/systemd/system/mysqld.service
- name: reload config
shell:
cmd: systemctl daemon-reload
- name: restart mysqld
service:
name: mysqld
state: started
enabled: yes
- name: set mysql passwd
shell:
cmd: /usr/local/mysql/bin/mysql -uroot -e "set password=password('123.com')"
- name: set mysql env
script: mysql.sh
//mysql初始化
[root@ansible mysql]# vim files/initialization.sh
#!/bin/bash
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig
//环境变量
[root@ansible files]# cat mysql.sh
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile.d/mysql.sh
//模板文件
[root@ansible templates]# vim my.cnf.j2
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
[root@ansible templates]# vim mysqld.service.j2
[Unit]
Description=mysql server daemon
After=network.targe
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
[root@ansible templates]#
[root@ansible ansible]# vim mysql.yml
---
- name: use mysql role
hosts: node1
roles:
- mysql
[root@ansible ansible]# ansible-playbook mysql.yml
PLAY [use mysql role] **************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [node1]
TASK [create user mysql] ***********************************************************************************************************************************************
ok: [node1]
TASK [mysql : install pkgs] ********************************************************************************************************************************************
ok: [node1]
TASK [mysql : unzip] ***************************************************************************************************************************************************
changed: [node1]
TASK [mysql : create link] *********************************************************************************************************************************************
ok: [node1]
TASK [mysql : create directory] ****************************************************************************************************************************************
ok: [node1]
TASK [mysql initialization.sh] *****************************************************************************************************************************************
changed: [node1]
TASK [mysql : copy config] *********************************************************************************************************************************************
changed: [node1]
TASK [mysql : replace file1] *******************************************************************************************************************************************
ok: [node1]
TASK [mysql : replace file2] *******************************************************************************************************************************************
ok: [node1]
TASK [copy mysql.service] **********************************************************************************************************************************************
ok: [node1]
TASK [mysql : reload config] *******************************************************************************************************************************************
changed: [node1]
TASK [restart mysqld] **************************************************************************************************************************************************
changed: [node1]
TASK [set mysql passwd] ************************************************************************************************************************************************
changed: [node1]
TASK [set mysql env] ***************************************************************************************************************************************************
changed: [node1]
PLAY RECAP *************************************************************************************************************************************************************
node1 : ok=15 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
//在node1上查看
[root@node1 ~]# systemctl status mysqld
● mysqld.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-11-07 16:07:59 CST; 59min ago
Main PID: 15518 (mysqld_safe)
Tasks: 29 (limit: 11175)
Memory: 189.3M
CGroup: /system.slice/mysqld.service
├─15518 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
└─15708 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=node1.e>
部署php
[root@ansible php]# vim tasks/main.yml
---
# tasks file for php
- name: install packages
yum:
name: "{{ php_pack }}"
state: present
- name: unzip
unarchive:
src: php-7.1.10.tar.gz
dest: /usr/local
- name: install php
script: php.sh
- name: cp template
template:
src: php-fpm.service.j2
dest: /usr/lib/systemd/system/php-fpm.service
- name: refresh
shell:
cmd: systemctl daemon-reload
- name: set php service
service:
name: php-fpm
state: started
enabled: yes
- name: modify apache config
replace:
path: /etc/httpd24/httpd.conf
regexp: "index.html"
replace: "index.php index.html"
- name: rm file
shell:
cmd: rm -rf /usr/local/apache/htdocs/index.html
- name: cp index.php
template:
src: index.php.j2
dest: /usr/local/apache/htdocs/index.php
owner: apache
group: apache
- name: restart httpd
service:
name: httpd
state: restarted
[root@ansible php]# vim files/php.sh
#!/bin/bash
cd /usr/local/php-7.1.10
./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-mysql-sock=/tmp/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
make
make install
echo 'export PATH=/usr/local/php/bin:$PATH' > /etc/profile.d/php.sh
source /etc/profile.d/php.sh
ln -s /usr/local/php7/include/ /usr/include/php
echo "/usr/local/php7/lib" > /etc/ld.so.conf.d/php.conf
ldconfig
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
echo "AddType application/x-httpd-php .php" >>/etc/httpd24/httpd.conf
echo "AddType application/x-httpd-php-source .phps" >>/etc/httpd24/httpd.conf
sed -i 's/index.html/index\.php index\.html/' /etc/httpd24/httpd.conf
//模板文件
[root@ansible php]# vim templates/php-fpm.service.j2
[Unit]
Description=php-fpm server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
//变量文件
[root@ansible php]# vim vars/main.yml
---
# vars file for php
php_pack:
- openssl
- openssl-devel
- bzip2-devel
- libcurl
- libcurl-devel
- libicu-devel
- libjpeg
- libjpeg-devel
- libpng
- libpng-devel
- openldap-devel
- freetype
- freetype-devel
- gmp
- gmp-devel
- libmcrypt
- libmcrypt-devel
- readline
- readline-devel
- libxslt
- libxslt-devel
- mhash
- mhash-devel
- php-mysqlnd
- sqlite-devel
- libzip-devel
- libxml2-devel
- pcre-devel
- http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
[root@ansible php]# vim templates/index.php.j2
<?php
phpinfo();
?>
[root@ansible ansible]# vim php.yml
---
- name: use php role
hosts: node1
roles:
- php
[root@ansible ansible]# ansible-playbook php.yml
PLAY [use php role] ****************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [node1]
TASK [php : install packages] ******************************************************************************************************************************************
ok: [node1]
TASK [php : unzip] *****************************************************************************************************************************************************
ok: [node1]
TASK [install php] *****************************************************************************************************************************************************
changed: [node1]
TASK [php : cp template] ***********************************************************************************************************************************************
ok: [node1]
TASK [php : refresh] ***************************************************************************************************************************************************
changed: [node1]
TASK [set php service] *************************************************************************************************************************************************
ok: [node1]
TASK [php : modify apache config] **************************************************************************************************************************************
changed: [node1]
TASK [php : rm file] ***************************************************************************************************************************************************
[WARNING]: Consider using the file module with state=absent rather than running 'rm'. 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.
changed: [node1]
TASK [cp index.php] ****************************************************************************************************************************************************
ok: [node1]
TASK [php : restart httpd] *********************************************************************************************************************************************
changed: [node1]
PLAY RECAP *************************************************************************************************************************************************************
node1 : ok=11 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@node1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*