LAMP-shell one-click deployment script

Deploy Apache

#!/bin/bash
echo "正在解压包……"
P1=$(find / -name httpd*.bz2)
P2=$(find / -name apr-util*tar.gz)
P3=$(find / -name apr-[0-9]*tar.gz)
tar zvxf $P2 -C /opt >/dev/null
tar zvxf $P3 -C /opt >/dev/null

read -p "您的Apache源码包名是否为$P1(yes|no)" PAN
if [ $PAN = yes ]
then
echo "正在解压Apache"
 tar jvxf $P1 -C /opt >/dev/null
else
exit 
fi

P4=$(find /opt/ -name "apr-[0-9].[0-9]*")
P5=$(find /opt/ -name "apr-util-[0-9].[0-9]*")
P6=$(find /opt/ -name "httpd-[0-9].[0-9]*")
mv $P4 $P6/srclib/apr
mv $P5 $P6/srclib/apr-util 

##################
echo "正在安装环境"
yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl >/dev/null

cd $P6

echo "配置Apache"
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi

echo "###编译及安装###" 
make && make install  


ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/

echo "
[Unit]
Description=The Apache HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/httpd/logs/httpd.pid
ExecStart= /usr/local/bin/apachectl $OPTIONS
ExecrReload= /bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target" >/lib/systemd/system/httpd.service

systemctl start httpd.service
systemctl enable httpd.service
systemctl is-enabled httpd.service

E1="www.*.com"
while true
do
read -p "请输入Apache服务器域名如(www.baidu.com):" E
#####[[ $E =~ $E1 ]]这个格式可以判断正则####################
if [[ $E =~ $E1 ]]
then
echo "您输入的域名为:$E"
sed -i -r '197s/.*/ServerName www.kgc.com:80/' /etc/httpd.conf
exit 0
else
echo "格式不对,重新输入"
fi
done

read -p "请输入服务器IP地址:" I
sed -n -r '51s/.*/Listen $I:80/p' /etc/httpd.conf

sed -n -r '52s/^/#/p' /etc/httpd.conf

systemctl restart httpd.service

Deploy MySQL

#!/bin/bash
yum -y install \
ncurses \
ncurses-devel \
bison \
gcc \
gcc-c++ \
expect \
cmake

useradd -s /sbin/nologin  mysql

P1=$(find / -name mysql-boost-*.gz)
read -p "核对软件名称$P1是否正确(yes|no):" T

if [ $T = yes ]
then
tar zvxf $P1 -C /opt
else
exit
fi

P2=$(find /opt/ -name mysql-[0-9].*[0-9])
cd $P2

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

make -j3 && make install

chown -R mysql:mysql /usr/local/mysql/

echo "
[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
" >/etc/my.cnf

chown mysql:mysql /etc/my.cnf

echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
echo 'export PATH' >> /etc/profile
source /etc/profile

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 enable mysqld
systemctl start mysqld
systemctl status mysqld


/usr/bin/expect <<EOF
spawn /usr/local/mysql/bin/mysqladmin -uroot -p password
expect {
    
    
"Enter" {
    
    send "\r";exp_continue}
"password" {
    
    send "abc123\r";exp_continue}
"password" {
    
    send "abc123\r"}
}
expect eof
EOF

echo "安装完成,请执行 source /etc/profile 命令,完成优化。"

Deploy PHP

#!/bin/bash

P1=$(find / -name php*.bz2)

read -p "您的PHP源码包名是否为$P1(yes|no)" PAN

if [ $PAN = yes ]
then
echo "正在解压PHP源码包"
 tar jvxf $P1 -C /opt >/dev/null
else
exit
fi

P2=$(find /opt/ -name "php-[0-9].[0-9]*")
cd $P2

echo "正在安装环境################################################"
yum -y install \
gd \
libjpeg \
libjpeg-devel \
libpng \
libpng-devel \
freetype \
freetype-devel \
libxml2 \
libxml2-devel \
zlib \
zlib-devel \
curl \
curl-devel \
openssl \
openssl-devel


./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/httpd/bin/apxs \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip

make -j3 && make install

cp php.ini-development /usr/local/php/lib/php.ini

sed -i -r '/mysqli.default_socket =/s/$/ mysqli.default_socket = \/usr\/local\/mysql\/mysql.sock/' /usr/local/php/lib/php.ini
sed -i -r '/date.timezone =/s/$/ Asia\/Shanghai/' /usr/local/php/lib/php.ini
sed -i -r '/date.timezone =/s/^;//' /usr/local/php/lib/php.ini

sed -i '393aAddType application/x-httpd-php\ .php\nAddType application/x-httpd-php\ .php' /usr/local/httpd/conf/httpd.conf

sed -i -r '/DirectoryIndex index.html/s/$/ php.html/' /usr/local/httpd/conf/httpd.conf

echo "<?php
phpinfo();
?> " >/usr/local/httpd/htdocs/index.php

systemctl restart httpd





Guess you like

Origin blog.csdn.net/CN_LiTianpeng/article/details/109154925