利用shell脚本实现lamp部署

lamp部署脚本

项目目录

[root@localhost lamp]# pwd
/root/lamp
[root@localhost lamp]# tree
.
├── lamp.sh
└── packages
    ├── apr-1.7.0.tar.gz
    ├── apr-util-1.6.1.tar.gz
    ├── httpd-2.4.46.tar.bz2
    └── mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

1 directory, 5 files

脚本内容

[root@localhost lamp]# bash lamp.sh 
#!/bin/bash
echo "------------------"
echo "作者:leidazhuang"
echo "时间:2021/4/6"
echo "本脚本用于部署lamp"
echo "------------------"
#关闭防火墙selinx
echo "正在关闭防火墙和selinx..."
sleep 1s
echo "关闭完成"
setenfore 0 &> /dev/null
systemctl stop firewalld &> /dev/null
#开始部署lamp
echo "开始部署lamp"
sleep 1s

#安装工具包
echo "正在安装工具包..."
sleep 1s
yum -y install bzip2  vim make wget openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ libxml2-devel &> /dev/null  
#解压apr apr-util httpd mysql安装包
echo "正在解压安装包..."
sleep 1s
tar xf ./packages/apr-1.7.0.tar.gz -C /usr/src/
tar xf ./packages/apr-util-1.6.1.tar.gz -C /usr/src
tar xf ./packages/httpd-2.4.46.tar.bz2 -C /usr/src
tar xf ./packages/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local

#lamp-httpd
echo "------正在部署httpd------"
sleep 1s
#创建apache用户
echo "正在验证apache用户..."
sleep 1s
id apache &> /dev/null
if [ $? -eq 0 ];then
	echo "apache用户已存在"
else
	echo "正在创建apache用户..."
	sleep 1s
	useradd -r -M -s /sbin/nolgin apache
	echo "apache创建成功"
fi
echo "安装apr..."
ls /usr/local/apr-1.7.0 &> /dev/null
if [ $? -eq 0 ];then
	rm -rf /usr/src/apr-1.7.0 &> /dev/null
	rm -rf /usr/local/apr/ &> /dev/null
	cd /usr/src/apr-1.7.0
	sed -i '/$RM "$cfgfile"/d' configure
	echo "正在编译安装apr,请耐心等待..."
	./configure --prefix=/usr/local/apr &> /dev/null
	make &> /dev/null
	make install &> /dev/null
	cd
	echo "apr安装完成"
else
	cd /usr/src/apr-1.7.0
	sed -i '/$RM "$cfgfile"/d' configure
	echo "正在编译安装apr,请耐心等待..."  
  	./configure --prefix=/usr/local/apr &> /dev/null
	make &> /dev/null
	make install &> /dev/null               
	cd          
    	echo "apr安装完成"
fi
echo "安装apr-util..."
ls /usr/lcoal/apr-util-1.6.1 &> /dev/null
if [ $? -eq 0 ];then
	rm -rf /usr/src/apr-uitl-1.6.1 &> /dev/null
	rm -rf /usr/local/apr-uitl/ &> /dev/null
	cd /usr/src/apr-util-1.6.1
	echo "正在编译安装apr-util,请耐心等待..."
	./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr &> /dev/null
	make &> /dev/null
	make install &> /dev/null
	cd
	echo "apr-util安装完成"	
else
	cd /usr/src/apr-util-1.6.1
	echo "正在编译安装apr-util,请耐心等待..."
	./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr &> /dev/null
	make &> /dev/null
	make install &> /dev/null
	cd
	echo "apr-util安装完成"
fi
echo "安装httpd..."
ls /usr/local/httpd &> /dev/null
if [ $? -eq 0 ];then
	rm -rf /usr/src/httpd-2.4.46 &> /dev/null
	rm -rf /usr/local/httpd &> /dev/null
	cd /usr/src/httpd-2.4.46
	echo "正在编译安装httpd,请耐心等待..."
	./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 &>/tmp/apache_install.log
	make &> /dev/null
	make install &> /dev/null
	cd
	echo "httpd安装完成"
else
	cd /usr/src/httpd-2.4.46
	echo "正在编译安装httpd,请耐心等待..."
	./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 &>/tmp/apache_install.log
	make &> /dev/null
	make install &> /dev/null
	cd
	echo "httpd安装完成"
