环境准备
服务器系统 | Centos6.8 |
---|---|
内存 | 1G |
CPU | 2核 |
IP地址 | 10.0.0.20 |
服务 | MysqL数据库 |
服务器系统 | Centos6.8 |
---|---|
内存 | 1G |
CPU | 2核 |
IP地址 | 10.0.0.21 |
服务 | Ansible管理主机 |
服务器系统 | Centos6.8 |
---|---|
内存 | 1G |
CPU | 2核 |
IP地址 | 10.0.0.22 |
服务 | LNMP环境的搭建并上线电商项目 |
在21Ansible管理主机上操作,我们需要准备Nginx源码包和电商包
创建目录
mkdir lnmp
进入目录
cd lnmp/
解压上传到服务器上的nginx包
tar zxf nginx-1.12.0.tar.gz
进入解压过后的nginx目录
cd nginx-1.12.0/conf/
拷贝Nginx配置文件至lnmp目录下
cp nginx.conf ../../
cd ../../
删除nginx目录
rm -rf nginx-1.12.0
编写php测试页面
vim index.php
<?php
phpinfo();
?>
将php测试页面文件改名
mv index.php info.php
vim /etc/ansible/hosts
编辑nginx配置文件
vim /root/lnmp/nginx.conf
编辑yml文件
vim lnmp.yml
---
- hosts: webservers
vars:
- NGINX_USER: www
- PORT: 8000
- ROOT: /opt/html
tasks:
- name: "分发nginx源码包"
unarchive: src=nginx-1.12.0.tar.gz dest=/root/
- name: "安装nginx依赖库和环境"
yum: name=gcc,gcc-c++,pcre-devel,zlib-devel,php,php-mysql,php-gd,php-fpm,unzip state=latest
- name: "编译安装nginx"
shell: cd /root/nginx-1.12.0 && ./configure && make && make install
- name: "创建{{NGINX_USER}}用户"
user: name={{NGINX_USER}} shell=/sbin/nologin
- name: "推送nginx配置文件"
template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
tags: nginx-config
notify: reload-nginx
- name: "创建{{ROOT}}"
file: path={{ROOT}} state=directory owner=www group=www mode=777
- name: "关闭防火墙"
service: name=iptables state=stopped
- name: "启动nginx"
shell: /usr/local/nginx/sbin/nginx
- name: "启动php-fpm"
service: name=php-fpm state=started
- name: "分发测试页面"
copy: src=info.php dest={{ROOT}}
- name: "推送电商源码包到服务器"
unarchive: src=tinyshopV2.5_data.zip dest={{ROOT}} mode=777
handlers:
- name: "reload-nginx"
shell: /usr/local/nginx/sbin/nginx -s reload
- hosts: dbservers
tasks:
- name: "安装mysql"
yum: name=mysql,mysql-server state=latest
- name: "启动MySQL"
service: name=mysqld state=restarted enabled=yes
- name: "关闭防火墙"
service: name=iptables state=stopped
# - name: "关闭selinux"
# shell: setenforce 0
- name: "创建数据库"
shell: mysql -e "create database ds charset utf8;"
- name: "给LNMP环境的主机授权远程登录账号"
shell: mysql -e "grant all on ds.* to 'aaronszm'@'10.0.0.%' identified by '123';"
扫描二维码关注公众号,回复:
9187826 查看本文章
检查yml文件的语法是否正确
ansible-playbook --syntax-check lnmp.yml
执行ansible剧本命令
ansible-playbook lnmp.yml