1.apache简介
Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中。
2. apache 的配置文件
/etc/httpd/conf/httpd.conf ##apache的主配置文件
/etc/httpd/conf.d/*.conf ##/etc/httpd目录下所有以.conf结尾的都为apache的子配置文件
3.apache的默认端口
80
4.apache的基本配置
1)修改默认发布文件
默认发布文件一般在/var/www/html/目录下
[root@test ~]# vim /etc/httpd/conf/httpd.conf ##修改主配置文件
163 <IfModule dir_module>
164 DirectoryIndex index.html exe.html ##在这一行添加exe.html,访问时会默认访问exe.html文件
165 </IfModule>
[root@test ~]# systemctl restart httpd.service ##重启httpd服务
[root@test ~]# vim /var/www/html/exe.html ##编写 默认发布文件
<h1> hello world</h1>
效果如下:
2) 修改默认发布目录
默认发布目录为/var/www/html/
[root@test ~]# mkdir /westos
[root@test ~]# mkdir /westos/www/
[root@test ~]# mkdir /westos/www/test ##建立目录
[root@test ~]# vim /westos/www/test/exe.html ##编写文件内容
<h1>The world is very beautiful </h1>
[root@test ~]# vim /etc/httpd/conf/httpd.conf ##配置文件
119 #DocumentRoot "/var/www/html" ##注释之前的
120 DocumentRoot "/westos/www/test" ####添加此目录为默认发布目录
121 <Directory "/westos/www/test"> ##设置所有都可以访问
122 Require all granted ##允许所有可以访问
123 </Directory>
[root@test ~]# systemctl restart httpd.service ##重启服务
[root@test ~]# getenforce
Enforcing
[root@test ~]# setenforce 0 ##设置selinux为permissive
[root@test ~]# getenforce
Permissive
如果selinux是disabled状态时,就配置完文件之后就可以访问了,
如果selinux是enforcing状态时,先关闭selinux,更改上下文之后,再设置selinux为enforcing
[root@test ~]# vim /etc/sysconfig/selinux
SELINUX=disabled
[root@test ~]# semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?' ##更改文件安全上下文
[root@test ~]# restorecon -RvvF /westos ##刷新
[root@test ~]# vim /etc/sysconfig/selinux
SELINUX=enforcing
3限制用户访问目录
[root@test html]# mkdir /var/www/html/admin ##建立目录
[root@test html]# vim /var/www/html/admin/exe.html
<h1> ......</h1>
[root@test test]# vim /etc/httpd/conf/httpd.conf ##配置文件
119 DocumentRoot "/var/www/html"
120 <Directory "/var/www/html/admin">
121 Order Deny,Allow ##设置顺序 先Deny 后Allow,当范围有冲突时,以后一个为准
122 Deny from All #禁止所有用户可以访问
123 Allow from 172.25.254.20 ##允许172.25.254.20访问
124 </Directory>
[root@test html]# systemctl restart httpd.service
172.25.254.20主机测试如下: 可以访问
其他主机测试如下:禁止访问
4设置用户访问密码,限制用户访问
1.生成加密文件
[root@test ~]# htpasswd -cm /etc/httpd/accessuer exe.html
New password: ##输入密码
Re-type new password: ##再次输入一遍
Adding password for user exe.html
##htpasswd为加密的命令 -c 创建一个加密文件 -m 默认apache htpassswd命令采用MD5算法对密码进行加密 /etc/httpd/accessuser为加密文件 exe.html为用户名
[root@test ~]# cat /etc/httpd/accessuer ##查看加密文件
exe.html:$apr1$d.HmY5G4$iFvyMFoTOEAmbdgcrqrGD1
[root@test ~]# vim /etc/httpd/conf/httpd.conf
120 <Directory "/var/www/html/exe.html"> ##添加这段内容
121 AuthUserFile /etc/httpd/accessuser ##用户认证文件
122 AuthName "please input your name and password!!" ##用户提示信息
123 AuthType basic ##认证类型
124 Require valid-user ##认证用户,认证文件中全部用户都可以访问
【Require user exe.html】只允许认证文件中exe.html用户访问
125 </Directory>
[root@test ~]# systemctl restart httpd.service
测试如下:
ip加上具体文件位置,如果只是ip的话,会直接访问出结果,这是因为我们只把一个具体的文件进行加密认证,如果只是ip,主配置文件里边写了允许所有访问。
5.apache语言支持
1) html语言
[root@test ~]# vim /var/www/html/exe.html
<h1> hello world</h1>
[root@test ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
Require all granted ##允许所有可以访问
</Directory>
[root@test ~]# systemctl restart httpd.service
测试如下
2) php语言
[root@test ~]# yum install php -y 安装php服务
[root@test ~]# vim /var/www/html/index.php ##编写发布文件 phpinfo是一个运行指令,为显示php服务器的配置信息
<?php
phpinfo();
?>
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
<IfModule dir_module>
DirectoryIndex index.html aa.html index.php #添加默认发布类型
</IfModule>
[root@localhost ~]# systemctl restart httpd.service
测试如下:
3) cgi语言
通用网关接口(CGI)是网站上放置动态内容的最简单的方法。CGI脚本可用于许多目的,但是谨慎控制使用哪个CGI脚本以及允许谁添加和运行这些脚本十分重要。编写质量差的CGI脚本可能为外部攻击者提供了破坏网站及其内容安全性的途径。因此,在Web服务器级别和SELinux策略级别,都存在用于限制CGI脚本使用的设置。
[root@test /]# mkdir /var/www/html/cgi ##建立发布目录
[root@test /]# vim /index.cgi ##编写发布文件 一定要在根目录下
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[root@test html]# vim /etc/httpd/conf/httpd.conf ##修改配置文件
<Directory "/var/www/html/cgi">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
[root@test /]# chmod +x index.cgi ##给文件一个可执行权限
[root@test /]# ll
-rwxr-xr-x. 1 root root 68 May 15 02:11 index.cgi
[root@test /]# setenforce 0 ##调整selinux为peimissive 或者也可以关闭selinux,也可以更改文件安全上下文
[root@test /]# getenforce
Permissive
[root@test /]# systemctl restart httpd.service ##重启服务
测试如下: