shell脚本 一键部署 ——LAMP 架构(Apache Mysql PHP 论坛)含可复制代码

LAMP 架构(Apache Mysql PHP 论坛)

使用前请检查yum仓库是否建立,能正常时候yum install安装软件即可,在编写脚本时,分模块进行编写并测试!

并把所需要的安装包全部放到/opt目录下

 

具体脚本如下(分模块):

 

  1 #!/bin/bash
  2 #Apache安装
  3 
  4 ########安装Apache#########################
  5 read -p "输入你需要监听的IP地址:" ip
  6 read -p "输入IP地址的端口:" dk
  7 read -p "输入IP地址对应的域名(例如www.baidu.com):" ym
  8 ######第一模块,Apache的安装##############
  9 systemctl stop firewalld
 10 systemctl disable firewalld
 11 setenforce 0
 12 
 13 #安装环境依赖包
 14 yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl &> /dev/null
 15 if [ $? -eq 0 ];then
 16 echo "Apache环境依赖包安装完成!"
 17 fi
 18 
 19 #配置软件模块
 20 cd /opt/
 21 tar zxvf apr-1.6.2.tar.gz  &> /dev/null
 22 tar zxvf apr-util-1.6.0.tar.gz &> /dev/null
 23 tar jxvf httpd-2.4.29.tar.bz2  &> /dev/null
 24 
 25 mv apr-1.6.2 /opt/httpd-2.4.29/srclib/apr
 26 mv apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-util
 27 
 28 cd /opt/httpd-2.4.29/
 29 
 30 ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi &> /dev/null
 31 
 32 if [ $? -eq 0 ];then
 33 echo "Apache模块配置完成!"
 34 fi
 35 
 36 #编译及安装
 37 make -j $1 &> /dev/null
 38 make install &> /dev/null
 39 if [ $? -eq 0 ];then
 40 echo "Apache编译安装完成!"
 41 fi
 42 
 43 #优化配置文件路径
 44 cp /usr/local/httpd/conf/httpd.conf{,.bak}
 45 ln -s /usr/local/httpd/conf/httpd.conf /etc/
 46 ln -s /usr/local/httpd/bin/* /usr/local/bin/
 47 
 48 cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
 49 chmod 777 /etc/init.d/httpd
 50 sed -i '1a #!/bin/bash\n# chkconfig: 35 85 21\n# description: Apache is a World Wide Web server' /etc/init.d/httpd
 51 
 52 chkconfig --add httpd
 53 systemctl start httpd.service
 54 
 55 #修改httpd的配置文件
 56 sed -i '52s/Listen 80/Listen '$ip':'$dk'/' /usr/local/httpd/conf/httpd.conf
 57 sed -i '197s/#//' /usr/local/httpd/conf/httpd.conf
 58 sed -i '197s/www.example.com/'$ym'/' /usr/local/httpd/conf/httpd.conf
 59 
 60 systemctl restart httpd.service
 61 echo "$ip $ym" >> /etc/hosts
 62 echo "Apache 安装完毕!"
 63 
 64 
 65 #!/bin/bash
 66 #mysql安装
 67 
 68 #############安装mysql#################
 69 
 70 #安装环境依赖包
 71 yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake &> /dev/null
 72 if [ $? -eq 0 ];then
 73 echo -e "mysql环境依赖包安装完成!"
 74 fi
 75 
 76 #配置软件模块
 77 cd /opt
 78 tar zxvf mysql-5.7.17.tar.gz &> /dev/null
 79 tar zxvf boost_1_59_0.tar.gz &> /dev/null
 80 
 81 cd /opt
 82 mv boost_1_59_0 /usr/local/boost
 83 
 84 cd /opt/mysql-5.7.17/
 85 
 86 cmake \
 87 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
 88 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
 89 -DSYSCONFDIR=/etc \
 90 -DSYSTEMD_PID_DIR=/usr/local/mysql \
 91 -DDEFAULT_CHARSET=utf8  \
 92 -DDEFAULT_COLLATION=utf8_general_ci \
 93 -DWITH_EXTRA_CHARSETS=all \
 94 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
 95 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
 96 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
 97 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
 98 -DMYSQL_DATADIR=/usr/local/mysql/data \
 99 -DWITH_BOOST=/usr/local/boost \
100 -DWITH_SYSTEMD=1 &> /dev/null
101 
102 if [ $? -eq 0 ];then
103 echo "mysql模块配置完成!"
104 fi
105 
106 #编译及安装
107 make -j $1  &> /dev/null
108 make install &> /dev/null
109 
110 if [ $? -eq 0 ];then
111 echo "mysql编译安装完成!"
112 fi
113 
114 #创建mysql用户
115 useradd -M -s /sbin/nologin  mysql
116 
117 echo -e "
118 [client]
119 port = 3306
120 default-character-set=utf8
121 socket=/usr/local/mysql/mysql.sock
122 
123 [mysql]
124 port = 3306
125 default-character-set=utf8
126 socket = /usr/local/mysql/mysql.sock
127 auto-rehash
128 
129 [mysqld]
130 user = mysql
131 basedir=/usr/local/mysql
132 datadir=/usr/local/mysql/data
133 port = 3306
134 character-set-server=utf8
135 pid-file = /usr/local/mysql/mysqld.pid
136 socket=/usr/local/mysql/mysql.sock
137 bind-address = 0.0.0.0
138 skip-name-resolve
139 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES " > /etc/my.cnf
140 
141 #更改mysql安装目录和配置文件的属主属组
142 chown -R mysql:mysql /usr/local/mysql/
143 chown mysql:mysql /etc/my.cnf
144 
145 #设置路径环境变量
146 echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
147 source /etc/profile
148 
149 #初始化数据库
150 cd /usr/local/mysql/bin/
151 ./mysqld \
152 --initialize-insecure \
153 --user=mysql \
154 --basedir=/usr/local/mysql \
155 --datadir=/usr/local/mysql/data  &> /dev/null
156 
157 if [ $? -eq 0 ];then
158 echo "mysql初始化数据库完成!"
159 fi
160 
161 #添加mysqld系统服务
162 cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
163 systemctl daemon-reload
164 systemctl start mysqld.service
165 systemctl enable mysqld
166 
167 
168 rpm -q expect
169 if [ $? -eq 1 ];then
170 yum -y install expect
171 fi
172 
173 source /etc/profile
174 cd /usr/local/mysql/bin
175 #修改mysql登录密码
176 /usr/bin/expect <<EOF
177 spawn mysqladmin -u root -p password "abc123"
178 expect "Enter password:"
179 send "\r"
180 expect eof
181 EOF
182 
183 #授权远程登录
184 /usr/bin/expect <<EOF
185 spawn mysql -u root -p
186 expect "Enter password:" {send "abc123\r"}
187 expect "mysql>" {send "grant all privileges on *.* to 'root'@'%' identified by 'abc123';\r"}
188 expect "mysql>" {send "show databases;\r"}
189 expect "mysql>" {send "quit\r"}
190 expect eof
191 EOF
192 
193 echo "mysql安装完毕!"
194 
195 
196 #!/bin/bash
197 #安装PHP
198 
199 ##########安装PHP环境######################
200 #安装GD库和GD库关联程序,用来处理和生成图片
201 yum -y install \
202 gd \
203 libjpeg libjpeg-devel \
204 libpng libpng-devel \
205 freetype freetype-devel \
206 libxml2 libxml2-devel \
207 zlib zlib-devel \
208 curl curl-devel \
209 openssl openssl-devel &> /dev/null
210 
211 if [ $? -eq 0 ];then
212 echo "PHP关联程序安装完成!"
213 fi
214 
215 #配置软件模块
216 cd /opt
217 tar jxvf php-7.1.10.tar.bz2 &> /dev/null
218 cd /opt/php-7.1.10/
219 ./configure \
220 --prefix=/usr/local/php7 \
221 --with-apxs2=/usr/local/httpd/bin/apxs \
222 --with-mysql-sock=/usr/local/mysql/mysql.sock \
223 --with-config-file-path=/usr/local/php7 \
224 --with-mysqli \
225 --with-zlib \
226 --with-curl \
227 --with-gd \
228 --with-jpeg-dir \
229 --with-png-dir \
230 --with-freetype-dir \
231 --with-openssl \
232 --enable-mbstring \
233 --enable-xml \
234 --enable-session \
235 --enable-ftp \
236 --enable-pdo \
237 --enable-tokenizer \
238 --enable-zip &> /dev/null
239 
240 if [ $? -eq 0 ];then
241 echo "PHP软件模块配置完成!"
242 fi
243 
244 #编译及安装
245 make -j $1 &> /dev/null
246 make install &> /dev/null
247 
248 
249 if [ $? -eq 0 ];then
250 echo "PHP编译安装完成!"
251 fi
252 
253 #复制模板作为PHP 的主配置文件,并进行修改
254 cp /opt/php-7.1.10/php.ini-development /usr/local/php7/php.ini
255 
256 sed -i '939s/;//' /usr/local/php7/php.ini
257 sed -i '939s8=8= Asia/Shanghai8' /usr/local/php7/php.ini
258 sed -i '1170s8=8= /usr/local/mysql/mysql.sock8' /usr/local/php7/php.ini
259 
260 #优化把PHP 的可执行程序文件放入路径环境变量的目录中便于系统识别
261 ln -s /usr/local/php7/bin/* /usr/local/bin/
262 
263 #修改httpd 服务的配置文件,让apache支持PHP
264 sed -i '256s/index.html/index.html index.php/' /usr/local/httpd/conf/httpd.conf
265 sed -i '393a AddType application/x-httpd-php .php\nAddType application/x-httpd-php-source .phps' /usr/local/httpd/conf/httpd.conf
266 
267 #验证PHP 测试页
268 mv /usr/local/httpd/htdocs/index.html /usr/local/httpd/htdocs/index.html.bak
269 
270 echo -e '
271 <?php
272 phpinfo();
273 ?>' > /usr/local/httpd/htdocs/index.php
274 
275 systemctl restart httpd.service
276 
277 if [ $? -eq 0 ];then
278 echo "PHP安装完成,请前往浏览器去验证!"
279 fi
280 
281 #!/bin/bash
282 #安装论坛
283 
284 ##############安装论坛#########################
285 #创建数据库,并进行授权
286 source /etc/profile
287 /usr/bin/expect <<EOF
288 spawn mysql -u root -p
289 expect "Enter password:" {send "abc123\r"}
290 expect "mysql>" {send "CREATE DATABASE bbs;\r"}
291 expect "mysql>" {send "GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'abc123';\r"}
292 expect "mysql>" {send "flush privileges;\r"}
293 expect "mysql>" {send "show databases;\r"}
294 expect "mysql>" {send "quit\r"}
295 expect eof
296 EOF
297 
298 #解压论坛压缩包
299 cd /opt
300 unzip /opt/Discuz_X3.4_SC_UTF8.zip -d /opt/dis  &> /dev/null
301 cd /opt/dis/dir_SC_UTF8/
302 cp -r upload/ /usr/local/httpd/htdocs/bbs
303 
304 #更改论坛目录的属主
305 cd /usr/local/httpd/htdocs/bbs
306 chown -R daemon ./config
307 chown -R daemon ./data
308 chown -R daemon ./uc_client
309 chown -R daemon ./uc_server/data
310 echo "论坛安装完成,请前往浏览器去访问!"

 

posted on 2021-08-10 17:26  笑洋仟  阅读(309)  评论(0编辑  收藏  举报

levels of contents