LAMP脚本部署

1.LAMP版本
操作系统:centos8
httpd:
Server version: Apache/2.4.48 (Unix)
mysql:
Server version: 5.7.33 MySQL
php:
PHP 8.0.10
2.脚本目录结构
tree /tmp/lamp/
/tmp/lamp/
├── install_lamp.sh
└── soft
    ├── apr-1.7.0.tar.bz2
    ├── apr-util-1.6.1.tar.bz2
    ├── httpd-2.4.48.tar.bz2
    ├── mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
    └── php-8.0.10.tar.xz
3.脚本内容
#!/bin/bash
if [ $UID -ne 0 ];then
	echo -e "\e[1;31m 请使用管理员身份运行 \e[0m"

fi

#variables
makedir=/usr/local
tardir=/usr/src
packdir=/tmp/lamp
datadir=/opt/data

read -p "请输入要安装的php版本(默认回车是php-7.4.24 其它字符php-8.0.11版本)" count
if [ -z $count ];then
	count=7
else
	count=8
fi

echo -e "\e[1;33m 正在安装依赖包 \e[0m"
##install packages
yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make epel-release  "@Development Tools" ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs &>/dev/null
sleep 2
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 &>/dev/null
yum -y install libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel  epel-release php-mysqlnd.x86_64 libsqlite3x-devel oniguruma  &>/dev/null
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  libzip-devel &>/dev/null

######################################apache###############################

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

#解压包
cd $packdir/soft/
tar -xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C $makedir
tar -xf apr-1.7.0.tar.bz2 -C $tardir
tar -xf apr-util-1.6.1.tar.bz2 -C $tardir
tar -xf httpd-2.4.48.tar.bz2 -C $tardir
tar -xf php-8.0.11.tar.xz -C $tardir
tar -xf php-7.4.24.tar.xz -C $tardir

#修改apr包
sed -i '/$RM "$cfgfile"/d' $tardir/apr-1.7.0/configure



echo -e "\e[1;33m 正在编译apache \e[0m"
cd $tardir/apr-1.7.0
if [ ! -d /usr/local/apr ];then
	./configure --prefix=/usr/local/apr && make && make install
fi
cd ../apr-util-1.6.1/
if [ ! -d /usr/local/apr-util ];then
	./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install
fi
cd ../httpd-2.4.48/
if [ ! -d /usr/local/apache/ ];then
	./configure --prefix=/usr/local/apache \
		--sysconfdir=/etc/httpd24 \
		--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

#apache mysql 环境配置 头文件映射
echo "export PATH=$makedir/apache/bin:\$PATH" > /etc/profile.d/apache.sh
if [ ! -d /usr/local/mysql ];then
	ln -s $makedir/mysql-5.7.33-linux-glibc2.12-x86_64  $makedir/mysql
fi
echo "export PATH=$makedir/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh
chown -R mysql.mysql $makedir/mysql*

#apache-service文件配置
cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=httpd server daemon
After=network.target httpd-keygen.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.service 
echo -e "\e[1;33m apache自启动 \e[0m"
##########################################mysql######################################################
if [ ! -d $datadir ];then
	mkdir $datadir
fi
chown -R mysql.mysql $datadir

#mysql初始化
echo -e "\e[1;33m 正在进行mysql初始化 \e[0m"
DB=$(ls /opt/data/ |wc -l)
if [ $DB -eq 0 ];then
	/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
fi

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

#配置mysql脚本
sed -ri "s#^(basedir=).*#\1$makedir/mysql#g" $makedir/mysql/support-files/mysql.server 
sed -ri "s#^(datadir=).*#\1$datadir#g" $makedir/mysql/support-files/mysql.server 

#mysql.service文件
cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysql server daemon
After=network.target mysql-keygen.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 

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload 
systemctl enable --now httpd.service 
echo -e "\e[1;33m mysql自启动 \e[0m"

##########################################################	php	##############################################
#编译php7.4.24
function php7(){
	echo -e "\e[1;31m 正在编译PHP7.4.24 \e[0m"
	cd /usr/src/php-7.4.24/
	if [ ! -d /usr/local/php7 ];then
	./configure --prefix=/usr/local/php7  \
		--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=$makedir/php7/bin:\$PATH" > /etc/profile.d/php.sh
	cp /usr/src/php-7.4.24/php.ini-production /etc/php.ini 
	cp /usr/src/php-7.4.24/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
	chmod +x /etc/rc.d/init.d/php-fpm 
	cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
	cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

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

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

[Install]
WantedBy=multi-user.target
EOF
	systemctl daemon-reload 
	systemctl enable --now php-fpm.service 
	echo -e "\e[1;33m php自启动 \e[0m"
}


#编译php-8.0.11
function php8(){
	echo -e "\e[1;31m 正在编译PHP8.0.11 \e[0m"
	cd /usr/src/php-8.0.11/
	if [ ! -d /usr/local/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=$makedir/php8/bin:\$PATH" > /etc/profile.d/php.sh
	cp /usr/src/php-8.0.11/php.ini-production /etc/php.ini 
	cp /usr/src/php-8.0.11/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
	chmod +x /etc/rc.d/init.d/php-fpm 
	cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
	cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf

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

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

[Install]
WantedBy=multi-user.target
EOF
	systemctl daemon-reload 
	systemctl enable --now php-fpm.service 
	echo -e "\e[1;33m php自启动 \e[0m"
}


if [ $count -eq 7 ];then
	php7	
else
	php8
fi

############################index.php配置
mkdir -p /usr/local/apache/htdocs/test
chown -R apache: /usr/local/apache/htdocs/test/
cat > /usr/local/apache/htdocs/test/index.php <<EOF
<?php
	phpinfo();
?>
EOF
cat > /etc/httpd24/extra/httpd-vhosts.conf <<EOF
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/test"
    ServerName www.clq.com
    DirectoryIndex index.php
    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

sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf 
sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf 
sed -i '/httpd-vhosts/s/#//g' /etc/httpd24/httpd.conf 
grep 'index.php' /etc/httpd24/httpd.conf  &>/dev/null
if [ $? -eq 1 ];then
	sed  -i 's/index.html/index.php index.html/g' /etc/httpd24/httpd.conf 
fi
grep 'httpd-php .php' /etc/httpd24/httpd.conf &>/dev/null
if [ $? -eq 1 ];then
	sed  -i '399iAddType application/x-httpd-php .php' /etc/httpd24/httpd.conf 
fi
grep 'httpd-php-source .phps' /etc/httpd24/httpd.conf  &>/dev/null
if [ $? -eq 1 ];then
	sed  -i '400iAddType application/x-httpd-php-source .phps' /etc/httpd24/httpd.conf 
fi

systemctl stop firewalld
systemctl restart hhtpd.service
5.php页面

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神慕蔡蔡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值