Linux之Apache

 Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中。 本文章主要讲解了apache的使用。
一 安装

[root@apache-server ~]# yum install httpd httpd-manual -y #安装httpd软件及其手册
[root@apache-server ~]# systemctl start httpd #打开httpd
[root@apache-server ~]# systemctl enable httpd #让httpd开机启动

火墙设置:

[root@apache-server ~]# systemctl start firewalld #打开火墙并进行配置,或者直接关闭火墙
[root@apache-server ~]# firewall-cmd --permanent --add-service=http #使防火墙支持http协议
[root@apache-server ~]# firewall-cmd --reload #加载火墙策略

 apache的默认发布目录为/var/www/html,默认发布文件为index.html,在/var/www/html目录下创建index.html文件。在浏览器访问时显示的就为index.html文件中的内容。

[root@apache-server ~]# cd /var/www/html/
[root@apache-server html]# vim index.html
<h1>hello,apache</h1>

 测试:在测试端打开浏览器,输入apache服务器的IP地址

在这里插入图片描述
 apache手册:
在这里插入图片描述
二 apache信息
1.端口
 apache的默认发布端口为80,浏览器默认打开的端口也为80,所以在只输入IP地址时打开的就是80端口。apache除了80端口,还可以使用别的端口。如下,将发布端口设置为8080:

[root@apache-server ~]# vim /etc/httpd/conf/httpd.conf  #apache主配置文件

在这里插入图片描述

[root@apache-server ~]# firewall-cmd --permanent --add-port=8080/tcp #配置火墙允许访问8080/tcp端口
[root@apache-server ~]# firewall-cmd --reload #加载火墙
[root@apache-server ~]# systemctl restart httpd.service 

 浏览器默认为80端口,所以在直接输入IP时不能访问:
在这里插入图片描述
 访问apache服务器的8080端口就可以访问成功:
在这里插入图片描述
 如果selinux为Enforcing状态时,selinux只允许特定的http的端口,如果要将apache的发布端口设置为selinux不允许的端口,如6666,需要让selinux允许6666端口。

[root@apache-server ~]# firewall-cmd --permanent --add-port=6666/tcp
[root@apache-server ~]# firewall-cmd --reload success
[root@apache-server ~]# vim /etc/httpd/conf/httpd.conf

在这里插入图片描述

[root@apache-server ~]# semanage port -a -t http_port_t -p tcp 6666 #让selinux允许6666为apache的发布端口
[root@apache-server ~]# systemctl restart httpd.service 

在这里插入图片描述
 semanage port -l | grep http 查看selinux所允许的apache的发布端口。
测试:
在这里插入图片描述
 如果selinux为Disabled或者Premissing状态时,直接修改apache的主配置文件并重启服务就可以了。
2.发布文件
 apache的默认发布文件为index.html,在默认发布目录下只要存在index.html文件,默认情况下在访问apache服务时显示的就为index.html文件中的内容。
apache的默认发布文件在其主配置文件中进行说明。要修改其默认发布目录需要在配置文件中修改:

[root@apache html]# vim /etc/httpd/conf/httpd.conf 

在这里插入图片描述
 164行中test.html写在index.html前面表示如果在apache默认发布目录下存在test.html文件,则test.html为默认发布文件,test.html不存在时index.html为默认发布文件。

[root@apache ~]# cd /var/www/html/
[root@apache html]# vim test.html 
<h1>test.html</h1>
[root@apache html]# systemctl restart httpd.service 

 浏览器访问测试时直接访问的为test.html中的文件:
在这里插入图片描述
3.发布目录
 apache的默认发布目录为/var/www/html,以下步骤可以将默认发布目录修改为/westos/web/html/。

[root@apache html]# mkdir /westos/web/html -p
[root@apache html]# vim /westos/web/html/index.html 
<h1>/westos/web/index.html</h1>

 如果selinux为Enforcing状态时,需要修改安全上下文,如果selinux为Diseabled状态时,可以不用修改。

[root@apache html]# semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?' #修改安全上下文
[root@apache html]# restorecon -RvvF /westos/

 在主配置文件中给 /westos/web/html目录授权:

[root@apache-server ~]# vim /etc/httpd/conf/httpd.conf 

在这里插入图片描述
 重启服务后测试访问的为/westos/web/html/index.html 文件:

[root@apache-server ~]# systemctl restart httpd.service 

在这里插入图片描述
三 apache虚拟机
 apache虚拟机就是在使用不同域名访问时,所访问的地址不同,配置如下。

