Apache添加为Linux的服务及开机自动启动

合集下载

Linux—添加开机启动(服务脚本)

Linux—添加开机启动(服务脚本)

Linux—添加开机启动(服务脚本)系统启动时需要加载的配置⽂件/etc/profile、/root/.bash_profile/etc/bashrc、/root/.bashrc/etc/profile.d/*.sh、/etc/profile.d/lang.sh/etc/sysconfig/i18n、/etc/rc.local(/etc/rc.d/rc.local)⼀、修改开机启动⽂件:/etc/rc.local(或者/etc/rc.d/rc.local)# 1.编辑rc.local⽂件[root@localhost ~]# vi /etc/rc.local# 2.修改rc.local⽂件,在 exit 0前⾯加⼊以下命令。

保存并退出。

/etc/init.d/mysqld start # mysql开机启动/etc/init.d/nginx start # nginx开机启动supervisord -c /etc/supervisor/supervisord.conf # supervisord开机启动/bin/bash /server/scripts/test.sh >/dev/null2>/dev/null# 3.最后修改rc.local⽂件的执⾏权限[root@localhost ~]# chmod +x /etc/rc.local[root@localhost ~]# chmod755 /etc/rc.local⼆、⾃⼰写⼀个shell脚本将写好的脚本(.sh⽂件)放到⽬录 /etc/profile.d/ 下,系统启动后就会⾃动执⾏该⽬录下的所有shell脚本。

三、通过chkconfig命令设置# 1.将(脚本)启动⽂件移动到 /etc/init.d/或者/etc/rc.d/init.d/⽬录下。

(前者是后者的软连接)mv /www/wwwroot/test.sh /etc/rc.d/init.d# 2.启动⽂件前⾯务必添加如下三⾏代码,否侧会提⽰chkconfig不⽀持。

Linux1 WEB服务器 Apache的启动与停止

Linux1 WEB服务器  Apache的启动与停止

Linux1 WEB服务器Apache的启动与停止一般启动和停止某个服务有两种方式,即命令行方式和图形方式。

本节主要介绍如何用使用命令方式启动和停止Apache服务器,以及如何开机自动加载该httpd 服务。

1.Apache服务器的启动Apache服务器安装完成之后,其服务名为httpd,用户可以通过命令来操作该服务进而管理Apache服务器。

一般情况下,在启动httpd服务之前需要查看该服务是否已经执行,用户可以输入“ps –aux|grep httpd”命令,并按【回车】键,如果出现图7-40所示的信息则表示httpd服务已经在运行。

图7-40 服务已经运行假如httpd服务已关闭,那么用户可以输入“service httpd start”命令,并按【回车】键,启动Apache服务器,如图7-41所示。

图7-41 启动Apache服务器另外,用户也可以输入“/etc/rc.d/init.d/httpd start”命令,并按【回车】键,启动Apache服务器,如图7-42所示。

图7-42 启动Apache服务器2.Apache服务器的停止用户可以在终端中,输入“service httpd stop”命令,并按【回车】键,停止httpd 服务,如图7-43所示。

图7-43 停止Apache服务器另外,用户还可以输入“/etc/rc.d/init.d/httpd stop”命令,并按【回车】键,停止httpd服务,如图7-44所示。

图7-44 停止Apache服务器3.Apache服务器的重新启动当用户修改完配置文件后,可以输入“service httpd restart”命令,并按【回车】键,重启httpd服务,使配置文件生效,如图7-45所示。

图7-45 重新启动Apache服务器另外,用户还可以输入“/etc/rc.d/init.d/httpd restart”命令,并按【回车】键,重新启动httpd服务,如图7-46所示。

Linux下Apache服务的部署和配置

Linux下Apache服务的部署和配置

Linux下Apache服务的部署和配置⽬录1 Apache的作⽤2 Apache的安装3 apache的启⽤4 apache的基本信息5 apache的访问控制5.1 基于客户端ip的访问控制5.2 基于⽤户认证的访问控制6 apache的虚拟主机7 apache的加密访问8 ⽹页重写9 正向代理10 反向代理11 apache ⽀持的语⾔1 Apache的作⽤解析⽹页语⾔,如html,php,jsp等接收web⽤户的请求,并给予⼀定的响应2 Apache的安装安装apche软件:dnf install httpd.x86_64 -y3 apache的启⽤开启apache服务并设置开机启动:systemctl enable --now httpd查看apache服务的状态:systemctl enable --now httpd查看⽕墙信息:firewall-cmd --list-all 在⽕墙中永久开启http服务:firewall-cmd --permanent --add-service=http在⽕墙中永久开启https服务: f irewall-cmd --permanent --add-service=https在不改变当前⽕墙状态的情况下刷新防⽕墙:firewall-cmd --reload4 apache的基本信息apche的基本信息服务名称:httpd主配置⽂件:/etc/httpd/conf/httpd.conf⼦配置⽂件:/etc/httpd/conf.d/*.conf默认发布⽬录:/var/www/html默认端⼝:80 (http),443(https)⽇志⽂件:/etc/httpd/logs开启apche服务后,输⼊ip查看默认发布页⾯:(1)更改apche服务的端⼝号查看httpd服务的默认端⼝号:netstat -antlupe |grep httpd编辑配置⽂件:/etc/httpd/conf/httpd.conf,修改端⼝号重启httpd服务:systemctl restart httpd查看httpd服务的端⼝号:netsat -antlupe | grep httpd更改端⼝号后,输⼊ip后⽆法正常连接,原因是8080端⼝未添加在⽕墙中在防⽕墙⾥添加888端⼝号:firewall-cmd --permanent --add-port=888/tcp在不改变当前⽕墙状态的情况下刷新防⽕墙:firewall-cmd --reload输⼊IP地址:端⼝号,可以正常访问(2)修改apche的默认发布⽂件默认⽬录:cd /var/www/html在⽂件默认发布⽬录下新建⼀个⽂件index.html输⼊:http://172.25.254.144查看默认发布⽂件就是访问apache时没有指定⽂件名,即默认访问的⽂件,此⽂件可以指定多个,但有访问顺序。

Linux下Apache的安装与配置

Linux下Apache的安装与配置

Linux下Apache的安装与配置⼀、编译安装1、解决依赖关系⼆、后续操作1、启动httpd两种⽅法:第⼀种、/usr/local/apache/bin/apachectl start第⼆种⽅法:先修改http.pid⽂件位置打开配置⽂件增加⼀⾏vim /etc/httpd/httpd.conf 增加PidFile “/var/run/httpd.pid”为了启动httpd更加⽅便,#!/bin/bash## httpd Startup script for the Apache HTTP Server## chkconfig: - 85 15# description: Apache is a World Wide Web server. It is used to serve \# HTML files and CGI.# processname: httpd# config: /etc/httpd/conf/httpd.conf# config: /etc/sysconfig/httpd# pidfile: /var/run/httpd.pid# Source function library.. /etc/rc.d/init.d/functionsif [ -f /etc/sysconfig/httpd ]; then. /etc/sysconfig/httpdfi# Start httpd in the C locale by default.HTTPD_LANG=${HTTPD_LANG-"C"}# This will prevent initlog from swallowing up a pass-phrase prompt if# mod_ssl needs a pass-phrase from the user.INITLOG_ARGS=""# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server# with the thread-based "worker" MPM; BE WARNED that some modules may not# work correctly with a thread-based MPM; notably PHP will refuse to start.# Path to the apachectl script, server binary, and short-form for messages.apachectl=/usr/local/apache/bin/apachectlhttpd=${HTTPD-/usr/local/apache/bin/httpd}prog=httpdpidfile=${PIDFILE-/var/run/httpd.pid}lockfile=${LOCKFILE-/var/lock/subsys/httpd}RETVAL=0start() {echo -n $"Starting $prog: "LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONSRETVAL=$?echo[ $RETVAL = 0 ] && touch ${lockfile}return $RETVAL}stop() {echo -n $"Stopping $prog: "killproc -p ${pidfile} -d 10 $httpdRETVAL=$?echo[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}}reload() {echo -n $"Reloading $prog: "if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; thenRETVAL=$?echo $"not reloading due to configuration syntax error"failure $"not reloading $httpd due to configuration syntax error"elsekillproc -p ${pidfile} $httpd -HUPRETVAL=$?fiecho}# See how we were called.case "$1" instart)start;;stop)stop;;status)status -p ${pidfile} $httpdRETVAL=$?;;restart)stopstart;;condrestart)if [ -f ${pidfile} ] ; thenstopstartfi;;reload)reload;;graceful|help|configtest|fullstatus)$apachectl $@RETVAL=$?;;*)echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" exit 1esacexit $RETVAL将以上代码加⼊到vim /etc/init.d/httpd中⽽后为此脚本赋予执⾏权限:chmod +x /etc/rc.d/init.d/httpd加⼊服务列表:chkconfig --add httpd给3,5启动chkconfig --level 3,5 httpd on最后加路径将 export PATH=$PATH:/usr/local/apache/binvim /etc/profile.d/httpd.sh完成后重新登录就可以了。

LINUX系统中设置开机自动执行命令

LINUX系统中设置开机自动执行命令

LINUX系统中设置开机⾃动执⾏命令###1、开机⾃动执⾏命令配置⽂件位置[root@wx-local01 ~]# cd /etc/rc.d/[root@wx-local01 rc.d]# ls -l /etc/rc.d/总⽤量4drwxr-xr-x. 2 root root 107 9⽉510:40 init.ddrwxr-xr-x. 2 root root 45 11⽉172020 rc0.ddrwxr-xr-x. 2 root root 45 11⽉172020 rc1.ddrwxr-xr-x. 2 root root 45 11⽉172020 rc2.ddrwxr-xr-x. 2 root root 72 5⽉2614:02 rc3.ddrwxr-xr-x. 2 root root 72 5⽉2614:02 rc4.ddrwxr-xr-x. 2 root root 72 5⽉2614:02 rc5.ddrwxr-xr-x. 2 root root 45 11⽉172020 rc6.d-rwxr-xr-x. 1 root root 881 5⽉2716:06 rc.local[root@wx-local01 rc.d]# tree /etc/rc.d//etc/rc.d/├── init.d│├── functions│├── mysql -> /home/wx/mysql/support-files/mysql.server│├── netconsole│├── network│├── README│└── syschronzed_date├── rc0.d│├── K50netconsole -> ../init.d/netconsole│└── K90network -> ../init.d/network├── rc1.d│├── K50netconsole -> ../init.d/netconsole│└── K90network -> ../init.d/network├── rc2.d│├── K50netconsole -> ../init.d/netconsole│└── S10network -> ../init.d/network├── rc3.d│├── K50netconsole -> ../init.d/netconsole│├── S10network -> ../init.d/network│└── S63syschronzed_date -> ../init.d/syschronzed_date├── rc4.d│├── K50netconsole -> ../init.d/netconsole│├── S10network -> ../init.d/network│└── S63syschronzed_date -> ../init.d/syschronzed_date├── rc5.d│├── K50netconsole -> ../init.d/netconsole│├── S10network -> ../init.d/network│└── S63syschronzed_date -> ../init.d/syschronzed_date├── rc6.d│├── K50netconsole -> ../init.d/netconsole│└── K90network -> ../init.d/network└── rc.local8 directories, 24 files2、设置开机⾃启动命令[root@wx-local01 rc.d]# vim /etc/rc.d/rc.local#!/bin/bash# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES## It is highly advisable to create own systemd services or udev rules# to run scripts during boot instead of using this file.## In contrast to previous versions due to parallel execution during boot# this script will NOT be run after all other services.## Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure# that this script will be executed during boot.touch /var/lock/subsys/local#开机执⾏脚本启动harborsh /scripts/shell/start_harbor/power_on_self_starting_harbor.sh3、设置执⾏权限【不设置执⾏权限,启动后不执⾏⽂件中内容】[root@wx-local01 rc.d]# chmod +x /etc/rc.d/rc.local###。

linux-Apache 配置

linux-Apache 配置

Apache 配置一、安装Apache下载地址:/1. 安装Apache# tar zxvf httpd-2.2.11.tar.gz# cd httpd-2.2.11# ./configure --prefix=/usr/local/apache --enable-so//编译时加上加载模块参数--enable-so# make# make install2. 配置系统启动时自动启动Apache服务。

# vi /etc/rc.d/rc.local//在rc.local上加入一行/usr/local/apache/bin/apachectl –k start。

二、配置Apache1. 修改httpd.conf文件# vi /usr/local/apache/conf/httpd.conf1)设置根目录的路径根目录是指Apache存放配置文件和日志文件的目录,配置参数为ServerRoot,默认位于“/u sr/local/apache”。

命令如下:2)设置监听IP地址及端口号默认侦听本机所有IP地址的TCP80端口,命令如下:Listen 80用户也可以按自己的需求,使用多个Listen语句在多个地址和端口上侦听客户端请求。

比如:Listen 192.168.99.9:80Linsten 172.16.0.20:80803)设置系统管理员E-m ail使用ServerAdmin参数设置管理员E-m ail,比如管理员的Email地址为root@guoxuemin. cn:4)设置服务器主机的名称参数ServerName用来设置服务器的主机名称,如果没有域名则填入服务器的IP地址,比如服务器的IP地址为192.168.99.9:5)设置主目录的路径用户可以使用参数Document Root配置服务器主目录默认路径,比如,主目录路径为:6)设置默认文件Apache的默认文件名为index.ht ml,可以使用Directory Index参数来配置,比如,将ind ex.php设置为默认文件名:7)测试:打开浏览器,输入地址:http://192.168.99.9,可以打开站点了:2. 配置目录权限使用<Directory 目录路径>和</Directory>设置目录的权限。

Linux系统系统服务自动重启脚本(Python版)

Linux系统系统服务自动重启脚本(Python版)

Linux系统系统服务自动重启脚本(Python版)在Linux系统中,经常会遇到系统服务崩溃或异常停止的情况。

为了确保系统的稳定性和可靠性,我们需要及时检测并自动重启这些服务。

本文将介绍一种使用Python编写的Linux系统服务自动重启脚本。

首先,我们需要确定需要监控和重启的系统服务。

可以通过以下命令查看当前正在运行的服务列表:```shellsystemctl list-units --type service```在这个列表中,我们可以选择需要自动重启的服务,并记录下服务的名称。

接下来,我们使用Python编写脚本来实现自动重启功能。

首先,我们导入相应的模块:```pythonimport subprocessimport time```然后,我们定义一个函数来检测和重启服务:def check_and_restart_service(service_name):while True:# 检测服务状态result = subprocess.run(["systemctl", "is-active", service_name], capture_output=True)status = result.stdout.decode().strip()if status != "active":# 重启服务subprocess.run(["systemctl", "restart", service_name])print(f"Service {service_name} restarted at {time.strftime('%Y-%m-%d %H:%M:%S')}")time.sleep(60) # 每隔60秒检测一次服务状态```在函数中,我们通过调用`subprocess.run`函数执行相应的命令,并利用`capture_output=True`参数来获取命令的输出结果。

Linux系统服务自动重启脚本使用Python编写的Linux系统服务自动重启工具

Linux系统服务自动重启脚本使用Python编写的Linux系统服务自动重启工具

Linux系统服务自动重启脚本使用Python编写的Linux系统服务自动重启工具在Linux系统中,有许多重要的服务需要持续运行以确保系统的稳定性和正常运行。

然而,由于各种原因,这些服务有时会出现异常崩溃或意外停止的情况,需要人工干预以重新启动这些服务。

为了提高系统的可用性和减少人工干预的需要,我们可以使用Python编写一个自动重启脚本,以便在服务停止时自动重新启动它们。

下面是这个Linux系统服务自动重启脚本的使用说明和示例代码:1. 环境准备在运行脚本之前,我们需要确保已经安装了Python解释器和相关的依赖库。

在大多数Linux发行版中,Python解释器是默认安装的,我们可以使用以下命令确认是否已经安装了Python:```python --version```如果已经安装,则会显示Python的版本号。

2. 脚本功能介绍这个自动重启脚本的主要功能是监控指定的系统服务,并在服务停止后自动重新启动它们。

脚本会周期性地检查服务的运行状态,如果服务停止,则会自动执行重启操作。

3. 用户配置在运行脚本之前,我们需要进行一些用户配置,以便脚本知道要监控哪些服务,并且知道如何执行重启操作。

在脚本的配置部分,我们可以设置以下参数:- 服务名称:指定要监控的服务的名称。

可以是单个服务,也可以是多个服务。

- 重启命令:指定服务启动的命令或脚本。

这个命令将在服务停止后执行以重新启动服务。

- 监控间隔:指定脚本检查服务状态的时间间隔,单位为秒。

下面是一个示例配置:```services = {'apache2': {'restart_cmd': 'systemctl restart apache2','interval': 10},'mysql': {'restart_cmd': 'service mysql restart','interval': 60},}```在这个示例配置中,我们监控了两个服务,分别是apache2和mysql。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Apache添加为Linux的服务及开机自动启动
在Linux系统中我一般采用编译源码的方式来安装Apache服务器.
说明:
Apache安装路径为:/usr/local/apache2
Linux的启动级别为3,版本为Centos5
一、Apache添加为Linux的服务
Apache安装完毕后,并不能通过作为linux服务来启动,通常需要执行apache安装bin 目录下的apachectl来启动服务.
# /usr/local/apache2/bin/apachectl start
如果希望加入到linux服务中。

将apachectl这个文件复制到/etc/rc.d/init.d下,更改为相应的服务名称即可,例如: httpd
# cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd
二、Apache设置为开机自启动
编辑器打开httpd文件,并在第一行#!/bin/sh下增加以下注释信息:
# chkconfig: 35 70 30
# description: Apache
并保存.
第一行3个数字参数意义分别为:哪些Linux级别需要启动httpd(3,5);启动序号(70);关闭序号(30)。

70是指apache的启动顺序号,如果别的程序的启动顺序号比70小,比如34,65,则需要等这些程序都启动以后,才启动apache。

建议将apache的启动顺序排在mysql之
后!
30是指系统关闭时,apache的停止顺序号.
最后执行:
#chkconfig –add httpd
将apache的自启动文件加到rc.d下的对应目录下,只有这样,Linux才能正确完成开机自启动.
运行#chkconfig –list,查看httpd是否在其中.。

相关文档
最新文档