草庐IT

Ansible部署LNMP

tushanbu 2023-03-28 原文

Ansible部署LNMP


环境介绍:

系统 ip 主机名 服务
centos8 192.168.222.250 ansible ansinle
ceotos8 192.168.222.137 nginx nginx
centos8 192.168.222.138 mysql mysql
centos8 192.168.222.139 php php

nginx-1.22.0
mysql-8.0.30
php-8.1.11

基础准备工作

阿里云官网

[root@localhost ~]# hostnamectl set-hostname ansible
[root@localhost ~]# bash
//下载阿里源
[root@ansible ~]# cd /etc/yum.repos.d/
[root@ansible yum.repos.d]# rm -rf *
[root@ansible yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo     
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0   3574      0 --:--:-- --:--:-- --:--:--  3574
[root@ansible yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@ansible yum.repos.d]# cd
//安装ansible
[root@ansible ~]# dnf -y install platform-python
[root@ansible ~]# dnf -y install centos-release-ansible-29
[root@ansible ~]# dnf -y install ansible
[root@ansible ~]# ansible --version    //查看版本
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Sep 10 2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
[root@ansible ~]# ssh-keygen -t rsa    //生成一对公钥一对私钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:egpQ3eohggaGw65/rQd3/DVnLCKZwJr89+4VpC7obsU root@ansible
The key's randomart image is:
+---[RSA 3072]----+
|                 |
|o    . .         |
|+o  ... .   .    |
|+o .  o.   o     |
|.oo..oo+So. ..   |
|o  o=o.+E.. +.+  |
|.   .=+ooo.o.=   |
| .  .o=o....     |
|  ...=+. ++      |
+----[SHA256]-----+
[root@ansible ~]# ssh-copy-id 192.168.222.137  //与nginx受控机互信
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.222.137 (192.168.222.137)' can't be established.
ECDSA key fingerprint is SHA256:jJ7HFCOrVQKPjfacavF08vxsn4hSKTG3q9SV78ApryQ.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.222.137's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.222.137'"
and check to make sure that only the key(s) you wanted were added.
[root@ansible ~]# ssh-copy-id 192.168.222.138  //与mysql受控机互信
[root@ansible ~]# ssh-copy-id 192.168.222.139  //与php受控机互信
//将受控主机加入ansible清单
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# ls
ansible.cfg  hosts  roles
[root@ansible ansible]# touch inventory
[root@ansible ansible]# ls
ansible.cfg  hosts  inventory  roles
[root@ansible ansible]# vim ansible.cfg 
#inventory      = /etc/ansible/hosts    //取消注释并修改为下面这样
inventory       = /etc/ansible/inventory
[root@ansible ansible]# vim inventory 
[root@ansible ansible]# cat inventory 
[web]    //受控主机
192.168.222.137
192.168.222.138
192.168.222.139


[nginx]
192.168.222.137
[mysql]
192.168.222.138
[php]
192.168.222.139
[root@ansible ansible]# cd
[root@ansible ~]# ansible all --list-hosts
  hosts (3):
    192.168.222.137
    192.168.222.138
    192.168.222.139
//检查机器节点是否连通
[root@ansible ~]# ansible web -m ping
192.168.222.139 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.222.137 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.222.138 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
//将设置好的阿里源传到受控主机
[root@ansible ~]# ansible nginx  -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo  dest=/etc/yum.repos.d/CentOS-Base.repo'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "8bbf30b2d80c3b97292ca7b32f33ef494269a5b8",
    "dest": "/etc/yum.repos.d/CentOS-Base.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "ed031c350da2532e6a8d09a4d9b05278",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 1653,
    "src": "/root/.ansible/tmp/ansible-tmp-1666511143.7368824-130351-128775339422969/source",
    "state": "file",
    "uid": 0
}
[root@ansible ~]# ansible mysql  -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo  dest=/etc/yum.repos.d/CentOS-Base.repo'
192.168.222.138 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "8bbf30b2d80c3b97292ca7b32f33ef494269a5b8",
    "dest": "/etc/yum.repos.d/CentOS-Base.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "ed031c350da2532e6a8d09a4d9b05278",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 1653,
    "src": "/root/.ansible/tmp/ansible-tmp-1666511161.8907917-130929-57801171367377/source",
    "state": "file",
    "uid": 0
}
[root@ansible ~]# ansible php  -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo  dest=/etc/yum.repos.d/CentOS-Base.repo'
192.168.222.139 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "8bbf30b2d80c3b97292ca7b32f33ef494269a5b8",
    "dest": "/etc/yum.repos.d/CentOS-Base.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "ed031c350da2532e6a8d09a4d9b05278",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 1653,
    "src": "/root/.ansible/tmp/ansible-tmp-1666511167.7952082-131147-42422946741004/source",
    "state": "file",
    "uid": 0
}
//查看受控机上是否有阿里源
[root@nginx ~]# ls /etc/yum.repos.d/
CentOS-Base.repo
[root@mysql ~]# ls /etc/yum.repos.d/
CentOS-Base.repo
[root@php ~]# ls /etc/yum.repos.d/
CentOS-Base.repo
//给受控主机安装epel源
[root@ansible ~]# ansible web -m yum -a 'name=epel-release state=present' 
192.168.222.137 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": "Nothing to do",
    "rc": 0,
    "results": []
}
192.168.222.139 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": "Nothing to do",
    "rc": 0,
    "results": []
}
192.168.222.138 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": "Nothing to do",
    "rc": 0,
    "results": []
}
//检查受控是否安装
[root@nginx ~]# rpm -qa|grep epel-release
epel-release-8-11.el8.noarch
[root@mysql ~]# rpm -qa|grep epel-release
epel-release-8-11.el8.noarch
[root@php ~]# rpm -qa|grep epel-release
epel-release-8-11.el8.noarch
[root@ansible ~]# systemctl stop firewalld.service 
[root@ansible ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@ansible ~]# setenforce 0
[root@ansible ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
//关闭受控主机的防火墙和selinux
[root@ansible ~]# ansible web -m service -a 'name=firewalld state=stopped enabled=no'
[root@ansible ~]# ansible web -m lineinfile -a "path=/etc/selinux/config regexp='SELINUX=.*' line='SELINUX=disabled'"
[root@ansible ~]# ansible web -m reboot  //重启受控主机
//检查防火墙是否关闭
[root@nginx ~]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@mysql ~]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@php ~]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

