目录
一、关闭 selinux
1、关闭 selinux
2、修改 selinux 的配置文件
二、关闭防火墙
三、安装 epel 源
四、yum 安装 Apache软件
五、启动 Apache 网站
六、查看httpd服务是否启动
七、Apache 配置实例
例1:建立网站主页,在网站根目录下建立一个主页文件
例2:将网站家目录修改成:/www目录
例3:修改主页类型或者主页名
八、Apache 常用命令
1、源码包安装
2、解压并编译
3、常用命令
九、Apache 配置用户认证
十、Apache 配置默认虚拟主机
十一、Apache 配置rewrite规则
1、域名301跳转
2、禁止指定user_agent
3、通过rewrite 限制某个目录
十二、Apache 配置日志切割与管理
1、日志切割
2、不记录指定文件类型的日志
十三、配置静态缓存
十四、配置防盗链
十五、访问控制
1、 针对某些目录进行访问控制,禁止一些IP地址的访问
2、针对URI 做限制访问
十六、apache 禁止解析php
[root@localhost ~]# setenforce 0
[root@localhost www]# vim /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# yum -y install epel-release.noarch
[root@localhost ~]# yum -y install httpd
[root@localhost conf]# systemctl restart httpd
[root@localhost conf]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 84651 root 4u IPv6 407850 0t0 TCP *:http (LISTEN)
httpd 84652 apache 4u IPv6 407850 0t0 TCP *:http (LISTEN)
httpd 84653 apache 4u IPv6 407850 0t0 TCP *:http (LISTEN)
httpd 84654 apache 4u IPv6 407850 0t0 TCP *:http (LISTEN)
httpd 84655 apache 4u IPv6 407850 0t0 TCP *:http (LISTEN)
httpd 84656 apache 4u IPv6 407850 0t0 TCP *:http (LISTEN)
[root@localhost conf]# cd /var/www/html/
[root@localhost html]# vi index.html
welcome to apache
验证:
[root@localhost html]# curl 10.0.0.129
welcome to apache
或者
[root@localhost conf]# echo 'welcome to apache' > /var/www/html/index.html
[root@localhost html]# mkdir /www
[root@localhost html]# vi /etc/httpd/conf/httpd.conf
119 DocumentRoot "/www"
131
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# cd /www
[root@localhost www]# vi index.html
www 2
验证:
[root@localhost www]# curl 10.0.0.129
www 2
[root@localhost www]# vim /etc/httpd/conf/httpd.conf
164 DirectoryIndex index.php
[root@localhost www]# systemctl reload httpd
[root@localhost www]# echo "php main page" >/www/index.php
验证:[root@localhost www]# curl 10.0.0.129
php main page
[root@localhost ~]# yum -y install gcc make zlib-devel pcre-devel openssl-devel
[root@localhost ~]# yum -y install lrzsz-0.12.20-36.el7.x86_64
[root@localhost tmp]# rz //上传httpd的源码包
httpd-2.4.51.tar.gz的下载链接:Download - The Apache HTTP Server Project
[root@localhost tmp]# tar xf httpd-2.4.51.tar.gz
[root@localhost tmp]# cd httpd-2.4.51/
[root@localhost httpd-2.4.51]# yum -y install apr-*
[root@localhost httpd-2.4.51]# ./configure --prefix=/usr/local/apache2 && make && make install
[root@localhost ~]# /usr/local/apache2/bin/apachectl -M # 查看常见的模
块(包括动态和静态)
[root@localhost ~]# /usr/local/apache2/bin/apachectl -l # 查看加载的静
态模块
[root@localhost ~]# /usr/local/apache2/bin/apachectl -t # 检查配置文件
有无语法错误
[root@localhost ~]# /usr/local/apache2/bin/apachectl graceful # 加载配置文件,但
不重启
[root@localhost ~]# /usr/local/apache2/bin/apachectl start/restart/stop # 启动/
重启/停止apache服务
[root@localhost httpd-2.4.51]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
DocumentRoot "/data/www/abc"
# /data/www/abc为将要访问的页面的目录
AllowOverride AuthConfig
AuthName "web"
AuthType Basic
AuthUserFile /data/.htpasswd # 指定存放用户名和密码的文件
require valid-user
[root@localhost httpd-2.4.51]# vim /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf //把这个字段的注释去掉
htpasswd 生成密码文件
[root@localhost httpd-2.4.51]# htpasswd -c /data/.htpasswd web
[root@localhost httpd-2.4.51]# /usr/local/apache2/bin/apachectl graceful
[root@localhost httpd-2.4.51]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
DocumentRoot "/tmp/111" # 该目录可以不存在
ServerName 111.com
DocumentRoot "/data/www"
ServerName www.test.com
ServerAlias www.aaa.com
[root@localhost ~]# vi /usr/local/apache2/conf/httpd.conf
Require all granted //在207行中修改文件
添加本地解析
[root@localhost ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.129 www.111.com
10.0.0.129 www.aaa.com
10.0.0.129 www.test.com
[root@localhost ~]# vi /usr/local/apache2/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so //把#注释去掉
[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
DocumentRoot "/data/www"
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.aaa.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.bbb.com$
RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L]
重启服务
[root@localhost www]# /usr/local/apache2/bin/apachectl restart
DocumentRoot "/data/www"
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^.*curl.* [NC,OR] # 禁止curl和chrome浏览器访问
RewriteCond %{HTTP_USER_AGENT} ^.*chrome.* [NC] # NC是不区分大小写的意思
RewriteRule .* - [F] # 为禁止的意思
DocumentRoot "/data/www"
RewriteEngine on
RewriteCond %{REQUEST_URI} ^.*/tmp/.* [NC] # 禁止访问tmp目录
RewriteRule .* - [F] # 为禁止的意思
打开虚拟主机配置文件里的日志记录,日志的路径在/usr/local/apache2/logs下
ErrorLog "logs/test.com-error_log" # 错误日志
CustomLog "logs/test.com-access_log" common # common 是日志类型
修改虚拟主机的access log的定义:
[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
DocumentRoot "/data/www"
ServerName www.test.com
CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test.com-access_%Y%m%d_log 86400" combined
重启服务
[root@localhost www]# /usr/local/apache2/bin/apachectl restart
[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
DocumentRoot "/data/www"
ServerName www.test.com
ErrorLog "logs/test.com-error_log"
SetEnvIf Request_URI ".*\.gif$" image-request
SetEnvIf Request_URI ".*\.jpg$" image-request
SetEnvIf Request_URI ".*\.png$" image-request
SetEnvIf Request_URI ".*\.bmp$" image-request
SetEnvIf Request_URI ".*\.swf$" image-request
SetEnvIf Request_URI ".*\.js$" image-request
SetEnvIf Request_URI ".*\.css$" image-request
CustomLog "|/usr/local/apache2/bin/rotatelogs -l
/usr/local/apache2/logs/test.com-access_%Y%m%d_log 86400" combined env=!imagerequest
对于图片,html,css,Javascripts等,让客户端缓存,让访问时不需要去服务器上访问,提高用户体验,需要提前确认是否支持mod_expires.c模块
进入http主配置文件,把mod_expires.c模块注释去掉
[root@localhost www] vi /usr/local/apache2/conf/httpd.conf
LoadModule expires_module modules/mod_expires.so
[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
DocumentRoot "/data/www"
ServerName www.test.com
ExpiresActive on
ExpiresByType image/gif "access plus 1 days"
ExpiresByType image/jpeg "access plus 24 hours"
ExpiresByType image/png "access plus 24 hours"
ExpiresByType text/css "now plus 2 hours"
ExpiresByType application/x-javascript "now plus 2 hours"
ExpiresByType application/x-shockwave-flash "now plus 2 hours"
ExpiresDefault "now plus 0 min"
进入/data/www,上传一张图片作为测试
[root@www www]# rz
[root@www www]# chmod 777 123.PNG
[root@www www]# ls
123.PNG abc index.html
[root@www www]# /usr/local/apache2/bin/apachectl restart
查看配置结果
[root@www www]# curl -x 10.0.0.128:80 'http://www.test.com/123.PNG' -I
HTTP/1.1 200 OK
Date: Thu, 04 Nov 2021 12:23:11 GMT
Server: Apache/2.4.51 (Unix)
Last-Modified: Thu, 04 Nov 2021 12:14:47 GMT
ETag: "1ba1e-5cff578f7dbc0"
Accept-Ranges: bytes
Content-Length: 113182
Cache-Control: max-age=86400
Expires: Fri, 05 Nov 2021 12:23:11 GMT
Content-Type: image/png
防止其他的网站大量使用自己网站里的一些图片,流量跑的是自己的网站,造成带宽的浪费。防止图片被盗用。
在虚拟主机中配置文件(配置中的文字作为说明,配置的时候不用加上)
[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
DocumentRoot "/data/www"
ServerName www.test.com
SetEnvIfNoCase Referer "^http://.*\.test\.com" local_ref # 给网址做标注
# 规定一下哪些类型的文件
Order Allow,Deny # 禁止引用
Allow from env=local_ref # 允许local_ref引用
[root@www www]# /usr/local/apache2/bin/apachectl restart
[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
DocumentRoot "/data/www"
ServerName www.test.com
AllowOverride None
Options None
Order allow,deny
Allow from all
deny from 192.168.206.1
[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
DocumentRoot "/data/www"
ServerName www.test.com
Order allow,deny
Allow from all
Deny from 192.168.206.1
多用在在网络安全方面,比如:某些目录上传文件,避免上传的有木马,针对某些路径禁止解析php。
编辑虚拟主机的配置文件
# 对data目录进行php解析限制。
php_admin_flag engine off # 将php解析引擎关闭
# 匹配
Order deny,allow
Deny from all # 禁止解析所有,若不加filesmatch,只是将engine off,在浏览器 访问该文件时,会将php文件下载下来,这样不好