Linux的Apache服务

一、Apache 服务的搭建与配置
1、Apache 服务的搭建

操作系统: centos7 ( CentOS-7-x86. _64-Minimal-1708 )
硬件: (这个根据项目运行和配置建议设置, 一般我先配个1核1G )

2、关闭selinux

1.修改selinux的配置文件
[root@qfedu.com ~]# vim /etc/selinux/config
●将内容中的SELINUX=enforcing修改为SELINUX=disabled>
2.关闭selinux
[root@qfedu.com ~]# setenforce 0
[root@qfedu.com ~]# reboot
[root@qfedu.com ~]# sestatus
SELinux status: disabled

3、安装epel源

[root@qfedu.com ~]# yum -Y install epel-release. noarch

4、yum安装Apache软件

[root@qfedu.com ~]# yum -y install httpd*
[root@qfedu.com ~]# rpm -ga | grep httpd --查看安装的http包
[root@qfedu.com ~]# rpm -qa | grep httpd
httpd-too1s-2.4.6-90. e17.centos.x86_ .64
httpd-itk-2.4.7.04-2.e17.x86_ 64
httpd-manua1-2.4.6-90. e17. centos .noarch
httpd-2.4.6-90.e17.centos.x86_ 64
httpd-deve1-2.4.6-90. e17.centos.x86
64
安装成功后,会产生下面两个文件:

[root@qfedu.com ~]# /etc/httpd/conf/httpd.conf #主配置文件
[root@qfedu.com ~]# /ar/www/html #默认网站家目录

5、认识配置文件里的主要参数

