shell脚本:一键安装LAMP、LNMP脚本

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS PostgreSQL,高可用系列 2核4GB
简介:

历时三天边写边测试,终于完成目前最长代码的脚本,总共253行,在2台机器安装LAMP,LNMP都顺利通过,脚本可做为生产环境使用。


脚本需要注意的地方:
1:脚本适用于centos系统6.5以上,干净系统;如安装过其他环境需要修改脚本内容;
2:脚本适用于:apache 2.4.x 版本;mysql5.5.x以上版本;php5.5.x以上版本;
3:mysql 、php、 apache、 nginx 安装包事先要下载到/usr/local/src目录下,如名字不对需要更改脚本里的软件包名称;

脚本安装的版本为以下,如需其他名称,请自行修改软件安装包名称和对应的目录名即可;
httpd-2.4.7.tar.gz 、mysql-5.6.25.tar.gz 、nginx-1.6.2.tar.gz、 php-5.6.8.tar.gz
4:脚本没有增加判断,设定的环境为干净系统;


脚本内容如下:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/bin/bash
#written by mofansheng @2015-11-06
#one key install LAMP or LNMP scripts; apply to apache 2.4.x,mysql 5.6.x,nginx 1.6.x;
 
/etc/init .d /functions
 
#check the results of the command execution
 
function  check_ok(){
   if  [ $? - eq  0 ]
    then
      continue
   else
      echo  "please check error"
      exit
   fi
}
 
function  yum_update(){
  #set yum repos
  echo  "===update yum repos,it will take serval mintinues==="
  yum  install  wget -y
  mv  /etc/yum .repos.d /CentOS-Base .repo  /etc/yum .repos.d /CentOS-Base .repo.bak
  wget -O  /etc/yum .repos.d /CentOS-Base .repo http: //mirrors .aliyun.com /repo/Centos-6 .repo &> /dev/null
  wget -O  /etc/yum .repos.d /epel .repo http: //mirrors .aliyun.com /repo/epel-6 .repo &> /dev/null
  yum clean all &> /dev/null
  yum makecache &> /dev/null
  check_ok
  action   "yum repos update is ok"  /bin/true
}
 
function  yum_depend(){
    #install dependencies packages
    yum  install  wget gcc gcc-c++  make  re2c curl curl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel libmcrypt libmcrypt-devel zlib zlib-devel openssl openssl-devel freetype freetype-devel gd gd-devel perl perl-devel ncurses ncurses-devel bison bison-devel libtool gettext gettext-devel cmake  bzip2  bzip2 -devel pcre pcre-devel -y
}
 
function  install_mysql(){
  echo  "mysql5.6.25 will be installed,please be patient"
  cd  /usr/local/src
  tar  -zxf mysql-5.6.25. tar .gz
  cd  mysql-5.6.25
  cmake -DCMAKE_INSTALL_PREFIX= /usr/local/mysql  -DMYSQL_DATADIR= /data/mysql  -DMYSQL_UNIX_ADDR= /usr/local/mysql/mysql .sock -DSYSCONFDIR= /etc  -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
  check_ok
  make  &&  make  install
  check_ok
  
  useradd  -M -s  /sbin/nologin  mysql
  mkdir  -p  /data/mysql
  chown  -R mysql:mysql  /data/mysql/
  chown  -R mysql:mysql  /usr/local/mysql/
  check_ok
  cd  /usr/local/mysql/scripts/
  . /mysql_install_db  --basedir= /usr/local/mysql/  --datadir= /data/mysql/  --user=mysql
  check_ok
  /bin/cp  /usr/local/mysql/my .cnf  /etc/my .cnf
  sed  -i  '/^\[mysqld\]$/a\user = mysql\ndatadir = /data/mysql\ndefault_storage_engine = InnoDB\n'  /etc/my .cnf
  check_ok
  
  cp  /usr/local/mysql/support-files/mysql .server  /etc/init .d /mysqld
  sed  -i  's#^datadir=#datadir=/data/mysql#'  /etc/init .d /mysqld
  sed  -i  's#^basedir=#basedir=/usr/local/mysql#'  /etc/init .d /mysqld
  service mysqld start
  chkconfig --add mysqld
  chkconfig mysqld on
  check_ok
  
  iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
  /etc/init .d /iptables  save
  check_ok
  echo  "export PATH=$PATH:/usr/local/mysql/bin"  >> /etc/profile
  source  /etc/profile
  check_ok
}
 