管理nginx受管主机部署nginx服务

//创建系统用户nginx
[root@ansible ansible]# ansible nginx -m user -a 'name=nginx system=yes shell=/sbin/nologin state=present'
//安装依赖包
[root@ansible ansible]# ansible nginx -m yum -a 'name=pcre-devel,openssl,openssl-devel,gd-devel,gcc,gcc-c++,make,wget state=present'
//创建日志存放目录并修改目录所属主,组
[root@ansible ansible]# ansible nginx -m file -a 'path=/var/log/nginx state=directory'
[root@ansible ansible]# ansible nginx -m file -a 'path=/var/log/nginx state=directory owner=nginx group=nginx'
//下载nginx包并解压
[root@ansible ansible]# ansible nginx -a 'wget http://nginx.org/download/nginx-1.22.0.tar.gz'
[root@ansible ansible]# ansible nginx -a 'tar xf nginx-1.22.0.tar.gz'
//编写编译脚本,然后进行编译安装
[root@ansible ansible]#  mkdir scripts/
[root@ansible ansible]# vim scripts/nginx.sh
[root@ansible ansible]# cat scripts/nginx.sh
#!/bin/bash

cd nginx-1.22.0
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx 
--with-debug  --with-http_ssl_module --with-http_realip_module
--with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module  
[root@ansible ansible]# chmod +x scripts/nginx.sh 
[root@ansible ansible]# ansible nginx -m script -a '/etc/ansible/scripts/nginx.sh'
[root@ansible ansible]# ansible nginx -m shell -a 'cd nginx-1.22.0 && make && make install'
//配置环境变量
[root@ansible ansible]# ansible nginx -m shell -a 'echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh'
[root@ansible ansible]# ansible nginx -m shell -a '. /etc/profile.d/nginx.sh'
//开启服务
[root@ansible ansible]# ansible nginx  -a 'nginx'
//查看端口
[root@ansible ansible]# ansible nginx  -a 'ss -antl'
192.168.222.137 | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:80        0.0.0.0:*          
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*          
LISTEN 0      128             [::]:22           [::]:*  
//关闭服务        
[root@ansible ansible]# ansible nginx  -a 'nginx -s stop'
192.168.222.137 | CHANGED | rc=0 >>
//查看端口
[root@ansible ansible]# ansible nginx  -a 'ss -antl'
192.168.222.137 | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*          
LISTEN 0      128             [::]:22           [::]:*   
//编写脚本将其加入systemd服务中       
[root@ansible ansible]# vim scripts/systemd.sh
[root@ansible ansible]# cat scripts/systemd.sh
#!/bin/bash

cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
PrivateTmp= true

[Install]
WantedBy=multi-user.target
EOF
[root@ansible ansible]# chmod +x scripts/systemd.sh 
//启动服务并设置开机自启
[root@ansible ansible]# ansible nginx -m service -a 'name=nginx state=started enabled=yes'
//查看端口
[root@ansible ansible]# ansible nginx  -a 'ss -antl'
192.168.222.137 | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:80        0.0.0.0:*          
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*          
LISTEN 0      128             [::]:22           [::]:*   
//脚本写入php网页信息
[root@ansible ansible]# vim scripts/nginx.php.sh
[root@ansible ansible]# cat scripts/nginx.php.sh
#!/bin/bash
cat > /usr/local/nginx/html/index.php <<EOF
<?php
        phpinfo();
?>
EOF
[root@ansible ansible]# chmod +x scripts/nginx.php.sh
//修改nginx服务的配置
[root@ansible ansible]# ansible nginx -m script -a '/etc/ansible/scripts/nginx.php.sh'
       

管理mysql受管主机部署mysql服务

//创建系统用户msyql
[root@ansible ansible]# ansible mysql -m user -a 'name=mysql system=yes shell=/sbin/nologin state=present'
//安装依赖包
[root@ansible ansible]# ansible mysql -m yum -a 'name=ncurses-compat-libs,perl,ncurses-devel,openssl-devel,openssl,cmake,mariadb-devel state=present'
//下载mysql包并解压
[root@ansible ansible]# ansible mysql -a 'wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz'
[root@ansible ansible]# ansible mysql -a  'tar xf mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz '
//修改mysql数据库名称
ansible mysql -a 'mv mysql-8.0.30-linux-glibc2.12-x86_64 mysql'
[root@ansible ansible]# ansible mysql -a 'mv mysql /usr/local/'
//修改目录/usr/local/mysql的属主属组
[root@ansible ansible]# ansible mysql -m file -a 'path=/usr/local/mysql owner=mysql group=mysql'
//配置环境变量
[root@ansible ansible]# ansible mysql -m shell -a 'echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh'
[root@ansible ansible]# ansible mysql -m shell -a 'source /etc/profile.d/mysql.sh'
//做头文件
[root@ansible ansible]# ansible mysql -a 'ln -sv /usr/local/mysql/include/ /usr/include/mysql'
192.168.222.138 | CHANGED | rc=0 >>
'/usr/include/mysql/include' -> '/usr/local/mysql/include/'
//配置lib库文件
[root@ansible ansible]# ansible mysql -m shell -a 'echo "/usr/local/mysql/lib/" > /etc/ld.so.conf.d/mysql.conf'
//编辑man文档
[root@ansible ansible]# ansible mysql -a 'sed -i "22a MANDATORY_MANPATH                         /usr/local/mysql/man" /etc/man_db.conf'
//建立数据存放目录
[root@ansible ansible]# ansible mysql -m file -a 'path=/opt/data state=directory owner=mysql group=mysql'
//初始化数据库
[root@ansible ansible]# ansible mysql -m shell -a '/usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/'
192.168.222.138 | CHANGED | rc=0 >>
2022-10-23T12:55:05.564725Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.30) initializing of server in progress as process 529198
2022-10-23T12:55:05.577111Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-10-23T12:55:06.494304Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-10-23T12:55:08.076144Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: :rg0o-At;Vop
//配置服务启动脚本
[root@ansible ansible]# ansible mysql -a 'cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld'
[root@ansible ansible]# ansible mysql -a 'sed  -i "46cbasedir=/usr/local/mysql" /etc/init.d/mysqld'
[root@ansible ansible]# ansible mysql -a 'sed  -i "47cdatadir=/opt/data" /etc/init.d/mysqld'
//编写脚本添加mysql配置文件和mysql的service文件
[root@ansible ansible]# vim scripts/mysql.sh
[root@ansible ansible]# cat scripts/mysql.sh
#!/bin/bash

