apache(Web服务器):

Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性
被广泛使用,是最流行的Web服务器端软件之一。

apache的安装与访问:

注意:httpd是apache服务的软件包名称
[root@localhost mnt]# yum install httpd -y 安装阿帕奇服务

基于linux下的apache服务器部署_apache

httpd服务的主要配置文件如下:

配置文件的名称

存放位置

服务目录

/etc/httpd

主配置文件

/etc/httpd/conf/httpd.conf

网站数据目录

/var/www/html

访问日志

/var/log/httpd/access_log

错误日志

/var/log/httpd/error_log

[root@localhost mnt]# systemctl start httpd  打开阿帕奇服务
[root@localhost html]# systemctl stop firewalld 关闭防火墙
[root@localhost mnt]# netstat -antlupe | grep httpd 查看端口,apache默认端口为80
tcp6 0 0 :::80 :::* LISTEN 0 67447 4947/httpd
[root@localhost mnt]# cd /var/www/html/ 进入到apache的默认访问目录

基于linux下的apache服务器部署_apache_02

[root@localhost html]# vim index.html   在浏览器输入172.25.254.121查看,默认访问文件为index.html

基于linux下的apache服务器部署_vim_03


基于linux下的apache服务器部署_vim_04

[root@localhost html]# vim index.html  加粗字体访问

基于linux下的apache服务器部署_apache_05


基于linux下的apache服务器部署_vim_06

[root@localhost html]# vim test.html  
[root@localhost html]# cat test.html 加入绝对路径测试,输入172.25.254.121/test.html查看
test.age

基于linux下的apache服务器部署_vim_07


基于linux下的apache服务器部署_apache_08

配置httpd服务程序时最常用的参数以及用途描述:

参数

用途

ServerRoot

服务目录

ServerAdmin

管理员邮箱

User

运行服务的用户

Group

运行服务的用户组

ServerName

网站服务器的域名

DocumentRoot

网站数据目录

Directory

网站数据目录的权限

Listen

监听的IP地址与端口号

DirectoryIndex

默认的索引页界面

ErrorLog

错误日志文件

CustomLog

访问日志文件

Timeout

网页超时时间,默认为300秒

1,apache基础配置–修改默认端口:

[root@localhost html]# vim /etc/httpd/conf/httpd.conf 更改端口为8080
[root@localhost html]# systemctl restart httpd 修改配置文件必须重启httpd服务
[root@localhost html]# netstat -antlupe | grep httpd 查看端口
tcp6 0 0 :::8080 :::* LISTEN 0 70526 5159/httpd

基于linux下的apache服务器部署_apache_09

[root@localhost html]# vim /etc/httpd/conf/httpd.conf  将端口改回来
[root@localhost html]# systemctl restart httpd 重启服务
[root@localhost html]# netstat -antlupe | grep httpd 查看端口
tcp6 0 0 :::80 :::* LISTEN 0 70936 5198/httpd

基于linux下的apache服务器部署_html_10


操作演示:

基于linux下的apache服务器部署_vim_11

2, 修改apache默认访问目录:##

[root@localhost html]# mkdir /haha/html -p
[root@localhost html]# cd /haha/html/
[root@localhost html]# vim index.html编辑默认发布文件

基于linux下的apache服务器部署_vim_12

[root@localhost html]# vim /etc/httpd/conf/httpd.conf    更改配置文件修改默认访问目录

基于linux下的apache服务器部署_html_13

[root@localhost html]# systemctl restart httpd   重启服务

基于linux下的apache服务器部署_vim_14

在浏览器测试:

基于linux下的apache服务器部署_html_15

3,修改默认索引页界面为xfl.html:

[root@localhost html]# vim xfl.html

基于linux下的apache服务器部署_apache_16

[root@localhost html]# vim /etc/httpd/conf/httpd.conf 添加新文件允许直接访问

基于linux下的apache服务器部署_apache_17

[root@localhost html]# systemctl restart httpd

基于linux下的apache服务器部署_vim_18

在浏览器测试:

基于linux下的apache服务器部署_vim_19

4、配置默认访问目录:

[root@localhost html]# mkdir linux/
[root@localhost html]# ls
index.html linux xfl.html
[root@localhost html]# cd linux/
[root@localhost linux]# ls
[root@localhost linux]# vim index.html 编写自定义文件

基于linux下的apache服务器部署_html_20

[root@localhost linux]# vim /etc/httpd/conf/httpd.conf
#将/haha/html下的目录/linux也设置为可访问的目录以及里面的文件

基于linux下的apache服务器部署_apache_21

[root@localhost linux]# systemctl restart httpd

基于linux下的apache服务器部署_apache_22

在浏览器测试:

