php程序开机自动启动初探

PHP是一种解释型的编程语言,如果没有解释器,PHP程序其实和普通文本并没有什么区别。也因为此PHP的执行必须依赖PHP的运行环境。在开发过程中我们很容易把一个可执行的程序加入到服务器的开机启动项中,但是PHP因其特殊性,对于刚入门的新手显得略微复杂,所以本篇文章波波分享的就是如何把一个PHP文件加入到开机启动项中。

环境配置:Centos+php7.2

方法一:修改服务器启动项。

这是最简单的方法,尤其适合新手采用。

1、SSH登录服务器,打开并编辑“/etc/rc.local”文件。

2、复制如下内容,追加到文件中“exit 0”前面的位置。

  1. /usr/bin/env php /磁盘/路径/test.php start -d
  2. exit 0

方法二:脚本启动。

这种方式一般适合熟悉SHELL编程的老鸟。

1、我们需要将需要启动的php脚本文件赋予可执行权限。

  1. chmod +x /磁盘/路径/test.php

2、编写启动脚本。以下代码是我从网上找来的样本仅供参考。

  1. #!/bin/bash
  2. #chkconfig:2345 90 10
  3. #description:activitytask service
  4. #processname:activitytask
  5. php=/usr/bin/php
  6. root_path="/vagrant/activity_task/"
  7. start_file="web_start.php"
  8. case "$1" in
  9. start)
  10. echo 'starting activity_task web_start.php'
  11. echo "$php ${root_path}${start_file} start -d"
  12. $php ${root_path}${start_file} start -d
  13. ;;
  14. stop)
  15. echo "stoping activity_task...web_start.php"
  16. echo "$php ${root_path}${start_file} stop"
  17. $php ${root_path}${start_file} stop
  18. ;;
  19. status)
  20. echo "status activity_task...web_start.php"
  21. echo "$php ${root_path}${start_file} status"
  22. $php ${root_path}${start_file} status
  23. ;;
  24. restart)
  25. echo "restarting activity_task...web_start.php"
  26. echo "$php ${root_path}${start_file} reload"
  27. $php ${root_path}${start_file} reload
  28. ;;
  29. *)
  30. echo "Usage: $0 {start|stop|restart|restart}"
  31. exit 1
  32. ;;
  33. esac

上面这个脚本比较全,有点类似服务的启动,停止与重启。单纯的开机启动要比这个简单的多。切记上述脚本也要赋予可执行的权限。

3、配置开机启动。

  1. chkconfig service_name on

当然开机启动的方法不止这两种,还可以通过其他外部插件进行实现。更多操作方法自己摸索吧!

 

你想把广告放到这里吗?

发表评论

您必须 登录 才能发表留言!