1. 最终效果及说明
1)主机说明:
ansible: 192.168.20.121
webserver: 192.168.20.122
2)最终访问效果如下
结果与test2.php里边的内容一致,证明LNMP平台完好
3)剧本文件展示及说明
[root@localhost lnmp]# ls
modifynginx.yml mysql.yml php.yml startservice.yml test2.php
modifyphp.yml nginx.yml sequence.txt test1.php testpage.yml
[root@localhost lnmp]# cat sequence.txt
sequence of these yml file:
1. nginx.yml
2. mysql.yml
3. php.yml
4. modifyphp.yml
5. modifynginx.yml
6. startservice.yml
7. testpage.yml
notice:
1. you can use command in ansible's shell to test the lnmp, normally, test test2.php is enough if it show correct message.
2. you can write thest yml file in one combined yml file in order, I split these for convience of checking error
2. 各个yml文件内容展示
1) nginx.yml
[root@localhost lnmp]# cat nginx.yml
- hosts: webserver
remote_user: root
tasks:
- name: add user
user: name=nginx shell=/sbin/nologin create_home=no
- name: resolve dependency
yum: name=pcre-devel,zlib-devel,openssl-devel,gcc,gcc-c++
- name: unarchive tarball
unarchive: src=/root/nginx-1.15.4.tar.gz dest=/usr/src
- name: configure and install nginx
shell: ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_gzip_static_module && make -j4 && make install
args:
chdir: /usr/src/nginx-1.15.4
- name: make soft link file
file: path=/usr/sbin/nginx src=/usr/local/nginx/sbin/nginx state=link
2)mysql.yml
[root@localhost lnmp]# cat mysql.yml
- hosts: webserver
remote_user: root
tasks:
- name: resolve dependency
yum: name=ncurses-devel,bison,cmake
- name: unarchive tarball
unarchive: src=/root/mysql-5.5.22.tar.gz dest=/usr/src
- name: configure and install mysql
shell: cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all && make -j4 && make install
args:
chdir: /usr/src/mysql-5.5.22
- name: add user mysql
user: name=mysql shell=/sbin/nologin create_home=no
- name: change owner of mysql directory
file: path=/usr/local/mysql owner=mysql group=mysql recurse=yes
- name: link lib file
file: path=/usr/lib/libmysqlclient.so.18 src=/usr/local/mysql/lib/libmysqlclient.so.18 state=link
- name: copy configuration file
copy: src=/usr/src/mysql-5.5.22/support-files/my-large.cnf dest=/etc/my.cnf remote_src=yes
- name: copy command script
copy: src=/usr/src/mysql-5.5.22/support-files/mysql.server dest=/etc/init.d/mysqld remote_src=yes
- name: modify command script
replace: path=/etc/init.d/mysqld regexp="^basedir=" replace="basedir=/usr/local/mysql/"
- name: modify command script
replace: path=/etc/init.d/mysqld regexp="^datadir=" replace="datadir=/usr/local/mysql/data/"
- name: give executable privilege to scriptt
file: path=/etc/init.d/mysqld mode=0755
- name: initialize mysql service
shell: /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
- name: make soft link of mysql command
shell: ln -s /usr/local/mysql/bin/* /usr/bin
- name: start mysql
shell: /etc/init.d/mysqld start
- name: change password
shell: mysqladmin -uroot password 123456
3)php.yml
[root@localhost lnmp]# cat php.yml
- hosts: webserver
remote_user: root
tasks:
- name: resolve dependency
yum: name=libxml2-devel,gd,libjpeg-devel,libpng-devel
- name: unarchive tarball
unarchive: src=/root/php-7.2.0.tar.gz dest=/usr/src
- name: configure php
shell: ./configure --prefix=/usr/local/php --with-gd --with-zlib --with-jpeg-dir=/usr/lib --with-pdo-mysql=mysqlnd --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/usr/local/php --enable-mbstring --enable-fpm
args:
chdir: /usr/src/php-7.2.0
- name: install php
shell: make -j8 && make install
args:
chdir: /usr/src/php-7.2.0
- name: copy configuration file
copy: src=/usr/src/php-7.2.0/php.ini-development dest=/usr/local/php/php.ini remote_src=yes
- name: copy configuration file
copy: src=/usr/src/php-7.2.0/sapi/fpm/init.d.php-fpm dest=/etc/init.d/php-fpm remote_src=yes
- name: copy configuration file
copy: src=/usr/local/php/etc/php-fpm.conf.default dest=/usr/local/php/etc/php-fpm.conf remote_src=yes
- name: copy configuration file
copy: src=/usr/local/php/etc/php-fpm.d/www.conf.default dest=/usr/local/php/etc/php-fpm.d/www.conf remote_src=yes
- name: give executable privilege
file: path=/etc/init.d/php-fpm mode=0755
- name: make soft link of command script
shell: ln -s /usr/local/php/bin/* /usr/bin
- name: make soft link of command script
shell: ln -s /usr/local/php/sbin/* /usr/sbin
4)modifyphp.yml
[root@localhost lnmp]# cat modifyphp.yml
- hosts: webserver
remote_user: root
tasks:
- name: modify php.ini
replace: path=/usr/local/php/php.ini regexp="^short_open_tag = Off" replace="short_open_tag = On"
- name: modify php-fpm.conf 1
replace: path=/usr/local/php/etc/php-fpm.conf regexp="^;pid = run/php-fpm.pid" replace="pid = run/php-fpm.pid"
- name: modify php-fpm.conf 2
replace: path=/usr/local/php/etc/php-fpm.conf regexp="^; process.max = 128" replace="process.max = 128"
- name: modify php-fpm.conf 3
replace: path=/usr/local/php/etc/php-fpm.conf regexp="^;events.mechanism = epoll" replace="events.mechanism = epoll"
5) modifynginx.yml
[root@localhost lnmp]# cat modifynginx.yml
- hosts: webserver
remote_user: root
tasks:
- name: modify 1
replace: path=/usr/local/nginx/conf/nginx.conf regexp="^ index index.html index.htm;" replace=" index index.php index.html index.htm;"
- name: remove "#"
shell: sed -i '65,71s/#/\ /g' /usr/local/nginx/conf/nginx.conf
- name: modify php location
replace: path=/usr/local/nginx/conf/nginx.conf regexp="fastcgi_params" replace="fastcgi.conf"
6)startservice.yml
[root@localhost lnmp]# cat startservice.yml
- hosts: webserver
remote_user: root
tasks:
- name: start nginx
shell: nginx
- name: start php
shell: /etc/init.d/php-fpm start
7)testpage.yml
[root@localhost lnmp]# cat testpage.yml
- hosts: webserver
remote_user: root
tasks:
- name: copy test1.php
copy: src=/root/test1.php dest=/usr/local/nginx/html/
- name: copy test2.php
copy: src=/root/test2.php dest=/usr/local/nginx/html/
8) test page
[root@localhost lnmp]# cat test1.php
<?php
phpinfo();
?>
[root@localhost lnmp]# cat test2.php
<?php
$con = new mysqli('127.0.0.1','root','123456','test');
if (!$con)
die("connect error:".mysqli_connect_error());
else
echo "connect mysql successfully\n";
?>
3. ansible playbook使用说明
- 不要太在乎warning,有的时候使用在shell模块中使用sed命令比直接使用replace模块更高效
- 尽量用专门的模块做专门的事情,比如用unarchive模块解压,用file模块做连接,用user模块添加用户等
- 上边写这些的时候是分开写的 便于测试可用性 如果是想要一个整体的 把他们写在一起就好了