Apache web服务
# yum install -y httpd httpd-manual
启动apache服务:
# systemctl start httpd ; systemctl enable httpd
查看监听端口:
# ss -antlp |grep httpd
LISTEN 0 128 :::80 :::*
Apache主配置文件: /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd" 用于指定Apache的运行目录
Listen 80 监听端口
User apache 运行apache程序的用户和组
Group apache
ServerAdmin [email protected] 管理员邮箱
DocumentRoot "/var/www/html" 网页文件的存放目录
<Directory "/var/www/html"> <Directory>语句块自定义目录权限
Require all granted
</Directory>
ErrorLog "logs/error_log" 错误日志存放位置
AddDefaultCharset UTF-8 默认支持的语言
IncludeOptional conf.d/*.conf 加载其它配置文件
DirectoryIndex index.html 默认主页名称
修改apache默认发布文件
vim /etc/httpd/conf/httpd.conf
####################
164 DirectoryIndex westos.html
####################
修改默认发布目录
selinux状态为disabled
vim /etc/httpd/conf/httpd.conf
##############################
120 DocumentRoot "/westos/www/test"
<Directory "/westos/www/test">
Require all granted
</Directory>
####################################
apache的访问控制
设定ip的访问
首先把前一个实验的默认发布目录改回来
#############################################
#
DocumentRoot "/var/www/html"
#DocumentRoot "/westos/www/test"
<Directory "/westos/www/test">
Require all granted
</Directory>
#############################################
<Directory "/var/www/html/admin"> ##允许所有人访问admin目录但是拒绝166主机
125 Order Allow,Deny
126 Allow from All
127 Deny from 172.25.254.66
128 </Directory>
###############################################
333#############################################
<Directory "/var/www/html/admin"> ##只允许66主机访问admin目录
Order Deny,Allow
Allow from 172.25.254.66
Deny from All
</Directory>
设定用户
[email protected] html]# htpasswd -m /etc/httpd/accessuser admin ###给admin设置钥匙
[[email protected] html]# vim /etc/httpd/conf/httpd.conf
##############################################33
#
DocumentRoot "/var/www/html"
#DocumentRoot "/westos/www/test"
<Directory "/westos/www/test">
Require all granted
</Directory>
<Directory "/var/www/html/admin">
AuthUserFile /etc/httpd/accessuser ##用户认证文件
AuthName "please input your name and password !!" ## ##用户认证提示信息
AuthType basic ##认证类型
Require valid-user ##认证用户 认证文件中 所以用户可以访问
</Directory>
『 Require user admin 』 ##只允许认证文件中admin用户可以访问
################################################################
apache语言支持
php html cgi
html语言默认支持
php语言
yum install php -y
systemctl restart httpd
CGI语言
[[email protected] ~]# mkdir /var/www/html/cgi ##创建cgi目录
[[email protected] cgi]# vim index.cgi ##创建执行文件
[[email protected] cgi]# vim index.cgi
[[email protected] cgi]# chmod +x index.cgi ##给执行权限
[[email protected] cgi]# vim /etc/httpd/conf/httpd.conf
测试
Apache虚拟主机
可以让一台Apache服务器在访问不同域名的时候显示不同主页
建立测试页 配置
[[email protected] ~]# cd /var/www/
[[email protected] www]# mkdir virtual/news.westos.com/html -p
[[email protected] www]# mkdir virtual/money.westos.com/html -p
[[email protected] www]# echo "money.westos.com's page" >virtual/money.westos.com/html/index.html
[[email protected] www]# echo "news.westos.com's page" >virtual/news.westos.com/html/index.html
[[email protected] www]# vim /etc/httpd/conf.d/default.conf ##指定域名的访问都访问default
#################################################
<Virtualhost _default_:80> ##虚拟主机开启的端口
DocumentRoot "/var/www/html" ##虚拟主机的默认发布目录
CustomLog "logs/default.log" combined ##虚拟主机的日志
</Virtualhost>
###################################################
[[email protected] www]# vim /etc/httpd/conf.d/news.conf ###指定域名news.westos.com的访问到指定默认发布目录中
######################################################
<Virtualhost *:80>
ServerName "news.westos.com"
DocumentRoot "/var/www/virtual/news.westos.com/html"
CustomLog "logs/news.log" combined
</Virtualhost>
<Directory "/var/www/virtual/news.westos.com/html" > ##默认发布目录的访问授权
Require all granted
</Directory>
##################################################
[[email protected] www]# vim /etc/httpd/conf.d/money.conf ##同news同理
[[email protected] www]# systemctl restart httpd
测试效果:
https:
Hyper Text Transfer Protocol over Secure Socket Layer
通过ssl
yum install mod_ssl-y
yum install mod_ssl -y
yum install crypto-utils -y
genkey www.westos.com
vim /etc/httpd/conf.d/login.conf
#############################################
<Virtualhost *:443>
ServerName "login.westos.com"
DocumentRoot "/var/www/virtual/login.westos.com/html" ##虚拟主机默认发布目录
CustomLog "logs/login.log" combined
SSLEngine on ##开启HTTPS功能
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt ##证书
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key ##**
</Virtualhost>
<Directory "/var/www/virtual/login.westos.com/html"> ##默认发布目录
Require all granted
</Directory>
<Virtualhost *:80> ##网页重写实现自动访问HTTPS
ServerName "login.westos.com"
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>
###################################################
* ^(/.*)$ 客户主机在地址栏中写入所有字符 测试中的login.westos.com
* https:// 定向成为访问协议
*%{HTTP_HOST} 客户请求主机
*$1 表示^(/.*)$
*[redirect=301] 临时重定向 302永久重定向
mkdir /var/www/virtual/login.westos.com/html -p
vim /var/www/virtual//login.westos.com/html/index.html
systemctl restart httpd
测试:
vim /etc/hosts ##172.25.254.166 login.westos.com