一、企业中常用的web服务,用后来提供http://(超文本传输协议)
二、apache的安装部署
yum install httpd -y ##下载httpd服务 -y为yes
yum install httpd-manual.noarch -y ##此为httpd的一个说明,在浏览器中使用:172.25.254.173/manaul
systemctl stop firewalld ##关闭防火墙
systemctl disabled firewalld ##开机自动关闭防火墙
systemctl start httpd ##开启httpd服务
systemctl enable httpd ##开机自动开启httpd服务
三、apache的基础信息
#主配置目录: /etc/httpd/conf
#主配置文件: /etc/httpd/conf/http.conf
#子配置目录: /etc/http/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
查看端口:
测试:172.25.254.173:8080
注意:此时直接输入ip的不能进入的,端口已经被修改。
四、修改默认发布文件
默认发布文件就是访问apache时没有指定文件名称时默认访问的文件
这个文件可以指定多个,有访问顺序,当index.html文件不存在时,自动读取test.html文件。
vim /etc/httpd/cong/httpd.conf
164 <IfModule dir_module>
DirectoryIndex index.html test.html #当index.html不存在时访问test.html
</IfModule>
测试结果:
在Index.html文件存在时,默认读取index.html文件
当index.html文件不存在时,读取test.html文件
五、修改默认发布目录
vim /etc/httpd/conf/httpd.conf
119 #DocumentRoot "/var/www/html" ##防止将文件改错,将本行复制并注销。
120 DocumentRoot "/www/html" ##将发布目录改为/www/html。
121 <Directory "/www/" >
122 Require all granted
123 </Directory>
semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?'
storecon -RvvF /www/
##selinux在为强制状态时,不会读取/www/目录下的文件。
##因为安全上下文的关系,此时将安全上下文改为httpd的安全上下文模式。
测试结果:
六、apache的虚拟主机
mkdir -p /var/www/virtual/c.westos.com/html ##-p为在不存在的目录下新建立目录
mkdir -p /var/www/virtual/linux.westos.com/html
vim /var/www/virtual/c.westos.com/html/index.html
vim /var/www/virtual/linux.westos.com/html/index.html
vim /etc/httpd/conf.d/adefault.conf
<VirtualHost _default_:80>
DocumentRoot "/var/www/html"
CustomLog "logs/www.westos.com.log" combined
</VirtualHost>
vim /etc/httpd/conf.d/linux.conf
<VirtualHost *:80>
ServerName linux.westos.com
DocumentRoot "/var/www/virtual/linux.westos.com/html"
CustomLog "logs/linux.westos.com.logs" combined
</VirtualHost>
<Directory "/var/www/virtual/linux.westos.com/html">
Require all granted
</Directory>
vim /etc/httpd/conf.d/c.conf
<VirtualHost *:80>
ServerName c.westos.com #指定站点名称
DocumentRoot "/var/www/virtual/c.westos.com/html" #站点默认发布目录
CustomLog "logs/c.westos.com.logs" combined #站点日志combined表示四种日志的集和
</VirtualHost>
<Directory "/var/www/virtual/c.westos.com/html">
Require all granted
</Directory>
测试:
在测试主机中做好本地解析
vim /etc/hosts
172.25.254.32 linux.westos.com
七、apache内部的访问控制
1.针对与主机的访问控制
<Directory "/var/www/html/test">
Order allow,deny ##列表的读取顺序,后读取的列表会覆盖先读的列表内容里重复部分
Allow from all
Deny from 172.25.254.73 ##允许所有人访问,除了主机172.25.254.73
</Directory>
测试:
2.针对用户的访问控制
htpasswd -cm /etc/httpd/userpass zhaoyan ##创建第一个用户使用参数-cm,
htpasswd -m /etc/httpd/userpass zhaoyan1 ##创建以后的用户不能使用-c参数,否则会将前面所有用户的信息进行了覆盖
vim /etc/httpd/adefault.conf
<Directory "/var/www/html/zhaoyan">
AuthUserFile /etc/httpd/userpass
AuthName "Please input your name and passwd"
AuthType basic
Require user zhaoyan #只有admin用户可以登陆
# Require valid-user #合法用户都可以登陆
</Directory>
测试:
八、apache支持的语言
1.html
上述的所有语言均为html,在浏览器中实现。
2.php ##此php语言可以直接在manaul中找到语句,复制即可。
vim /var/www/html/index.php
<?php
phpinfo();
?>
yum install php -y
systemctl restart httpd
测试
172.25.254.32/index.php
3.cgi
mkdir -p /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/index.cgi
#!/usr/bin/perl
print "Content-type: text\html\n\n";
print `date`;
chmod +x /var/www/html/cgi/index.cgi
/var/www/html/cgi/index.cgi
vim /etc/httpd/conf.d/adefault.conf
<Directory "/var/www/html/cgi">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
测试
172.25.254.232/cgi/index.cgi
九、https
http这个传输协议传输数据是以明码传输的,https://hostname这种连接方式,采用的是SSL加密的机制。
yum install mod_ssl.x86_64 -y
yum install crypto-utils -y
genkey www.zhaoyan.com
vim /etc/httpd/conf.d/ssl/conf
100
#SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateFile /etc/pki/tls/certs/www.zhaoyan.com.crt
108
#SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
SSLCertificateKeyFile /etc/pki/tls/private/www.zhaoyan.com.key
测试
https://172.25.254.173
查看证书:
十、自动跳转至443
vim /etc/httpd/login.conf
<VirtualHost *:443>
ServerName login.westos.com
DocumentRoot /var/www/html/virtual/login.westos.com/html
CustomLog "logs/login.logs" combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.zhaoyan.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.zhaoyan.com.key
</VirtualHost>
<Directory "/var/www/html/virtual/login.westos.com/html">
Require all granted
</Directory>
<VirtualHost *:80>
ServerName login.westos.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>
测试:
做好本地解析:vim /etc/hosts
172.25.254.173 Login.westos.com