#!/bin/sh set -e CheckRoot() { #check if currect use is root user CUR_USER=`whoami` if [ $CUR_USER != 'root' ];then echo 'The operation will modify system files,you should be root!' exit 1 fi } MkDir() { if [ ! -d $1 ];then mkdir $1 > /dev/null fi } RollBack() { ORIG_DIR=`pwd` cd /etc/yum.repos.d > /dev/null MkDir /ect/yum.repos.d/configYum_rb mv *.repo configYum_rb/ > /dev/null mv backup/*.repo . > /dev/null rm -fr backup/ configYum_rb/ > /dev/null yum clean all > /dev/null yum makecache > /dev/null yum repolist all } LocalMount() { #read -p 'please input iso path:\n' iso_path #read -p 'please input mount path:\n' mount_dir ISO_PATH=/dev/sr0 MOUNT_DIR=$1 mkdir -p $MOUNT_DIR CURR_DATE=`date +%Y%m%d%H%M` mount -t iso9660 -o loop ${ISO_PATH} ${MOUNT_DIR} is_mount=`df -h |grep ${MOUNT_DIR}` if [ -z "${is_mount}" ] ;then echo "check mount iso or mount path" exit 2 else cp /etc/fstab /etc/fstab.${CURR_DATE}.bak echo "${ISO_PATH} ${MOUNT_DIR} iso9660 loop 0 0" >>/etc/fstab mount -a fi } HttpMount() { yum -y install httpd http-devel > /dev/null service httpd start checkconfig httpd on ISO_PATH=/dev/sr0 MOUNT_DIR=/mnt mkdir -p $MOUNT_DIR mount -t iso9660 -o loop ${ISO_PATH} ${MOUNT_DIR} cp -rp ${MOUNT_DIR}/* /var/www/html/centos6.4/x86_64/ cd /var/www/html/ && createrepo -p -d -o centos6.4/x86_64/ centos6.4/x86_64/ } Create() { echo 'Begin to create local yum source ... ...' yum install -y createrepo > /dev/null #ORIG_DIR=`pwd` #backup currect config if [ ! -d "/etc/yum.repos.d" ];then echo '/etc/yum.repos.d is not existed , please check your system!' exit 1 fi cd /etc/yum.repos.d > /dev/null MkDir /etc/yum.repos.d/backup mv ./*.repo backup/ #create local.repo if [ $2 = "local" ];then cat >> local.repo <<EOF [localrepo] name=localrepo baseurl=file://$1 gpgcheck=0 enabled=1 EOF # elif [ $2 = "http" ];then ip=`ip a | grep -i inet | grep -v 127 |awk -F" " '{print $2}' |cut -d"/" -f1` cat >> http.repo <<EOF [httprepo] name=httprepo baseurl=http://$ip/centos6.4/x86_64 gpgcheck=0 enabled=1 EOF else Help fi createrepo $1 > /dev/null if [ $? -ne 0];then echo 'create repo failed !' exit 1 fi yum clean all > /dev/null yum makecache > /dev/null yum repolist all } Help() { cat <<EOF There are 2 ways to call this script, for example: 1. To create Yum repo: ./configYum.sh yum's path [local|http|ftp] 2. To rollback previous operation: ./configYum.sh b EOF } Main() { if [ $# -ne 2 ];then Help elif [ -d $1 ] && [ "$2" = "local" ];then CheckRoot LocalMount $1 Create $* elif [ "$2" = "http" ];then CheckRoot HttpMount Create $* elif [ $1 = 'b' ];then CheckRoot RolllBack else Help fi } Main $*
2、
yum命令
安装某个软件
yum install pkg
检查某个软件的更新
yum check-update
删除某个软件
yum remove pkg
搜索某个软件
yum search pkg
清除yum缓存
yum clean all
更新所有已经安装的软件
yum update
升级系统版本
yum upgrade
rpm 命令
安装某个软件
rpm -ivh pkg
更新某个软件
rpm -Uvh pkg
删除某个软件
rpm -e pkg
列出系统中已安装的软件
rpm -qa
查询某个软件是否已安装
rpm -qa pkg
查询某个文件所属的软件包
rpm -qf /path/to/file
查询某个软件所包含的文件
rpm -ql pkg
查询某个已经安装的软件的详细信息
rpm -qi pkg
查询某个已经安装的软件的的依赖关系
rpm -qR pkg
查询某个已经安装的软件的文档目录
rpm -qd pkg
注:系统编码使用UTF-8,加载mysql与apache的环境变量 apache编译包版本为2.4.4,DocumentRoot路径/usr/local/apache2/htdocs,加载https加密通道. mysql编译包版本为5.6.13,库文件路径/usr/local/mysql/data. mysql安装初始账户密码为root/123456 php编译包版本为5.4.13,默认加载了笔者线上常用的模块 # cat install_lamp.sh ---------------------------------------------------- #!/bin/bash PATH=/bin:/usr/bin:/sbin:/usr/sbin::/usr/local/bin:/usr/local/sbin; export PATH # LAMP目录 DEFAULT_DIR=$(pwd) LOG_DIR=$(pwd)/log MYSQL_DIR=/usr/local/mysql APACHE_DIR=/usr/local/apache2 PHP_DIR=/usr/local/php5 PCRE_DIR=/usr/local/pcre . lib/check_env.sh . lib/mysql.sh . lib/apache.sh . lib/php.sh echo "Welcome to Onekey LAMP installation,select option for your choice. 1 install all service(apache + php + mysql) 2 install apache+php 3 install apache 4 install mysql 5 quit" sleep 0.1 read -p "Please Input 1,2,3,4,5: " SERVER_ID if [[ $SERVER_ID == 1 ]]; then check_env_ins mysql_ins apache_ins php_ins elif [[ $SERVER_ID == 2 ]]; then check_env_ins apache_ins php_ins elif [[ $SERVER_ID == 3 ]]; then check_env_ins apache_ins elif [[ $SERVER_ID == 4 ]]; then check_env_ins mysql_ins else exit fi ------------------------------------------------------- # cat lib/check_env.sh ---------------------------------------------------- # check the system environment function check_env_ins { local IN_LOG=$LOG_DIR/check_env_install-$(date +%F).log echo "check the system environment..." sleep 1 # 判断是否为root用户 if [ $UID != 0 ]; then echo "You must be root to run the install script." exit 0 fi # 关闭相关服务和SELINUX echo "Stop useless service..." sleep 1 chkconfig httpd off 2>/dev/null chkconfig mysql off 2>/dev/null service httpd stop 2>/dev/null service mysql stop 2>/dev/null iptables -F >> $IN_LOG 2>&1 service iptables save 2>/dev/null setenforce 0 >> $IN_LOG 2>&1 sed -i '/SELINUX/s/enforcing/disabled/g' /etc/selinux/config >> $IN_LOG 2>&1 sleep 1 # 判断能否访问公网 echo 8.8.8.8 >> /etc/resolv.conf >> $IN_LOG 2>&1 echo "Check your Networking..." NET_ALIVE=$(ping 8.8.8.8 -c 5 |grep 'received'|awk 'BEGIN {FS=","} {print $2}'|awk '{print $1}') if [ $NET_ALIVE == 0 ]; then echo "Network is not active,please check your network configuration!" exit 0 else echo "Network is active,continue.." sleep 1 fi # 安装开发包(使用默认CENTOS更新源): echo "Install the Dependency package..." sleep 1 yum -y install lsof wget gcc-c++ ncurses ncurses-devel cmake \ make perl bison openssl openssl-devel gcc* libxml2 \ libxml2-devel curl-devel libjpeg* libpng* freetype* # 同步时间 echo "synchronization time..." ntpdate tiger.sina.com.cn >> $IN_LOG 2>&1 hwclock -w sleep 1 } ---------------------------------------------------- # cat lib/apache.sh ---------------------------------------------------- # apache install function function apache_ins { local IN_LOG=$LOG_DIR/apache_install-$(date +%F).log echo "Install the Apache service..." sleep 1 # pcre依赖包安装 echo "install the dependency pcre package..." sleep 1 cd $DEFAULT_DIR/src tar -xzvf pcre-8.32.tar.gz >> $IN_LOG 2>&1 cd pcre-8.32 ./configure --prefix=$PCRE_DIR >> $IN_LOG 2>&1 make >> $IN_LOG 2>&1 make install >> $IN_LOG 2>&1 # apache安装 echo "install apache package..." sleep 1 # 注:httpd-2.4.3-deps.tar.bz2已集成APR,安装apache前检查pcre是否安装成功. cd $DEFAULT_DIR/src tar jxvf httpd-2.4.4.tar.bz2 >> $IN_LOG 2>&1 tar jxvf httpd-2.4.3-deps.tar.bz2 >> $IN_LOG 2>&1 cp -rf httpd-2.4.3/* httpd-2.4.4 cd httpd-2.4.4 ./configure --prefix=$APACHE_DIR --enable-so --enable-rewrite \ -enable-ssl=static -with-ssl=/usr/local/ssl -enable-mods-shared=all \ --with-pcre=$PCRE_DIR/bin/pcre-config >> $IN_LOG 2>&1 make >> $IN_LOG 2>&1 make install >> $IN_LOG 2>&1 # 判断目录是否创建: if [ ! -d $APACHE_DIR ];then echo "$APACHE_DIR is not exist,stop..." exit 0 fi # apache配置项: echo "configuration after the installation...." sleep 1 # 防止apache启动报错. echo "ServerName localhost:80" >> $APACHE_DIR/conf/httpd.conf # 设置开机启动 echo "$APACHE_DIR/bin/apachectl start" >> /etc/rc.local # 启动apache echo "start apache httpd service..." sleep 1 $APACHE_DIR/bin/apachectl start >> $IN_LOG 2>&1 # 设置环境变量 echo "PATH=$PATH:$APACHE_DIR/bin;export PATH" >> /etc/profile source /etc/profile # 判断服务是否启动 PORT_80=$(lsof -i:80|wc -l) if [ $PORT_80 == 0 ]; then echo "Apache httpd service is not active,please check your configure!" exit 0 else echo "Congratulation,Apache httpd service has installed correctly!" fi } ---------------------------------------------------- # cat lib/mysql.sh ---------------------------------------------------- # mysql install function function mysql_ins { local IN_LOG=$LOG_DIR/mysql_install-$(date +%F).log echo "Install the MySQL service..." sleep 1 # 安装前的初始配置工作: echo "The initial configuration before installation..." sleep 1 mkdir -p $MYSQL_DIR >> $IN_LOG 2>&1 useradd -d $MYSQL_DIR mysql >> $IN_LOG 2>&1 mkdir -p $MYSQL_DIR/data >> $IN_LOG 2>&1 mkdir -p $MYSQL_DIR/log >> $IN_LOG 2>&1 chown -R mysql:mysql $MYSQL_DIR/data/ >> $IN_LOG 2>&1 chown -R mysql:mysql $MYSQL_DIR/log/ >> $IN_LOG 2>&1 chmod 750 $MYSQL_DIR/data >> $IN_LOG 2>&1 chmod 750 $MYSQL_DIR/log >> $IN_LOG 2>&1 # 解包编译安装: echo "make install the MySQL package..." sleep 1 cd $DEFAULT_DIR cd src/ tar -zxvf mysql-5.6.13.tar.gz >> $IN_LOG 2>&1 cd mysql-5.6.13 cmake -DCMAKE_INSTALL_PREFIX=$MYSQL_DIR \ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ -DDEFAULT_CHARSET=gbk \ -DDEFAULT_COLLATION=gbk_chinese_ci \ -DEXTRA_CHARSETS=all \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DENABLED_LOCAL_INFILE=1 \ -DMYSQL_DATADIR=$MYSQL_DIR/data \ -DMYSQL_USER=mysql \ -DMYSQL_TCP_PORT=3306 \ -DSYSCONFDIR=/etc \ -DWITH_SSL=yes >> $IN_LOG 2>&1 make >> $IN_LOG 2>&1 make install >> $IN_LOG 2>&1 # 判断目录是否创建: if [ ! -d $MYSQL_DIR ];then echo "$MYSQL_DIR is not exist,stop..." exit 0 fi # mysql配置项: echo "configuration after the installation...." sleep 1 [ -e /etc/my.cnf ] && rm -rf /etc/my.cnf >> $IN_LOG 2>&1 cd $DEFAULT_DIR cp conf/my.cnf /etc/my.cnf >> $IN_LOG 2>&1 # 将mysql的库文件路径加入系统的库文件搜索路径中 ln -s $MYSQL_DIR/lib/mysql /usr/lib/mysql >> $IN_LOG 2>&1 # 输出mysql的头文件到系统头文件 ln -s $MYSQL_DIR/include/mysql /usr/include/mysql >> $IN_LOG 2>&1 # 进入安装路径,初始化配置脚本 echo "Initialize the configuration of the MySQL..." sleep 1 cd $MYSQL_DIR scripts/mysql_install_db --user=mysql --datadir=$MYSQL_DIR/data >> $IN_LOG 2>&1 # 复制mysql启动脚本到系统服务目录 cp $MYSQL_DIR/support-files/mysql.server /etc/init.d/mysqld >> $IN_LOG 2>&1 # 系统启动项相关配置 chkconfig --add mysqld >> $IN_LOG 2>&1 chkconfig --level 35 mysqld on >> $IN_LOG 2>&1 # 启动mysql service mysqld start >> $IN_LOG 2>&1 # 配置权限 echo "Configure MySQL authority..." sleep 1 $MYSQL_DIR/bin/mysqladmin -u root password 123456 >> $IN_LOG 2>&1 #给root用户非本地链接所有权限,并改密码和赋予其给其他人下发权限. $MYSQL_DIR/bin/mysql -u root -p123456 -e "grant all privileges on *.* to root@'%' identified by '123456' with grant option;" >> $IN_LOG 2>&1 $MYSQL_DIR/bin/mysql -u root -p123456 -e "grant all privileges on *.* to root@'localhost' identified by '123456' with grant option;" >> $IN_LOG 2>&1 # 设置环境变量 echo "PATH=$PATH:$MYSQL_DIR/bin;export PATH" >> /etc/profile source /etc/profile # 判断服务是否启动 PORT_3306=$(lsof -i:3306|wc -l) if [ $PORT_3306 == 0 ]; then echo "MySQL service is not active,please check your configure!" exit 0 else echo "Congratulation,MySQL service has installed correctly!" fi } ---------------------------------------------------- # cat lib/php.sh ---------------------------------------------------- # php install function function php_ins { local IN_LOG=$LOG_DIR/php_install-$(date +%F).log echo "Install the PHP module for apache..." sleep 1 cd $DEFAULT_DIR/src tar -jxvf php-5.4.13.tar.bz2 >> $IN_LOG 2>&1 cd php-5.4.13 # 此处编译安装了我们项目经常用到的PHP模块,如有其它需要可以自定义添加. ./configure --prefix=$PHP_DIR --with-apxs2=$APACHE_DIR/bin/apxs \ --with-libxml-dir=/usr/local/lib --with-zlib-dir=/usr/local/lib \ --with-mysql=$MYSQL_DIR --with-mysqli=$MYSQL_DIR/bin/mysql_config \ --with-gd --enable-soap --enable-sockets --enable-xml --enable-mbstring \ --with-png-dir=/usr/local --with-jpeg-dir=/usr/local --with-curl=/usr/lib \ --with-freetype-dir=/usr/include/freetype2/freetype/ --enable-bcmath \ --enable-calendar --enable-zip --enable-maintainer-zts >> $IN_LOG 2>&1 make >> $IN_LOG 2>&1 make install >> $IN_LOG 2>&1 # 判断目录是否创建: if [ ! -d $PHP_DIR ];then echo "$PHP_DIR is not exist,stop..." exit 0 fi # PHP配置项: echo "configuration after the installation...." sleep 1 cp php.ini-development $PHP_DIR/lib/php.ini echo "AddType application/x-httpd-php .php" >> $APACHE_DIR/conf/httpd.conf sed -i '/DirectoryIndex index.html/s/$/ &index.php/g' $APACHE_DIR/conf/httpd.conf # 隐藏PHP版本信息: # echo "expose_php = Off" >> $PHP_DIR/lib/php.ini # 关闭警告及错误信息,爆路径: echo "display_errors = Off" >> $PHP_DIR/lib/php.ini # 调整时区,防止phpinfo()函数报错. echo "date.timezone =PRC" >> $PHP_DIR/lib/php.ini # 开启php错误日志并设置路径. echo "log_errors = On" >> $PHP_DIR/lib/php.ini echo "error_log = $APACHE_DIR/logs/php_error.log" >> $PHP_DIR/lib/php.ini cd $DEFAULT_DIR cp conf/info.php $APACHE_DIR/htdocs >> $IN_LOG 2>&1 # 重启apache: echo "restart apache to load the php module..." $APACHE_DIR/bin/apachectl stop >> $IN_LOG 2>&1 $APACHE_DIR/bin/apachectl start >> $IN_LOG 2>&1 # 判断PHP是否加载: PHP_LOAD=$(curl --head http://localhost/info.php |grep PHP |wc -l) if [ $PHP_LOAD == 0 ]; then echo "PHP does not load,please check your configure!" exit 0 else echo "Congratulation,PHP module has installed correctly!" fi }
原创文章,作者:未闻花名,如若转载,请注明出处:http://www.178linux.com/49565