基于linux下的apache服务器部署_vim_23

apache的安全访问模式-基于用户的访问认证:

[root@localhost westos]# cd /etc/httpd/
[root@localhost httpd]# ls
conf conf.d conf.modules.d logs modules run
[root@localhost httpd]# htpasswd -cm apacheuser admin 建立用户
[root@localhost httpd]# cat apacheuser
admin:$apr1$/s.CSA09$.aDX.Tcgl8V7zKSDjjqqF0
[root@localhost httpd]# htpasswd -cm apacheuser tom 再次加入参数c会覆盖原来的,参数c表示创建
[root@localhost httpd]# cat apacheuser
tom:$apr1$i4qTag3h$N6tq1/im23Go00jq/.jpS1
[root@localhost httpd]# htpasswd -m apacheuser admin
[root@localhost httpd]# cat apacheuser 查看建立的用户
tom:$apr1$i4qTag3h$N6tq1/im23Go00jq/.jpS1
admin:$apr1$tN60p7ht$H7iL10QCHKCL.QAfrsiIf.

基于linux下的apache服务器部署_apache_24

恢复apache的默认目录,建立westos:

[root@testdb httpd]# cd /var/www/html/
[root@testdb html]# ls
index.html ks.cfg test.html
[root@testdb html]# mkdir westos 建立westos目录
[root@testdb html]# cd westos/
[root@testdb westos]# vim index.html 写入访问时看到的文件

基于linux下的apache服务器部署_vim_25

