ansible自动化安装lnmp
今天尝试用ansible自动化编译安装了lnmp环境,是以把自己的见解和大家分享,不足之处还望大家指正。
创新互联公司-专业网站定制、快速模板网站建设、高性价比辽中网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式辽中网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖辽中地区。费用合理售后完善,十年实体公司更值得信赖。
lnmp的构成
lnmp = linux + nginx + MySQL + php/python/perl
下面给大家介绍下我的安装步骤
系统:linux7
首先下载lnmp环境所需模块,创建ansible角色
# mkdir -pv /tmp/roles/{mysql,php,nginx}/{files,vars,templates,tasks,handlers,meta}
# cd /tmp/roles/mysql/
第一步:安装mysql,我安装的版本是5.6.x
编写脚本
vim /tmp/roles/mysql/files/mysql.sh
#!/bin/bash mount -o loop /dev/sr0 /yum yum clean all yum list #导入lnmp环境所需依赖包 yum install cmake* gcc gcc-c++ ncurses-devel perl libxml* libcurl* libjpeg* libpng* freetype* sleep 10 #解压mysql压缩包 tar xzf /root/Downloads/bag/11-mysql-5.6.26.tar.gz -C /usr/src cd /usr/src/mysql-5.6.26 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=gbk,gb2312 -DENABLED_LOCAL_INFILE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 && make && make install #安装后,编写mysql配置文件 #1,手动编写my.cnf配置文件 mkdir /usr/local/mysql/etc cat > /usr/local/mysql/etc/my.cnf << EOF [mysqld] port=3307 datadir=/data56 pid-file=/var/run/mysqld/mysql56.pid socket=/tmp/mysql.sock log-error=/var/log/mysqld/mysql56-err.log user=mysql [client] port=3307 socket=/tmp/mysql.sock EOF #2:创建mysql相关目录,并修改权限 useradd -u 27 mysql mkdir /data56 /var/run/mysqld /var/log/mysqld chown mysql.mysql /data56 /var/run/mysqld /var/log/mysqld /usr/local/mysql/ -R #3:初始化数据库 /usr/local/mysql/scripts/mysql_install_db --defaults-file=/usr/local/mysql/etc/my.cnf --basedir=/usr/local/mysql --user=mysql #--拷贝服务启动脚本 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql56 cp /usr/local/mysql/bin/mysql /usr/bin/
保存退出
创建yml文件
touch tasks/main.yml handlers/main.yml vars/main.yml
编辑任务
vim tasks/main.yml
- name: copy tar copy: src=/run/media/zsy/KINGSTON/运维资料/20-Nginx(web服务器、反向代理负>载均衡、LNMP)/Nginx-软件包/bag dest=/root/Downloads/ tags: - tar - name: copy mysql.sh to dbserver copy: src=mysql.sh dest=/tmp/ - name: bash mysql.sh shell: /bin/bash /tmp/mysql.sh tags: - sh - name: install configure template: src=mysql dest=/etc/init.d/mysql56 tags: - tem notify: - restarted mysql
保存退出
创建site.yml
#vim /tmp/site.yml
- hosts: dbserver remote_user: root roles: - mysql
至此完成数据库的安装
第二步:安装PHP,我这安装的是5.6.x版本
# cd /tmp/roles/php/
创建yml
# touch tasks/main.yml handlers/main.yml vars/main.yml
编写脚本
vim files/php.sh
#!/bin/bash #安装个大模块 #1,安装libiconv-1.13.tar.gz --语言编码转换 install_libiconv(){ tar xzf /root/Downloads/bag/4-libiconv-1.13.tar.gz -C /usr/src cd /usr/src/libiconv-1.13/ ./configure && make && make install echo /usr/local/lib > /etc/ld.so.conf.d/lnmp.conf /sbin/ldconfig } #2.安装mhash-0.9.9.9.tar.bz2 install_mhash-0.9.9.9(){ tar xf /root/Downloads/bag/7-mhash-0.9.9.9.tar.bz2 -C /usr/src cd /usr/src/mhash-0.9.9.9/ ./configure && make && make install /sbin/ldconfig } #3.安装libmcrypt-2.5.8.tar.bz2 install_libmcrypt-2.5.8(){ tar xf /root/Downloads/bag/5-libmcrypt-2.5.8.tar.bz2 -C /usr/src cd /usr/src/libmcrypt-2.5.8/ ./configure && make && make install /sbin/ldconfig } #4.安装mcrypt-2.6.6.tar.gz install_mcrypt-2.6.6(){ tar xzf /root/Downloads/bag/6-mcrypt-2.6.6.tar.gz -C /usr/src cd /usr/src/mcrypt-2.6.6 ./configure && make && make install /sbin/ldconfig } #5.安装pcre-7.9.tar.gz--perl兼容正则表达式,--或者使用rpm自带的也可以(yum install pcre pcre-devel -y) install_pcre-7.9(){ tar xzf /root/Downloads/bag/8-pcre-7.9.tar.gz -C /usr/src/ cd /usr/src/pcre-7.9/ ./configure && make && make install /sbin/ldconfig } #6.编译安装php install_php(){ tar xf /root/Downloads/bag/12-php-5.6.12.tar.bz2 -C /usr/src/ cd /usr/src/php-5.6.12/ ln -s /usr/local/mysql/lib/libmysqlclient.so /usr/lib/ ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18 echo '/usr/local/lib' >> /etc/ld.so.conf /sbin/ldconfig ./configure --prefix=/usr/local/php/ --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --enable-mysqlnd --with-pcre-dir=/usr/local/ --with-mcrypt=/usr/local/ --with-pdo-mysql=/usr/local/mysql --enable-opcache=no && make ZEND_EXTRA_LIBS='-liconv' && make install } #7.安装缓存模块memcache install_memcache(){ tar xf /root/Downloads/bag/10-memcache-2.2.7.tgz -C /usr/src/ cd /usr/src/memcache-2.2.7/ /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install echo /usr/src/memcache-2.2.7/modules/ >> /etc/ld.so.conf.d/lnmp.conf /sbin/ldconfig } #8.安装绘图引擎模块p_w_picpathmagick,与GD类似 install_p_w_picpathmagick(){ tar xzf /root/Downloads/bag/2-ImageMagick-6.7.8-9.tar.gz -C /usr/src/ cd /usr/src/ImageMagick-6.7.8-9/ ./configure make;make install /sbin/ldconfig } #9.安装imagick(连接php与p_w_picpathmagick的通道) install_imagick(){ tar xf /root/Downloads/bag/3-imagick-3.1.2.tgz -C /usr/src/ cd /usr/src/imagick-3.1.2/ /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make;make install echo /usr/src/imagick-3.1.2/modules >> /etc/ld.so.conf.d/lnmp.conf /sbin/ldconfig } install_libiconv && install_mhash-0.9.9.9 && install_libmcrypt-2.5.8 && install_mcrypt-2.6.6 && install_pcre-7.9 && install_php && install_memcache && install_p_w_picpathmagick && install_imagick && cp /usr/src/php-5.6.12/php.ini-production /usr/local/php/etc/php.ini
注:如PHP版本小于5.6还需安装php的缓存加速器opcache
编辑任务
#vim tasks/main.yml
- name: copy shell copy: src=php.sh dest=/tmp/ - name: bash shell shell: /bin/bash /tmp/php.sh - name: install configure template: src=php.ini dest=/usr/local/php/etc/php.ini tags: - ini
把角色加到site.yml中
#vim /tmp/site.yml
- hosts: dbserver remote_user: root roles: - mysql - php
至此可完成PHP的安装
第三步:编译安装nginx,版本1.13.x
#cd /tmp/roles/nginx
编写脚本
#vim files/nginx.sh
install_nginx(){ #新建nginx用户 useradd nginx tar xzf /root/Downloads/bag/1-nginx-1.8.1.tar.gz -C /usr/src cd /usr/src/nginx-1.8.1/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module && make && make install } install_nginx #配置php-fpm配置文件 (配置fastcgi) mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf mkdir /var/run/fastcgi chown nginx.nginx /var/run/fastcgi/ mkdir /lnmp/web -p ulimit -SHn 65535 >> /etc/rc.local
创建yml文件
# touch tasks/main.yml handlers/main.yml vars/main.yml
创建任务
# vim tasks/main.yml
- name: copy shell copy: src=nginx.sh dest=/tmp/ - name: bash shell shell: /bin/bash /tmp/nginx.sh - name: change php-fpm.conf template: src=php-fpm.conf dest=/usr/local/php/etc/php-fpm.conf - name: change nginx.conf template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf - name: change files open command: ulimit -SHn 65535
把任务加到site.yml中
# vim /tmp/site.yml
- hosts: dbserver remote_user: root roles: - mysql - php - nginx
最后执行ansible-playbook /tmp/site.yml
执行过程比较久,需耐心等待。至此ansible自动化搭建lnmp环境圆满结束。
以上属个人搭建步骤,其中有些小步骤没有写上,如模版的配置,毕竟大家所需模版的配置不一。
如有更好的建议,请留下你珍贵的一言,谢谢。
文章标题:ansible自动化安装lnmp
网站链接:http://scjbc.cn/article/gihgpp.html