Apache添加为Linux的服务及开机自动启动
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是否在其中.。
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的启动与停止一般启动和停止某个服务有两种方式,即命令行方式和图形方式。
本节主要介绍如何用使用命令方式启动和停止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服务的部署和配置⽬录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的安装与配置⼀、编译安装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系统中设置开机⾃动执⾏命令###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 配置
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系统服务自动重启脚本。
首先,我们需要确定需要监控和重启的系统服务。
可以通过以下命令查看当前正在运行的服务列表:```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系统服务自动重启脚本的使用说明和示例代码: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。
Linux网络操作系统项目教程项目13配置与管理Apache服务器ppt课件
③ 让防火墙放行http服务,重启httpd服务。
[root@RHEL7-1 ~]# firewall-cmd --permanent --add-service=http[root@RHEL7-1 ~]# firewall-cmd --reload[root@RHEL7-1 ~]# firewall-cmd --list-all
图13-4 Apache服务器运行正常
任务2 认识Apache服务器的配置文件
在Linux系统中配置服务,其实就是修改服务的配置文件,httpd服务程序的主要配置文件及存放位置如表13-1所示。
Apache服务器的主配置文件是httpd.conf,该文件通常存放在/etc/httpd/conf目录下。文件看起来很复杂,其实很多是注释内容。本节先作大略介绍,后面的章节将给出实例,非常容易理解。
HTTP(Hypertext Transfer Protocol,超文本传输协议)可以算得上是目前国际互联网基础上的一个重要组成部分。而Apache、IIS服务器是HTTP协议的服务器软件,微软的Internet Explorer和Mozilla的Firefox则是HTTP协议的客户端实现。
(2)更改当前的SELinux值,后面可以跟Enforcing、Permissive或者1、0。[root@RHEL7-1 ~]# setenforce 0[root@RHEL7-1 ~]# getenforcePermissive
13.2.3 子任务3 测试httpd服务是否安装成功
启动Apache服务器,并设置开机自动加载Apache服务。[root@RHEL7-1 ~]# systemctl start httpd[root@RHEL7-1 ~]# systemctl enable httpd[root@RHEL7-1 ~]# firefox http://127.0.0.1如果看到图13-4所示的提示信息,则表示Apache服务器已安装成功。也可以在Applications菜单中直接启动firefox,然后输入在地址栏输入http://127.0.0.1,测试是否成功安装。
Linux——搭建Apache(httpd)服务器
Linux——搭建Apache(httpd)服务器⼀、基本概念Apache(或httpd)是Internet上使⽤最多的Web服务器技术之⼀,使⽤的传输协议是http超⽂本传输协议(⼀个基于超⽂本的协议),⽤于通过⽹络连接来发送和接受对象。
有两个版本:http:超⽂本传输协议,通过线路以明⽂形式发送,默认情况下使⽤80/TCP(也可以使⽤其他端⼝)https:经TLS/SSL安全加密的超⽂本传输协议,默认情况下使⽤端⼝443/TCP⼆、了解Apache的配置⽂件1、配置⽂件的分类在Linux系统中配置服务,其实就是修改服务的配置⽂件,httpd服务程序的主要配置⽂件及存放位置如下:配置⽂件的名称存放位置服务⽬录/etc/httpd主配置⽂件/etc/httpd/conf/httpd.conf虚拟主机配置⽂件/etc/httpd/conf.d⽇志⽂件/etc/httpd/logs⽹站数据⽬录/var/www/html2、主配置⽂件的重要参数主配置⽂件/etc/httpd/conf/httpd.conf参数⽤途ServerRoot服务⽬录ServerAdmin管理员邮箱User运⾏服务的⽤户Group运⾏服务的⽤户组ServerName⽹站服务器的域名DocumentRoot⽂档根⽬录(⽹站数据⽬录)Directory⽹站数据⽬录的权限Listen监听的IP地址与端⼝号DirectoryIndex默认的索引页页⾯ErrorLog错误⽇志⽂件CustomLog访问⽇志⽂件Timeout⽹页超时时间,默认为300秒3、Directory标签<Directory "/var/www/html">AllowOverride None #设置.htaccess⽂件中的指令类型,None表⽰禁⽌使⽤.htaccess,该参数⼀般不改Require all granted #设置权限,默认开启所有客户机访问权限</Directory>三、如何配置Apache服务器⾸先准备:主机名、⽹络、yum源1、更改主机名:[root@localhost ~]# hostnamectl set-hostname $主机名[root@localhost ~]# bash #环境变量重载2、配置⽹络(1)虚拟交换机、⽹络适配器选择仅主机模式,并且配置为192.168.100.0⽹段;(2)编辑⽹络配置⽂件:[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33修改: BOOTPROTO=static #改为静态IP地址ONBOOT=yes #改为开机⾃启IPADDR=192.168.100.10PREFIX=24 或者 NETMASK=255.255.255.0(3)重启⽹络服务:[root@localhost ~]# systemctl restart network3、配置yum源(1)先在VMware⾥⾯把系统镜像⽂件连接到虚拟机的光驱上;(2)挂载光驱⾥的镜像:[root@localhost ~]# mount /dev/cdrom /media(3)修改yum源配置⽂件:[root@localhost ~]# vim /etc/yum.repos.d/local.repo[rhel]name=rhelbaseurl=file:///mediaenabled=1gpgcheck=0(4)清空yum源缓存信息:[root@localhost ~]# yum clean all(5)检索当前yum源信息:[root@localhost ~]# yum repolist任务⼀:配置简单的httpd服务1、安装httpd服务[root@server ~]# yum -y install httpd2、启动httpd服务[root@server ~]# systemctl restart httpd[root@server ~]# systemctl enable httpd3、配置防⽕墙[root@server ~]# firewall-cmd --permanent --add-service=http[root@server ~]# firewall-cmd --reload4、关闭SELinux[root@server ~]# setenforce 05、客户端测试[root@client ~]# firefox http://IP地址或者 curl http://IP地址任务⼆:配置基于⽤户的个⼈⽹站注意:该⽤户必须在Linux系统中存在1、新建⼀个⽤户(⽹站基于该⽤户)[root@server ~]# useradd user0[root@server ~]# passwd user02、修改⽤户的家⽬录权限,使其他⽤户具有读取和执⾏的权限[root@server ~]# chmod -R 705 /home/user03、创建存放⽤户个⼈主页空间的⽬录,写user0的⽹页⽂件[root@server ~]# mkdir /home/user0/public_html[root@server ~]# cd /home/user0/public_html[root@server ~]# echo "this is user0's web">>index.html4、修改基于⽤户的httpd配置⽂件[root@server ~]# vim /etc/httpd/conf.d/userdir.conf修改: UserDir enabled #开启,表⽰让httpd服务程序开启个⼈⽤户主页功能UserDir public_html #去注释,UserDir参数表⽰⽹站数据在⽤户家⽬录中的保存⽬录名称5、配置防⽕墙(同上)[root@server ~]# firewall-cmd --permanent --add-service=http[root@server ~]# firewall-cmd --reload6、修改selinux权限[root@server ~]# getsebool -a|grep home[root@server ~]# setsebool httpd_enable_homedirs on7、重启服务[root@server ~]# systemctl restart httpd8、客户端测试[root@client ~]# firefox http://IP地址/~username 或者curl http://IP地址/~username任务三:配置基于域名访问的虚拟主机1、新建虚拟主机的⽹页⽂件[root@server ~]# mkdir /www/one /www/two[root@server ~]# cd /www/one[root@server ~]# echo "this is a web for virtual host one">>index.html[root@server ~]# cd /www/two[root@server ~]# echo "this is a web for virtual host two">>index.html[root@server ~]# chmod o+x /www2、配置虚拟主机的⽂件[root@server ~]# cd /etc/httpd/conf.d[root@server ~]# vim vhost.conf<Directory /www/one> #设置⽹站⽬录权限Require all granted #开启所有客户机访问权限</Directory><VirtualHost 192.168.100.10> #虚拟主机ServerName #定义服务器名称DocumentRoot /www/one/ #⽹站数据⽬录</VirtualHost><Directory /www/two>Require all granted</Directory><VirtualHost 192.168.100.11>ServerName DocumentRoot /www/two/</VirtualHost>3、做域名解析⽂件server/client[root@server ~]# vim /etc/hosts192.168.100.10 192.168.100.11 4、配置防⽕墙(同上)[root@server ~]# firewall-cmd --permanent --add-service=http[root@server ~]# firewall-cmd --reload5、修改虚拟主机⽹页⽂件的selinux上下⽂类型[root@server ~]# semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?'[root@server ~]# restorecon -RFv /www6、重启服务[root@server ~]# systemctl restart httpd任务四:配置基于端⼝访问的虚拟主机1——新建虚拟主机的⽹页⽂件[root@server ~]# mkdir /www/8088[root@server ~]# echo "this is a web for port 8088 ">>index.html[root@server ~]# mkdir /www/8089[root@server ~]# echo "this is a web for port 8089 ">>index.html2——配置虚拟主机的⽂件[root@server ~]# cd /etc/httpd/conf.d[root@server ~]# vim vhost.conf<Directory /www/8088/>Require all granted</Directory><virtualHost 192.168.100.10:8088>DocumentRoot /www/8088/</virtualHost><Directory /www/8089/>Require all granted</Directory><virtualHost 192.168.100.10:8089>DocumentRoot /www/8089/</virtualHost>3、配置防⽕墙[root@server ~]# firewall-cmd --permanent --zone=public --add-port=8089/tcp[root@server ~]# firewall-cmd --permanent --zone=public --add-port=8088/tcp[root@server ~]# firewall-cmd --reload4、关闭SELinux[root@server ~]# setenforce 05、重启服务[root@server ~]# systemctl restart httpd6、使⽤浏览器访问任务五:配置基于TLS加密的虚拟主机注意:经TLS/SSL安全加密的超⽂本传输协议,默认情况下使⽤端⼝443/TCP 1、安装TLS加密软件,⽹站内容不⽤明⽂传输[root@server ~]# yum -y install mod_ssl2、⽣成密钥[root@server ~]# openssl genrsa >tlsweb.key3、⽣成证书请求⽂件[root@server ~]# openssl req -new -key tlsweb.key > tlsweb.csr4、⽣成证书⽂件[root@server ~]# openssl req -x509 -days 365 -key tlsweb.key -in tlsweb.csr >tlsweb.crt5、修改ssl.conf配置⽂件[root@server ~]# vim /etc/httpd/conf.d/ssl.confSSLCertificateFile /etc/pki/tls/certs/tlsweb.crtSSLCertificateKeyFile /etc/pki/tls/private/tlsweb.key6、把证书⽂件拷贝到ssl.conf配置⽂件⾥的对应路径下⾯[root@server ~]# cp tlsweb.crt /etc/pki/tls/certs/7、把秘钥⽂件拷贝到ssl.conf配置⽂件⾥的对应路径下⾯[root@server ~]# cp tlsweb.key /etc/pki/tls/private/。