[root@apache-server ~]# mkdir /var/www/virtual/
[root@apache-server ~]# mkdir /var/www/virtual/news/html -p #news站点的默认发布目录
[root@apache-server ~]# mkdir /var/www/virtual/music/html -p #music站点的默认发布目录
[root@apache-server ~]# vim /var/www/html/index.html #默认情况下的发布文件
<h1>hello,apache</h1>
[root@apache-server ~]# vim /var/www/virtual/news/html/index.html #news站点的发布文件
<h1>hell news</h1>
[root@apache-server ~]# vim /var/www/virtual/music/html/index.html #music站点的发布文件
<h1>hello music</h1>

 设置三个站点的配置文件,/etc/httpd/conf.d/目录下的文件都为apache的子配置文件。

[root@apache-server ~]# cd /etc/httpd/conf.d/
[root@apache-server conf.d]# vim a_defaults.conf #默认情况下访问站点的配置文件
<VirtualHost _default_:80>
	DocumentRoot /var/www/html
	CustomLog logs/default.log combined #日志
</VirtualHost>
[root@apache-server conf.d]# vim news.conf  #news站点的配置文件
<VirtualHost *:80>
	ServerName news.westos.com
	DocumentRoot /var/www/virtual/news/html #当访问的域名为news.westos.com时访问/var/www/virtual/news/html文件中的内容
	CustomLog logs/news.log combined #news站点的日志,combined混合型日志类型
</VirtualHost>
<Directory "/var/www/virtual/news/html">
	Require all granted #允许所有人访问/var/www/virtual/news/html目录下的文件
</Directory>
[root@apache-server conf.d]# vim music.conf  #music站点的配置文件,参数与news站点配置文件参数代表意义相同
<VirtualHost _default_:80>
<VirtualHost *:80>
	ServerName music.westos.com
	DocumentRoot /var/www/virtual/music/html
	CustomLog logs/music.log combined
</VirtualHost>
<Directory "/var/www/virtual/music/html">
	Require all granted
</Directory>

 重启服务后在测试端做域名解析:

[root@apache-server conf.d]# systemctl restart httpd.service 
[root@foundation80 Desktop]# vim /etc/hosts
172.25.254.180 www.westos.com news.westos.com music.westos.com login.westos #172.25.254.180为apache服务器的IP地址

 测试:访问www.westos.com时显示/var/www/html/index.html文件内容
在这里插入图片描述
 访问news.westos.com时,显示/var/www/virtual/news/html/index.html文件中内容
在这里插入图片描述
 访问music.westos.com时显示/var/www/virtual/music/html/index.html文件中内容。
在这里插入图片描述
四 apache的访问控制
 apache的访问控制有两种方式,一种基于IP地址,一种基于帐号-密码形式。
1.基于IP的访问控制
 控制默认情况下的apache访问,修改其配置文件:

  [root@apache-server ~]# cd /etc/httpd/conf.d/
    [root@apache-server conf.d]# vim a_defaults.conf 
    <VirtualHost _default_:80>
    	DocumentRoot /var/www/html
    	CustomLog logs/default.log combined
    </VirtualHost>
    <Directory "/var/www/html">
    	Order Allow,Deny #先读取Allow,再读取Deny,与下两行表示允许除了172.25.254.80主机外其它主机的访问
    	Allow from all 
    	Deny from 172.25.254.80
    </Directory>
    [root@apache-server conf.d]# systemctl restart httpd.service 

 测试:直接172.25.254.80主机访问apache服务时不能读取/var/www/html中的文件
在这里插入图片描述
2.基于帐号密码方式的访问控制

[root@apache-server conf.d]# htpasswd -cm http_userlist admin #添加apache帐号admin
[root@apache-server conf.d]# htpasswd -m http_userlist admin1 #添加apache帐号admin1,再次添加时不能用-c参数,否则会将原来的帐号覆盖掉。
[root@apache-server conf.d]# vim a_defaults.conf 
<VirtualHost _default_:80>
	DocumentRoot /var/www/html
	CustomLog logs/default.log combined
</VirtualHost>
<Directory "/var/www/html">
	AuthUserFile /etc/httpd/conf.d/http_userlist #用户帐号密码文件
	AuthName "plase input Username and password:" #登陆时显示内容
	AuthType basic #认证类型
#	Require user admin #只允许admin用户登陆
	Require valid-user #允许所有人登陆
</Directory>
[root@apache-server conf.d]# systemctl restart httpd.service 

 测试:登陆时需要正确的帐号和密码才能访问
在这里插入图片描述
五 apache支持的语言
 apache支持多种语言,如html,php,perl,python等,以下简单介绍如何让apache支持php,perl和python。
