阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

分析安装包LNMP中的apache.sh脚本

175次阅读
没有评论

共计 9880 个字符,预计需要花费 25 分钟才能阅读完成。

分析 shell 脚本,学习方法,这个其实是在 lnmp 环境下再安装 apache, 实现 lnmpa 环境。

#!/bin/bash
# 定义 PATH 变量
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
# 声明 PATH 为环境变量
export PATH
# Check if user is root
#id -u 当前用户的 uid,也可以使用反引号 `id -u`,效果一样,0 为 root 的用户 id
if [$(id -u) != “0” ]; then
    echo “Error: You must be root to run this script, please use root to install lnmp”
    exit 1
fi
# 清屏操作
clear
# 可以识别特殊字符
printf “=======================================================================\n”
printf “Install Apache for LNMP V1.0  ,  Written by Licess \n”
printf “=======================================================================\n”
printf “LNMP is a tool to auto-compile & install Nginx+MySQL+PHP on Linux \n”
printf “This script is a tool to install Apache for lnmp \n”
printf “\n”
printf “For more information please visit http://www.lnmp.org \n”
printf “=======================================================================\n”
# 把当前路径给变量 cur_dir
cur_dir=$(pwd)
# 这个重点说一下,察看 ip,
# 通过 1 次 grep, 过滤掉不包含 inet 的行
#2 次反向 grep 也就是取不包含的行,过滤掉包含 127.0.0.1 和 inet6 的行
#awk 打印第 2 列
#tr -d 去掉 addr:,这样就只剩下 ip
ipv4=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk ‘{print $2}’|tr -d “addr:”`
#set Server Administrator Email Address
    ServerAdmin=””
        #读取用户键盘输入还可以使用 -t 30 指定时间限制
    read -p “Please input Administrator Email Address:” ServerAdmin
    if [“$ServerAdmin” == “”]; then
        echo “Administrator Email Address will set to webmaster@example.com!”
        ServerAdmin=”webmaster@example.com”
    else
    echo “===========================”
    echo Server Administrator Email=”$ServerAdmin”
    echo “===========================”
    fi
        #定义函数
    get_char()
    {
        #打印当前终端的设置赋予变量
    SAVEDSTTY=`stty -g`
        #echo input characters
    stty -echo
    stty cbreak
        #把指定的输入文件拷贝到指定的输出文件中,并且在拷贝的过程中可以进行格式转换
        #if = 输入文件,bs = bytes 同时设置读 / 写缓冲区的字节数 (等于设置 obs 和 ibs),count = blocks 只拷贝输入的 blocks 块
    dd if=/dev/tty bs=1 count=1 2> /dev/null
    stty -raw
    stty echo
    stty $SAVEDSTTY
    }
    echo “”
    echo “Press any key to start install Apache for LNMP or Press Ctrl+C to cancel…”
    char=`get_char`
printf “===================== Check And Download Files =================\n”
#test 测试 如果文件的长度不为零,则为真
if [-s httpd-2.2.22.tar.gz]; then
  echo “httpd-2.2.22.tar.gz [found]”
  else
  echo “Error: httpd-2.2.22.tar.gz not found!!!download now……”
  wget -c http://soft.vpser.net/web/apache/httpd-2.2.22.tar.gz
fi
#mod_rpaf 是 apache 的一个模块,目的是为了获取访客的真实 IP
if [-s mod_rpaf-0.6.tar.gz]; then
  echo “mod_rpaf-0.6.tar.gz [found]”
  else
  echo “Error: mod_rpaf-0.6.tar.gz not found!!!download now……”
  wget -c http://soft.vpser.net/web/apache/rpaf/mod_rpaf-0.6.tar.gz
fi
#php 源码
if [-s php-5.2.17.tar.gz]; then
  echo “php-5.2.17.tar.gz [found]”
  else
  echo “Error: php-5.2.17.tar.gz not found!!!download now……”
  wget -c http://soft.vpser.net/web/php/php-5.2.17.tar.gz
fi
printf “=========================== install Apache ======================\n”
# 停止当前的其他服务
echo “Stoping Nginx…”
/etc/init.d/nginx stop
echo “Stoping MySQL…”
/etc/init.d/mysql stop
echo “Stoping PHP-FPM…”
/etc/init.d/php-fpm stop
if [-s /etc/init.d/memceached]; then
  echo “Stoping Memcached…”
  /etc/init.d/memcacehd stop
fi
# 备份
echo “Backup old php configure files…..”
mkdir /root/lnmpbackup/
cp /root/lnmp /root/lnmpbackup/
cp /usr/local/php/etc/php.ini /root/lnmpbackup/
cp /usr/local/php/etc/php-fpm.conf /root/lnmpbackup/
# 开始编译安装
cd $cur_dir
rm -rf httpd-2.2.22/
tar zxvf httpd-2.2.22.tar.gz
cd httpd-2.2.22/
./configure –prefix=/usr/local/apache –enable-headers –enable-mime-magic –enable-proxy –enable-so –enable-rewrite –enable-ssl –enable-deflate –enable-suexec –disable-userdir –with-included-apr –with-mpm=prefork –with-ssl=/usr –disable-userdir –disable-cgid –disable-cgi –with-expat=builtin
make && make install
cd ..
mv /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.bak
#\cp 不走 alias 的 cp
\cp $cur_dir/conf/httpd.conf /usr/local/apache/conf/httpd.conf
\cp $cur_dir/conf/httpd-default.conf /usr/local/apache/conf/extra/httpd-default.conf
\cp $cur_dir/conf/httpd-vhosts.conf /usr/local/apache/conf/extra/httpd-vhosts.conf
\cp $cur_dir/conf/httpd-mpm.conf /usr/local/apache/conf/extra/httpd-mpm.conf
\cp $cur_dir/conf/rpaf.conf /usr/local/apache/conf/extra/rpaf.conf
#sed 实现内容替换
sed -i ‘s/#ServerName www.example.com:80/ServerName www.lnmp.org:88/g’ /usr/local/apache/conf/httpd.conf
sed -i ‘s/ServerAdmin you@example.com/ServerAdmin ‘$ServerAdmin’/g’ /usr/local/apache/conf/httpd.conf
#sed -i ‘s/www.lnmp.org/’$domain’/g’ /usr/local/apache/conf/extra/httpd-vhosts.conf
sed -i ‘s/webmaster@example.com/’$ServerAdmin’/g’ /usr/local/apache/conf/extra/httpd-vhosts.conf
mkdir -p /usr/local/apache/conf/vhost
# 向 /usr/local/apache/conf/httpd.conf 中末尾追加内容 Include conf/vhost/*.conf
cat >>/usr/local/apache/conf/httpd.conf<<EOF
Include conf/vhost/*.conf
EOF
# 安装 rpaf
tar -zxvf mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6/
/usr/local/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
cd ..
# 创建软链
ln -s /usr/local/lib/libltdl.so.3 /usr/lib/libltdl.so.3
#sed -i ‘s#your_ips#’$ipv4’#g’ /usr/local/apache/conf/extra/rpaf.conf
echo “Stop php-fpm…..”
rm -rf /usr/local/php/
cd $cur_dir
if [-s php-5.2.17]; then
rm -rf php-5.2.17
fi
tar zxvf php-5.2.17.tar.gz
cd php-5.2.17/
wget -c http://soft.vpser.net/web/php/bug/php-5.2.17-max-input-vars.patch
# 打一个补丁这里需要了解 diff 和 patch
patch -p1 < php-5.2.17-max-input-vars.patch
./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/php/etc –with-apxs2=/usr/local/apache/bin/apxs –with-mysql=/usr/local/mysql –with-mysqli=/usr/local/mysql/bin/mysql_config –with-iconv-dir –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –disable-rpath –enable-discard-path –enable-magic-quotes –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-mbstring –with-mcrypt –enable-ftp –with-gd –enable-gd-native-ttf –with-openssl –with-mhash –enable-pcntl –enable-sockets –with-xmlrpc –enable-zip –enable-soap –without-pear –with-gettext –with-mime-magic
if cat /etc/issue | grep -Eqi ‘(Debian|Ubuntu)’;then
    cd ext/openssl/
    wget -c http://soft.vpser.net/lnmp/ext/debian_patches_disable_SSLv2_for_openssl_1_0_0.patch
    patch -p3 <debian_patches_disable_SSLv2_for_openssl_1_0_0.patch
    cd ../../
fi
rm -rf libtool
cp /usr/local/apache/build/libtool .
make ZEND_EXTRA_LIBS=’-liconv’
make install
mkdir -p /usr/local/php/etc
cp php.ini-dist /usr/local/php/etc/php.ini
cd ../
# 安装 PDO 扩展
cd $cur_dir/php-5.2.17/ext/pdo_mysql/
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config –with-pdo-mysql=/usr/local/mysql
make && make install
cd $cur_dir/
# 添加 php extensions
sed -i ‘s#extension_dir = “./”#extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/”\nextension = “memcache.so”\nextension = “pdo_mysql.so”\n#’ /usr/local/php/etc/php.ini
sed -i ‘s#output_buffering = Off#output_buffering = On#’ /usr/local/php/etc/php.ini
sed -i ‘s/post_max_size = 8M/post_max_size = 50M/g’ /usr/local/php/etc/php.ini
sed -i ‘s/upload_max_filesize = 2M/upload_max_filesize = 50M/g’ /usr/local/php/etc/php.ini
sed -i ‘s/;date.timezone =/date.timezone = PRC/g’ /usr/local/php/etc/php.ini
sed -i ‘s/short_open_tag = Off/short_open_tag = On/g’ /usr/local/php/etc/php.ini
sed -i ‘s/; cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g’ /usr/local/php/etc/php.ini
sed -i ‘s/max_execution_time = 30/max_execution_time = 300/g’ /usr/local/php/etc/php.ini
sed -i ‘s/disable_functions =.*/disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,fsockopen/g’ /usr/local/php/etc/php.ini
# 根据系统安装 ZendOptimizer
if [`getconf WORD_BIT` = ’32’] && [`getconf LONG_BIT` = ’64’] ; then
        wget -c http://soft.vpser.net/web/zend/ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz
        tar zxvf ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz
    mkdir -p /usr/local/zend/
    cp ZendOptimizer-3.3.9-linux-glibc23-x86_64/data/5_2_x_comp/ZendOptimizer.so /usr/local/zend/
else
        wget -c http://soft.vpser.net/web/zend/ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
    tar zxvf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
    mkdir -p /usr/local/zend/
    cp ZendOptimizer-3.3.9-linux-glibc23-i386/data/5_2_x_comp/ZendOptimizer.so /usr/local/zend/
fi
# 给 php.ini 追加内容
cat >>/usr/local/php/etc/php.ini<<EOF
;eaccelerator
;ionCube
[Zend Optimizer]
zend_optimizer.optimization_level=1
zend_extension=”/usr/local/zend/ZendOptimizer.so”
EOF
cd $cur_dir
cp conf/proxy.conf /usr/local/nginx/conf/proxy.conf
mv /usr/local/nginx/conf/nginx.conf /root/lnmpbackup/
cp conf/nginx_a.conf /usr/local/nginx/conf/nginx.conf
# 添加 apache 自动启动脚本
echo “Download new Apache init.d file……”
wget -c http://soft.vpser.net/lnmp/ext/init.d.httpd
cp init.d.httpd /etc/init.d/httpd
chmod +x /etc/init.d/httpd
echo “Test Nginx configure files…”
/usr/local/nginx/bin/nginx -t
echo “ReStarting Nginx……”
/etc/init.d/nginx restart
echo “Starting Apache…..”
/etc/init.d/httpd restart
# 更新系统启动项
echo “Remove old startup files and Add new startup file…..”
if cat /etc/issue | grep -Eqi ‘(Debian|Ubuntu)’;then
    update-rc.d -f httpd defaults
    update-rc.d -f php-fpm remove
else
    sed -i ‘/php-fpm/’d /etc/rc.local
    chkconfig –level 345 php-fpm off
    chkconfig –level 345 httpd on
fi
cd $cur_dir
rm -f /etc/init.d/php-fpm
mv /root/vhost.sh /root/lnmp.vhost.sh
cp vhost_lnmpa.sh /root/vhost.sh
chmod +x /root/vhost.sh
cp lnmpa /root/
chmod +x /root/lnmpa
printf “====================== Upgrade to LNMPA completed =====================\n”
printf “You have successfully upgrade from lnmp to lnmpa,enjoy it!\n”
printf “=======================================================================\n”
printf “LNMP is a tool to auto-compile & install Nginx+MySQL+PHP on Linux \n”
printf “This script is a tool to upgrade from lnmp to lnmpa \n”
printf “\n”
printf “For more information please visit http://www.lnmp.org \n”
printf “=======================================================================\n”

相关阅读:

Ubuntu 13.04 安装 LAMP\Vsftpd\Webmin\phpMyAdmin 服务及设置 http://www.linuxidc.com/Linux/2013-06/86250.htm

CentOS 6.4 下的 LNMP 生产环境搭建及安装脚本 http://www.linuxidc.com/Linux/2013-11/92428.htm

生产环境实用之 LNMP 架构的编译安装 +SSL 加密实现 http://www.linuxidc.com/Linux/2013-05/85099.htm

LNMP 全功能编译安装 for CentOS 6.3 笔记 http://www.linuxidc.com/Linux/2013-05/83788.htm

CentOS 6.3 安装 LNMP (PHP 5.4,MyySQL5.6) http://www.linuxidc.com/Linux/2013-04/82069.htm

在部署 LNMP 的时候遇到 Nginx 启动失败的 2 个问题 http://www.linuxidc.com/Linux/2013-03/81120.htm

Ubuntu 安装 Nginx php5-fpm MySQL(LNMP 环境搭建) http://www.linuxidc.com/Linux/2012-10/72458.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计9880字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中