[root@localhost ~]# mkdir lamp
[root@localhost ~]# cd lamp/
[root@localhost lamp]# mkdir files
[root@localhost lamp]# ls
files
[root@localhost lamp]# touch lamp_install.sh
[root@localhost lamp]# chmod +x lamp_install.sh
[root@localhost lamp]# ls
files lamp_install.sh
[root@localhost lamp]# ls files/
apr-1.7.0.tar.gz httpd-2.4.54.tar.gz mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
apr-util-1.6.1.tar.gz libzip-1.3.2.tar.gz php-7.4.29.tar.gz
[root@localhost lamp]# vim lamp_install.sh
[root@localhost lamp]# cat lamp_install.sh
#!/bin/bash
#设置执行权限
if [ $UID -ne 0 ];then
echo "请以管理员用户进行执行"
exit
fi
#打印当前绝对路径
script_path=$(pwd)
port=3306
#安装apache
#定义变量
apache_version=2.4.54
install_dir=/usr/local/apache
#创建用户
id apache &> /dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin apache
else
echo "用户已存在"
fi
#安装依赖包
yum -y install epel-release &&
dnf groups mark install 'Development Tools' -y
dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ wget vim make
dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
dnf -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 ncurses-compat-libs libsqlite3x-devel libzip-devel perl
#解压源码包
tar xf files/apr-1.7.0.tar.gz -C /tmp/
tar xf files/apr-util-1.6.1.tar.gz -C /tmp/
tar xf files/httpd-$apache_version.tar.gz -C /tmp/
#编译安装apr
cd /tmp/apr-1.7.0
if [ ! -d /usr/local/apr ];then
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr &&
make && make install
else
ls /usr/local
echo "apr 编译安装完成"
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
else
ls /usr/local/
echo "apr-util 编译安装完成"
fi
#编译安装httpd
cd ../httpd-$apache_version/
if [ ! -d ${install_dir} ];then
./configure --prefix=${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
else
ls ${install_dir}
echo "httpd 编译安装完成"
fi
#设置环境变量,man文档,头文件
echo "export PATH=${install_dir}/bin:$PATH" > /etc/profile.d/apache.sh
ln -s ${install_dir}/include /usr/include/apache &> /dev/null
grep 'apache' /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
sed -i "22a MANDATORY_MANPATH ${install_dir}/man" /etc/man_db.conf
fi
#将其加入systemd服务里面
cat > /usr/lib/systemd/system/httpd.service /dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin mysql
else
echo "用户已存在"
fi
#解压软件包,修改目录和所属组
if [ ! -d /usr/local/mysql ];then
echo "解压软件包"
tar xf ${script_path}/files/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local
cd /usr/local
mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
fi
chown -R mysql.mysql /usr/local/mysql
#设置环境变量
echo 'export PATH=/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
#做头文件
ln -s /usr/local/mysql/include /usr/include/mysql
#配置lib
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig
#设置man文档
grep '/usr/local/mysql/man' /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
sed -i "22a MANDATORY_MANPATH /usr/local/mysql/man" /etc/man_db.conf
fi
#建立数据存放目录
if [ -d /opt/xbz ];then
mkdir -p /opt/xbz
fi
chown -R mysql.mysql /opt/xbz
#格式化数据
/usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/xbz &> /tmp/passwd
password=$(grep 'password' /tmp/passwd |awk '{print $NF}')
#生成数据配置文件
cat > /etc/my.cnf /etc/profile.d/php7.sh
#lib库文件
echo "${php_install_dir}/lib" > /etc/ld.so.conf.d/php.conf
ldconfig
sleep 2
#做头文件
ln -s ${php_install_dir}/include /usr/include/php &> /dev/null
#配置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 ${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
echo "PHP安装完成."
sleep 3
#查看端口
ss -antl
#配置PHP界面
cat > ${install_dir}/htdocs/index.php
EOF
#配置Apache
cat > ${install_dir}/conf/extra/vhosts.conf
DocumentRoot "${install_dir}/htdocs"
ServerName xbz.example.com
ErrorLog "logs/xbz.example.com-error_log"
CustomLog "logs/xbz.example.com-access_log" common
ProxyRequests Off
ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000${install_dir}/htdocs/$1
Options none
AllowOverride none
Require all granted
EOF
#配置Apache主配置文件
grep 'AddType application/x-httpd-php .php' ${install_dir}/conf/httpd.conf &> /dev/null
if [ $? -ne 0 ];then
sed -i '/bAddType.*.gz .tgz/a AddType application/x-httpd-php .php' ${install_dir}/conf/httpd.conf
fi
grep 'AddType application/x-httpd-php-source .phps' ${install_dir}/conf/httpd.conf &> /dev/null
if [ $? -ne 0 ];then
sed -i '/bAddType.*.php/a AddType application/x-httpd-php-source .phps' ${install_dir}/conf/httpd.conf
fi
sed -i '/index.html/c DirectoryIndex index.php index.html' ${install_dir}/conf/httpd.conf
#启动Apache相关的模块
sed -i '/proxy_module/s/#//g' ${install_dir}/conf/httpd.conf
sed -i '/proxy_fcgi_module/s/#//g' ${install_dir}/conf/httpd.conf
#配置httpd主配置文件包含vhosts文件
grep 'Include conf/extra/vhosts.conf' ${install_dir}/conf/httpd.conf &> /dev/null
if [ $? -ne 0 ];then
echo 'Include conf/extra/vhosts.conf' >> ${install_dir}/conf/httpd.conf
fi
#设置其所属主组
chown -R apache.apache ${install_dir}
#重启httpd,mysql和php
systemctl restart httpd
service mysqld restart
service php-fpm restart
sleep 3
#查看端口
ss -antl
#添加防火墙规则
firewall-cmd --add-rich-rule 'rule family=ipv4 source address=0.0.0.0/0 port port=80 protocol=tcp accept' --permanent &> /dev/null
firewall-cmd --reload
访问:
参与评论
手机查看
返回顶部