[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf   设定仅允许admin用户访问指定网页

基于linux下的apache服务器部署_apache_26

[root@localhost httpd]# systemctl restart httpd  用浏览器查看网页时需要登陆

基于linux下的apache服务器部署_vim_27


登陆admin用户:

基于linux下的apache服务器部署_apache_28


可以查看:

基于linux下的apache服务器部署_html_29

[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf   设定建立的所有用户都可以访问

基于linux下的apache服务器部署_vim_30

[root@localhost httpd]# systemctl restart httpd 用浏览器查看进行测试之前建立的tom用户

基于linux下的apache服务器部署_vim_31


可以查看:

基于linux下的apache服务器部署_html_32

apache的安全访问模式–基于IP的访问认证:

[root@localhost westos]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/westos">
122 Order Allow,Deny Allow在前先读取Allow
123 Allow from All
124 Deny from 172.25.254.84 Deny会覆盖Allow,意味着仅仅172.25.254.84的主机不可以查看,也就是我的真机IP进行测试
125 </Directory>

配置黑名单:

基于linux下的apache服务器部署_html_33

[root@localhost westos]# systemctl restart httpd  在浏览器测试172.25.254.121/westos/

IP为172.25.254.84在黑名单里面不允许查看:

基于linux下的apache服务器部署_vim_34


其他IP的虚拟机不在黑名单均可以查看:

基于linux下的apache服务器部署_apache_35


配置白名单:

[root@localhost westos]# vim /etc/httpd/conf/httpd.conf  配置白名单
<Directory "/var/www/html/westos">
Order Deny,Allow Deny在前先读取Deny
Allow from 172.25.254.84
Deny from All 意味着仅仅172.25.254.84可以查看文件内容
</Directory>

基于linux下的apache服务器部署_html_36

[root@localhost westos]# systemctl restart httpd
[root@localhost westos]# vim /etc/httpd/conf/httpd.conf 白名单在真机和虚拟机浏览器进行效果查看

IP为172.25.254.84在白名单可以访问网页:

基于linux下的apache服务器部署_html_37


其他IP的虚拟机不在白名单不可以访问网页:

基于linux下的apache服务器部署_vim_38


恢复实验环境:

基于linux下的apache服务器部署_html_39


操作演示:

基于linux下的apache服务器部署_html_40

基于域名的用户认证:

[root@foundation21 ~]# vim /etc/hosts 编辑配置文件

基于linux下的apache服务器部署_html_41

[root@foundation21 ~]# cat /etc/hosts 添加解析
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.250 content.example.com
172.25.254.121 www.westos.com news.westos.com music.westos.com login.westos.com

基于linux下的apache服务器部署_vim_42


在虚拟机配置文件:

[root@localhost httpd]# cd /etc/httpd
[root@localhost httpd]# ls
apacheuser conf conf.d conf.modules.d logs modules run
[root@localhost httpd]# cd conf.d/
[root@localhost conf.d]# ls
autoindex.conf README userdir.conf welcome.conf
[root@localhost conf.d]# vim default.conf 设定配置文件

基于linux下的apache服务器部署_vim_43

[root@localhost conf.d]# mkdir /var/www/virtual/westos.com/news/ -p   建立两个目录
[root@localhost conf.d]# mkdir /var/www/virtual/westos.com/music/ -p
[root@localhost conf.d]# vim /var/www/virtual/westos.com/news/index.html

基于linux下的apache服务器部署_vim_44

[root@localhost conf.d]# vim /var/www/virtual/westos.com/music/index.html

基于linux下的apache服务器部署_apache_45

[root@localhost conf.d]# vim news.conf 配置文件

基于linux下的apache服务器部署_apache_46

[root@localhost conf.d]# cp news.conf music.conf  使用:%s/news/music/g替换文件
[root@localhost conf.d]# vim music.conf 配置文件

基于linux下的apache服务器部署_apache_47

[root@localhost conf.d]# systemctl restart httpd
[root@localhost conf.d]# ls
autoindex.conf music.conf README welcome.conf
default.conf news.conf userdir.conf

基于linux下的apache服务器部署_html_48

在浏览器分别测试会出现不同的效果:

​1.www.westos.com​​,这里可以看到的是apache的默认访问文件index.html

基于linux下的apache服务器部署_vim_49

​2.news.wests.com​

基于linux下的apache服务器部署_apache_50

​3.music.westos.com​

基于linux下的apache服务器部署_html_51

简述HTTPS和HTTP区别:

HTTPS是以安全为目标的HTTP通道,简单讲是HTTP的安全版。https协议需要到ca申请证书,一般免费证书很少,需要
交费。而http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议http和https使用的是完全
不同的连接方式用的端口也不一样,前者是80,后者是443。http的连接很简单,是无状态的。HTTPS协议是由SSL+HTTP
协议构建的可进行加密传输、身份认证的网络协议 要比http协议安全。

HTTPS安装配置–安装证书及其私钥:

[root@localhost conf.d]# yum install mod_ssl.x86_64 -y 安装服务

基于linux下的apache服务器部署_apache_52

[root@localhost conf.d]# ls /etc/httpd/conf.d/
autoindex.conf music.conf README userdir.conf
default.conf news.conf ssl.conf welcome.conf
[root@localhost conf.d]# systemctl restart httpd 重启服务
[root@localhost conf.d]# yum install crypto-utils.x86_64 -y 安装私钥

基于linux下的apache服务器部署_apache_53

[root@localhost conf.d]# genkey www.westos.com 进入图形化界面

基于linux下的apache服务器部署_apache_54

选取安装密钥大小:

基于linux下的apache服务器部署_apache_55

基于linux下的apache服务器部署_apache_56

基于linux下的apache服务器部署_apache_57

进入虚拟机敲击键盘,不然会一直卡着:

基于linux下的apache服务器部署_apache_58

询问是否发送一个证书请求到证书当局,点击NO

基于linux下的apache服务器部署_apache_59

基于linux下的apache服务器部署_vim_60

配置自己的证书:

基于linux下的apache服务器部署_html_61

证书配置完成:

基于linux下的apache服务器部署_vim_62

用另外一台主机原因是方便替换配置文件,其实都是连接的一个虚拟机:

[root@foundation84 ~]# ssh root@172.25.254.221
root@172.25.254.221's password:
Last login: Sat May 26 21:32:23 2018
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# ls
autoindex.conf music.conf README tmprequest welcome.conf
default.conf news.conf ssl.conf userdir.conf
[root@localhost conf.d]# vim ssl.conf 查看主配置文件进行替换

基于linux下的apache服务器部署_apache_63

[root@localhost conf.d]# systemctl restart httpd 重启服务

基于linux下的apache服务器部署_html_64

在浏览器中测试https://www.westos.com进会进入下载证书界面:

基于linux下的apache服务器部署_html_65


基于linux下的apache服务器部署_apache_66


下载证书:

基于linux下的apache服务器部署_vim_67


点击网址左边的锁图标可以展示证书:

基于linux下的apache服务器部署_vim_68

清楚网页缓存历史的步骤,方便进行实验演示:

基于linux下的apache服务器部署_vim_69


基于linux下的apache服务器部署_vim_70


基于linux下的apache服务器部署_apache_71

网页重写:

[root@foundation21 ~]# vim /etc/hosts  加入login解析

基于linux下的apache服务器部署_apache_72

在虚拟机进行配置:

[root@localhost conf.d]# ls
autoindex.conf music.conf README tmprequest welcome.conf
default.conf news.conf ssl.conf userdir.conf
[root@localhost conf.d]# cp news.conf login.conf
[root@localhost conf.d]# vim login.conf 编辑login配置文件

基于linux下的apache服务器部署_vim_73

[root@localhost conf.d]# systemctl restart httpd
[root@localhost conf.d]# mkdir -p /var/www/virtual/westos.com/login/
[root@localhost conf.d]# vim /var/www/virtual/westos.com/login/index.html

基于linux下的apache服务器部署_vim_74

在浏览器测试是得点击下载证书就会出现自己所写的内容:

基于linux下的apache服务器部署_html_75


基于linux下的apache服务器部署_html_76


当输入login.westos.com会自动跳转到https加密方式。

基于linux下的apache服务器部署_apache_77

简述CGI和PHP语言:

CGI是通用网关协议,是开发动态网页的一个标准,遵循这个标准就可以用BAT、CMD、sh、PERL、C、C++、PERL以及
PHP等语言编写程序处理网页请求和返回数据到客户端浏览器。CGI是协议和标准,是一种规范,不是语言。
PHP是一个动态网页开发语言,主要用于处理浏览器提交的数据以及返回结果给浏览器。PHP可以以CGI方式工作,也可以
以ISAPI、NSAPI等模块方式工作。

PHP语言支持:

一开始在浏览器输入172.25.254.221查询到Hello world

基于linux下的apache服务器部署_vim_78

[root@localhost conf.d]# cd /var/www/html/  
[root@localhost html]# ls
index.html test.html westos
[root@localhost html]# vim index.php

基于linux下的apache服务器部署_html_79

[root@localhost html]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# ls
autoindex.conf login.conf news.conf ssl.conf userdir.conf
default.conf music.conf README tmprequest welcome.conf
[root@localhost conf.d]# vim /etc/httpd/conf/httpd.conf 编辑改变默认访问顺序

基于linux下的apache服务器部署_vim_80

[root@localhost conf.d]# systemctl restart httpd   重启服务进去浏览器查看为空白

基于linux下的apache服务器部署_apache_81

[root@localhost conf.d]# yum install php -y  安装PHP
[root@localhost conf.d]# systemctl restart httpd 重启服务

基于linux下的apache服务器部署_html_82


安装之后在网页测试就可以看到PHP界面:

基于linux下的apache服务器部署_vim_83

CGI语言支持:

[root@localhost conf.d]# cd /var/www/html/
[root@localhost html]# ls
index.html index.php test.html westos
[root@localhost html]# mkdir cgi
[root@localhost html]# ls
cgi index.html index.php test.html westos
[root@localhost html]# vim cgi/index.cgi 编辑CGI配置文件

基于linux下的apache服务器部署_vim_84

[root@localhost html]# chmod +x cgi/index.cgi   增加权限
[root@localhost html]# systemctl restart httpd 重启服务
[root@localhost html]# ./cgi/index.cgi 运行脚本
Content-type: text/html

Tue May 29 23:02:38 EDT 2018
进入浏览器输入172.25.254.121/cgi/index.cgi就会出现文件内容而不执行

基于linux下的apache服务器部署_vim_85


修改配置文件使得网页可以实时运行.cgi文件:

[root@localhost html]# ls
cgi index.html index.php test.html westos
[root@localhost html]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# ls
autoindex.conf login.conf news.conf README tmprequest welcome.conf
default.conf music.conf php.conf ssl.conf userdir.conf
[root@localhost conf.d]# vim default.conf 编辑配置文件

基于linux下的apache服务器部署_vim_86

[root@localhost conf.d]# systemctl restart httpd重启服务
进去浏览器输入172.25.254.121/cgi/index.cgi就会自动运行脚本

基于linux下的apache服务器部署_html_87

创建一个 Discuz论坛:

[root@localhost ~]# systemctl start mariadb  开启数据库服务
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls 这个安装包可以在网上下载
cgi Discuz_X3.2_SC_UTF8.zip index.html index.php test.html westos
[root@localhost html]# unzip Discuz_X3.2_SC_UTF8.zip

基于linux下的apache服务器部署_vim_88

[root@localhost html]# ls
cgi index.html readme upload westos
Discuz_X3.2_SC_UTF8.zip index.php test.html utility
[root@localhost html]# chmod 777 /var/www/html/upload/ -R
[root@localhost html]# yum install php-mysql -y

基于linux下的apache服务器部署_apache_89

[root@localhost html]# systemctl restart httpd

基于linux下的apache服务器部署_apache_90


在浏览器输入172.25.254.221/upload进去安装。

基于linux下的apache服务器部署_html_91


基于linux下的apache服务器部署_vim_92


基于linux下的apache服务器部署_html_93


用之前建立的用户认证进行登陆:数据库的信息与配置数据库时候一致即可

基于linux下的apache服务器部署_vim_94


基于linux下的apache服务器部署_vim_95


基于linux下的apache服务器部署_vim_96


最后输入用户密码就可以登陆:

基于linux下的apache服务器部署_vim_97