美文网首页
环境变量&自启进程

环境变量&自启进程

作者: lemon_jun | 来源:发表于2018-11-05 14:12 被阅读0次

    添加环境变量

    1、临时生效,直接运行下面程序:

    export PATH=$PATH:/usr/local/webserver/php/bin //路径为安装PHP的bin目录
    

    2、仅对当前登录用户有效

    vim ~/.bash_profile
    将/usr/local/webserver/php/bin/ 等加入到PATH=$PATH:$HOME/bin之后
    例:PATH=$PATH:$HOME/bin:/usr/local/webserver/php/bin:/usr/local/webserver/mysql
    

    3、对所有系统用户,永久生效

    修改 /etc/profile 文件
    在文件末尾添加
    PATH=$PATH:/usr/local/webserver/php/bin:/usr/local/webserver/mysql
    exprot PATH
    保存后退出(:wq)
    运行 source ./profile 即可
    运行 echo $PATH 可查看是否添加成功
    

    服务器重启,进程自动重启

    使用service命令进行php-fpm启停控制

    1、进入目录 cd /etc/init.d/
    2、编辑脚本 vim php-fpm
    3、保存(:wq) 后赋文件权限 chmod 755 ./php-fpm
    4、测试:service php-fpm stop/start 停止/启动
    将以下代码复制放入/etc/init.d/php-fpm文件中

    #!/bin/sh
    # chkconfig: 2345 10 95
    # description: PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation \
    
    
    # Source function library. 
    . /etc/rc.d/init.d/functions  
     
    # Source networking configuration.  
    . /etc/sysconfig/network  
     
    # Check that networking is up.  
    [ "$NETWORKING" = "no" ] && exit 0  
     
    phpfpm="/usr/local/webserver/php/sbin/php-fpm"
    prog=$(basename ${phpfpm})  
     
    lockfile=/var/lock/subsys/phpfpm
     
    start() {  
        [ -x ${phpfpm} ] || exit 5  
        echo -n $"Starting $prog: "  
        daemon ${phpfpm}
        retval=$?  
        echo  
        [ $retval -eq 0 ] && touch $lockfile  
        return $retval  
    }  
     
    stop() {  
        echo -n $"Stopping $prog: "  
        killproc $prog -QUIT  
        retval=$?  
        echo  
        [ $retval -eq 0 ] && rm -f $lockfile  
        return $retval  
    }  
     
    restart() {  
        configtest || return $?  
        stop  
        start  
    }  
     
    reload() {  
        configtest || return $?  
        echo -n $"Reloading $prog: "  
        killproc ${phpfpm} -HUP  
        RETVAL=$?  
        echo  
    }  
     
    force_reload() {  
        restart  
    }  
     
    configtest() {  
      ${phpfpm} -t
    }  
     
    rh_status() {  
        status $prog  
    }  
     
    rh_status_q() {  
        rh_status >/dev/null 2>&1  
    }  
     
    case "$1" in  
        start)  
            rh_status_q && exit 0  
            $1  
            ;;  
        stop)  
            rh_status_q || exit 0  
            $1  
            ;;  
        restart|configtest)  
            $1  
            ;;  
        reload)  
            rh_status_q || exit 7  
            $1  
            ;;  
        status)  
            rh_status  
            ;;  
        *)  
            echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"  
            exit 2  
    esac
    

    开机自动启动PHP

    只需要在init.d目录下执行chkconfig php-fpm on 即可

    chkconfig命令简略

    chkconfig --list 显示所有系统服务的运行状态on/off,如果指定了NAME 那么只显示指定服务在不同运行级的状态。
    chkconfig --add name 增加一项新的服务,chkconfig确保每个运行级有一项启动或杀死的入口,如果缺少 会从缺省的脚本自行建立。
    chkconfig --del name 删除一项服务,并把相关链接符号从/etc/rc[0-6].d中删除。
    chkconfig [--level levels] name:设置某一服务在指定的运行级是被启动,停止还是重置。

    Nginx自启添加如PHP相同,下面为脚本代码

    添加命令 chkconfig nginx on
    全部设置好后~测试:
    1、重启服务器 shutdown -r
    2、重新登录服务器ps aux | less 显示所有正在运行的进程

    #!/bin/bash
    # nginx Startup script for the Nginx HTTP Server
    # it is v.0.0.2 version.
    # chkconfig: - 85 15
    # description: Nginx is a high-performance web and proxy server.
    #              It has a lot of features, but it's not for everyone.
    # processname: nginx
    # pidfile: /var/run/nginx.pid
    # config: /usr/local/nginx/conf/nginx.conf
    #下面修改为自己的目录
    nginxd=/usr/local/nginx/sbin/nginx
    nginx_config=/usr/local/nginx/conf/nginx.conf
    nginx_pid=/usr/local/nginx/logs/nginx.pid
    RETVAL=0
    prog="nginx"
    # Source function library.
    . /etc/rc.d/init.d/functions
    # Source networking configuration.
    . /etc/sysconfig/network
    # Check that networking is up.
    [ ${NETWORKING} = "no" ] && exit 0
    [ -x $nginxd ] || exit 0
    # Start nginx daemons functions.
    start() {
    if [ -e $nginx_pid ];then
       echo "nginx already running...."
       exit 1
    fi
       echo -n $"Starting $prog: "
       daemon $nginxd -c ${nginx_config}
       RETVAL=$?
       echo
       [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
       return $RETVAL
    }
    # Stop nginx daemons functions.
    stop() {
            echo -n $"Stopping $prog: "
            killproc $nginxd
            RETVAL=$?
            echo
            [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
    }
    # reload nginx service functions.
    reload() {
        echo -n $"Reloading $prog: "
        #kill -HUP `cat ${nginx_pid}`
        killproc $nginxd -HUP
        RETVAL=$?
        echo
    }
    # See how we were called.
    case "$1" in
    start)
            start
            ;;
    stop)
            stop
            ;;
    reload)
            reload
            ;;
    restart)
            stop
            start
            ;;
    status)
            status $prog
            RETVAL=$?
            ;;
    *)
            echo $"Usage: $prog {start|stop|restart|reload|status|help}"
            exit 1
    esac
    exit $RETVAL
    

    MySql自启

    1、将服务账号拷贝到 init.d下

    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

    2、赋予可执行权限

    chmod 755 /etc/init.d/mysqld

    3、加入服务

    chkconfig mysqld on
    chkconfig --list

    相关文章

      网友评论

          本文标题:环境变量&自启进程

          本文链接:https://www.haomeiwen.com/subject/btijxqtx.html