shell脚本部署lamp

编译安装lamp架构

首先我们先创建目录用来存放脚本

[root@localhost script]# tree 
.
├── lamp.sh
└── packages
    ├── apr-1.7.0.tar.gz
    ├── apr-util-1.6.1.tar.gz
    ├── httpd-2.4.51.tar.gz
    ├── mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
    └── php-8.0.12.tar.gz

编写脚本

#!/bin/bash
route=/usr/local
path=/usr/src
data=/opt/data

if [ ! -d $path/apr-1.7.0 ];then
    tar xf packages/apr-1.7.0.tar.gz -C $path
fi

if [ ! -d $path/apr-util-1.6.1 ];then
    tar xf packages/apr-util-1.6.1.tar.gz -C $path
fi

if [ ! -d $path/httpd-2.4.51 ];then
    tar xf packages/httpd-2.4.51.tar.gz -C $path
fi

if [ ! -d $route/php-8.0.12 ];then
        tar xf packages/php-8.0.12.tar.gz -C $route
fi

if [ ! -d $route/mysql-5.7.34-linux-glibc2.12-x86_64 ];then
    tar xf packages/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C $route
    cd $route 
    ln -s mysql-5.7.34-linux-glibc2.12-x86_64 mysql
fi


yum -y install epel-release wget make openssl openssl-devel pcre pcre-devel gcc gcc-c++ zlib-devel expat-devel zlib expat

yum groups mark install 'Development Tools' -y

id apache &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin apache
fi

cd $path/apr-1.7.0
if [ ! -d $route/apr ];then
    sed -i 's/$RM "cfgfile"/d' configure
    ./configure --prefix=/usr/local/apr
    make && make install
fi

cd $path/apr-util-1.6.1
if [ ! -d $route/apr-util-1.6.1 ];then
    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
    make && make install
fi

cd $path/httpd-2.4.51
if [ ! -d $route/httpd-2.4.51 ];then
    ./configure --prefix=/usr/local/apache \
        --enable-so \
        --enable-ssl \
        --enable-cgi \
        --enable-rewrite \
        --with-zlib \
        --with-pcre \
        --with-apr=/usr/local/apr \
        --with-apr-util=/usr/local/apr-util/ \
        --enable-modules=most \
        --enable-mpms-shared=all \
        --with-mpm=prefork
    make && make install
fi

if [ $? -eq 0 ];then
        echo "apache 编译完成"
else
    echo "apache 编译失败,请检查"
fi

ln -s /usr/local/apache/include/ /usr/include/httpd
echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh

cd $route/apache/conf/
sed -i '203s/#//g' httpd.conf

touch /usr/lib/systemd/system/httpd.service
sum=$(cat /usr/lib/systemd/system/httpd.service | wc -l)
if [ $sum -lt 2 ];then
cat > /usr/lib/systemd/system/httpd.service << EOF
[Unit]
Description=httpd server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF
fi

systemctl daemon-reload
systemctl enable --now httpd


echo "安装mysql依赖"
yum -y install gcc gcc-c++ make zlib zlib-devel pcre pcre-devel openssl openssl-devel ncurses-compat-libs perl ncurses-devel cmake

id mysql &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin mysql
fi

cd $route
chown -R mysql.mysql mysql/
echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh

mkdir -p /opt/data
chown -R mysql.mysql /opt/data/


look=$( ls $data | wc -l)
if [ $look -eq 0 ];then 
$route/mysql/bin/mysqld --initialize-insecure --user mysql --datadir /opt/data/

cat > /etc/my.cnf << EOF
[mysqld]
basedir = $route/mysql
datadir = $data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF

sed -ri "s#^(basedir=).*#\1$route/mysql#g" /usr/local/mysql/support-files/mysql.server

sed -ri "s#^(datadir=).*#\1$data#g" /usr/local/mysql/support-files/mysql.server

cat > /usr/lib/systemd/system/mysql.service << EOF
[Unit]
Description=mysql server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop= /usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF
fi

systemctl daemon-reload 
systemctl enable --now mysql

echo "部署PHP"

yum -y install epel-release
yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel libzip-devel

wget http://mirror.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
yum -y install oniguruma-devel-6.8.2-2.el8.x86_64.rpm

cd $route/php-8.0.12
if [ ! -d $route/php8 ];then
    ./configure --prefix=/usr/local/php8  --with-config-file-path=/etc --enable-fpm --disable-debug --disable-rpath --enable-shared --enable-soap --with-openssl --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --enable-exif  --enable-ftp --enable-gd --with-jpeg --with-zlib-dir --with-freetype --with-gettext --enable-mbstring --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-readline --enable-shmop --enable-simplexml --enable-sockets --with-zip --enable-mysqlnd-compression-support --with-pear --enable-pcntl --enable-posix
    make && make install
fi

echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh

cd $route/php-8.0.12
cp php.ini-production /etc/php.ini

cd $route/php-8.0.12/sapi/fpm
cp init.d.php-fpm /etc/init.d/php-fpm

chmod +x /etc/rc.d/init.d/php-fpm

cp $route/php8/etc/php-fpm.conf.default $route/php8/etc/php-fpm.conf
cp $route/php8/etc/php-fpm.d/www.conf.default $route/php8/etc/php-fpm.d/www.conf

touch /usr/lib/systemd/system/php-fpm.service
view=$(cat /usr/lib/systemd/system/php-fpm.service | wc -l)
if [ $view -lt 2 ];then
cat > /usr/lib/systemd/system/php-fpm.service << EOF
[Unit]
Description=php server daemon
After=network.target

[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF
fi

systemctl daemon-reload
systemctl enable --now php-fpm

sed -i '120s/#//g' /usr/local/apache/conf/httpd.conf
sed -i '124s/#//g' /usr/local/apache/conf/httpd.conf


cd $route/apache/htdocs && mkdir test
cd $route/apache/htdocs/test
if [ ! -s index.php ];then
cat > index.php << EOF
<?php
    phpinfo();
?>
EOF


chown -R apache.apache /usr/local/apache/htdocs/
cd $route/apache/conf
cat >> httpd.conf << EOF
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/test"
    ServerName www.test.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/test/$1
    <Directory "/usr/local/apache/htdocs/test">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>
EOF

cd $route/apache/conf
sed -i '261s/index.html/index.php &/g' httpd.conf
fi

systemctl restart php-fpm.service
systemctl restart httpd.service
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,902评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,037评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,978评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,867评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,763评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,104评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,565评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,236评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,379评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,313评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,363评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,034评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,637评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,719评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,952评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,371评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,948评论 2 341

推荐阅读更多精彩内容