1.什么是apache?
Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行
Apache是世界使用排名第一的Web服务器软件,它可以运行在几乎所有广泛使用的计算机平台上
由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一,也叫网页服务器软件
它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中
同时Apache音译为阿帕奇,是北美印第安人的一个部落,叫阿帕奇族,在美国的西南部,也是一个基金会的名称、一种武装直升机等
什么是http://协议?
HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写
是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议
HTTP是基于TCP/IP通信协议来传递数据(HTML 文件, 图片文件, 查询结果等)
HTTP是一个属于应用层的面向对象的协议,由于其简捷、快速的方式,适用于分布式超媒体信息系统
HTTP协议工作于客户端-服务端架构为上,浏览器作为HTTP客户端通过URL向HTTP服务端即WEB服务器发送所有请求
Web服务器根据接收到的请求后,向客户端发送响应信息
2.apache的安装部署
关闭selinux
yum install httpd -y ##安装apache软件
yum install httpd-manual ##安装apache的手册
systemctl start httpd
systemctl enable httpd
firewall-cmd --permanent --add-service=http ##火墙开启http服务
firewall-cmd --reload
firewall-cmd --list-all
注:
/var/www/html
##apache的根目录,默认发布目录
/var/www/html/index.html
##apache的默认发布文件
vim /var/www/html/index.html
<h1>hello westos</h1>
测试:
http://172.25.254.121
http://172.25.254.121/manual
3.apache的基础信息
主配置目录 | /etc/httpd/conf |
主配置文件 | /etc/httpd/conf/httpd.conf |
子配置目录 | /etc/httpd/conf.d/ |
子配置文件 | /etc/httpd/conf.d/*.conf |
默认发布目录 | /var/www/html |
默认发布文件 | index.html |
默认端口 | 80 |
默认安全上下文 | httpd_sys_content_t |
程序开启默认用户 | apache |
apache日志 | /etc/httpd/logs/* |
- 修改默认端口:
vim /etc/httpd/conf/httpd.conf
##编写配置文件
43 Listen 8080
##修改默认端口为8080
firewall-cmd --permanent --add-port=8080/tcp
##给火墙添加8080端口
firewall-cmd --reload
##重新加载
systemctl restart httpd
##重启服务
netstat -pan | grep httpd
##查看端口信息
在浏览器测试8080端口:
- 修改默认发布文件:
默认发布文件就是访问apache时没有指定文件名时默认访问的文件;
这个文件可以指定多个,有访问顺序
vim /etc/httpd/conf/httpd.conf
##编写配置文件
164 DirectoryIndex westos index.html
##当westos不存在时访问index.html
编写/var/www/html/westos
文件
在浏览器测试:
- 修改默认发布目录:
vim /etc/httpd/conf/httpd.conf
##编写配置文件
建立/westos/html
目录,将index.html移动至此目录下
用浏览器测试:
-
访问控制
vim /var/www/html/index.html <h1>172.25.254.121</h1> mkdir /var/www/html/zhang vim /var/www/html/zhang/index.html <h1>how are you</h1>
vim/etc/httpd/conf/httpd.conf
120 <Directory "/var/www/html/zhang">
121 Order Allow,Deny ##先读allow,再读deny
122 Allow from All
123 Deny from 172.25.254.221 ##读到deny会覆盖allow
124 < /Directory>
systemctl restart httpd
在浏览器测试:
vim/etc/httpd/conf/httpd.conf
systemctl restart httpd
- 指定用户访问:
htpasswd -cm westosuser admin
##第一次添加用户时加-c参数
htpasswd -m westosuser admin1
##第二次添加用户
cat westosuser
##查看密码
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/westos">
AuthUserFile /etc/httpd/conf/westosuser
AuthType basic
AuthName "Plesae input your name and password!!!"
Require user admin ##允许admin用户加密访问
#Require valid-user ##所有的加密用户都可访问
</Directory>
systemctl restart httpd
输入刚建立的用户及密码进入:
修改参数,所有用户可进入
- 虚拟主机搭建
vim /etc/hosts
##添加解析
在浏览器输入www.westos.com即可查看
添加news和music的目录及编写.html文件
mkdir /var/www/virtual/news/html -p
mkdir /var/www/virtual/music/html -p
vim /var/www/virtual/news/html/index.html
cp /var/www/virtual/news/html/index.html /var/www/virtual/music/html/index.html
vim /var/www/virtual/music/html/index.html
cd /etc/httpd/conf.d/
vim default.conf ##编写虚拟主机配置文件
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog logs/default.log combined
</VirtualHost>
vim news.conf ##编辑news虚拟站点的基本信息和授权信息
<VirtualHost *:80>
ServerName news.westos.com
DocumentRoot /var/www/virtual/news/html
CustomLog logs/new.log combined
</VirtualHost>
<Directory "/var/www/virtual/news/html">
Require all granted
</Directory>
cp news.conf music.conf
vim music.conf ##编辑music虚拟站点的基本信息和授权信息
<VirtualHost *:80>
ServerName music.westos.com
DocumentRoot /var/www/virtual/music/html
CustomLog logs/new.log combined
</VirtualHost>
<Directory "/var/www/virtual/music/html">
Require all granted
</Directory>
systemctl restart httpd
用浏览器访问music.westos.com/news.westos.com
apache支持的语言
- php超文本预处理器
vim /var/www/html/index.php ##编辑php默认发布文件
<?php
phpinfo();
?>
yum install php -y
systemctl restart httpd
在浏览器可看到php表格
systenctl restart httpd
mkdir /var/www/html/cgi
vim index.cgi
#! /usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
chmod 775 index.cgi ##更改权限
./index.cgi ##执行文件
vim /etc/httpd/conf.d/default.conf
<VirtualHost _default_:80>
DocumentRoot /var/www/html
Custommlog logs/default.log combined
</VirtualHost>
<Directory "/var/www/html/cgi">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
systemctl restart httpd
在浏览器访问http://172.25.254.121/cgi/index.cgi
- https 安全超文本传输协议,在http协议上增加ssl加密传送信息
yum install mod_ssl -y
systemctl restart httpd
增加火墙功能
在浏览器访问https://172.25.254.121 此时的加密未得到任何认证
- 自签名证书:
yum install -y crypto-utils
genkey www.westos.com ##对域名主机生成证书和密钥,进入图形安装
进入加密页面
选择钥匙大小
是否给CA发送证书申请,选择否
给私钥加密,选next
填写基本信息
vim /etc/httpd/conf.d/ssl.conf ##通过配置文件告诉apache生成的密钥和证书文件
101 SSLCentificateFile /etc/pki/tls/certs/www.westos.com.crt
108 SSLCentificateKeyFile /etc/pki/tls/private/www.westos.com.key
systemctl restart httpd
在浏览器可查看自签名证书的发布信息
- 网页重写(客户在浏览器输入80端口可跳转到443端口)
cd /etc/httpd/conf.d/
cp news.conf login.conf
vim login.conf
<VirtualHost *:443>
ServerName login.westos.com
DocumentRoot /var/www/virtual/login/html
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/login/html">
Require all granted
</Directory>
vim /etc/hosts
172.25.254.121 login.westos.com
mkdir -p /var/www/virtual/login/html
vim /var/www/virtual/login/html/index.html
<h1>login page</h1>
systemctl restart httpd
在浏览器查看
- 网页重写
vim login.conf
<VirtualHost *:80>
ServerName login.westos.com
RewriteEngine On
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>
注:
#^(/.*)$表示客户在浏览器中输入的所有字符
# 1 表 示 ( / . ∗ ) 1表示^(/.*) 1表示(/.∗)的值
#redirect=301表示转换时永久的
#%{HTTP_HOST}表示客户请求主机
systemctl restart httpd
##wsgi
mkdir /var/www/wsgi
cd /var/www/wsgi/
从172.25.254.250/RHCEPACKAGES/materials上下载script.wsgi
cd /etc/httpd/conf.d/
vim vhost.conf
<VirtualHost *:80>
ServerName www.westoswsgi.com
WSGIScriptAlias / /var/www/wsgi/script.wsgi
</VirtualHost>
yum install mod_wsgi -y
vim /etc/hosts
172.25.254.121 www.westoswsgi.com
用浏览器访问www.westoswsgi.com