saltstack部署nginx+php

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

因为基本上生产环境中都是nginx+php的环境,所以就不单独列出salt部署php的过程了,这里就结合我在生产环境中的脚本进行nginx+php环境的部署。

部署规划:

1)编译安装libiconv、libmcrypt、mhash以及mcrypt

2)编译安装php

3)添加启动停止脚本

4)添加到系统服务并设置开机启动

5)拷贝日志切割脚本

6)添加日志切割定时任务

7)安装memcached/redis/protobuf扩展

8)修改php.ini加载php扩展

salt部署目录架构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
[root@salt-master ~] # tree /srv/salt/base/
/srv/salt/base/
├──  cron
│   ├── files
│   │   ├── nginx_cut_log.sh
│   │   └── php_cut_log.sh
│   ├── nginx.sls
│   └── php.sls
├── nginx
│   ├── files
│   │   ├── nginx
│   │   ├── nginx-1.6.3. tar .gz
│   │   └── nginx.conf
│   ├──  install .sls
│   └── service.sls
├── packages
│   └──  install .sls
├── pcre
│   ├── files
│   │   └── pcre-8.37. tar .gz
│   └──  install .sls
├── php
│   ├── extension.sls
│   ├── files
│   │   ├── libmemcached-1.0.18. tar .gz
│   │   ├── memcached-2.2.0.tgz
│   │   ├── php-5.6.16. tar .gz
│   │   ├── php-fpm
│   │   ├── php-fpm.conf
│   │   ├── php.ini
│   │   ├── phpredis-2.2.4. tar .gz
│   │   └── protobuf.so
│   ├──  install .sls
│   ├── libiconv
│   │   └── files
│   │       └── libiconv-1.14. tar .gz
│   ├── libiconv.sls
│   ├── libmcrypt
│   │   └── files
│   │       └── libmcrypt-2.5.8. tar .gz
│   ├── libmcrypt.sls
│   ├── mcrypt
│   │   └── files
│   │       └── mcrypt-2.6.8. tar .gz
│   ├── mcrypt.sls
│   ├── mhash
│   │   └── files
│   │       └── mhash-0.9.9.9. tar .gz
│   ├── mhash.sls
│   └── service.sls
├──  top .sls
└── user
     ├── nginx.sls
     └── php.sls
18 directories, 34 files

安装libiconv:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@salt-master base] # cat php/libiconv.sls 
libiconv- source - install :
   file .managed:
     - name:  /opt/tools/libiconv-1 .14. tar .gz
     source : salt: //php/libiconv/files/libiconv-1 .14. tar .gz
     - user: root
     - group: root
     - mode: 755
   cmd.run:
     - name:  cd  /opt/tools/  &&  tar  -zxf libiconv-1.14. tar .gz &&  cd  libiconv-1.14 && . /configure  --prefix= /usr/local  &&  make  &&  make  install  &&  /sbin/ldconfig 
     - unless:  test  -e  /usr/local/lib/libiconv .so.2.5.1
     - require:
       file : libiconv- source - install

安装limcrypt:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@salt-master base] # cat php/libmcrypt.sls 
libmcrypt- source - install :
   file .managed:
     - name:  /opt/tools/libmcrypt-2 .5.8. tar .gz
     source : salt: //php/libmcrypt/files/libmcrypt-2 .5.8. tar .gz
     - user: root
     - group: root
     - mode: 755
   cmd.run:
     - name:  cd  /opt/tools  &&  tar  -zxf libmcrypt-2.5.8. tar .gz &&  cd  libmcrypt-2.5.8 && . /configure  &&  make  &&  make  install  && ldconfig &&  cd  libltdl && . /configure  -- enable -ltdl- install  &&  make  &&  make  install  &&  /sbin/ldconfig
     - unless:  test  -e  /usr/local/lib/libmcrypt .so.4.4.8
     - require:
       file : libmcrypt- source - install

安装mhash:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@salt-master base] # cat php/mhash.sls 
mhash- source - install :
   file .managed:
     - name:  /opt/tools/mhash-0 .9.9.9. tar .gz
     source : salt: //php/mhash/files/mhash-0 .9.9.9. tar .gz
     - user: root
     - group: root
     - mode: 755
   cmd.run:
     - name:  cd  /opt/tools/  &&  tar  -zxf mhash-0.9.9.9. tar .gz &&  cd  mhash-0.9.9.9 && . /configure  &&  make  &&  make  install  &&  ln  -s  /usr/local/lib/libmcrypt .la  /usr/lib/libmcrypt .la &&  ln  -s  /usr/local/lib/libmcrypt .so  /usr/lib/libmcrypt .so &&  ln  -s  /usr/local/lib/libmcrypt .so.4  /usr/lib/libmcrypt .so.4 &&  ln  -s  /usr/local/lib/libmcrypt .so.4.4.8  /usr/lib/libmcrypt .so.4.4.8 &&  ln  -s  /usr/local/lib/libmhash .a  /usr/lib/libmhash .a &&  ln  -s  /usr/local/lib/libmhash .la  /usr/lib/libmhash .la &&  ln  -s  /usr/local/lib/libmhash .so  /usr/lib/libmhash .so &&  ln  -s  /usr/local/lib/libmhash .so.2  /usr/lib/libmhash .so.2 &&  ln  -s  /usr/local/lib/libmhash .so.2.0.1  /usr/lib/libmhash .so.2.0.1 &&  ln  -s  /usr/local/bin/libmcrypt-config  /usr/bin/libmcrypt-config
     - unless:  test  -e  /usr/local/lib/libmhash .a
     - require:
       file : mhash- source - install

