千家信息网

ansible自动化安装lnmp

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,今天尝试用ansible自动化编译安装了lnmp环境,是以把自己的见解和大家分享,不足之处还望大家指正。lnmp的构成lnmp = linux + nginx + mysql + php/python
千家信息网最后更新 2025年12月02日ansible自动化安装lnmp

今天尝试用ansible自动化编译安装了lnmp环境,是以把自己的见解和大家分享,不足之处还望大家指正。

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/bashmount -o loop /dev/sr0 /yumyum clean allyum 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/srccd /usr/src/mysql-5.6.26cmake . -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/etccat > /usr/local/mysql/etc/my.cnf << EOF[mysqld]port=3307datadir=/data56pid-file=/var/run/mysqld/mysql56.pidsocket=/tmp/mysql.socklog-error=/var/log/mysqld/mysql56-err.loguser=mysql[client]port=3307socket=/tmp/mysql.sockEOF#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/mysql56cp /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/srccd /usr/src/libiconv-1.13/./configure && make && make installecho /usr/local/lib  > /etc/ld.so.conf.d/lnmp.conf/sbin/ldconfig}#2.安装mhash-0.9.9.9.tar.bz2install_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.bz2install_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.gzinstall_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.编译安装phpinstall_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.安装缓存模块memcacheinstall_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 nginxtar xzf /root/Downloads/bag/1-nginx-1.8.1.tar.gz -C /usr/srccd /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.confmkdir /var/run/fastcgichown nginx.nginx /var/run/fastcgi/mkdir /lnmp/web -pulimit -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环境圆满结束。

以上属个人搭建步骤,其中有些小步骤没有写上,如模版的配置,毕竟大家所需模版的配置不一。

如有更好的建议,请留下你珍贵的一言,谢谢。

配置 文件 任务 模块 版本 环境 脚本 步骤 至此 编译 自动化 数据 数据库 模版 缓存 角色 服务 珍贵 均衡 耐心 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 昌平二手服务器回收价钱 浪潮服务器登录管理端口 节日期间机关确保网络安全 开发还是网络安全 数据库安全的看法 精准数据库营销系统 建立才c 与数据库登陆 北京明嘉大有网络技术 服务器github管理代码 防范化解网络安全风险研究 同徽网络技术有限公司 饥荒联机版私人服务器有用吗 软件开发产业规划书 虹口区个人软件开发报价联系方式 互联网及多媒体科技是学什么 健康网络技术服务有限公司 网络安全包括信息安全么 c 数据库 面试题 竹山好的软件开发学习 云服务器控制物理机 北京和武诚信网络技术 网络安全对教育的影响 string数据库蛋白分析 数据库触发器一些难写的案例 深圳市科创网络技术有限公司 2019年网络安全会议 医疗器械网络安全研究资料 河北大数据软件开发大概多少钱 浪潮猎鹰服务器管理软件下载 中卫市公安局网络安全保卫支队
0