linux shell每天一阅 -- 安装nginx以及apache

时间:2023-03-10 00:39:58
linux shell每天一阅 -- 安装nginx以及apache

当然这个博客原代码是转载大神的。。。

自动安装Nginx脚本,采用case方式,选择方式,也可以根据实际需求改成自己想要的脚本mynginx.sh

  1. #!/bin/sh
  2. ###nginx install shell
  3. ###wugk 2012-07-14
  4. ###PATH DEFINE
  5. SOFT_PATH=/data/soft/
  6. NGINX_FILE=nginx-1.2.0.tar.gz
  7. DOWN_PATH=http://nginx.org/download/
  8. if[ $UID -ne 0 ];then
  9. echo This script must use administrator or root user ,please exit!
  10. sleep 2
  11. exit 0
  12. fi
  13. if[ ! -d $SOFT_PATH ];then
  14. mkdir -p $SOFT_PATH
  15. fi
  16. download ()
  17. {
  18. cd $SOFT_PATH ;wget $DOWN_PATH/$NGINX_FILE
  19. }
  20. install ()
  21. {
  22. yum install pcre-devel -y
  23. cd $SOFT_PATH ;tar xzf $NGINX_FILE ;cd nginx-1.2.0/ &&./configure –prefix=/usr/local/nginx/ –with-http_stub_status_module –with-http_ssl_module
  24. [ $? -eq 0 ]&&make &&make install   #[ ]条件判断用的可以
  25. }
  26. start ()
  27. {
  28. lsof -i :80[ $? -ne 0 ]&&/usr/local/nginx/sbin/nginx   #lsof -i :80 //显示所有打开80端口的进程
  29. }
  30. stop ()
  31. {
  32. ps -ef |grep nginx |grep -v grep |awk ‘{print $2}’|xargs kill -9  #本文最nice的一个就是用了xargs这个命令,将上一个命令的输出作为下一个命令的参数
  33. }
  34. exit ()
  35. {
  36. echo $? ;exit
  37. }
  38. ###case menu #####
  39. case $1 in
  40. download )
  41. download
  42. ;;
  43. install )
  44. install
  45. ;;
  46. start )
  47. start
  48. ;;
  49. stop )
  50. stop
  51. ;;
  52. * )
  53. echo “USAGE:$0 {download or install or start or stop}”
  54. exit
  55. esac

脚本执行:

./mynginx.sh download

./mynginx.sh install

./mynginx.sh start

./mynginx.sh stop

下文为apache shell脚本: 1 #!/bin/sh 2

 #by z.jason --
#using install apache if [ $UID -ne ]; then
echo " you must are root or administrator,please exit ! "
sleep
exit
fi yum_download ()
{
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo # update yum
} install ()
{
yum -y install httpd
} start ()
{
lsof -i : if [ $? -ne ];then service httpd start else echo "apache is running !" fi
#当然可以一句写成 lsof -i :80;[ $? -ne 0 ]&&service httpd start||echo "apache is running ! "就是如果lsof没有输出就会返回1,也就是失败的意思。
} stop ()
{ ps -ef |grep httpd |grep -v grep |awk ‘{print $}’|xargs kill -
#service httpd stop
} restart ()
{
servcie httpd restart
} case $ in
yum_download )
yum_download
;; install )
install
;; start )
start
;; stop )
stop
;; restart )
restart
;; * )
echo "please input yum_download or install or start or stop or restart"
esac

脚本执行:

./myapache.sh yum_download

./myapache.sh install

./myapache.sh start

./myapache.sh stop

其实两个代码的区别还是有的,前者使用源码安装,而后者使用yum安装,个人比较喜欢用yum,因为yum简单而且不用去处理软件的依赖关系。