fi
echo "正在配置httpd..."
sleep 1s
#环境变量
echo "export PATH=/usr/local/apache/bin:\$PATH" > /etc/profile.d/httpd.sh
#映射
ln -s /usr/lcoal/apache/include/ /usr/include/httpd &> /dev/null
#取消名字警告
sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf
echo "-----httpd部署成功-----"
sleep 1s
echo "下面开始部署mysql"

#lamp-mysql
echo "-----正在部署mysql-----"
sleep 1s
#创建mysql用户
echo "正在验证mysql用户..."
sleep 1s
id mysql &> /dev/null
if [ $? -eq 0 ];then
	echo "mysql用户已存在"
else
	echo "正在创建mysql用户..."
	sleep 1s
	useradd -r -M -s /sbin/nolgin mysql
	echo "mysql用户创建成功"
fi
echo "正在安装依赖包..."
sleep 1s
yum -y install libncurses* &> /dev/null
echo "安装完成"
sleep 1s
echo "正在配置mysql..."
sleep 1s
ln -sv /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql &> /dev/null
chown -R mysql.mysql /usr/local/mysql &> /dev/null
echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh &> /dev/null
source /etc/profile.d/mysql.sh &> /dev/null
mkdir -p /opt/data &> /dev/null
chown -R mysql.mysql /opt/data/ &> /dev/null
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/ &> /dev/null
ln -sv /usr/local/mysql/include/ /usr/local/include/mysql &> /dev/null
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf &> /dev/null
ldconfig &> /dev/null

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
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld &> /dev/null
sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

service mysqld start &> /dev/null
chkconfig --add mysqld &> /dev/null
chkconfig mysqld on &> /dev/null
read -p "请输入您要设置的数据库密码:" mysql_password
/usr/local/mysql/bin/mysql -uroot -e "set password=password('$mysql_password')"
echo "-----mysql部署完成-----"
sleep 1s
echo "下面开始部署php"

#lamp-php
echo "-----正在部署php-----"
yum -y install php* &> /dev/null

echo "正在配置php..."
sleep 1s
sed -i '/^listen/clisten = 0.0.0.0:9000' /etc/php-fpm.d/www.conf
systemctl enable php-fpm &> /dev/null
systemctl start php-fpm &> /dev/null
echo "-----php部署完成-----"
sleep 1s
#配置
echo "正在配置虚拟主机..."
sleep 1s
cat > /usr/local/apache/htdocs/index.php <<EOF
<?php
    phpinfo();
    ?>
EOF
    chown -R apache.apache /usr/local/apache/htdocs/
cat > /usr/local/apache/conf/extra/vhosts.conf <<'EOF'
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs"
    ServerName www.leidazhuang.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/$1
    <Directory "/usr/local/apache/htdocs">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>
EOF

sed -i '398aAddType application/x-httpd-php-source .phps' /usr/local/apache/conf/httpd.conf
sed -i '398aAddType application/x-httpd-php .php' /usr/local/apache/conf/httpd.conf
sed -i '/    DirectoryIndex/s/index.html/index.php index.html/g' /usr/local/apache/conf/httpd.conf
sed -i '/proxy_module/s/#//g' /usr/local/apache/conf/httpd.conf
sed -i '/proxy_fcgi_module/s/#//g' /usr/local/apache/conf/httpd.conf
echo 'Include conf/extra/vhosts.conf' >> /usr/local/apache/conf/httpd.conf 
echo "虚拟主机配置完成"
sleep 1s

#启动三个服务
echo "启动httpd mysql php"
sleep 1s
/usr/local/apache/bin/apachectl restart
service mysqld restart
systemctl restart php-fpm

#验证
echo "查看端口"
ss -antl
echo "lamp部署完成"

测试访问

查看本机ip

[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:3f:e9:2f brd ff:ff:ff:ff:ff:ff
    inet 192.168.110.30/24 brd 192.168.110.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever

通过网页访问

posted @ 2021-04-07 18:00  我爱吃芹菜~  阅读(595)  评论(0编辑  收藏  举报
Title