cat > /etc/my.cnf <<EOF
[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
EOF

cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target

[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
EOF
[root@ansible ansible]# chmod +x scripts/mysql.sh
[root@ansible ansible]# ansible mysql -m script -a '/etc/ansible/scripts/mysql.sh'
//重新加载配置
[root@ansible ansible]# ansible mysql -a 'systemctl daemon-reload'
//开启服务并设置开机自启
[root@ansible ansible]# ansible mysql -m service -a 'name=mysqld state=started enabled=yes'
//查看端口
[root@ansible ansible]# ansible mysql -a 'ss -antl'
192.168.222.138 | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port  Peer Address:PortProcess       
LISTEN 0      128          0.0.0.0:22         0.0.0.0:*                 
LISTEN 0      128             [::]:22            [::]:*          
LISTEN 0      70                 *:33060            *:*          
LISTEN 0      128                *:3306             *:*          
//修改数据库密码
[root@ansible ansible]# ansible mysql -a 'mysqladmin -uroot -p":rg0o-At;Vop" password 123456'
192.168.222.138 | CHANGED | rc=0 >>
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
//重启mysql服务
[root@ansible ansible]# ansible mysql -m service -a 'name=mysqld state=restarted'
//查看端口
[root@ansible ansible]# ansible mysql -a 'ss -antl'
192.168.222.138 | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port  Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:22         0.0.0.0:*            
LISTEN 0      128             [::]:22            [::]:*          
LISTEN 0      70                 *:33060            *:*          
LISTEN 0      128                *:3306             *:*          

管理php受管主机部署php服务

//编写脚本安装依赖包
[root@ansible ansible]# vim scripts/php_install.sh
[root@ansible ansible]# cat scripts/php_install.sh
#!/bin/bash
yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make
--allowerasing
yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel php-mysqlnd libxml2-devel sqlite-devel https://vault.centos.org/centos/8/PowerTools/x86_64/os/Packages/onigurumadevel-6.8.2-2.el8.x86_64.rpm
yum -y install https://vault.centos.org/centos/8/AppStream/x86_64/os/Packages/libzip-devel1.5.1-2.module_el8.2.0+313+b04d0a66.x86_64.rpm --nobest
yum install libxml2-devel -y
yum install sqlite-devel  -y
yum -y install libcurl-devel
yum -y install gmp-devel
yum  install net-snmp-devel -y
wget https://libzip.org/download/libzip-1.3.2.tar.gz
yum -y install libzip libzip-devel
[root@ansible ansible]# chmod +x scripts/php_install.sh 
[root@ansible ansible]# ansible php -m script -a '/etc/ansible/scripts/php_install.sh'
//下载PHP并解压
[root@ansible ansible]# ansible php -a 'wget https://www.php.net/distributions/php-8.1.11.tar.gz'
[root@ansible ansible]# ansible php -a 'tar xf php-8.1.11.tar.gz -C /usr/src'
//编写脚本编译安装php
root@ansible ansible]# vim scripts/php.sh 
[root@ansible ansible]# cat scripts/php.sh 
#!/bin/bash

cd /usr/src/php-8.1.11/

./configure --prefix=/usr/local/php8 --with-config-file-path=/usr/local/php8/etc --enable-fpm --enable-mysqlnd --with-mysqli --with-pdo-mysql --enable-gd --with-jpeg --with-freetype --with-gettext --with-curl --with-openssl --enable-sockets --enable-mbstring --enable-xml --with-zip --with-zlib --with-snmp --with-mhash --enable-ftp --enable-bcmath --enable-soap --enable-shmop --enable-sysvsem --enable-pcntl --with-gmp
[root@ansible ansible]# chmod +x scripts/php.sh 
[root@ansible ansible]# ansible php -m script -a '/etc/ansible/scripts/php.sh'
...
 "+--------------------------------------------------------------------+",
        "| License:                                                           |",
        "| This software is subject to the PHP License, available in this     |",
        "| distribution in the file LICENSE. By continuing this installation  |",
        "| process, you are bound by the terms of this license agreement.     |",
        "| If you do not agree with the terms of this license, you must abort |",
        "| the installation process at this point.                            |",
        "+--------------------------------------------------------------------+",
        "",
        "Thank you for using PHP.",
        ""
    ]
}
[root@ansible ansible]# ansible php -m shell -a 'cd /usr/src/php-8.1.11/ && make && make install'
//安装后配置环境变量
[root@ansible ansible]# ansible php -m shell -a 'echo "export PATH=/usr/local/php8/bin/:$PATH" > /etc/profile.d/php8.sh'
[root@ansible ansible]# ansible php -m shell -a 'source /etc/profile.d/php8.sh'
//查看版本
[root@ansible ansible]# ansible php -a 'php -v'
192.168.222.139 | CHANGED | rc=0 >>
PHP 8.1.11 (cli) (built: Oct 24 2022 00:39:21) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.11, Copyright (c) Zend Technologies
//配置php-fpm
[root@ansible ansible]# ansible php -a '\cp /usr/src/php-8.1.11/php.ini-production /etc/php.ini'
[root@ansible ansible]# ansible php -a '\cp /usr/src/php-8.1.11/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm'
[root@ansible ansible]# ansible php -m file -a 'path=/etc/init.d/php-fpm mode=755'
[root@ansible ansible]# ansible php -a '\cp /usr/local/php8/etc/php-fpm.conf.default  /usr/local/php8/etc/php-fpm.conf'
[root@ansible ansible]# ansible php -a '\cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf'
//启动php-fpm
[root@ansible ansible]# ansible php -a 'service php-fpm start'
192.168.222.139 | CHANGED | rc=0 >>
Starting php-fpm  done
//查看端口
[root@ansible ansible]# ansible php -a 'ss -anlt'
192.168.222.139 | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
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      128             [::]:22           [::]:*          
//连接nginx和php,生成php测试页面
[root@ansible ansible]# ansible php -m file -a 'path=/usr/local/nginx state=directory'
[root@ansible ansible]# ansible php -m file -a 'path=/usr/local/nginx/html state=directory'
//编写脚本添加php测试页面
[root@ansible ansible]# vim scripts/php.nginx.sh
[root@ansible ansible]# cat scripts/php.nginx.sh
#!/bin/bash