安装mcrypt:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@salt-master base] # cat php/mcrypt.sls 
mcrypt- source - install :
   file .managed:
     - name:  /opt/tools/mcrypt-2 .6.8. tar .gz
     source : salt: //php/mcrypt/files/mcrypt-2 .6.8. tar .gz
     - user: root
     - group: root
     - mode: 644
   cmd.run:
     - name:  cd  /opt/tools/  &&  tar  -zxf mcrypt-2.6.8. tar .gz &&  cd  mcrypt-2.6.8 &&  /sbin/ldconfig  && . /configure  &&  make  &&  make  install
     - unless:  test  -e  /usr/local/bin/mcrypt
     - require:
       file : mcrypt- source - install

安装php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@salt-master base] # cat php/install.sls 
include:
   - packages. install
   - user.php
   - php.libiconv
   - php.libmcrypt
   - php.mhash
   - php.mcrypt
php- source - install :
   file .managed:
     - name:  /opt/tools/php-5 .6.16. tar .gz
     source : salt: //php/files/php-5 .6.16. tar .gz
     - user: root
     - group: root
     - mode: 755
   cmd.run:
     - name:  cd  /opt/tools/  &&  tar  -zxf php-5.6.16. tar .gz &&  cd  php-5.6.16 && . /configure  --prefix= /usr/local/php  --with-config- file -path= /usr/local/php/etc  --with-libxml- dir  -- enable -xml -- enable -fpm --with-fpm-user=www --with-fpm-group=www -- enable -bcmath -- enable -mbstring -- enable -gd-native-ttf -- enable -sockets -- enable -mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd -- enable -zip -- enable -inline-optimization --with-gd --with-bz2 --with-zlib --with-mcrypt --with-mhash --with-openssl --with-xmlrpc --with-iconv- dir  --with-freetype- dir  --with-jpeg- dir  --with-png- dir  --without-pear --disable-ipv6 --disable-pdo --with-gettext --disable-debug --without-pdo-sqlite --disable-rpath -- enable -shmop -- enable -sysvsem --with-curl --with-curlwrappers -- enable -mbregex -- enable -pcntl -- enable -soap -- enable -sigchild -- enable -pdo &&  make  ZEND_EXTRA_LIBS= '-liconv'  &&  make  install
     - unless:  test  -d  /usr/local/php
     - require:
       file : php- source - install
       - user: php-user-group
       - cmd: libiconv- source - install
       - cmd: libmcrypt- source - install
       - cmd: mcrypt- source - install
       - cmd: mhash- source - install

配置php服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[root@salt-master base] # cat php/service.sls 
include:
   - php. install
   cron .php
/usr/local/php/etc :
   file .directory:
     - user: www
     - group: www
     - mode: 644
     - makedirs: True
/usr/local/php/etc/php-fpm .conf:
   file .managed:
     source : salt: //php/files/php-fpm .conf
     - user: www
     - group: www
     - mode: 644
/etc/php .ini:
   file .managed:
     source : salt: //php/files/php .ini
     - user: www
     - group: www
     - mode: 644
/etc/profile :
   file .append:
     - text:
       export  PATH=$PATH: /usr/local/php/bin : /usr/local/php/sbin
php-init:
   file .managed:
     - name:  /etc/init .d /php-fpm
     source : salt: //php/files/php-fpm
     - user: root
     - group: root
     - mode: 755
   cmd.run:
     - name: chkconfig --add php-fpm &&  source  /etc/profile
     - unless: chkconfig --list| grep  php-fpm
     - require:
       file : php-init
php-service:
   service.running:
     - name: php-fpm
     enable : True
     - restart: True
     - require:
       - cmd: php-init
     watch :
       file /etc/php .ini
       file /usr/local/php/etc/php-fpm .conf

部署定时任务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@salt-master base] # cat cron/php.sls 
php-crond-job:
   file .managed:
     - name:  /opt/tools/scripts/php_cut_log .sh
     source : salt: //cron/files/php_cut_log .sh
     - user: root
     - group: root
     - mode: 755
/bin/bash  /opt/tools/scripts/php_cut_log .sh > /dev/null  2>&1:
   cron .present:
     - identifier:  cut  php daily logs job
     - user: root
     - minute:  '0'
     - hour:  '0'
     - require:
       file : php-crond-job

安装php扩展:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
[root@salt-master base] # cat php/extension.sls 
include:
   - php.service
