####apache服务####

本文详细介绍了Apache web服务的安装部署,包括修改默认端口、设置默认发布文件和目录、虚拟主机配置、访问控制、支持的语言(如HTML、PHP、CGI、WSGI)以及HTTPS的启用和网页重写等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#1.apache
企业中常用的web服务,用来提供http://(超文本传输协议)

#2.apachec的安装部署

yum install httpd -y         			       	##安装apache
yum install httpd-manual 		                ##安装apache手册
systemctl start httpd
systemctl enable httpd
firewall-cmd --list-all			                ##列出火墙信息
firewall-cmd --permanent --add-service=httpd	##永久允许http
firewall-cmd --reload							##火墙从新加载

/var/www/html/									##默认发布目录
/var/www/html/index.html						##apache默认发布文件
vim /var/www/html/index.html
<h1> hello world </h1>

:wq

#3.apache的基础信息

主配置目录		/etc/httpd/conf/
主配置文件		/etc/httpd/conf/httpd.conf
默认发布目录		/var/www/html/
默认发布文件		index.html
默认端口			80
默认安全上下文	httpd_sys_content_t
程序开启默认用户	apache
apache日志		/etc/httpd/logs/*

#修改默认端口
vim /etc/httpd/conf/httpd.conf
42 Listen 8080
在这里插入图片描述
irewall-cmd --permanent --add-port=8080
firewall-cmd --reload

#测试
http://172.25.254.238:8080

#修改默认发布文件
默认发布文件就是访问apache时没有指定文件名称时默认访问的文件,这个文件可以指定多个,又访问顺序

vim /etc/httpd/conf/httpd.conf
164 DirectoryIndex test.html index.html
在这里插入图片描述

#测试
vim /var/www/html/test.html
hallo linux
:wq
在这里插入图片描述
http://172.25.254.238
在这里插入图片描述

#修改默认发布目录
vim /etc/httpd/conf/httpd.conf

120 DocumentRoot "/westos/html"
124 <Directory "/westos/html">
125         Require all granted
126 </Directory>

semanage fcontext -a -t httpd_sys_content_t ‘/westos(/.*)?’
restorecon -RvvF /westos/
在这里插入图片描述

#测试
vim /westos/html/index.html
/westos/html/index.html
:wq
http://172.25.254.238
在这里插入图片描述

#4.apache的虚拟主机
vim /etc/httpd/conf.d/adefault.conf

<VirtualHost _default_:80>
        DocumentRoot "/var/www/html"
</VirtualHost>

vim /etc/httpd/conf.d/news.conf

<VirtualHost *:80>
        ServerName "news.westos.com"  		##指定站点名成
        DocumentRoot "/var/www/virtual/westos.com/news"	##站点默认发布目录
        CustomLog logs/news.log combined	##站点日志combined表示四种日志的集合
</VirtualHost>
<Directory "/var/www/virtual/westos.com/news">
        Require all granted
</Directory>

在这里插入图片描述
#测试
vim /var/www/virtual/westos.com/news/index.html
news.linux
:wq
在这里插入图片描述
http://news.westos.com/
在这里插入图片描述

#5.apache内部的访问控制
1.针对与主机的访问控制
cd /etc/httpd/conf.d/
vim adefault.conf

<VirtualHost _default_:80>
        DocumentRoot "/var/www/html"
</VirtualHost>
<Directory "var/www/html">
        Require all granted
        Order Deny,allow
        Allow from 172.25.254.38
        Deny from all

在这里插入图片描述
#测试
在238主机:
http://172.25.254.238
在这里插入图片描述
在38主机:
http://172.25.254.238
在这里插入图片描述

2.用户方式的访问控制

htpasswd -cm /etc/httpd/htuser admin
New password: 
Re-type new password: 

cd /var/www/html/
vim admin
ni hao
:wq

vim adefault.conf

Directory "var/www/html/admin">
        AuthUserFile    "/etc/httpd/htuser"
        AuthName        "Please input username and password"
        AuthType        Basic
        Require user    admin		##只允许damin户
        Require         valid-user	##允许valid-user文件下的全部用户
 </Directory>

#测试
http://172.25.254.238
在这里插入图片描述
在这里插入图片描述
#6.支持的语言
1.html

2.php
vim /var/www/html/index.php

<?php phpinfo(); ?>

yum install php -y

#测试
172.25.254.238/index.php

3.cgi
mkdir /var/www/html/cgi

semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?'
restorecon -RvvF /var/www/html/cgi/

在这里插入图片描述
vim /var/www/html/cgi/index.cgi

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;

在这里插入图片描述

chmod +x /var/www/html/cgi/index.cgi

./index.cgi ##执行脚本看是否执行
在这里插入图片描述

vim adefault.conf

<Directory "/var/www/html/cgi">
        Options +ExecCGI
        AddHandler cgi-script .cgi
</Directory>

systemctl restart httpd在这里插入图片描述

#测试
http://172.25.254.238/cgi
在这里插入图片描述

4.wsgi
vim /var/www/html/cgi/script.wsgi

#!/usr/bin/env python
import time

def application (environ, start_response):
  response_body = 'UNIX EPOCH time is now: %s\n' % time.time()
  status = '200 OK'
  response_headers = [('Content-Type', 'text/plain'),
                      ('Content-Length', '1'),
                      ('Content-Length', str(len(response_body)))]
  start_response(status, response_headers)
  return [response_body]

在这里插入图片描述
vim /etc/httpd/conf.d/adefault.conf
<

VirtualHost _default_:80>
        DocumentRoot "/var/www/html"
        WSGIScriptAlias /WSGI /var/www/html/cgi/script.wsgi
</VirtualHost>

在这里插入图片描述
systemctl restart httpd

#测试
http://172.25.254.238/WSGI
在这里插入图片描述
#7.https
yum install mod_ssl
yum install crypto-utils.x86_64
systemctl restart httpd

genkey www.westos.com
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
cd /etc/httpd/conf.d/
vim ssl.conf

101 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
109 SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key

:wq

systemctl restart httpd

#测试
https://172.25.254.238
在这里插入图片描述
在这里插入图片描述

#8.设定https虚拟主机并设定网页重写
vim /etc/httpd/conf.d/z_login.conf
<

VirtualHost *:80>
        ServerName "login.westos.com"
        RewriteEngine on
        RewriteRule ^(/.*)$     https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>


<VirtualHost *:443>
        ServerName "login.westos.com"
        DocumentRoot "/var/www/virtual/westos.com/login"
        CustomLog logs/login.log        combined
        SSLEngine on
        SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
        SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
</VirtualHost>
<Directory "/var/www/virtual/westos.com/login">
        Require all granted
</Directory>

在这里插入图片描述
^(/.*)$ ##客户在浏览器地址中输入的所有字符
https:// ##强制客户加密访问
%{HTTP_HOST} ##客户请求主机
KaTeX parse error: Expected 'EOF', got '#' at position 4: 1 #̲#表示^(/.*)的值
[redirect=301] ##永久重写 302临时

#测试
login.westos.com
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值