function  install_apache(){
  echo  "apache2.4.7 will be installed,please be patient"
  cd  /usr/local/src
  wget http: //mirrors .cnnic.cn /apache/apr/apr-1 .5.2. tar .gz
  wget http: //mirrors .cnnic.cn /apache/apr/apr-util-1 .5.4. tar .gz
  check_ok
  tar  zxf apr-1.5.2. tar .gz
  cd  apr-1.5.2
  . /configure  --prefix= /usr/local/apr
  check_ok
  make  &&  make  install
  check_ok
  
  cd  /usr/local/src
  tar  zxf apr-util-1.5.4. tar .gz
  cd  apr-util-1.5.4
  . /configure  --prefix= /usr/local/apr-util  --with-apr= /usr/local/apr
  check_ok
  make  &&  make  install
  check_ok
  
  cd  /usr/local/src
  tar  zxf httpd-2.4.7. tar .gz
  /bin/cp  -r apr-1.5.2  /usr/local/src/httpd-2 .4.7 /srclib/apr
  /bin/cp  -r apr-util-1.5.4  /usr/local/src/httpd-2 .4.7 /srclib/apr-util
  cd  httpd-2.4.7
  . /configure  --prefix= /usr/local/apache2  --with-apr= /usr/local/apr  --with-apr-util= /usr/local/apr-util/  --with-pcre -- enable -mods-shared=most -- enable -so --with-included-apr
  check_ok
  make  &&  make  install
  check_ok
  
  echo  "export PATH=$PATH:/usr/local/apache2/bin"  >> /etc/profile
  source  /etc/profile
  check_ok
  
  iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
  /etc/init .d /iptables  save
  check_ok
  /usr/local/apache2/bin/apachectl
  check_ok
}
 
function  install_php(){
  echo  "php5.6.8 will be installed,please be patient"
  cd  /usr/local/src
  tar  zxf php-5.6.8. tar .gz
  cd  php-5.6.8
  . /configure    --prefix= /usr/local/php    --with-apxs2= /usr/local/apache2/bin/apxs    --with-config- file -path= /usr/local/php/etc    --with-mysql= /usr/local/mysql    --with-libxml- dir    --with-gd   --with-jpeg- dir    --with-png- dir    --with-freetype- dir    --with-iconv- dir    --with-zlib- dir    --with-bz2   --with-openssl   --with-mcrypt   -- enable -soap   -- enable -gd-native-ttf   -- enable -mbstring   -- enable -sockets   -- enable -exif   --disable-ipv6
  check_ok
  make  &&  make  install
  check_ok
  
  cp  /usr/local/src/php-5 .6.8 /php .ini-production  /usr/local/php/etc/php .ini
  sed  -i  's#^;date.timezone =#date.timezone=Asia/Shanghai#'  /usr/local/php/etc/php .ini
  check_ok
  
}
 
function  set_lamp(){
  sed  -i  '/AddType application\/x-gzip .gz .tgz/a\    AddType application/x-httpd-php .php\n'  /usr/local/apache2/conf/httpd .conf
  sed  -i  's#index.html#index.html index.php#'  /usr/local/apache2/conf/httpd .conf
  sed  -i  '/#ServerName www.example.com:80/a\ServerName localhost:80\n'  /usr/local/apache2/conf/httpd .conf
  check_ok
cat  >> /usr/local/apache2/htdocs/test .php<<EOF
<?php
echo  "PHP is OK\n" ;
?>
EOF
 
  /usr/local/apache2/bin/apachectl  graceful
  check_ok
  curl localhost /test .php
  check_ok
  action  "LAMP is install success"  /bin/true
}
 
function  install_phpfpm(){
  echo  "php5.6.8 will be installed,please be patient"
  useradd  -s  /sbin/nologin  php-fpm
  cd  /usr/local/src
  tar  zxf php-5.6.8. tar .gz
  cd  php-5.6.8
  . /configure  --prefix= /usr/local/php-fpm  --with-config- file -path= /usr/local/php-fpm/etc  -- enable -fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=mysqlnd  --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-libxml- dir  --with-gd --with-jpeg- dir  --with-png- dir  --with-freetype- dir  --with-iconv- dir  --with-zlib- dir  --with-mcrypt -- enable -soap -- enable -gd-native-ttf -- enable - ftp  -- enable -exif --disable-ipv6 --with-pear --with-curl -- enable -bcmath -- enable -mbstring -- enable -sockets --with-gettext
  check_ok
  make  &&  make  install
  check_ok
  
  cp  /usr/local/src/php-5 .6.8 /php .ini-production  /usr/local/php-fpm/etc/php .ini
  sed  -i  's#^;date.timezone =#date.timezone=Asia/Shanghai#'  /usr/local/php-fpm/etc/php .ini
  cd  /usr/local/php-fpm/etc/
  mv  php-fpm.conf.default php-fpm.conf
  check_ok
  
  cp  /usr/local/src/php-5 .6.8 /sapi/fpm/init .d.php-fpm  /etc/init .d /php-fpm
  chmod  755  /etc/init .d /php-fpm
  chkconfig --add php-fpm
  chkconfig php-fpm on
  service php-fpm start
  check_ok
}
 
