Apchece服务

来源:互联网 发布:复杂网络特性 编辑:程序博客网 时间:2024/06/03 21:45

1.Apchece 服务

(1)安装httpd服务

       搭建yum源,并安装httpd服务

搭建yum源在之前的博客中已经提到,在此不在赘述。

yum repolist                                    ##查看yum源配置信息

yum install -y httpd                          ##安装httpd服务


yum install httpd-manual -y            ## 安装httpd手册


systemctl start httpd                             ##开启httpd服务

                                                              

systemctl enable httpd                         ##开机自启动httpd   



测试

http://172.25.254.110

http://172.25.254.110/manual

在浏览器中输入装有httpd服务的IP

注意:如果没有成功需要先关闭防火墙

systemctl stop firewalld.service


(2)apache基础信息

/etc/httpd/conf                            ##httpd主配置目录

/etc/httpd/conf/httpd.conf             ##httpd主配置文件

/etc/httpd/conf.d/                          ##子配置目录


/var/www/html/                             ##默认发布目录

80                                            ##默认端口


httpd_sys_content_t                  ##默认安全上下文

apache                                         ##程序开启默认用户

/etc/httpd/logs/*                             ##apache日志系统存放路径(httpd服务开启后)


修改默认端口

vim /etc/httpd/conf/httpd.conf


将系统中的默认端口改为8080,然后重启httpd服务

更改之后,当我们需要访问该ip时需要后面加:8080端口数字,

(注意由于selinux安全上下文限制,此端口不能任意改动)


(3)修改默认发布文件

默认发布文件就是在没有指定文件名称时,访问apache的默认文件

这个文件可以指定多个,有访问顺序

vim /etc/httpd/conf/httpd.conf

167     DirectoryIndex   index.html     tests.html                 ##首先访问tests.html,并且当tests.html文件不存在时访问index.html



(4)修改默认发布目录

 

首先创建一个新的目录

mkdir /westos/www/html -p

vim  /westos/www/html/index.html

编辑新的默认发布目录指定文件,让其最后的结果显示如下:


编辑配置文件

vim   /etc/httpd/conf/httpd.conf

120  DocumentRoot "/westos/www/html"
121 <Directory "/westos/www">
122         Require all granted
123 </Directory>


此时重启系统我们会发现访问页面时发现无法访问到修改后的发布目录,此时我们需要修改默认的安全上下文


[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?'

[root@localhost ~]# restorecon -RvvF /westos/

修改成功

(5)apache的虚拟主机

创建虚拟主机目录及默认发布文件内容

mkidr  /var/www/virtual/news/html -p

mkdir /var/www/virtual/video/html -p

vim /var/www/virtual/news/html/index.html

<h1> news.westos.com</h1>

vim  /var/www/virtual/video/html/index.html

 <h1> video.westos.com</h1>     

添加配置文件,使其访问发布目录及发布文件,为了减少主配置文件的冗余,我们将其写入到子配置文件

vim  /etc/httpd/conf.d/a_default.conf               ##访问发布的默认配置,及主网页

vim  /etc/httpd/conf.d/news.conf                    ##子网页apache配置文件

vim  /etc/httpd/conf.d/video.conf                    ##子网页apache配置文件

  1 <VirtualHost _default_:80>

  2         DocumentRoot  "/var/www/html"          ##文件路径

  3         CustomLog  "logs/www.westos.com.log"  combined    ##定义日志的路径,combined 四种日志的结合

  4 </VirtualHost>



  1 <VirtualHost *:80>
  2         ServerName news.westos.com                    ##域名
  3         DocumentRoot "/var/www/virtual/news/html/"
  4         CustomLog "logs/news.logs" combined
  5 </VirtualHost>
  6 <Directory "/var/www/virtual/news/html/">
  7         Require all granted
  8 </Directory>




  配置测试主机的域名解析文件

vim  /etc/hosts




测试:


(6)apache内部访问控制

a.争对于主机的访问控制


  1 <VirtualHost _default_:80>

  2         DocumentRoot "/var/www/html"

  3         CustomLog "logs/www.westos.com.log" combined

  4 </VirtualHost>

  5 <Directory "/var/www/html/apple">

  6         Order allow,deny

  7         Allow from 172.25.254.68

  8         Deny from all
  9 </Directory>

     先允许172.25.254.68登陆  ,然后拒绝所有人         

b.用户访问方式的控制

[root@localhost conf.d]# htpasswd -cm /etc/httpd/userpass  boy  ##创建一个需要密码认证的用户boy
New password:
Re-type new password:
Adding password for user boy




编辑配置文件开启密码认证:






(7) apache支持的语言

1.html

2.php

a.首先安装php

yum install php -y

b.简单的php命令

   编辑配置文件


vim /var/www/html/index.php

  <?php
           phpinfo();
  ?>

查看输出


3. cgi

cd /var/www 

[root@localhost www]# vim cgi-bin/index.cgi             ##编辑脚本

  #!/usr/bin/perl

   print "Content-type:text/html\n\n";

   print `date`;

[root@localhost www]#chmod +x  cgi-bin/index.cgi               ##给脚本加上执行权限

[root@localhost www]# ./cgi-bin/index.cgi                                ##运行脚本
Content-type:text/html

Sun Nov 26 13:06:37 EST 2017



编辑apache配置文件,从而使其以网页形式输出

vim /etc/httpd/conf.d/a_default.conf

 16 </Directory>

 17 Alias    /cgi    /var/www/cgi-bin         设置别名

 18 <Directory "/var/www/cgi-bin">

 19         Options +ExecCGI              

 20    AddHandler cgi-script .cgi

 21 </Directory>



重启服务

systemctl  restart  httpd

测试:


 











原创粉丝点击