[root@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf
serverRoot “/etc/httpd” #存放配置文件的目录
Listen 80 #Apache服务监听端口
user apache #子进程的用户
Group apache #子进程的组
ServerAdmin root@localhost #设置管理员邮件地址
DocumentRoot “/var /www/html” #网站家目录
#设置DocumentRoot指定目录的属性
<Directory /va/www/html"> #网站容器开始标识
options Indexes Fo1lowSymLinks #找不到主页时,以目录的方式呈现,并允许链接到网站根目录以外
Allowoverride None #none不使用.htaccess控制,all允许
Require a11 granted #granted表示运行所有访问,denied表示拒绝所有访问

#容器结束
DirectoryIndex index.html#定义主页文件,当访问到网站目录时如果有定义的主页文件,网站会自动访问
AdddefaultCharset UTF-8 #字符编码,如果中文的话。有可能需要改为gb2312或者gbk,因你的网站文件的默认编码而异

9.访问验证httpd服务
●启动成功后使用浏览器或者curl输入自己的IP地址会看到一个欢迎页面
[root@qfedu.com -]# curl 192. 168.10.183
●每次打开浏览器不是很方便,因此我们可以使用文本浏览器,方便测试。
[root@qfedu.com ~]# yum -y install elinks #安装elinks文本浏览器
[root@qfedu.com ~]# elinks 192.168.5.20 #拨ctrl+C退出
10、Apache 配置实例

建立网站主页,在网站根目录下建立一个主页文件
格式1 :
[root@qfedu.com ~]# echo ‘main page’ > /var /w/html/index. html#往index. html添加内容
[root@qfedu. com ~]#systemctl restart httpd.service
#重启服务
[root@qfedu.com ~]# firefox http://192.168.10.183
#在浏览器进行
测试,显示的信息为刚才我们输入的内容main page
格式2 :
[root@qfedu.com ~]# vim /var/www/html/ index .html
#把主页文件写成html标签的格式,添加一下内容

测试站点

欢迎来到测试站点!

[root@qfedu.com ~]# systemctl restart httpd.service #重启服务 [root@qfedu.com ~]# firefox http://192.168.1.183 #在测览器进行测试,显示的信息为刚才我们输入的内容main page
6、Apache常用命令
[root@qfedu.com ~]# yum -y install gcc make zlib-devel pcre pcre-devel openssl-devel
[root@qfedu.com ~]# yum -y install apr-*
[root@qfedu.com ~]# ./configure --prefix=/usr/local/apache2 && make && make install

[root@qfedu.com ~]# /usr/local/apache2/bin/apachectl -M #查看常见的模块(包括动态和静态)
[root@qfedu.com ~]# /usr/1ocal/apache2/bin/apachectl -l #查看加载的静态模块
[root@qfedu.com ~]# /usr/local/apache2/bin/apachectl -t #检查配置文件有无语法错误
新安装的环境会有错误,在vi /usr/local/apache2/conf/httpd.conf下输入ServerName localhost:80就行
[root@qfedu.com ~]# /usr/local/apache2/bin/apachectl graceful # 加载配置文件,但不重启
[root@qfedu.com ~]# /usr/local/apache2/bin/apachectl start/restart/stop #启动/重启/停止apache服务

7、Apache 配置用户认证

[root@qfedu.com ~]#vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

<Directory /data/ww/abc>    #/data/www/abc创建abc,为将要访问的页面的目录
Allowoverride AuthConfig
Authname "cct"
AuthType Basic
AuthuserFile /data/.htpasswd   #指定存放用户名和密码的文件
require valid-user
</Directory>
例:
<VirtualHost *:80>
        DocumentRoot "/data/www/abc"

        <Directory /data/www/abc>
                AllowOverride AuthConfig
                AuthName "dy"
                AuthType Basic
                AuthuserFile /data/.htpasswd
                require valid-user
        </Directory>
</VirtualHost>


●htpasswd 生成密码文件
[root@qfedu.com ~]# htpasswd -c /data/.htpasswd dy #指定用户名为dy.输入自已设定的密码
[root@qfedu.com ~]# /usr/local/apache2/bin/apachectl graceful #加载配置

8、Apache 配置默认虚拟主机

[root@qfedu.com ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/tmp/111” # 该目录可以不存在
servername www.111.com

<VirtualHost *:80>
DocumentRoot “/data/www”
Servername www.test.com
ServerAlias www.aaa.com

●将这段设定放在虚拟主机配置的最上面。除了已作过设置的域名( aaa、test )外,其余访问的域名均跳转到/tmp/111下。
cd /tmp //进入目录
mkdir 111 //创建一个名为111的目录
cd 111
vim index. html //进入里面写上你需要展示的东西
cd /
mkdir data
cd data/
mkdir www
cd www
vim index. html //进入里面写上你需要展示的东西
vim /etc/hosts //地址解析

9、Apache配置rewrite规则

●Apache中rewrite规则代码均写在

<VirtualHost *:80>
        DocumentRoot "/data/www"
        <IfModule mod_rewrite.c>
                RewriteEngine on
                RewriteCond %{HTTP_HOST} ^www.aaa.com$ [OR]
                RewriteCond %{HTTP_HOST} ^www.bbb.com$
                RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L]
        </IfModule>
</VirtualHost>

调用了rewrite模块,RewriteCond是跳转条件, RewriteRule是跳转规则。

调用了rewrite模块,RewriteCond是跳转条件, RewriteRule是跳转规则。

2.禁止指定user.agent

<VirtualHost *:80>
	DocumentRoot "/data/www"
        <IfModule mod_rewrite.c>
			RewriteCond %{HTTP_USER_AGENT}  ^.*curl.* [NC,OR]       # 禁止curl和chrome浏览器访问
			RewriteCond %{HTTP_USER_AGENT}  ^.*chrome.* [Nc]      # NC是不区分大小写的意思
			RewriteRule .* - [F]     #为禁止的意思
		</IfModule>
</VirtualHost>

3.通过rewrite限制某个目录

<VirtualHost *:80>
	DocumentRoot "/data/www"
        <IfModule mod_rewrite.c>
Rewri teCond %[REQUEST_URI}  ^.*/tmp/.* [NC]  #禁止访问tmp目录
RewriteRule .* - [F]
		</IfModule>
</VirtualHost>

4.rewrite 规则
●R=301强制外部重定向
●[F]禁用URL,返回403HTTP状态码。
●NC不区分大小写
●[OR]或者

5.rewrite 变量
%{HTTP_USER_AGENT} #表示:访问的user_ agent
%{HTTP_HOST} #表示:当前访问的网址,虫是指前缀部分,格式是www . xxx .com不包括"http://“和”/”
%{REQUEST_URI}#表示:表示访问的相对地址,就是相对根目录的地址,就是域名/后面的成分,格式上包括最前面的“/",
www.123.com/xiang/1.html # www.123.com表示HOST, xiang/1.html表示URI

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值