脚本安装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" execstop="${install_dir}/bin/apachectl" stop execreload="/bin/kill" -hup \$mainpid [install] wantedby="multi-user.target" eof #加载文件并设置开机自启 systemctl daemon-reload enable --now httpd #查看端口 ss -antl #安装mysql #安装依赖包 yum install -y libxml2-devel --allowerasing sqlite-devel bzip2 bzip2-devel libcurl-devel.x86_64 gd-devel config-manager --set-enabled powertools oniguruma oniguruma-devel readline-devel #定义变量 passwd="123456" #创建用户 id mysql &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin mysql
else
    echo "&#x7528;&#x6237;&#x5DF2;&#x5B58;&#x5728;"
fi
#&#x89E3;&#x538B;&#x8F6F;&#x4EF6;&#x5305;,&#x4FEE;&#x6539;&#x76EE;&#x5F55;&#x548C;&#x6240;&#x5C5E;&#x7EC4;
if [ ! -d /usr/local/mysql ];then
    echo "&#x89E3;&#x538B;&#x8F6F;&#x4EF6;&#x5305;"
    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
#&#x8BBE;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;
echo 'export PATH=/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
#&#x505A;&#x5934;&#x6587;&#x4EF6;
ln -s /usr/local/mysql/include /usr/include/mysql
#&#x914D;&#x7F6E;lib
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig
#&#x8BBE;&#x7F6E;man&#x6587;&#x6863;
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
#&#x5EFA;&#x7ACB;&#x6570;&#x636E;&#x5B58;&#x653E;&#x76EE;&#x5F55;
if [ -d /opt/xbz ];then
    mkdir -p /opt/xbz
fi
chown -R mysql.mysql /opt/xbz
#&#x683C;&#x5F0F;&#x5316;&#x6570;&#x636E;
/usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/xbz &> /tmp/passwd
password=$(grep 'password' /tmp/passwd |awk '{print $NF}')
#&#x751F;&#x6210;&#x6570;&#x636E;&#x914D;&#x7F6E;&#x6587;&#x4EF6;
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
#&#x914D;&#x7F6E;&#x670D;&#x52A1;&#x542F;&#x52A8;&#x811A;&#x672C;
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
#&#x542F;&#x52A8;mysql&#x5E76;&#x8BBE;&#x7F6E;&#x5F00;&#x673A;&#x81EA;&#x542F;
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 "&#x6570;&#x636E;&#x5E93;&#x7684;&#x5BC6;&#x7801;&#x662F;: $passwd"
#&#x67E5;&#x770B;&#x7AEF;&#x53E3;
ss -antl

#&#x5B89;&#x88C5;php
#&#x79FB;&#x9664;&#x7CFB;&#x7EDF;&#x4E2D;&#x7684;libzip&#x5305;
yum remove libzip -y
#&#x5B9A;&#x4E49;php&#x53D8;&#x91CF;
php_install_dir=/usr/local/php7
#&#x89E3;&#x538B;&#x8F6F;&#x4EF6;&#x5305;
if [ ! -d /usr/src/php-7.4.29 ];then
    echo "&#x6B63;&#x5728;&#x89E3;&#x538B;"
    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&#x5DF2;&#x89E3;&#x538B;"
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&#x5DF2;&#x89E3;&#x538B;"
fi
#&#x914D;&#x7F6E;&#x8BA9;&#x7CFB;&#x7EDF;&#x8BC6;&#x522B;libzip&#x5305;&#x7684;&#x73AF;&#x5883;
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&#x5DF2;&#x5B89;&#x88C5;"
fi
#&#x7F16;&#x8BD1;&#x5B89;&#x88C5;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&#x7F16;&#x8BD1;&#x5B89;&#x88C5;&#x5B8C;&#x6210;"
fi
#&#x8BBE;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;
echo "export PATH=${php_install_dir}/bin:${php_install_dir}/:sbin:\$PATH" > /etc/profile.d/php7.sh
#lib&#x5E93;&#x6587;&#x4EF6;
echo "${php_install_dir}/lib" > /etc/ld.so.conf.d/php.conf
ldconfig
sleep 2
#&#x505A;&#x5934;&#x6587;&#x4EF6;
ln -s ${php_install_dir}/include /usr/include/php &> /dev/null
#&#x914D;&#x7F6E;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
#&#x542F;&#x52A8;PHP,&#x5E76;&#x8BBE;&#x7F6E;&#x5F00;&#x673A;&#x81EA;&#x542F;
service php-fpm start
chkconfig --add php-fpm
echo "PHP&#x5B89;&#x88C5;&#x5B8C;&#x6210;."
sleep 3
#&#x67E5;&#x770B;&#x7AEF;&#x53E3;
ss -antl

#&#x914D;&#x7F6E;PHP&#x754C;&#x9762;
cat > ${install_dir}/htdocs/index.php <<eof <?php phpinfo(); ?>
EOF

#&#x914D;&#x7F6E;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>

EOF
#&#x914D;&#x7F6E;Apache&#x4E3B;&#x914D;&#x7F6E;&#x6587;&#x4EF6;
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

#&#x542F;&#x52A8;Apache&#x76F8;&#x5173;&#x7684;&#x6A21;&#x5757;
sed -i '/proxy_module/s/#//g' ${install_dir}/conf/httpd.conf
sed -i '/proxy_fcgi_module/s/#//g' ${install_dir}/conf/httpd.conf
#&#x914D;&#x7F6E;httpd&#x4E3B;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x5305;&#x542B;vhosts&#x6587;&#x4EF6;
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
#&#x8BBE;&#x7F6E;&#x5176;&#x6240;&#x5C5E;&#x4E3B;&#x7EC4;
chown -R apache.apache ${install_dir}
#&#x91CD;&#x542F;httpd,mysql&#x548C;php
systemctl restart httpd
service mysqld restart
service php-fpm restart
sleep 3
#&#x67E5;&#x770B;&#x7AEF;&#x53E3;
ss -antl
#&#x6DFB;&#x52A0;&#x9632;&#x706B;&#x5899;&#x89C4;&#x5219;
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
</eof></eof></eof>

访问:

脚本安装lamp

Original: https://www.cnblogs.com/tushanbu/p/16719392.html
Author: 涂山布
Title: 脚本安装lamp

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/573950/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球