verynginx
verynginx_deploy.yml --- - name: Deploy VeryNginx to sl group hosts: sl become: true roles: - verynginx tasks: --- # 1. 创建目标目录 - name: Ensure target directory exists ansible.builtin.file: path: /opt/ state: directory owner: root group: root mode: '0755' # 2. 上传打包好的 VeryNginx 文件 - name: Upload VeryNginx tarball ansible.builtin.copy: src: verynginx_192.tar.gz dest: /tmp/verynginx_192.tar.gz mode: '0644' # 3. 解压 VeryNginx - name: Extract VeryNginx ansible.builtin.unarchive: src: /tmp/verynginx_192.tar.gz dest: /opt/ remote_src: yes # 4. 创建用户组 - name: Create www group ansible.builtin.group: name: www state: present - name: Create www user ansible.builtin.user: name: www group: www state: present # 5. 发送库文件 - name: Copy libjemalloc.so.2 to /usr/lib64 ansible.builtin.copy: src: files/libjemalloc.so.2 # 需要复制的文件 dest: /usr/lib64/libjemalloc.so.2 # 目标路径 owner: root # 设置文件所有者为 root group: root # 设置文件所属组为 root mode: '0644' # 设置文件权限 # 5. 创建目录 - name: Ensure /data/wwwlogs directory exists ansible.builtin.file: path: /data/wwwlogs state: directory owner: root group: root mode: '0755' # 5. 创建日志文件 - name: Create /data/wwwlogs/error_nginx.log file ansible.builtin.file: path: /data/wwwlogs/error_nginx.log state: touch # 确保文件存在,若文件不存在则创建它 owner: root group: root mode: '0644' ## 4. 部署 Nginx 配置文件(可选) #- name: Deploy Nginx configuration # ansible.builtin.template: # src: nginx.conf.j2 # dest: /opt/verynginx/nginx/conf/nginx.conf # owner: root # group: root # mode: '0644' # notify: Restart VeryNginx # 5. 启动 VeryNginx - name: Start Nginx ansible.builtin.shell: | cd /opt/verynginx/openresty/nginx/sbin/ ./nginx async: 10 poll: 0 become: true # 如果需要管理员权限 - name: Ensure Nginx is running ansible.builtin.shell: | ps aux | grep nginx | grep -v grep register: nginx_status failed_when: nginx_status.stdout == "" become: true - name: Debug output ansible.builtin.debug: var: nginx_status
mysql8
---
- name: Install and configure MySQL 8.0.30
hosts: sl
become: true
tasks:
# 确保必要的依赖已安装
- name: Install required dependencies
ansible.builtin.yum:
name:
- libaio
- numactl
- tar
state: present
# 创建必要的目录
- name: Ensure installation and data directories exist
ansible.builtin.file:
path: "{{ item }}"
state: directory
mode: '0755'
with_items:
- /data
- /data/mysql8
- /data/mysql8_data
# 分发 MySQL 安装包
- name: Copy MySQL installation package to target hosts
ansible.builtin.copy:
src: /data/mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz
dest: /data/mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz
mode: '0644'
# 解压 MySQL 安装包
- name: Extract MySQL installation package
ansible.builtin.unarchive:
src: /data/mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz
dest: /data/mysql8
remote_src: yes
# 创建 MySQL 用户和组
- name: Create MySQL user and group
ansible.builtin.user:
name: mysql
system: yes
shell: /bin/false
# 设置数据目录权限
- name: Set permissions for MySQL data directory
ansible.builtin.file:
path: /data/mysql8_data
state: directory
owner: mysql
group: mysql
mode: '0755'
# 配置 MySQL 服务文件
- name: Replace MySQL configuration file with new content
ansible.builtin.copy:
dest: /etc/my.cnf
content: |
[mysqld]
basedir=/data/mysql8/mysql-8.0.30-linux-glibc2.12-x86_64
datadir=/data/mysql8_data
port=3306
socket=/tmp/mysql.sock
user=mysql
log-error=/var/log/mysqld.log
# Enable InnoDB with file per table
innodb_file_per_table=1
# Additional options can be added below as needed
mode: '0644'
backup: yes # 可选,保存备份
#创建日志文件赋权
- name: Create mysqld.log file if it does not exist
ansible.builtin.file:
path: /var/log/mysqld.log
state: touch # 创建文件,如果文件不存在
mode: '0660' # 文件权限,mysql 用户和组可读写
owner: mysql # 设置文件所有者为 mysql 用户
group: mysql # 设置文件所属组为 mysql 组
# 初始化 MySQL 数据目录
- name: Initialize MySQL data directory
ansible.builtin.shell: |
/data/mysql8/mysql-8.0.30-linux-glibc2.12-x86_64/bin/mysqld --initialize-insecure --user=mysql --basedir=/data/mysql8/mysql-8.0.30-linux-glibc2.12-x86_64 --datadir=/data/mysql8_data
args:
executable: /bin/bash
# 配置 MySQL 启动脚本
- name: Create MySQL systemd service file
ansible.builtin.copy:
dest: /etc/systemd/system/mysqld.service
content: |
[Unit]
Description=MySQL Server
After=network.target
[Service]
#Type=simple
Type=notify
ExecStart=/data/mysql8/mysql-8.0.30-linux-glibc2.12-x86_64/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql
#ExecStop=/data/mysql8/mysql-8.0.30-linux-glibc2.12-x86_64/bin/mysqladmin shutdown -u root -pabc123456
ExecStop=/data/mysql8/mysql-8.0.30-linux-glibc2.12-x86_64/bin/mysqladmin shutdown --socket=/data/mysql8_data/mysql.sock
User=mysql
Group=mysql
LimitNOFILE=65535
Restart=on-failure
[Install]
WantedBy=multi-user.target
mode: '0644'
# 启动并启用 MySQL 服务
- name: Start and enable MySQL service
ansible.builtin.systemd:
name: mysqld
state: started
enabled: true
#创建快捷方式
- name: Create symbolic link for MySQL binary
ansible.builtin.file:
src: /data/mysql8/mysql-8.0.30-linux-glibc2.12-x86_64/bin/mysql
dest: /usr/local/bin/mysql
state: link
# 设置 root@% 用户和密码
- name: Configure root@% user with password
ansible.builtin.shell: |
/data/mysql8/mysql-8.0.30-linux-glibc2.12-x86_64/bin/mysql -u root --socket=/tmp/mysql.sock -e "
CREATE USER 'root'@'%' IDENTIFIED BY 'abc123';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'abc123456';
FLUSH PRIVILEGES;"
args:
executable: /bin/bash
# 验证 root@% 用户是否创建成功
- name: Verify root@% user configuration
ansible.builtin.shell: |
/data/mysql8/mysql-8.0.30-linux-glibc2.12-x86_64/bin/mysql -u root -pabc123456 --host=127.0.0.1 --port=3306 -e "SELECT User, Host FROM mysql.user;"
register: mysql_user_check
- name: Output MySQL user check results
ansible.builtin.debug:
msg: "{{ mysql_user_check.stdout }}"php7.1.5编译
---
- name: Install PHP 7.1.5
hosts: sl
become: true
vars:
php_version: "7.1.5"
php_tarball: "/data/php-7.1.5.tar.gz"
install_dir: "/usr/local"
php_ini_path: "/usr/local/php/etc/php.ini"
tasks:
- name: Ensure required packages are installed
apt:
name:
- build-essential
- libxml2-dev
- libcurl4-openssl-dev
- libjpeg-dev
- libpng-dev
- libfreetype6-dev
- libonig-dev
- libzip-dev
- libssl-dev
- zlib1g-dev
state: present
when: ansible_os_family == "Debian"
- name: Ensure required packages are installed (RHEL-based)
yum:
name:
- gcc
- gcc-c++
- libxml2-devel
- curl-devel
- libjpeg-devel
- libpng-devel
- freetype-devel
- oniguruma-devel
- libzip-devel
- openssl-devel
- zlib-devel
state: present
when: ansible_os_family == "RedHat"
- name: Copy PHP tarball to remote server
copy:
src: "{{ php_tarball }}"
dest: "/tmp/php-{{ php_version }}.tar.gz"
- name: Extract PHP tarball
unarchive:
src: "/tmp/php-{{ php_version }}.tar.gz"
dest: "/tmp"
remote_src: yes
- name: Configure and compile PHP
shell: |
cd /tmp/php-{{ php_version }}
./configure --prefix={{ install_dir }}/php \
--with-config-file-path={{ install_dir }}/php/etc \
--enable-mbstring \
--enable-fpm \
--with-curl \
--with-openssl \
--with-zlib \
--with-jpeg \
--with-freetype
make && make install
args:
creates: "{{ install_dir }}/php/bin/php"
- name: Copy php.ini-production to php.ini
copy:
src: "/tmp/php-{{ php_version }}/php.ini-production"
dest: "{{ php_ini_path }}"
remote_src: true
- name: Ensure php-fpm log directory exists
file:
path: "/data/wwwlogs/php"
state: directory
owner: www
group: www
mode: '0755'
- name: Create php-fpm error log file
file:
path: "/data/wwwlogs/php/php-fpm.log"
state: touch
owner: www
group: www
mode: '0644'
- name: Create php-fpm slow log file
file:
path: "/data/wwwlogs/php/php-fpm.slow.log"
state: touch
owner: www
group: www
mode: '0644'
- name: Ensure php-fpm.d directory exists
file:
path: "{{ install_dir }}/php/etc/php-fpm.d"
state: directory
mode: '0755'
- name: Create default pool configuration
copy:
dest: "{{ install_dir }}/php/etc/php-fpm.d/www.conf"
content: |
[www]
user = www
group = www
listen = /run/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0660
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
slowlog = /data/wwwlogs/php/php-fpm.slow.log
- name: Configure PHP-FPM for startup
shell: |
cp {{ install_dir }}/php/etc/php-fpm.conf.default {{ install_dir }}/php/etc/php-fpm.conf
cp {{ install_dir }}/php/sbin/php-fpm {{ install_dir }}/php/sbin/php-fpm
echo "[Unit]
Description=The PHP FastCGI Process Manager
After=network.target
[Service]
Type=simple
ExecStart={{ install_dir }}/php/sbin/php-fpm --nodaemonize
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=process
PIDFile=/run/php-fpm.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target" > /etc/systemd/system/php-fpm.service
systemctl daemon-reload
args:
creates: "/etc/systemd/system/php-fpm.service"
- name: Create PHP-FPM configuration file
copy:
dest: "/usr/local/php/etc/php-fpm.conf"
content: |
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /data/wwwlogs/php/php-fpm.log
include=/usr/local/php/etc/php-fpm.d/*.conf
- name: Enable and start PHP-FPM
systemd:
name: php-fpm
enabled: yes
state: startedansible-playbook -i inventory php7.1.5.yml
php5.6解压
--- - name: Install PHP 5.6 and setup PHP-FPM hosts: sl become: yes # 使用 sudo 权限 tasks: # Step 1: Install rsync if not present - name: Ensure rsync is installed package: name: rsync state: present # Step 2: Copy the php5.6.tar.gz to the target machine - name: Copy php5.6.tar.gz to the target machine copy: src: "/data/php5.6.tar.gz" # 本地文件路径 dest: "/usr/local/php5.6.tar.gz" # Step 3: Extract php5.6.tar.gz to /usr/local - name: Extract php5.6.tar.gz to /usr/local unarchive: src: "/usr/local/php5.6.tar.gz" dest: "/usr/local" remote_src: yes # Step 4: Copy files from local /data/lib64 to remote /usr/lib64 - name: Copy files from /data/lib64 to /usr/lib64 synchronize: src: "/data/lib64/" dest: "/usr/lib64/" recursive: yes # Step 5: Create the log directory and error_log file - name: Ensure /data/wwwlogs/php56-fpm/log exists file: path: "/data/wwwlogs/php56-fpm/log" state: directory owner: www group: www mode: '0755' - name: Create the error_log file file: path: "/data/wwwlogs/php56-fpm/log/error_log" state: touch owner: www group: www mode: '0644' # Step 6: 新版systemd启动方式,/etc/systemd/system/php-fpm.service - name: Configure PHP 5.6 FPM service for systemd copy: dest: /etc/systemd/system/php5.6-fpm.service content: | [Unit] Description=PHP 5.6 FastCGI Process Manager After=network.target [Service] ExecStart=/usr/local/php5.6/sbin/php-fpm --nodaemonize ExecReload=/bin/kill -USR2 KillMode=process PIDFile=run/php-fpm.pid Restart=on-failure [Install] WantedBy=multi-user.target # Step 7: Configure PHP-FPM to start on boot - name: Reload systemd daemon command: systemctl daemon-reload - name: Enable and start PHP 5.6 FPM service systemd: name: php5.6-fpm enabled: yes state: started ##以下为老版本service启动方式 # # Step 6: Copy php-fpm init script to /etc/init.d # - name: Copy php5.6-fpm init script to /etc/init.d/ # copy: # src: "/data/php5.6-fpm" # 本地 init 脚本路径 # dest: "/etc/init.d/php5.6-fpm" # mode: '0755' # 设置执行权限 # # # Step 7: Configure PHP-FPM to start on boot # - name: Start PHP-FPM using shell command # shell: "/etc/init.d/php5.6-fpm start" # register: php_fpm_start_result # ignore_errors: yes # 忽略错误,避免任务失败导致后续任务中断 # # - name: Debug PHP-FPM start output # debug: # var: php_fpm_start_result.stdout
ansible-playbook -i inventory php5.6.yml
/usr/lib64/需要同步的文件
libiconv.so.2 libicudata.so.50 libicui18n.so.50 libicuio.so.50 libicuuc.so.50 libmcrypt.so.4
以下是启动脚本
vim /etc/init.d/php5.6-fpm
#! /bin/sh
### BEGIN INIT INFO
# Provides: php-fpm
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts php-fpm
# Description: starts the PHP FastCGI Process Manager daemon
### END INIT INFO
prefix=/usr/local/php5.6
exec_prefix=${prefix}
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"
wait_for_pid () {
try=0
while test $try -lt 35 ; do
case "$1" in
'created')
if [ -f "$2" ] ; then
try=''
break
fi
;;
'removed')
if [ ! -f "$2" ] ; then
try=''
break
fi
;;
esac
echo -n .
try=`expr $try + 1`
sleep 1
done
}
case "$1" in
start)
echo -n "Starting php-fpm "
$php_fpm_BIN --daemonize $php_opts
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
fi
wait_for_pid created $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Gracefully shutting down php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -QUIT `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if [ ! -r $php_fpm_PID ] ; then
echo "php-fpm is stopped"
exit 0
fi
PID=`cat $php_fpm_PID`
if ps -p $PID | grep -q $PID; then
echo "php-fpm (pid $PID) is running..."
else
echo "php-fpm dead but pid file exists"
fi
;;
force-quit)
echo -n "Terminating php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -TERM `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
$0 start
;;
reload)
echo -n "Reload service php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -USR2 `cat $php_fpm_PID`
echo " done"
;;
*)
echo "Usage: $0 {start|stop|force-quit|restart|reload|status}"
exit 1
;;
esacredis5
---
- name: Compile and Install Redis 5.0.14 from /data
hosts: sl
become: true
vars:
redis_version: "5.0.14"
redis_source_file: "/data/redis-{{ redis_version }}.tar.gz"
redis_source_dir: "/data/redis-{{ redis_version }}"
redis_install_dir: "/usr/local/redis"
redis_config_file: "/usr/local/redis/redis.conf"
redis_service_name: "redis"
redis_port: 6379
redis_user: "redis"
redis_group: "redis"
tasks:
- name: Install Development Tools on RHEL/CentOS #安装依赖
yum:
name: "{{ item }}"
state: present
loop:
- gcc
- make
- tcl
# - name: Install build-essential on Debian/Ubuntu #用条件判断安装依赖
# apt:
# name: "{{ item }}"
# state: present
# loop:
# - build-essential
# - tcl
# when: ansible_facts['os_family'] == 'Debian'
#
# - name: Install Development Tools on RHEL/CentOS
# yum:
# name: "{{ item }}"
# state: present
# loop:
# - gcc
# - make
# - tcl
# when: ansible_facts['os_family'] == 'RedHat'
- name: Create /data directory on target host #创建目标目录
ansible.builtin.file:
path: /data
state: directory
mode: '0755'
- name: Copy Redis source file to target host #分发redis源码包到目标主机
ansible.builtin.copy:
src: /data/redis-5.0.14.tar.gz
dest: /data/redis-5.0.14.tar.gz
mode: '0644'
- name: Extract Redis source file #解压源码包
ansible.builtin.unarchive:
src: /data/redis-5.0.14.tar.gz
dest: /data/
remote_src: yes
- name: Create Redis user and group #创建用户组
group:
name: "{{ redis_group }}"
state: present
- name: Create Redis user #创建用户
user:
name: "{{ redis_user }}"
group: "{{ redis_group }}"
home: "{{ redis_install_dir }}"
shell: /bin/false
state: present
- name: Check if Redis source file existsi #检查redis安装包
stat:
path: "{{ redis_source_file }}"
register: redis_source_file_check
- name: Fail if Redis source file does not exist #如果文件不存在
fail:
msg: "Redis source file {{ redis_source_file }} does not exist. Please ensure the package is available in /data."
when: not redis_source_file_check.stat.exists
- name: Extract Redis source file #提取redis包
unarchive:
src: "{{ redis_source_file }}"
dest: "/data"
remote_src: yes
- name: Compile and install Redis #编译安装
shell: |
cd {{ redis_source_dir }}
make
make PREFIX={{ redis_install_dir }} install
- name: Change ownership of Redis installation directory #目录权限
file:
path: "{{ redis_install_dir }}"
state: directory
owner: "{{ redis_user }}"
group: "{{ redis_group }}"
recurse: yes
- name: Create Redis configuration file #创建配置文件
copy:
dest: "{{ redis_config_file }}"
content: |
bind 0.0.0.0
port 6379
dir /var/lib/redis
logfile /var/log/redis.log
daemonize yes
pidfile /var/run/redis.pid
requirepass abc123
maxclients 10000
databases 32
mode: '0644'
owner: "{{ redis_user }}"
group: "{{ redis_group }}"
- name: Create Redis data directory #创建数据目录
file:
path: "{{ redis_install_dir }}/data"
state: directory
owner: "{{ redis_user }}"
group: "{{ redis_group }}"
- name: Start Redis server #启动
ansible.builtin.shell: |
redis-server /usr/local/redis/redis.conf
args:
executable: /bin/bash
- name: Check Redis process #检查进程
ansible.builtin.shell: |
ps aux | grep redis-server | grep -v grep
register: redis_status
failed_when: "'redis-server' not in redis_status.stdout"
- name: Output Redis status #输出状态
ansible.builtin.debug:
msg: "Redis is running: {{ redis_status.stdout }}"
# - name: Create systemd service file for Redis #ubuntu启动文件
# copy:
# dest: "/etc/systemd/system/{{ redis_service_name }}.service"
# content: |
# [Unit]
# Description=Redis In-Memory Data Store
# After=network.target
#
# [Service]
# User={{ redis_user }}
# Group={{ redis_group }}
# ExecStart={{ redis_install_dir }}/bin/redis-server {{ redis_config_file }}
# ExecStop=/bin/kill -s TERM $MAINPID
# Restart=always
#
# [Install]
# WantedBy=multi-user.target
#
# - name: Reload systemd daemon #重启
# shell: systemctl daemon-reload
#
# - name: Enable and start Redis service
# systemd:
# name: "{{ redis_service_name }}"
# enabled: true
# state: started
#
# - name: Verify Redis installation #验证安装
# shell: "{{ redis_install_dir }}/bin/redis-cli ping"
# register: redis_ping_output
#
# - name: Display Redis status #显示状态
# debug:
# msg: "{{ redis_ping_output.stdout }}"
#登录
redis-cli -h 127.0.0.1 -c -p 6379
验证密码
auth shimujiuxia
-
« 上一篇:
34.硬盘分区
-
36.vim基本1
:下一篇 »