1.php

[root@apache-server conf.d]# vim /var/www/html/index.php #该文件是用php语言写的一个测试页
<?php
	phpinfo();
?>
[root@apache-server conf.d]# yum install php -y #安装php
[root@apache-server conf.d]# systemctl restart httpd.service 

 测试:访问apache服务器的index.php文件能够被识别。
在这里插入图片描述
2.perl
方式一:

[root@apache-server ~]# cd /var/www/cgi-bin/
[root@apache-server cgi-bin]# vim index.cgi #这里的cgi文件是有perl语言写的显示时间的脚本
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[root@apache-server cgi-bin]# chmod +x index.cgi

 测试:
在这里插入图片描述
方式二:

[root@apache-server html]# cd /var/www/html/
[root@apache-server html]# mkdir cgi
[root@apache-server html]# vim cgi/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[root@apache-server html]# chmod +x cgi/index.cgi #给该文件添加执行权限
[root@apache-server html]# semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?'  #更改 /var/www/html/cgi目录及其目录下所有文件的安全上下文,如果selinux关闭,可以不用设置
[root@apache-server html]# restorecon -RvvF /var/www/html/cgi #刷新
[root@apache-server html]# vim /etc/httpd/conf.d/a_defaults.conf 
<VirtualHost _default_:80>
	DocumentRoot /var/www/html
	CustomLog logs/default.log combined
</VirtualHost>
<Directory "/var/www/html/cgi">
	Options +ExecCGI
    AddHandler cgi-script .cgi
</Directory>

 测试:
在这里插入图片描述
3.python

[root@apache-server ~]# cd /var/www/cgi-bin/
[root@apache-server cgi-bin]# ls
webapp.wsgi
[root@apache-server cgi-bin]# vim webapp.wsgi #该文件为python写的测试页面
#!/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]
[root@apache-server cgi-bin]# chmod +x webapp.wsgi #添加执行权限
[root@apache-server cgi-bin]# yum install mod_wsgi #安装wsgi模块
[root@apache-server cgi-bin]# cd /etc/httpd/conf.d/
[root@apache-server conf.d]# vim webapp.conf
<VirtualHost *:80>
	ServerName webapp.westos.com #当访问webapp.westos.com域名时访问 /var/www/cgi-bin下的文件
	DocumentRoot /var/www/cgi-bin
	CustomLog logs/webapp.log combined
	WSGIScriptAlias / /var/www/cgi-bin/webapp.wsgi
</VirtualHost>
[root@apache-server conf.d]# systemctl restart httpd.service

 测试:
在这里插入图片描述
四 https
 HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer 或 Hypertext Transfer Protocol Secure,超文本传输安全协议),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。

[root@apache-server ~]# yum install mod_ssl crypto-utils.x86_64  -y
[root@apache-server ~]# genkey www.westos.com #创建证书和密钥,出现以下界面如下选择

在这里插入图片描述
 加密字符长度,根据兴趣选择长度。
在这里插入图片描述
 生成随机数:
在这里插入图片描述
 在出现以下界面时需要移动鼠标或者从键盘中输入一些随即字符,进度条才会移动。
在这里插入图片描述
 是否上传CA进行认证。
在这里插入图片描述
在这里插入图片描述
 证书认证信息:
在这里插入图片描述
 再次访问时apache服务器时需要进行证书认证,点击I Understand the Risks,再点击Add Exception:
在这里插入图片描述
 出现如下界面,表示下载apache服务器证书,点击红框中的内容,就可以访问到apache:
在这里插入图片描述
 如果要清除证书点击浏览器由上角的散条杠,点击Preferences,出现如下界面。在这里插入图片描述
 选择Advanced中的View Certificates,选中认证过的证书并删除掉。下次访问时需要重新下载证书。
五 网页重写实现自动跳转https

[root@apache-server ~]# cd /etc/httpd/conf.d/
[root@apache-server conf.d]# vim login.conf
<VirtualHost *:443>
	SSLEngine on #开启https功能
	ServerName login.westos.com
	DocumentRoot /var/www/virtual/login/html
	CustomLog logs/login.log combined
	SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt #证书
	SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key #密钥
</VirtualHost>
<Directory "/var/www/virtual/login/html">
	Require all granted
</Directory>
<VirtualHost *:80>
	ServerName login.westos.com
	RewriteEngine on #开启重定向功能
	RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301] 
</VirtualHost>
[root@apache conf.d]# systemctl restart httpd.service 

 测试端解析,浏览器访问login.westos.com自动调转到https://login.westos.com
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值