shell脚本选择部署lamp和lnmp

1. 首先创建一个目录,用来存放脚本,下载安装包

[root@localhost ~]# mkdir /script
[root@localhost packages]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz

[root@localhost packages]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz

[root@localhost packages]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.49.tar.gz

[root@localhost packages]# wget https://www.php.net/distributions/php-8.0.12.tar.gz

[root@localhost packages]# wget https://nginx.org/download/nginx-1.20.1.tar.gz

[root@localhost ~]# tree /script/
/script/
├── 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.12.tar.gz

1 directory, 7 files

2. 编写脚本

#!/bin/bash
yum=/etc/yum.repos.d
unzip=/usr/src
data=/opt/data
route=/usr/local
path=/usr/lib/systemd/system
apache=/usr/local/apache/conf/httpd.conf
mysql=/usr/local/mysql/support-files/mysql.server
read -p "请问您要安装lamp还是lnmp:" version
if [ -z $version ];then
    version=lamp
fi

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


echo "解压所需的安装"

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

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

if [ ! -d $unzip/httpd-2.4.49 ];then
	tar xf packages/httpd-2.4.49.tar.gz -C $unzip
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 $route/mysql
                chown -R mysql.mysql mysql/
fi

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

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

if [ ! -e $yum/CentOS-Base.repo ];then
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
fi

yum -y install epel-release

function apache(){
yum -y install epel-release wget make openssl openssl-devel pcre pcre-devel gcc gcc-c++ zlib-devel expat-devel zlib expat

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

cd $unzip/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 $unzip/apr-util-1.6.1
if [ ! -d $route/apr-util ];then
	./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
	make && make install
fi

cd $unzip/httpd-2.4.49
if [ ! -d $route/httpd-2.4.49 ];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

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


ln -s /usr/local/apache/include/ /usr/include/httpd
sed -i 's/ServerName www.example.com:80/ServerName www.example.com:80/g' $apache

cat > $path/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

systemctl daemon-reload 
systemctl enable --now httpd
sed -i '120s/#//g' /usr/local/apache/conf/httpd.conf
sed -i '124s/#//g' /usr/local/apache/conf/httpd.conf

mkdir -p $route/apache/htdocs/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 /usr/local/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 /usr/local/apache/conf
sed -i '261s/index.html/index.php &/g' httpd.conf
fi
}

function nginx(){
	yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel
	id nginx &>/dev/null
	if [ $? -ne 0 ];then
		useradd -r -M -s /sbin/nologin nginx
	fi
mkdir /var/log/nginx
chown -R nginx.nginx /var/log/nginx

cd $route/nginx-1.20.1
if [ ! -d $route/nginx ];then
    ./configure --prefix=/usr/local/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 && make install
fi

cd $path
cat > nginx.service << EOF
[Unit]
Description=Nginx server daemon
After=network.target

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

[Install]
WantedBy=multi-user.target
EOF

echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
./etc/profile.d/nginx.sh


systemctl daemon-reload
systemctl enable --now nginx.service

echo "创建php访问界面"
cd $route/nginx/html
cat > index.php << EOF
<?php
    phpinfo();
?>
EOF

cd $route/nginx/conf/
sed -i  '45s/index  index.html index.htm;/index index.php index.html index.htm;/g' nginx.conf
sed -i '65,71s/#//g' nginx.conf
sed -i '69s/\/scripts/\$document_root/g' nginx.conf
}

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


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

if [ ! -d $data ];then
    mkdir -p $data
    chown -R mysql.mysql /opt/data/
fi

echo "初始化数据库"
content=$(ls $data | wc -l)
if [ $content -eq 0 ];then
    $route/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=$data

    cat > /etc/my.cnf << EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/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"  $mysql
    sed -ri "s#^(datadir=).*#\1$data#g"  $mysql
    cat > $path/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.service

echo "部署php"
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 http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

cd $route/php-8.0.12
if [ ! -d $route/php8 ];then
    ./configure --prefix=$route/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
fi

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

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 $route/php8/etc
cp php-fpm.conf.default php-fpm.conf

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

cat > $path/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

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

if [ $version == lamp ];then
    apache
else
    nginx
fi

echo $version
echo "修改nginx配置文件后需要使用nginx -s reload命令"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值