Apache的基本配置
首先使用yum命令安装httpd服务
yum install httpd -y
然后关闭防火墙,开启httpd服务
systemctl stop firewalld.service
systemctl start httpd
httpd的基本配置文件为/etc/httpd/conf/httpd.conf
我们可以使用vim进入查看或者修改其中的一些参数
默认的发布目录为/var/www/html
默认的发布文件为index.html
我们可以编辑/var/www/html下的index.html文件如下
vim /var/www/html/index.html
然后在浏览器中输入本机ip,有如下的效果
如果需要修改默认发布目录,需要将SELinux设置为disabled然后,进入主配置文件中修改
vim /etc/httpd/conf/httpd.conf
假设我要将默认发布目录设置为我新建的一个目录
mkdir /test/www -p
然后进入配置文件中修改如下(先注释119行原先的目录)
然后重启服务即可
systemctl restart httpd
tips:
如果在SElinux是enforcing的状态下,我们可以进行以下操作来解决上下文不匹配的问题
semanage fcontext -a -t httpd_sys_content_t '/test(/.*)?'
restorecon -RvvF /test/
apache虚拟主机
如果是同一个站点的不同网页,好比腾讯,www.qq.com与news.qq.com都是同一个IP,在这时就需要用到虚拟主机,虚拟主机配置的方法与流程大致如下
首先我们先将测试页建立好
cd /var/www/
mkdir virtual/news.test.com/html -p
echo "<h1>news.test.com's page</h1>" > virtual/news.test.com/html/index.html
配置默认的配置文件
vim /etc/httpd/conf.d/default.conf
<Virtualhost _default_:80>
DocumentRoot "/var/www/html"
CustomLog "logs/default.log" combined
</Virtualhost>
vim /etv/httpd/conf.d/news.conf #配置子配置文件
<Virtualhost *80> #虚拟主机的开启端口
ServerName "news.test.com" #主机名
DocumentRoot "/var/www/virtual/news.test.com/html"#默认发布目录
CustomLog "logs/new.log" combined #目录
</Virtualhost>
<Directory "/var/www/virtual/news.test.com/html">
Require all granted#默认发布目录授权
</Directory>
重启服务
systemctl restart network
然后修改/etc/hosts下文件进行测试
IP地址 www.test.com news.test.com
最后测试效果为
apache用户登录访问发布目录的设置
首先我们建立用户的目录
cd /var/www/html/
mkdir admin
vim index.html
接下来,开始设定用户的访问
htpasswd -m -c /etc/httpd/accessuser admin
设定用户名为admin,然后密码为redhat
然后进入主配置文件中进行修改
158<Directory "/var/www/html/admin">
159 AuthUserFile /etc/httpd/accessuser #用户认证文件
160 AuthName "please input your name and password!"#用户认证提示信息
161 AuthType basic #认证类型
162 Require valid-user#认证用户,认证文件用户中所有可以访问
163 </Directory>
重启服务
systemctl restart httpd
最后的测试效果如下
输入用户和密码之后,页面如下
自定义证书
HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。
首先我们安装ssl和加密的软件
yum install mod_ssl -y
yum intall crypto-utils -y
然后执行genkey命令加密 www.test.com网页
然后copy下证书文件的绝对地址和密钥的绝对地址
/etc/pki/tls/cert/www.westos.com.crt
/etc/pki/tls/private/www.test.com.key
然后进入ssl配置文件中进行以下修改
vim /etc/httpd/conf.d/ssl_conf
然后进入子配置文件中修改如下
vim /etc/httpd/conf.d/login.conf
<Virtualhost *:443>
ServerName "login.test.com"
DocumentRoot "/var/www/virtual/login.test.com/html"
CustomLog "logs/login.log" combined
SSlEngine on #开启https的功能
SSLCertificateFile /etc/pki/tls/cert/www.westos.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.test.com.key##证书文件与密钥文件
</Virtualhost>
<Directory "/var/www/virtual/login.test.com/html">
Require all granted
</Directory>
<Virtualhost *:80>##网络重写自动访问https
ServerName login.test.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>
tips:
在RULE中
^(/.*)https://1
^(/.*)客户主机在地址栏中写入的所有字符https://定向为访问协议1 就代表^(/.*)$ 输入的字符
301代表临时重定向,302代表永久重定向
然后重启httpd服务即可
systemctl restart httpd
apache的语言支持
http默认支持的有三种脚本语言
1.html
2.php
3.cgi
html之前已经演示过了,主要是php和cgi的使用
php语言只需要安装一个php支持的库
yum install php -y
cgi脚本