cat > /usr/local/nginx/html/index.php << EOF
<?php
     phpinfo();
?>
EOF
[root@ansible ansible]# chmod +x scripts/php.nginx.sh
[root@ansible ansible]# ansible php -m script -a '/etc/ansible/scripts/php.nginx.sh'
//修改php/usr/local/php8/etc/php-fpm.d/www.conf文件的clisten和clisten.allowed_clients指向
[root@ansible ansible]# ansible php -a 'sed -i "36clisten = 192.168.222.139:9000" /usr/local/php8/etc/php-fpm.d/www.conf'
[root@ansible ansible]# ansible php -a 'sed -i "63clisten.allowed_clients = 192.168.222.137" /usr/local/php8/etc/php-fpm.d/www.conf'
//修改nginx配置文件
[root@ansible ansible]# ansible nginx -a 'sed -i "45c                   index  index.php index.html index.htm;" /usr/local/nginx/conf/nginx.conf'
[root@ansible ansible]# ansible nginx -a 'sed -i "65c     location ~ \.php$ {" /usr/local/nginx/conf/nginx.conf'
[root@ansible ansible]# ansible nginx -a 'sed -i "66c     root           html;" /usr/local/nginx/conf/nginx.conf'
[root@ansible ansible]# ansible nginx -a 'sed -i "67c     fastcgi_pass   192.168.222.139:9000;" /usr/local/nginx/conf/nginx.conf'
[root@ansible ansible]# ansible nginx -a 'sed -i "68c     fastcgi_index  index.php;" /usr/local/nginx/conf/nginx.conf'
[root@ansible ansible]# ansible nginx -a 'sed -i "69c     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;" /usr/local/nginx/conf/nginx.conf'
[root@ansible ansible]# ansible nginx -a 'sed -i "70c      include        fastcgi_params;" /usr/local/nginx/conf/nginx.conf'
[root@ansible ansible]# ansible nginx -a 'sed -i "71c      }" /usr/local/nginx/conf/nginx.conf'
//重启nginx和php服务
[root@ansible ansible]# ansible nginx -m service -a 'name=nginx state=restarted'
[root@ansible ansible]# ansible php -a 'service php-fpm restart'
192.168.222.139 | CHANGED | rc=0 >>
Gracefully shutting down php-fpm warning, no pid file found - php-fpm is not running ?
Starting php-fpm  done
//查看端口
[root@ansible ansible]# ansible nginx -a 'ss -antl'
192.168.222.137 | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:80        0.0.0.0:*          
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*          
LISTEN 0      128             [::]:22           [::]:*          
[root@ansible ansible]# ansible php -a 'ss -antl'
192.168.222.139 | CHANGED | rc=0 >>
State  Recv-Q Send-Q   Local Address:Port Peer Address:PortProcess
LISTEN 0      128    192.168.222.139:9000      0.0.0.0:*          
LISTEN 0      128            0.0.0.0:22        0.0.0.0:*          
LISTEN 0      128               [::]:22           [::]:*          

