shell脚本 一键部署 ——LNMP 架构(Nginx Mysql PHP Discuz!论坛)含可复制代码
LNMP 架构(Nginx Mysql PHP Discuz!论坛)
使用前请检查yum仓库是否建立,能正常时候yum install安装软件即可,在编写脚本时,分模块进行编写并测试!
并把所需要的安装包全部放到/opt目录下,并确定源码编译时使用的核数
具体脚本如下(分模块):
1 #!/bin/bash 2 #安装nginx服务 3 4 #########安装nginx服务################ 5 systemctl stop firewalld 6 systemctl disable firewalld 7 setenforce 0 8 9 #安装依赖包 10 yum -y install pcre-devel zlib-devel gcc gcc-c++ make &> /dev/null 11 12 echo -e "\033[34;1m Nginx依赖包安装完成! \033[0m" 13 14 #创建运行用户 15 useradd -M -s /sbin/nologin nginx 16 17 #编译安装 18 cd /opt 19 tar zxvf nginx-1.12.2.tar.gz -C /opt/ &> /dev/null 20 21 cd nginx-1.12.2/ 22 ./configure \ 23 --prefix=/usr/local/nginx \ 24 --user=nginx \ 25 --group=nginx \ 26 --with-http_stub_status_module &> /dev/null 27 28 make -j $1 &> /dev/null 29 make install &> /dev/null 30 31 echo -e "\033[34;1m Nginx编译安装完成! \033[0m" 32 33 #优化路径 34 ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 35 36 #添加 Nginx 系统服务 37 cat > /lib/systemd/system/nginx.service <<-EOF 38 [Unit] 39 Description=nginx 40 After=network.target 41 [Service] 42 Type=forking 43 PIDFile=/usr/local/nginx/logs/nginx.pid 44 ExecStart=/usr/local/nginx/sbin/nginx 45 ExecReload=/bin/kill -s HUP $MAINPID 46 ExecStop=/bin/kill -s QUIT $MAINPID 47 PrivateTmp=true 48 [Install] 49 WantedBy=multi-user.target 50 EOF 51 52 chmod 754 /lib/systemd/system/nginx.service 53 systemctl daemon-reload 54 systemctl start nginx.service 55 systemctl enable nginx.service 56 57 echo -e "\033[34;1m Nginx安装完成,请前去浏览器访问测试! \033[0m" 58 59 #!/bin/bash 60 #安装Mysql 61 62 #########安装Mysql########### 63 #安装Mysql环境依赖包 64 yum -y install \ 65 ncurses \ 66 ncurses-devel \ 67 bison \ 68 cmake &> /dev/null 69 70 echo -e "\033[34;1m Mysql环境依赖包安装完成! \033[0m" 71 72 #创建运行用户 73 useradd -M -s /sbin/nologin mysql 74 75 #编译安装 76 cd /opt 77 tar zxvf mysql-boost-5.7.20.tar.gz &> /dev/null 78 79 cd /opt/mysql-5.7.20/ 80 cmake \ 81 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ 82 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ 83 -DSYSCONFDIR=/etc \ 84 -DSYSTEMD_PID_DIR=/usr/local/mysql \ 85 -DDEFAULT_CHARSET=utf8 \ 86 -DDEFAULT_COLLATION=utf8_general_ci \ 87 -DWITH_EXTRA_CHARSETS=all \ 88 -DWITH_INNOBASE_STORAGE_ENGINE=1 \ 89 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ 90 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ 91 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ 92 -DMYSQL_DATADIR=/usr/local/mysql/data \ 93 -DWITH_BOOST=boost \ 94 -DWITH_SYSTEMD=1 &> /dev/null 95 96 make -j $1 &> /dev/null 97 make install &> /dev/null 98 99 echo -e "\033[34;1m Mysql编译安装完成! \033[0m" 100 101 #修改mysql 配置文件 102 cat > /etc/my.cnf <<-EOF 103 [client] 104 port = 3306 105 socket=/usr/local/mysql/mysql.sock 106 107 [mysqld] 108 user = mysql 109 basedir=/usr/local/mysql 110 datadir=/usr/local/mysql/data 111 port = 3306 112 character-set-server=utf8 113 pid-file = /usr/local/mysql/mysqld.pid 114 socket=/usr/local/mysql/mysql.sock 115 bind-address = 0.0.0.0 116 skip-name-resolve 117 max_connections=2048 118 default-storage-engine=INNODB 119 max_allowed_packet=16M 120 server-id = 1 121 122 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 123 EOF 124 125 #更改mysql安装目录和配置文件的属主属组 126 chown -R mysql:mysql /usr/local/mysql/ 127 chown mysql:mysql /etc/my.cnf 128 129 #设置路径环境变量 130 echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile 131 source /etc/profile 132 133 #初始化数据库 134 cd /usr/local/mysql/bin/ 135 ./mysqld \ 136 --initialize-insecure \ 137 --user=mysql \ 138 --basedir=/usr/local/mysql \ 139 --datadir=/usr/local/mysql/data &> /dev/null 140 141 echo -e "\033[34;1m Mysql初始化数据库成功! \033[0m" 142 143 #添加mysqld系统服务 144 cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ 145 systemctl daemon-reload 146 systemctl start mysqld.service 147 systemctl enable mysqld 148 149 #修改mysql 的登录密码 150 yum -y install expect &> /dev/null 151 152 /usr/bin/expect <<EOF 153 spawn mysqladmin -u root -p password "abc123" 154 expect "Enter password:" 155 send "\r" 156 expect eof 157 EOF 158 159 #授权远程登录 160 /usr/bin/expect <<EOF 161 spawn mysql -u root -pabc123 162 expect "mysql>" {send "grant all privileges on *.* to 'root'@'%' identified by 'abc123';\r"} 163 expect "mysql>" {send "show databases;\r"} 164 expect "mysql>" {send "quit\r"} 165 expect eof 166 EOF 167 168 echo -e "\033[34;1m Mysql安装完成! \033[0m" 169 170 #!/bin/bash 171 #安装配置 PHP 解析环境 172 173 ##########安装配置 PHP 解析环境########## 174 #安装环境依赖包 175 yum -y install gd \ 176 libjpeg libjpeg-devel \ 177 libpng libpng-devel \ 178 freetype freetype-devel \ 179 libxml2 libxml2-devel \ 180 zlib zlib-devel \ 181 curl curl-devel \ 182 openssl openssl-devel &> /dev/null 183 184 echo -e "\033[34;1m PHP环境依赖包安装完成! \033[0m" 185 186 #编译安装 187 cd /opt 188 tar jxvf php-7.1.10.tar.bz2 &> /dev/null 189 190 cd php-7.1.10 191 ./configure \ 192 --prefix=/usr/local/php \ 193 --with-mysql-sock=/usr/local/mysql/mysql.sock \ 194 --with-mysqli \ 195 --with-zlib \ 196 --with-curl \ 197 --with-gd \ 198 --with-jpeg-dir \ 199 --with-png-dir \ 200 --with-freetype-dir \ 201 --with-openssl \ 202 --enable-fpm \ 203 --enable-mbstring \ 204 --enable-xml \ 205 --enable-session \ 206 --enable-ftp \ 207 --enable-pdo \ 208 --enable-tokenizer \ 209 --enable-zip &> /dev/null 210 211 make -j $1 &> /dev/null 212 make install &> /dev/null 213 214 echo -e "\033[34;1m PHP编译安装完成! \033[0m" 215 216 #路径优化 217 ln -s /usr/local/php/bin/* /usr/local/bin/ 218 ln -s /usr/local/php/sbin/* /usr/local/sbin/ 219 220 #调整PHP三个配置文件 221 #调整主配置文件 222 cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini 223 224 sed -i '939s/;/ /' /usr/local/php/lib/php.ini 225 sed -i '939s/date.timezone =/date.timezone = Asia\/Shanghai/' /usr/local/php/lib/php.ini 226 sed -i '1170s#socket =#socket = /usr/local/mysql/mysql.sock#' /usr/local/php/lib/php.ini 227 228 #调整进程服务配置文件 229 cd /usr/local/php/etc/ 230 cp php-fpm.conf.default php-fpm.conf 231 sed -i '17s/;/ /' /usr/local/php/etc/php-fpm.conf 232 233 #调整扩展配置文件 234 cd /usr/local/php/etc/php-fpm.d/ 235 cp www.conf.default www.conf 236 237 echo -e "\033[34;1m PHP配置文件调整完成! \033[0m" 238 239 #启动php-fpm 240 /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini 241 cd /opt/php-7.1.10/sapi/fpm 242 cp php-fpm.service /usr/lib/systemd/system/php-fpm.service 243 systemctl restart php-fpm.service 244 245 echo -e "\033[34;1m php-fpm启动成功! \033[0m" 246 247 #配置 Nginx 支持 PHP 解析 248 sed -i '65,71s/#/ /' /usr/local/nginx/conf/nginx.conf 249 sed -i '69s#scripts#/usr/local/nginx/html#' /usr/local/nginx/conf/nginx.conf 250 systemctl restart nginx.service 251 #并修改index.php的网页内容 252 cat > /usr/local/nginx/html/index.php <<EOF 253 <?php 254 phpinfo(); 255 ?> 256 EOF 257 258 echo -e "\033[34;1m PHP安装完成,请前往浏览器去访问! \033[0m" 259 260 #!/bin/bash 261 #部署 Discuz!社区论坛 Web 应用 262 263 ###########部署 Discuz!############# 264 #创建数据库,并进行授权 265 /usr/bin/expect <<EOF 266 spawn mysql -u root -p 267 expect "Enter password:" {send "abc123\r"} 268 expect "mysql>" {send "CREATE DATABASE bbs;\r"} 269 expect "mysql>" {send "GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'abc123';\r"} 270 expect "mysql>" {send "flush privileges;\r"} 271 expect "mysql>" {send "show databases;\r"} 272 expect "mysql>" {send "quit\r"} 273 expect eof 274 EOF 275 276 echo -e "\033[34;1m 数据库创建以及授权成功! \033[0m" 277 #解压论坛压缩包 278 cd /opt 279 unzip Discuz_X3.4_SC_UTF8.zip -d /opt/dis &> /dev/null 280 cd /opt/dis/dir_SC_UTF8/ 281 cp -r upload/ /usr/local/nginx/html/bbs/ 282 283 #调整论坛目录的权限 284 chmod -R 777 /usr/local/nginx/html/bbs/config/ 285 chmod -R 777 /usr/local/nginx/html/bbs/data/ 286 chmod -R 777 /usr/local/nginx/html/bbs/uc_client/ 287 chmod -R 777 /usr/local/nginx/html/bbs/uc_server/ 288 289 #修改nginx配置文件,允许访问 290 sed -i '45s/index.html/index.html index.php/' /usr/local/nginx/conf/nginx.conf 291 systemctl restart nginx.service 292 293 echo -e "\033[34;1m Discuz!论坛创建成功,请前往浏览器去安装并登录访问! \033[0m"
脚本运行结果:
将本人在工作学习中的一些知识记录并分享