libmemcached- source - install :
   file .managed:
     - name:  /opt/tools/libmemcached-1 .0.18. tar .gz
     source : salt: //php/files/libmemcached-1 .0.18. tar .gz
     - user: root
     - group: root
     - mode: 644
   cmd.run:
     - name:  cd  /opt/tools  &&  tar  -zxf libmemcached-1.0.18. tar .gz &&  cd  libmemcached-1.0.18 && . /configure  --prefix= /usr/local/libmemcached  --with-memcached &&  make  &&  make  install
     - unless:  test  -d  /usr/local/libmemcached
     - require:
       file : libmemcached- source - install
memcached- source - install :
   file .managed:
     - name:  /opt/tools/memcached-2 .2.0.tgz
     source : salt: //php/files/memcached-2 .2.0.tgz
     - user: root
     - group: root
     - mode: 644
   cmd.run:
     - name:  cd  /opt/tools/  &&  tar  -zxf memcached-2.2.0.tgz &&  cd  memcached-2.2.0 &&  /usr/local/php/bin/phpize  && . /configure  -- enable -memcached --with-php-config= /usr/local/php/bin/php-config  --with-libmemcached- dir = /usr/local/libmemcached  &&  make  &&  make  install
     - unless:  test  -x  /usr/bin/memcached
     - require:
       file : memcached- source - install
phpredis- source - install :
   file .managed:
     - name:  /opt/tools/phpredis-2 .2.4. tar .gz
     source : salt: //php/files/phpredis-2 .2.4. tar .gz
     - user: root
     - group: root
     - mode: 644
   cmd.run:
     - name:  cd  /opt/tools  &&  tar  -zxf phpredis-2.2.4. tar .gz &&  cd  phpredis-2.2.4 &&  /usr/local/php/bin/phpize  && . /configure  --with-php-config= /usr/local/php/bin/php-config  &&  make  &&  make  install
     - unless:  test  -e  /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/redis .so
     - require:
       file : phpredis- source - install
protobuf-extension:
   file .managed:
     - name:  /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/protobuf .so
     source : salt: //php/files/protobuf .so
     - user: www
     - group: www
     - mode: 755
extension-init:
   file .managed:
     - name:  /usr/local/php/etc/php .ini
     source : salt: //php/files/php .ini
     - user: www
     - group: www
     - replace: True
     - mode: 644
   cmd.run:
     - name:  chown  -R www:www  /usr/local/php  &&  source  /etc/profile
     - require:
       file : protobuf-extension
       file : extension-init
     watch
       file /usr/local/php/etc/php .ini

top.sls文件:

1
2
3
4
5
6
[root@salt-master base] # cat top.sls 
base:
   'salt-minion02.contoso.com' :
     - nginx.service
     - php.service
     - php.extension


本文转自 jerry1111111 51CTO博客,原文链接:http://blog.51cto.com/jerry12356/1934153,如需转载请自行联系原作者
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
2月前
|
Kubernetes 应用服务中间件 nginx
百度搜索:蓝易云【使用Kubernetes部署Nginx应用教程】
现在,你已经成功在Kubernetes集群上部署了Nginx应用。通过访问Service的外部IP地址,你可以访问Nginx服务。
38 4
|
3月前
|
安全 应用服务中间件 nginx
百度搜索:蓝易云【使用Debian、Docker和Nginx部署Web应用教程】
这些是在Debian上使用Docker和Nginx部署Web应用的基本步骤。根据您的需求和具体环境,可能还需要进行其他配置和调整。请确保在进行任何与网络连接和安全相关的操作之前,详细了解您的网络环境和安全需求,并采取适当的安全措施。
44 0
|
14天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
71 0
|
3月前
|
Serverless PyTorch 算法框架/工具
Serverless应用引擎部署问题之下个节点不可用如何解决
Serverless部署是指将应用程序部署到无服务器架构中,该架构允许开发者专注于代码而无需关心底层服务器的运行和维护;针对Serverless部署过程中可能遇到的挑战,本合集提供全面的指南和最佳实践,帮助开发者顺利实现应用的无服务器化部署。
365 2
|
3月前
|
应用服务中间件 Linux 网络安全
windows+linux环境下nginx部署环境
windows+linux环境下nginx部署环境
|
1月前
|
应用服务中间件 Linux nginx
web后端-linux-nginx-1.18操作命令和部署
web后端-linux-nginx-1.18操作命令和部署
|
3月前
|
应用服务中间件 Linux 网络安全
【Nginx】Nginx Linux 部署
【1月更文挑战第25天】【Nginx】Nginx Linux 部署
|
3月前
|
TensorFlow 算法框架/工具 数据安全/隐私保护
如何在云服务器使用docker快速部署jupyter web服务器(Nginx+docker+jupyter+tensorflow)
如何在云服务器使用docker快速部署jupyter web服务器(Nginx+docker+jupyter+tensorflow)
73 0
|
3月前
|
Linux 应用服务中间件 nginx
linux下 web 版 vscode本地部署 云端加个nginx同
linux下 web 版 vscode本地部署 云端加个nginx同
20 0
|
3月前
|
缓存 弹性计算 NoSQL
您在使用Serverless应用引擎(SAE)部署PHP应用时遇到了性能问题
【1月更文挑战第12天】【1月更文挑战第57篇】您在使用Serverless应用引擎(SAE)部署PHP应用时遇到了性能问题
351 1