访问:

有关Ansible部署LNMP的更多相关文章

  1. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  2. ruby-on-rails - Ruby on Rails 可以部署在 Azure 网站上吗? - 2

    我可以在Azure网站上部署RubyonRails吗? 最佳答案 还没有。目前仅支持.NET和PHP。 关于ruby-on-rails-RubyonRails可以部署在Azure网站上吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/12964010/

  3. jenkins部署1--jenkins+gitee持续集成 - 2

    前置步骤我们都操作完了,这篇开始介绍jenkins的集成。话不多说,看操作1、登录进入jenkins后会让你选择安装插件,选择第一个默认的就行。安装完成后设置账号密码,重新登录。2、配置JDK和Git都需要执行路径,所以需要先把执行路径找到,先进入服务器的docker容器,2.1JDK的路径root@69eef9ee86cf:/usr/bin#echo$JAVA_HOME/usr/local/openjdk-82.2Git的路径root@69eef9ee86cf:/#whichgit/usr/bin/git3、先配置JDK和Git。点击:ManageJenkins>>GlobalToolCon

  4. 深度学习部署:Windows安装pycocotools报错解决方法 - 2

    深度学习部署:Windows安装pycocotools报错解决方法1.pycocotools库的简介2.pycocotools安装的坑3.解决办法更多Ai资讯:公主号AiCharm本系列是作者在跑一些深度学习实例时,遇到的各种各样的问题及解决办法,希望能够帮助到大家。ERROR:Commanderroredoutwithexitstatus1:'D:\Anaconda3\python.exe'-u-c'importsys,setuptools,tokenize;sys.argv[0]='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-instal

  5. Ruby,使用包含 TK GUI 的 ocra 部署一个 exe - 2

    Ocra无法处理需要“tk”的应用程序require'tk'puts'nope'用奥克拉http://github.com/larsch/ocra不起作用(如链接中的一个问题所述)问题:https://github.com/larsch/ocra/issues/29(Ocra是1.9的"new"rubyscript2exe,本质上它用于将rb脚本部署为可执行文件)唯一的问题似乎是缺少tcl的DLL文件我不认为这是一个问题据我所知,问题是缺少tk的DLL文件如果它们是已知的,则可以在执行ocra时将它们包括在内有没有办法知道tk工作所需的DLL依赖项? 最佳答

  6. ruby-on-rails - NameError(未初始化常量 Unzipper::Zip)但仅在 Heroku 部署(Rails)上 - 2

    我有一个类unzipper.rb,它使用Rubyzip解压文件。在我的本地环境中,我可以成功解压缩文件,而无需使用require'zip'明确包含依赖项但是在Heroku上,我得到一个NameError(uninitializedconstantUnzipper::Zip)我只能通过使用明确的require来解决问题:为什么这在H​​eroku环境中是必需的,但在本地主机上却不是?我的印象是Rails自动需要所有gem。app/services/unzipper.rbrequire'zip'#OnlyrequiredforHeroku.Workslocallywithout!class

  7. ruby - Sinatra + Heroku + Datamapper 使用 dm-sqlite-adapter 部署问题 - 2

    出于某种原因,heroku尝试要求dm-sqlite-adapter,即使它应该在这里使用Postgres。请注意,这发生在我打开任何URL时-而不是在gitpush本身期间。我构建了一个默认的Facebook应用程序。gem文件:source:gemcuttergem"foreman"gem"sinatra"gem"mogli"gem"json"gem"httparty"gem"thin"gem"data_mapper"gem"heroku"group:productiondogem"pg"gem"dm-postgres-adapter"endgroup:development,:t

  8. ruby-on-rails - 在服务器上没有互联网访问权限的 Capistrano 部署 - 2

    如何使用Capistrano将Rails应用程序部署到无法访问外部网络或存储库的生产或暂存服务器?我已经设法完成部署的一半,并意识到Capistrano没有在我的本地机器上下载gitrepo,但它首先连接到远程服务器并尝试在那里下载Git存储库。我希望有一个类似Javaee的构建系统,其中创建可交付成果并将该可交付成果发送到服务器。就像您构建.ear文件并将其部署到您想要的任何服务器上一样。显然在RoR中,你被迫(据我所知)在该服务器上构建应用程序,在那里创建一个gem存储库,在那里克隆最新的分支等等。有什么方法可以将准备运行的包发送到远程服务器吗? 最佳答

  9. Streampark集成Cloudera Flink、ldap、告警,以及部署常见问题 - 2

    集成背景我们当前集群使用的是ClouderaCDP,Flink版本为ClouderaVersion1.14,整体Flink安装目录以及配置文件结构与社区版本有较大出入。直接根据Streampark官方文档进行部署,将无法配置FlinkHome,以及后续整体Flink任务提交到集群中,因此需要进行针对化适配集成,在满足使用需求上,尽量提供完整的Streampark使用体验。集成步骤版本匹配问题解决首先解决无法识别Cloudera中的FlinkHome问题,根据报错主要明确到的事情是无法读取到Flink版本、lib下面的jar包名称无法匹配。修改对象:修改源码:(解决无法匹配clouderajar

  10. ruby - 部署 Rack 应用程序的可靠方法 (Sinatra) - 2

    我正在寻找一种可靠的方式来部署Rack应用程序(在本例中为Sinatra应用程序)。请求将花费一些时间(0.25-0.5秒等待代理HTTP请求)并且可能会有相当大的流量。我应该使用传统的mongrel集群设置吗?使用HAProxy作为负载均衡器?恩金克斯?架子?您使用过哪些解决方案,有哪些优势? 最佳答案 Nginx/unicornFTW!前面的Nginx提供静态文件和unicorn处理Sinatra应用程序。优点:性能、使用unixsocks的良好负载平衡以及无需停机即可部署/升级(您可以在不停机的情况下升级Ruby/Nginx/

随机推荐