ansible 一键安装php环境
目录结构:
[root@szwpldb1081 php]# cat install_php.yaml --- - hosts: tmphost tasks: - name: install dependents import_tasks: common/install_dependents.yaml - name: install php import_tasks: common/install_php.yaml [root@szwpldb1081 php]# tree . ├── common │ ├── install_dependents.yaml │ └── install_php.yaml ├── install_php.yaml ├── install_php.yaml_bak └── template ├── httpd.conf ├── index.php ├── install_php └── php.ini
主yaml文件:
[root@szwpldb1081 php]# cat install_php.yaml --- - hosts: tmphost tasks: - name: install dependents import_tasks: common/install_dependents.yaml - name: install php import_tasks: common/install_php.yaml
php一些安装依赖包:
[root@szwpldb1081 php]# cat common/install_dependents.yaml --- - name: install gcc yum: name: gcc state: present - name: install gcc-c++ yum: name: gcc-c++ state: present - name: install bzip2-devel yum: name: bzip2-devel state: present - name: install libjpeg-devel yum: name: libjpeg-devel state: present - name: install libpng-devel yum: name: libpng-devel state: present - name: install freetype-devel yum: name: freetype-devel state: present - name: install freetype-devel yum: name: libxml2-devel
主要功能文件:
[root@szwpldb1081 php]# cat common/install_php.yaml --- - name: unarchive php-7.2.13.tar.gz to remonte host unarchive: src: "/usr/local/mysqltools/sps/php/php-7.2.13.tar.gz" dest: /tmp/php - name: copy install_php.sh to remonte host copy: src: /usr/local/mysqltools/sps/php/install_php.sh dest: /tmp/php/install_php.sh - name: install php shell: /bin/bash /tmp/php/install_php.sh - name: copy php.ini to remote copy: src: ./template/php.ini dest: /usr/local/php/etc/php.ini - name: remove /tmp/install_php.sh file: dest: /tmp/php/ state: absent
脚本实现安装:
[root@szwpldb1081 php]# cat /usr/local/mysqltools/sps/php/install_php.sh #!/bin/bash #安装php脚本 err_echo(){ echo -e "\033[41;37m[Error]: $1 \033[0m" exit 1 } info_echo(){ echo -e "\033[42;37m[Info]: $1 \033[0m" } check_file_is_exists(){ if [ ! -f "/usr/local/src/$1" ];then info_echo "$1开始下载" fi } check_exit(){ if [ $? -ne 0 ]; then err_echo "$1" exit 1 fi } check_success(){ if [ $? -eq 0 ];then info_echo "$1" fi } [ $(id -u) != "0" ] && err_echo "please run this script as root user." && exit 1 cat >> /etc/yum.repos.d << EEE # CentOS-Base.repo # # The mirror system uses the connecting IP address of the client and the # update status of each mirror to pick mirrors that are updated to and # geographically close to the client. You should use this for CentOS updates # unless you are manually picking other mirrors. # # If the mirrorlist= does not work for you, as a fall back you can try the # remarked out baseurl= line instead. # # [base] name=CentOS-7 - Base - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/7/os/$basearch/ http://mirrors.aliyuncs.com/centos/7/os/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/7/os/$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #released updates [updates] name=CentOS-7 - Updates - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/7/updates/$basearch/ http://mirrors.aliyuncs.com/centos/7/updates/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/7/updates/$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #additional packages that may be useful [extras] name=CentOS-7 - Extras - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/7/extras/$basearch/ http://mirrors.aliyuncs.com/centos/7/extras/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/7/extras/$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #additional packages that extend functionality of existing packages [centosplus] name=CentOS-7 - Plus - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/7/centosplus/$basearch/ http://mirrors.aliyuncs.com/centos/7/centosplus/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/7/centosplus/$basearch/ gpgcheck=1 enabled=0 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #contrib - packages by Centos Users [contrib] name=CentOS-7 - Contrib - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/7/contrib/$basearch/ http://mirrors.aliyuncs.com/centos/7/contrib/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/7/contrib/$basearch/ gpgcheck=1 enabled=0 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 EEE function init_servers(){ info_echo "开始初始化服务器" sed -i \'s/SELINUX=enforcing/SELINUX=disabled/g\' /etc/selinux/config setenforce 0 systemctl stop firewalld.service systemctl disable firewalld.service info_echo "更换阿里源" yum install wget -y wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y yum clean all } function install_package(){ info_echo "开始安装系统必备依赖包" yum install ntpdate gcc gcc-c++ wget lsof lrzsz systemd-devel -y info_echo "开始安装php所需依赖包" yum install -y libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel yum install -y systemd-devel mysql-devel yum install -y openjpeg-devel info_echo "开始安装nginx所需依赖包" yum install -y pcre pcre-devel zlib zlib-devel } function download_install_package(){ if [ ! -f "/usr/local/src/php-7.2.13.tar.gz" ];then info_echo "开始下载php-7.2.13.tar.gz" wget -P /usr/local/src http://cn2.php.net/distributions/php-7.2.13.tar.gz check_success "php-7.2.13.tar.gz已下载至/usr/local/src目录" else info_echo "php-7.2.13.tar.gz已存在,不需要下载" fi } function install_php(){ info_echo "开始安装php-7.2.13" sleep 2s groupadd php-fpm && useradd -s /sbin/nologin -g php-fpm -M php-fpm groupadd zabbix && useradd -g zabbix zabbix cd /usr/local/src tar xvf /usr/local/src/php-7.2.13.tar.gz cd /usr/local/src/php-7.2.13 ./configure \ --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --with-zlib-dir \ --with-freetype-dir \ --enable-mbstring \ --with-libxml-dir=/usr \ --enable-xmlreader \ --enable-xmlwriter \ --enable-soap \ --enable-calendar \ --with-curl \ --with-zlib \ --with-gd \ --with-pdo-sqlite \ --with-pdo-mysql \ --with-mysqli \ --with-mysql-sock \ --enable-mysqlnd \ --disable-rpath \ --enable-inline-optimization \ --with-bz2 \ --with-zlib \ --enable-sockets \ --enable-sysvsem \ --enable-sysvshm \ --enable-pcntl \ --enable-mbregex \ --enable-exif \ --enable-bcmath \ --with-mhash \ --enable-zip \ --with-pcre-regex \ --with-jpeg-dir=/usr \ --with-png-dir=/usr \ --with-openssl \ --enable-ftp \ --with-kerberos \ --with-gettext \ --with-xmlrpc \ --with-xsl \ --enable-fpm \ --with-fpm-user=php-fpm \ --with-fpm-group=php-fpm \ --with-fpm-systemd \ --disable-fileinfo check_exit "configure php-7.2.13失败" make -j8 && make install -j8 check_exit "make php-7.2.13失败" info_echo "开始配置php-7.2.13" cp /usr/local/src/php-7.2.13/php.ini-production /usr/local/php/etc/php.ini && cd /usr/local/php/etc && cp php-fpm.conf.default php-fpm.conf cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf cp /usr/local/src/php-7.2.13/sapi/fpm/php-fpm.service /usr/lib/systemd/system sed -i "s/;date.timezone =/date.timezone = Asia\/Shanghai/g" /usr/local/php/etc/php.ini sed -i "s#`grep max_execution_time /usr/local/php/etc/php.ini`#max_execution_time = 300#g" /usr/local/php/etc/php.ini sed -i "s#`grep post_max_size /usr/local/php/etc/php.ini`#post_max_size = 32M#g" /usr/local/php/etc/php.ini sed -i "s#`grep max_input_time\ = /usr/local/php/etc/php.ini`#max_input_time = 300#g" /usr/local/php/etc/php.ini sed -i "s#`grep memory_limit /usr/local/php/etc/php.ini`#memory_limit = 128M#g" /usr/local/php/etc/php.ini sed -i "s#`grep post_max_size /usr/local/php/etc/php.ini`#post_max_size = 32M#g" /usr/local/php/etc/php.ini sed -i "s/user = php-fpm/user = zabbix/g" /usr/local/php/etc/php-fpm.d/www.conf sed -i "s/group = php-fpm/group = zabbix/g" /usr/local/php/etc/php-fpm.d/www.conf systemctl start php-fpm systemctl enable php-fpm STAT=`echo $?` PORT=`netstat -lntup|grep php-fpm|wc -l` if [ $STAT -eq 0 ] && [ $PORT -eq 1 ];then info_echo "php-fpm启动成功" else err_echo "php-fpm未启动成功,请检查" exit 1 fi } function main(){ init_servers install_package download_install_package install_php } main
主要就是这个文件,里面有一些冗余的,可以根据自己的情况修改,最简单的就是一个脚本,直接被ansible调用就能安装上了,但是这种也有不好的地方,有时执行进度不能实时在ansible服务端输出,还需要
修改脚本或者修改ansible配置文件yaml输出配置,各有各的取舍。