Shell脚本一键部署LNMP架构

Shell脚本一键部署LNMP架构

具体安装流程:

安装nginx→安装MySQL数据库→安装PHP工具

#!/bin/bash

#远程挂载
df -hT | grep -o "cifs"
if [ $? -ne 0 ];then
echo "Start mount"
yum install expect -y
read -p "请输入提供硬盘主机IP:" gongip
/usr/bin/expect <<-EOF
spawn mount.cifs //$gongip/rpm /mnt/
expect {
	"Password*"
	{send "\r"}
}
expect eof
EOF
fi

#安装nginx
yum -y install \
gcc \
gcc-c++ \
pcre-devel \
zlib-devel \
expat-devel \
pcre
cd /mnt/
tar zxf nginx-1.12.0.tar.gz -C /opt/
useradd -M -s /sbin/nologin nginx
cd /opt/nginx-1.12.0/
./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#使nginx能够被识别

#Systemctl
cat > /lib/systemd/system/nginx.service <<-EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP \$MAINPID
ExecStop=/usr/bin/kill -s QUIT \$MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
chmod 754 /lib/systemd/system/nginx.service

#Service
cat > /etc/init.d/nginx <<-eof
#!/bin/bash
wenjian="/usr/local/nginx/sbin/nginx"
pid="/usr/local/nginx/logs/nginx.pid"
case \$1 in
start)
    \$wenjian ;;
stop)
    kill -s QUIT \$(cat \$pid) ;;
restart)
    \$0 stop
    \$0 start
;;
reload)
    kill -s HUP \$(cat \$pid) ;;
*)
    echo "Please,try again"
    exit 1 ;;
esac
exit 0
eof
chmod 754 /etc/init.d/nginx
systemctl stop firewalld.service 
setenforce 0
systemctl start nginx.service
systemctl enable nginx.service
netstat -atnp | egrep "(80|nginx)"
if [ $? -eq 0 ];then 
	echo "nginx服务启动成功"
else
	echo "nginx服务启动失败"
	exit 1
fi
service nginx stop

#安装mysql
yum -y install \
cmake \
ncurses \
ncurses-devel \
bison \
expect
tar zxf /mnt/mysql-boost-5.7.20.tar.gz -C /opt
cd /opt/mysql-5.7.20
useradd  -s /sbin/nologin mysql
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc/ \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
read -p "即将开启进行MySQL编译,时间很长。是否需要加速(y/n)?(默认不加速)" speed
case $speed in 
  y)
	make -j8
	make install ;;
  yes)
    make -j8
	make install ;;
  n)
    make && make install ;;
  no)
    make && make install ;;
  *)
    make 
	make install ;;
esac
chown -R mysql:mysql /usr/local/mysql/
sed -i '1,100d' /etc/my.cnf
cat > /etc/my.cnf <<-eof
[client]                            
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]                           
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]                          
user = mysql                  
basedir = /usr/local/mysql      
datadir = /usr/local/mysql/data    
port = 3306                    
character_set_server=utf8          
pid-file = /usr/local/mysql/mysqld.pid     
socket = /usr/local/mysql/mysql.sock    
server-id = 1

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
eof
chown mysql:mysql /etc/my.cnf
echo "PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH" >> /etc/profile
echo "export PATH" >> /etc/profile
echo "请结束后使用source /etc/profile进行刷新"
sleep 5
cd /usr/local/mysql
bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql/ \
--datadir=/usr/local/mysql/data
cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl start mysqld.service
systemctl enable mysqld.service
netstat -atnp | grep "3306"
if [ $? -eq 0 ];then
	echo "MySQL数据库启动成功"
	sleep 3
else 
	echo "启动失败"
	exit 1
fi
/usr/bin/expect <<-EOF
spawn mysqladmin -uroot -p password
expect {
        "Enter*"
        {spend "\r";exp_continue}
        "New*"
        {spend "123123\r";exp_continue}
        "Confirm*"
        {spend "123123\r"}
}
expect eof
EOF

#安装PHP工具
yum -y install \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel
tar xjf /mnt/php-7.1.10.tar.bz2 -C /opt/
cd /opt/php-7.1.10/
#配置PHP工具
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-zlib \
--with-mysqli \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
make && make install
phpcf1="/usr/local/php/lib/php.ini"
phpcf2="/usr/local/php/etc/php-fpm.conf"
cp php.ini-development $phpcf1
sed -i -e '939d' -e '938adate.timezone =Asia\/Shanghai' $phpcf1
sed -i -e '1170d' -e '1169amysqli.default_socket =\/usr\/local\/mysql\/mysql.sock' $phpcf1
cd /usr/local/php/etc
mv php-fpm.conf.default php-fpm.conf
sed -i -e '17d' -e '16apid = run\/php-fpm.pid'  $phpcf2
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
ln -s /usr/local/php/bin/* /usr/local/bin/
num=`ps -elf | grep "php-fpm" | wc -l`
if [ $num -gt 0 ];then 
	echo "fpm模块开启"
else
	echo "Failed"
	exit 1
fi

#nginx开启支持PHP
sed -i -e '65,71s/#//' -e '69s/\/scripts/\/usr\/local\/nginx\/html/' /usr/local/nginx/conf/nginx.conf
cat > /usr/local/nginx/html/index.php <<-eof
<?php
phpinfo();
?>
eof
systemctl start nginx.service
#检查服务、功能启动情况
jobs=`netstat -atnp | egrep "(80|3306|9000)" | grep "LISTEN" | wc -l`
if [ $jobs -eq 3 ];then 
	echo "LNMP架构成功"
	netstat -atnp | egrep "(80|3306|9000)" | grep "LISTEN"
else
	echo "请自行检查"
	netstat -atnp | egrep "(80|3306|9000)" | grep "LISTEN"
	exit 1
fi
echo "请使用source /etc/profile进行刷新"

你可能感兴趣的:(Shell脚本合集,LNMP架构,shell脚本,一键部署)