[root@SYL7 lamp]# vim install.sh
[root@SYL7 lamp]# cat install.sh
#!/bin/bashif [ $UID -ne 0 ];then
echo "Execute the script as an administrator."exit
fi
#定义变量
read-p "请输入apache安装目录的绝对路径(default:/usr/local/apache): " apache_install_dir
read-p "请输入mysql安装目录的绝对路径(default:/usr/local/mysql): " mysql_install_dir
read-p "请输入数据存放目录的绝对路径(default:/opt/data): " datadir
read-p "请输入要为数据库设置的密码(default:123456): " passwd
php_install_dir=/usr/local/php7
script_path=$(pwd)
port=3306if [ -z $datadir ];then
apache_install_dir=/usr/local/apache
fi
if [ -z $datadir ];then
mysql_install_dir=/usr/local/mysql
fi
if [ -z $datadir ];then
datadir=/opt/data
fi
if [ -z $passwd ];then
passwd=123456
fi
#安装Apache
#创建用户
id apache &>/dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin apache
else
echo "用户已存在"
fi
#安装依赖包
echo -e "\033[32mInstalling dependency packages \033[\0m\033[5;32m......\033[0m"
yum -y groups mark install "Development Tools"&&\
yum -y install openssl-devel pcre-devel expat-devel libtool make gcc gcc-c++ ncurses-devel openssl cmake mariadb-devel ncurses-compat-libs http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm libxml2 libxml2-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd sqlite-devel libzip-devel &>/dev/null &&\
sleep5
#解压软件包
rm -rf /usr/src/*
tar xf files/apr-1.7.0.tar.gz -C /usr/src
tar xf files/apr-util-1.6.1.tar.gz -C /usr/src
tar xf files/httpd-2.4.54.tar.gz -C /usr/src/
#编译安装apr
cd /usr/src/apr-1.7.0if [ ! -d /usr/local/apr ];then
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr && \
make && make install
fi
#编译安装apr-util
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
#编译安装httpd
cd ../httpd-2.4.54/if [ ! -d $apache_install_dir ];then
./configure --prefix=$apache_install_dir \
--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=$apache_install_dir/bin:\$PATH" >/etc/profile.d/apache.sh
ln -s $apache_install_dir/include /usr/include/apache &>/dev/null
grep "$apache_install_dir/man"/etc/man_db.conf &>/dev/null
if [ $? -ne 0 ];then
sed -i "/^MANDATORY.*share\/man/a MANDATORY_MANPATH\t\t\t$apache_install_dir/man" /etc/man_db.conf
fi
#编写service file
cat >/usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=httpd server daemon
After=network.target
[Service]
Type=forking
ExecStart=$apache_install_dir/bin/apachectl start
ExecStop=$apache_install_dir/bin/apachectl stop
ExecReload=/bin/kill-HUP \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
#加载文件
systemctl daemon-reload
#设置开机自启
systemctl enable --now httpd
ss -antl
echo -e "\033[32m Apache installation Complete \033[\0m"
sleep5
#安装mysql
#创建mysql用户
id mysql &>/dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin mysql
fi
export PATH=${mysql_install_dir}/bin:$PATH
#解压软件包,并修改目录及其属主
if [ ! -d $mysql_install_dir ];then
echo "Decompressing software package..."
tar xf ${script_path}/files/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local
sleep2
cd /usr/local
mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
else
echo "mysql 以修改."
fi
#修改mysql属主
chown -R mysql.mysql $mysql_install_dir
#设置环境变量
echo "export PATH=$mysql_install_dir/bin:\$PATH" >/etc/profile.d/mysql.sh
#头文件
ln -s $mysql_install_dir/include /usr/include/mysql &>/dev/null
#man文档
grep "$mysql_install_dir/man"/etc/man_db.conf &>/dev/null
if [ $? -ne 0 ];then
sed -i "/^MANDATORY.*share\/man/a MANDATORY_MANPATH\t\t\t$mysql_install_dir/man" /etc/man_db.conf
fi
#lib
echo "$mysql_install_dir/lib">/etc/ld.so.conf.d/mysql.conf
ldconfig
#判断目录存在
if [ ! -d $datadir ];thenmkdir-p $datadir
fi
#修改目录属性
chown -R mysql.mysql $datadir
echo "init mysql ......"
#初始化mysql
${mysql_install_dir}/bin/mysqld --initialize --user mysql --datadir $datadir &>/tmp/mysqlpasswd
#生成配置mysql文件
cat >/etc/my.cnf <<EOF
[mysqld]
basedir = $mysql_install_dir
datadir = $datadir
socket =/tmp/mysql.sock
port =3306
pid-file = $datadir/mysql.pid
user = mysql
skip-name-resolve
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF
if [ ! -f /etc/init.d/mysqld ];then
cp ${mysql_install_dir}/support-files/mysql.server /etc/init.d/mysqld
sed -i "/^basedir=/c basedir=$mysql_install_dir"/etc/init.d/mysqld
sed -i "/^datadir=/c datadir=$datadir"/etc/init.d/mysqld
fi
chmod +x /etc/init.d/mysqld
service mysqld start
chkconfig --add mysqld
sleep2
ss -antl
password=$(grep 'password' /tmp/mysqlpasswd |awk '{print $NF}')
${mysql_install_dir}/bin/mysql -uroot -p$password --connect-expired-password -e "set password = password('$passwd');"
echo "你的密码为: $passwd"
sleep5
#安装PHP
if [ ! -d /usr/src/php-7.4.30 ];then
echo "Decompressing software package..."
tar xf ${script_path}/files/php-7.4.30.tar.gz -C /usr/src
fi
cd /usr/src
if [ ! -d php-7.4.30 ];then
cd php-7.4.30
./configure --prefix=$php_install_dir \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--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-json \
--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=${php_install_dir}/bin:${php_install_dir}/:sbin:\$PATH" >/etc/profile.d/php7.sh
#lib文件
echo "${php_install_dir}/lib" >/etc/ld.so.conf.d/php.conf
ldconfig
sleep2
#头文件
ln -s ${php_install_dir}/include /usr/include/php &>/dev/null
#配置php-fpm
echo "$script_path"
\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
ls -l /etc/rc.d/init.d/
cd ${php_install_dir}/etc/
cp php-fpm.conf.default php-fpm.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf
#启动PHP,并设置开机自启
service php-fpm start
chkconfig --add php-fpm
ss -antl
echo "PHP安装完成."
sleep5
#配置PHP界面
cat > ${apache_install_dir}/htdocs/index.php <<EOF
<?php
phpinfo();
?>
EOF
#配置Apache
cat > ${apache_install_dir}/conf/extra/vhosts.conf <<EOF
<VirtualHost *:80>
DocumentRoot "${apache_install_dir}/htdocs"
ServerName www.example.com
ErrorLog "logs/www.example.com-error_log"
CustomLog "logs/www.example.com-access_log"common
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000${apache_install_dir}/htdocs/\$1<Directory "${apache_install_dir}/htdocs">
Options none
AllowOverride none
Require all granted
</Directory></VirtualHost>
EOF
#配置Apache主配置文件
grep 'AddType application/x-httpd-php .php' ${apache_install_dir}/conf/httpd.conf &>/dev/null
if [ $? -ne 0 ];then
sed -i '/\bAddType.*.gz .tgz/a \ AddType application/x-httpd-php .php' ${apache_install_dir}/conf/httpd.conf
fi
grep 'AddType application/x-httpd-php-source .phps' ${apache_install_dir}/conf/httpd.conf &>/dev/null
if [ $? -ne 0 ];then
sed -i '/\bAddType.*.php/a \ AddType application/x-httpd-php-source .phps' ${apache_install_dir}/conf/httpd.conf
fi
sed -i '/index.html/c \ DirectoryIndex index.php index.html' ${apache_install_dir}/conf/httpd.conf
#启动Apache相关的模块
sed -i '/proxy_module/s/#//g' ${apache_install_dir}/conf/httpd.conf
sed -i '/proxy_fcgi_module/s/#//g' ${apache_install_dir}/conf/httpd.conf
#使httpd主配置文件包含vhosts文件
grep 'Include conf/extra/vhosts.conf' ${apache_install_dir}/conf/httpd.conf &>/dev/null
if [ $? -ne 0 ];then
echo 'Include conf/extra/vhosts.conf' >> ${apache_install_dir}/conf/httpd.conf
fi
#设置权限
chown -R apache.apache $apache_install_dir
ls -l $apache_install_dir
#重启httpd,mysql和php
systemctl restart httpd
service mysqld restart
service php-fpm restart
sleep2
ss -antl
[root@SYL7 lamp]#
5. 验证
[root@SYL7 lamp]# bash
[root@SYL7 lamp]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands endwith;or \g.
Your MySQL connection id is2
Server version:5.7.38 MySQL Community Server (GPL)
Copyright (c)2000,2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.
mysql> quit
Bye
[root@SYL7 lamp]#