脚本安装lamp

脚本安装lamp

[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  --allowerasing && \
dnf groups mark install 'Development Tools' -y --allowerasing
dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ wget vim make  --allowerasing
dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel --allowerasing
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 --allowerasing
#解压源码包
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 <<EOF
[Unit]
Description=httpd server daemon
After=network.target

[Service]
Type=forking
ExecStart=${install_dir}/bin/apachectl start
ExecStop=${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

#安装mysql
#安装依赖包
yum install -y libxml2-devel --allowerasing 
yum install -y sqlite-devel  --allowerasing
yum install bzip2 bzip2-devel -y --allowerasing
yum install -y libcurl-devel.x86_64  --allowerasing
yum install -y gd-devel --allowerasing
yum config-manager --set-enabled PowerTools --allowerasing
yum -y install oniguruma oniguruma-devel  --allowerasing
yum -y install readline-devel --allowerasing
#定义变量
passwd=123456
#创建用户
id mysql &> /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 << EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/xbz
socket = /tmp/mysql.sock 
port = 3306 
pid-file = /opt/xbz/mysql.pid 
user = mysql 
skip-name-resolve 
EOF
#配置服务启动脚本
if [ ! -d /etc/init.d ];then
    mkdir -p /etc/init.d
fi
cp  -a   /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
sed -ri 's#^(datadir=).*#\1/opt/xbz#g' /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
#启动mysql并设置开机自启
service mysqld start
sleep 6
chkconfig --add mysqld
/usr/local/mysql/bin/mysql -uroot -p"$password" --connect-expired-password -e "set password = password('$passwd');"
echo "数据库的密码是: $passwd"
#查看端口
ss -antl

#安装php
#移除系统中的libzip包
yum remove libzip -y
#定义php变量
php_install_dir=/usr/local/php7
#解压软件包
if [ ! -d /usr/src/php-7.4.29 ];then
    echo "正在解压"
    tar xf ${script_path}/files/php-7.4.29.tar.gz -C /usr/src
else
    ls /usr/src/php-7.4.29
    sleep
    echo "php已解压"
fi
if [ ! -d /usr/src/libzip-1.3.2 ];then
    tar xf ${script_path}/files/libzip-1.3.2.tar.gz -C /usr/src
else
    ls /usr/src/libzip-1.3.2
    sleep
    echo "libzip已解压"
fi
#配置让系统识别libzip包的环境
if [ ! -d libzip-1.3.2 ];then
    cd /usr/src/libzip-1.3.2
    ./configure && make && make install
    export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
else
    echo "libzip已安装"
fi    
#编译安装php
if [ ! -d php-7.4.29 ];then
    cd /usr/src/php-7.4.29
    ./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
else
    ls $php_install_dir
    sleep 3
    echo "PHP编译安装完成"
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
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
<?php
    phpinfo();
?>
EOF

#配置Apache
cat > ${install_dir}/conf/extra/vhosts.conf <<EOF
<VirtualHost *:80>
    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
    <Directory "${install_dir}/htdocs">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>
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

访问:

作者:涂山布 原文地址:https://www.cnblogs.com/tushanbu/p/16719392.html

%s 个评论

要回复文章请先登录注册