function  install_nginx(){
  echo  "nginx1.6.2 will be installed,please be patient"
  cd  /usr/local/src
  tar  zxf nginx-1.6.2. tar .gz
  cd  nginx-1.6.2
  . /configure  --prefix= /usr/local/nginx  --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
  check_ok
  make  &&  make  install
  check_ok
  
  /usr/local/nginx/sbin/nginx
  check_ok
}
 
function  set_lnmp(){
  sed  -i  '56a\location ~ \.php$ {\n\    root           html;\n\    fastcgi_pass   127.0.0.1:9000;\n\    fastcgi_index  index.php;\n\    fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;\n\    include        fastcgi_params;\n\}\n'  /usr/local/nginx/conf/nginx .conf
  /usr/local/nginx/sbin/nginx  -s reload
  check_ok
  echo  -e  '<?php\n echo "nginx and PHP is OK";\n?>\n'  > /usr/local/nginx/html/index .php
  curl localhost /index .php
  check_ok
  action  "LNMP is install success"  /bin/true
}
 
function  install_lamp(){
  echo  "apache 2.4.7 mysql 5.6.24 php5.6.8 will be installed"
  echo  "===update yum repos and install dependecies packages,it will take serval mintinues==="
  yum_update
  check_ok
  yum_depend
  check_ok
  install_mysql
  check_ok
  install_apache
  check_ok
  install_php
  check_ok
  set_lamp
}
 
function  install_lnmp(){
  echo  "nginx1.6.2 mysql 5.6.24 php5.6.8 will be installed"
  echo  "===update yum repos and install dependecies packages,it will take serval mintinues==="
  yum_update
  check_ok
  yum_depend
  check_ok
  install_mysql
  check_ok
  install_phpfpm
  check_ok
  install_nginx
  check_ok
  set_lnmp
}
 
cat  <<EOF
     1:[ install  LAMP]
     2:[ install  LNMP]
     3:[ exit ]
EOF
read  -t 10 -p  "please input the num you want:"  input
case  ${input}  in
  1)
  install_lamp
  ;;
  2)
  install_lnmp
  ;;
  3)
  exit
  ;;
  *)
  printf  "You must input only in {1|2|3}\n"
esac





本文转自 模范生 51CTO博客,原文链接:http://blog.51cto.com/mofansheng/1711851,如需转载请自行联系原作者

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
目录
相关文章
|
6天前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
121 9
|
6天前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
58 2
|
25天前
|
数据采集 监控 Shell
无需Python:Shell脚本如何成为你的自动化爬虫引擎?
Shell脚本利用curl/wget发起请求,结合文本处理工具构建轻量级爬虫,支持并行加速、定时任务、增量抓取及分布式部署。通过随机UA、异常重试等优化提升稳定性,适用于日志监控、价格追踪等场景。相比Python,具备启动快、资源占用低的优势,适合嵌入式或老旧服务器环境,复杂任务可结合Python实现混合编程。
|
4月前
|
Shell
Shell脚本循环控制:shift、continue、break、exit指令
使用这些命令可以让你的Shell脚本像有生命一样动起来。正确使用它们,你的脚本就能像一场精心编排的舞蹈剧目,既有旋律的起伏,也有节奏的跳跃,最终以一场惊艳的表演结束。每一个动作、每一个转折点,都准确、优雅地完成所需要表达的逻辑。如此,你的脚本不只是冰冷的代码,它透过终端的界面,跳着有节奏的舞蹈,走进观众——使用者的心中。
203 60
|
7月前
|
关系型数据库 MySQL Shell
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。
|
3月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
209 1
|
5月前
|
存储 Unix Shell
确定Shell脚本在操作系统中的具体位置方法。
这对于掌握Linux的文件系统组织结构和路径方面的理解很有帮助,是我们日常工作和学习中都可能使用到的知识。以上讲解详细清晰,应用简便,是每一个想要精通操作系统的计算机爱好者必备的实用技能。
105 17
|
5月前
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
127 4
|
5月前
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
270 3
|
6月前
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
433 25