shell脚本实现部署lamp和lnmp

shell脚本实现部署lamp和lnmp

创建一个目录,用来存放脚本和安装包

[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# tree
.
└── services
    ├── install.sh
    └── packages
        ├── apr-1.7.0.tar.gz
        ├── apr-util-1.6.1.tar.gz
        ├── httpd-2.4.49.tar.gz
        ├── mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
        ├── nginx-1.20.1.tar.gz
        └── php-8.0.11.tar.gz

2 directories, 7 files

下载好需要部署服务的安装包

[root@localhost services]# cd packages/
[root@localhost packages]# ls
apr-1.7.0.tar.gz       httpd-2.4.49.tar.gz                         nginx-1.20.1.tar.gz
apr-util-1.6.1.tar.gz  mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz  php-8.0.11.tar.gz

编写脚本文件

[root@localhost services]# cat install.sh 
#!/bin/bash

#变量
apache_dir=/usr/src
install_dir=/usr/local
log_dir=/var/log
data_dir=/opt/data

#选择服务
read -p "请您输入需要安装的服务(lamp或者lnmp):" service
if [ -z $service ];then
    service=lamp
fi

echo $service | grep -E "^[lamp,lnmp]+$" &>/dev/null
if [ $? -ne 0 ];then
    service=lamp
fi

#选择密码
read -p "请输入您要使用的数据库密码:" passwd
if [ -z $passwd ];then
    passwd=1
fi

#解压安装包
if [ ! -d $apache_dir/apr-1.7.0 ];then
    tar xf packages/apr-1.7.0.tar.gz -C $apache_dir
fi

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

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

if [ ! -d $install_dir/nginx-1.20.1 ];then
    tar xf packages/nginx-1.20.1.tar.gz -C $install_dir
fi

if [ ! -d $install_dir/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 $install_dir
    ln -s mysql-5.7.34-linux-glibc2.12-x86_64  $install_dir/mysql
fi

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

#定义函数apache
function apache(){

#安装apache
    yum groups mark install "Development Tools" -y

    yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make

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

#编译安装
    cd $apache_dir/apr-1.7.0
    if [ ! -d $install_dir/apr ];then
        sed -i 's/$RM "$cfgfile"/d' configure
        ./configure --prefix=$install_dir/apr && make && make install
    fi

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

    cd ../httpd-2.4.49
    if [ ! -d $install_dir/httpd ];then 
         ./configure --prefix=$install_dir/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=$install_dir/apr --with-apr-util=$install_dir/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
    
    make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

#添加修改配置文件	 
    cat >> $install_dir/httpd/conf/httpd.conf <<EOF
<VirtualHost *:80>
    DocumentRoot "$install_dir/httpd/htdocs/test"
    ServerName www.pyd.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000$install_dir/httpd/htdocs/test/$1
    <Directory "$install_dir/httpd/htdocs/test">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>
EOF

    sed -i '261s/index.html/index.php index.html/g' /usr/local/httpd/conf/httpd.conf

    sed -i 's/#ServerName www.example.com:80/ServerName www.example.com:80/g' $install_dir/httpd/conf/httpd.conf
    
    sed -i '/proxy_module/s/#//g' $install_dir/httpd/conf/httpd.conf
    
    sed -i '/proxy_fcgi_module/s/#//g' $install_dir/httpd/conf/httpd.conf
    
    echo "export PATH=$install_dir/httpd/bin:$PATH" > /etc/profile.d/httpd.sh

#创建访问目录    
    mkdir -p $install_dir/httpd/htdocs/test
    
    chown -R apache.apache $install_dir/httpd/htdocs/

    cat > $install_dir/httpd/htdocs/test/index.php <<EOF
<?php
        phpinfo();
?>
EOF

#使用service控制apache
    cat > /usr/lib/systemd/system/httpd.service  << EOF
[Unit]
Description=The Apache http server
After=network.targe

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

[Install]
WantedBy=multi-user.target
EOF
#重新加载启动服务
    systemctl daemon-reload
    systemctl start httpd.service
    fi
}

#定义函数nginx
function nginx(){

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

    yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel

#创建日志存在目录
    if [ ! -d $log_dir/nginx ];then
        mkdir -p $log_dir/nginx
        chown -R nginx.nginx $log_dir/nginx
    fi


    if [ ! -d $install_dir/nginx-1.20.1 ];then
        tar xf packages/nginx-1.20.1.tar.gz -C $install_dir
    fi

#编译安装
    cd $install_dir/nginx-1.20.1
    if [ ! -d $install_dir/nginx ];then
        ./configure --prefix=$install_dir/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
    make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
    
    echo "export PATH=$install_dir/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh

#使用service控制nginx
    cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=Nginx server daemon
After=network.target 

[Service]
Type=forking
ExecStart=$install_dir/nginx/sbin/nginx 
ExecStop=$install_dir/nginx/sbin/nginx -s quit
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

#修改配置文件
    sed -i '45s/index  index.html index.htm;/index index.php index.html index.htm;/g' $install_dir/nginx/conf/nginx.conf
    sed -i '65s/#//g' $install_dir/nginx/conf/nginx.conf
    sed -i '66s/#//g' $install_dir/nginx/conf/nginx.conf
    sed -i '67s/#//g' $install_dir/nginx/conf/nginx.conf
    sed -i '68s/#//g' $install_dir/nginx/conf/nginx.conf
    sed -i '69s/#//g' $install_dir/nginx/conf/nginx.conf
    sed -i '69s!/scripts$fastcgi_script_name;!$Document_Root$fastcgi_script_name;!g' $install_dir/nginx/conf/nginx.conf
    sed -i '70s/#//g' $install_dir/nginx/conf/nginx.conf
    sed -i '71s/#//g' $install_dir/nginx/conf/nginx.conf
    sed -i '71s/#//g' $install_dir/nginx/conf/nginx.conf

#创建php访问界面
    cat > $install_dir/nginx/html/index.php <<EOF
<?php
        phpinfo();
?>
EOF

#重新加载启动服务
    systemctl daemon-reload 
    systemctl start nginx.service

    fi
}    

#根据用户的选择来调用定义的函数
if [ $service = lamp ];then
    apache
else
    nginx
fi

#安装mysql数据库
yum -y install ncurses-compat-libs

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

cd $install_dir
chown -R mysql.mysql mysql

echo "export PATH=$install_dir/mysql/bin:$PATH" > /etc/profile.d/mysql.sh

#创建数据存放目录
if [ ! -d $date_dir ];then
    mkdir -p $data_dir
    chown -R mysql.mysql $data_dir
fi

#初始化
content=$(ls $data_dir | wc -l)
if [ $content -eq 0 ];then
    $install_dir/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=$data_dir
fi

#生成配置文件
cat > /etc/my.cnf <<EOF
[mysqld]
port = 3306
basedir = $install_dir/mysql
datadir = $data_dir
socket = /tmp/mysql.sock
pid-file = $data_dir/mysql.pid
skip-name-resolve
EOF

#修改配置文件
sed -ri "s#^(basedir=).*#\1$install_dir/mysql#g"  $install_dir/mysql/support-files/mysql.server
sed -ri "s#^(datadir=).*#\1$data_dir#g"  $install_dir/mysql/support-files/mysql.server

#使用service控制mysql
cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysql server daemon
After=network.targe

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

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now mysqld

#根据用户的选择来设置密码
$install_dir/mysql/bin/mysqladmin -uroot password $passwd &>/dev/null

#安装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 libsqlite3x-devel php-mysqlnd libzip-devel
yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

#编译安装
cd $install_dir/php-8.0.11/
if [ ! -d $install_dir/php8 ];then
    ./configure --prefix=$install_dir/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 -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

    echo "export PATH=$install_directory/php8/bin:\$PATH" > /etc/profile.d/php.sh

#配置php-fpm
    cp php.ini-production /etc/php.ini
    cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    chmod +x /etc/rc.d/init.d/php-fpm
    cd $install_dir/php8/etc/
    cp php-fpm.conf.default php-fpm.conf
    cd php-fpm.d/
    cp www.conf.default www.conf

#使用service控制php
    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 start php-fpm.service

echo -e "你的密码设置成功,密码是 \033[1;32;40m $passwd \033[0m"
[root@localhost services]# 

验证效果

部署lamp

[root@localhost services]# bash -x install.sh 
+ apache_dir=/usr/src
+ install_dir=/usr/local
+ log_dir=/var/log
+ data_dir=/opt/data
+ read -p '请您输入需要安装的服务(lamp或者lnmp):' service   
请您输入需要安装的服务(lamp或者lnmp):   //默认安装lamp
+ '[' -z ']'
+ service=lamp
+ echo lamp
+ grep -E '^[lamp,lnmp]+$'
+ '[' 0 -ne 0 ']'
+ read -p 请输入您要使用的数据库密码: passwd
请输入您要使用的数据库密码:   //默认密码1
+ '[' -z ']'
+ passwd=1
+ '[' '!' -d /usr/src/apr-1.7.0 ']'
+ '[' '!' -d /usr/src/apr-util-1.6.1 ']'
+ '[' '!' -d /usr/src/httpd-2.4.49 ']'
+ '[' '!' -d /usr/local/nginx-1.20.1 ']'
+ '[' '!' -d /usr/local/mysql-5.7.34-linux-glibc2.12-x86_64 ']'
+ '[' '!' -d /usr/local/php-8.0.11 ']'
+ '[' lamp = lamp ']'
+ apache   //调用了函数apache
+ yum groups mark install 'Development Tools' -y
+
* * * * * * 过程略
+
+ systemctl daemon-reload
+ systemctl start php-fpm.service
+ echo -e '你的密码设置成功,密码是 \033[1;32;40m 1 \033[0m'
你的密码设置成功,密码是  1 
[root@localhost services]# ss -antl
State          Recv-Q          Send-Q                   Local Address:Port                   Peer Address:Port                    
LISTEN         0               128                          127.0.0.1:9000                        0.0.0.0:*                     
LISTEN         0               80                                   *:3306                              *:*                        
LISTEN         0               128                                  *:80                                *:*            

//查看apache状态
[root@localhost services]# systemctl status httpd.service 
● httpd.service - The Apache http server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2021-10-30 11:33:49 CST; 15min ago
 Main PID: 66521 (httpd)
    Tasks: 7 (limit: 23648)
   Memory: 7.0M
   CGroup: /system.slice/httpd.service
           ├─ 66521 /usr/local/httpd/bin/httpd -k start
           ├─ 66522 /usr/local/httpd/bin/httpd -k start
           ├─ 66523 /usr/local/httpd/bin/httpd -k start
           ├─ 66524 /usr/local/httpd/bin/httpd -k start
           ├─ 66525 /usr/local/httpd/bin/httpd -k start
           ├─ 66526 /usr/local/httpd/bin/httpd -k start
           └─187181 /usr/local/httpd/bin/httpd -k start

浏览器输入IP访问lamp
在这里插入图片描述

部署lnmp

[root@localhost services]# bash -x install.sh 
+ apache_dir=/usr/src
+ install_dir=/usr/local
+ log_dir=/var/log
+ data_dir=/opt/data
+ read -p '请您输入需要安装的服务(lamp或者lnmp):' service
请您输入需要安装的服务(lamp或者lnmp):lnmp   //输入lnmp
+ '[' -z lnmp ']'
+ echo lnmp
+ grep -E '^[lamp,lnmp]+$'
+ '[' 0 -ne 0 ']'
+ read -p 请输入您要使用的数据库密码: passwd
请输入您要使用的数据库密码:123   //自定义密码123
+ '[' -z 123 ']'
+ '[' '!' -d /usr/src/apr-1.7.0 ']'
+ '[' '!' -d /usr/src/apr-util-1.6.1 ']'
+ '[' '!' -d /usr/src/httpd-2.4.49 ']'
+ '[' '!' -d /usr/local/nginx-1.20.1 ']'
+ '[' '!' -d /usr/local/mysql-5.7.34-linux-glibc2.12-x86_64 ']'
+ '[' '!' -d /usr/local/php-8.0.11 ']'
+ '[' lnmp = lamp ']'
+ nginx   //调用了函数nginx
+
* * * * * * 过程略
+
+ systemctl daemon-reload
+ systemctl start php-fpm.service
+ echo -e '你的密码设置成功,密码是 \033[1;32;40m 123 \033[0m'
你的密码设置成功,密码是  123 


//查看80,3306,9000端口
[root@localhost services]# ss -antl
State          Recv-Q          Send-Q                   Local Address:Port                   Peer Address:Port                   
LISTEN         0               128                          127.0.0.1:9000                        0.0.0.0:*            
LISTEN         0               128                            0.0.0.0:80                          0.0.0.0:*                      
LISTEN         0               80                                   *:3306                              *:*            
[root@localhost services]# 

//查看nginx服务状态
[root@localhost services]# systemctl status nginx.service 
● nginx.service - Nginx server daemon
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2021-10-30 11:05:16 CST; 52min ago
 Main PID: 22087 (nginx)
    Tasks: 2 (limit: 49594)
   Memory: 2.8M
   CGroup: /system.slice/nginx.service
           ├─22087 nginx: master process /usr/local/nginx/sbin/nginx
           └─22088 nginx: worker process

1030 11:05:16 localhost.localdomain systemd[1]: Starting Nginx server daemon...
1030 11:05:16 localhost.localdomain systemd[1]: Started Nginx server daemon.

浏览器输入IP访问lnmp
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值