Apache的安装部署 1 (修改端口,修改默认发布目录,创建虚拟主机,访问控制,Apache支持的语言)

一·关于Apache
1.Apache的了解
  Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,它快速、
可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中。Apache HTTP服务器是一
个模块化的服务器,源于NCSAhttpd服务器,经过多次修改,成为世界使用排名第一的Web服务器软件。
Apache取自“a patchy server”的读音,意思是充满补丁的服务器,因为它是自由软件,所以不断有
人来为它开发新的功能、新的特性、修改原来的缺陷。
  Apache的特点是简单、速度快、性能稳定,并可做代理服务器来使用。Apache有多种产品,可以
支持SSL技术,支持多个虚拟主机。Apache是以进程为基础的结构,进程要比线程消耗更多的系统
开支,不太适合于多处理器环境,因此,在一个Apache Web站点扩容时,通常是增加服务器或扩充
群集节点而不是增加处理器。世界上很多著名的网站如Amazon、Yahoo!、W3 Consortium、Financial Times
等都是Apache的产物,
  它的成功之处主要在于它的源代码开放、有一支开放的开发队伍、支持跨平台的应用(可以运行
在几乎所有的Unix、Windows、Linux系统平台上)以及它的可移植性等方面。
2.Apache web服务器的特性
	1.支持最新的HTTP/1.1通信协议
	2.拥有简单而强有力的基于文件的配置过程
	3.支持网关接口
	4.支持基于IP和基于域名的虚拟主机
	5.支持多种方式的HTTP认证
	6.集成Perl处理模块
	7.集成代理服务器模块
	8.支持实时监视服务器状态和定制服务器日志
	9.支持服务器端包含指令(SSI)
	10.支持安全Socket层(SSL)
	11.提供用户会话过程的跟踪
	12.支持FastCGI
	13.通过第三方模块可以支持JavaServlets
二.Apache的安装部署
1.apache服务的基本配置
[root@apache-server ~]# yum install -y httpd.x86_64
[root@apache-server ~]# systemctl start httpd
[root@apache-server ~]# systemctl enable httpd
[root@apache-server ~]# netstat -antlp | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      1195/httpd  
[root@apache-server ~]# firewall-cmd --permanent --add-service=http   # 把http服务添加到火墙策略中
[root@apache-server ~]# firewall-cmd --permanent --add-service=https
[root@apache-server ~]# firewall-cmd --list-all 

在这里插入图片描述

打开网页访问
在这里插入图片描述

[root@apache-server ~]# cd /var/www/html/    # 这是httpd的默认发布目录
[root@apache-server html]# ls
[root@apache-server html]# vim index.html   # 这是httpd的默认发布文件
hello world                   # 添加内容

再次打开网页访问
在这里插入图片描述

[root@apache-server html]# yum install -y httpd-manual   # 这是提供apache使用的手册的软件
[root@apache-server html]# systemctl restart httpd

网页访问 http://172.25.254.142/manual/ ,即可看到手册内容
在这里插入图片描述

2.修改apache的端口
/etc/httpd/conf.d/    # 子配置目录
/etc/httpd/conf.d/.*   # 子配置文件
/etc/httpd/conf/      # 主配置目录
/etc/httpd/logs/*     # httpd的日志文件
httpd_sys_content_t    # 安全上下文 

[root@apache-server html]# rpm -qc httpd   # 查看httpd服务的所有目录

在这里插入图片描述

[root@apache-server html]# getenforce    # 查看selinux的状态 
Enforcing
[root@apache-server html]# vim /etc/httpd/conf/httpd.conf  
42 Listen 8080
[root@apache-server html]# systemctl restart httpd
[root@apache-server html]# netstat  -antlp | grep httpd

在这里插入图片描述

打开浏览器进行访问:发现不能访问(这是因为selinux阻止了)

在这里插入图片描述

[root@apache-server html]# firewall-cmd --add-port=8080/tcp --zone=public   # 在火墙中添加httpd的8080端口
success
[root@apache-server html]# firewall-cmd --add-port=6666/tcp --zone=public
success
[root@apache-server html]# firewall-cmd --list-all

在这里插入图片描述

再次打开浏览器进行访问,成功访问到内容

在这里插入图片描述

[root@apache-server html]# vim /etc/httpd/conf/httpd.conf   # 修改端口 为6666
42 Listen 6666
[root@apache-server html]# systemctl restart httpd    # 发现重启失败	

在这里插入图片描述

[root@apache-server html]# semanage port  -l | grep http  # 查看所有被默认可以使用的http端口,发现6666这个接口不在里面,所以不能重启失败

在这里插入图片描述

[root@apache-server html]# semanage port -a -t http_port_t -p tcp 6666   # 把6666这个接口添加到gttp的默认接口中	
[root@apache-server html]# semanage port  -l | grep http

在这里插入图片描述

[root@apache-server html]# systemctl restart httpd  # 重启服务成功
[root@apache-server html]# netstat  -antlp | grep httpd   # 查看接口修改成功

在这里插入图片描述

打开网页访问,因为apache的默认接口是80,所以当接口被修改时,必须带修改后的接口进行访问才能访问到内容

在这里插入图片描述

[root@apache-server html]# vim /etc/httpd/conf/httpd.conf   # 为了后面实验做成功,我们把端口修改回来
42 Listen 80
[root@apache-server html]# systemctl restart httpd
3.修改默认访问文件
[root@apache-server html]# pwd
 /var/www/html
[root@apache-server html]# vim test.html
 hello test
[root@apache-server html]# ls
 index.html  test.html
[root@apache-server html]# vim /etc/httpd/conf/httpd.conf
 163 <IfModule dir_module>   
 164     DirectoryIndex test.html index.html   # 谁在前先读谁,不存在的话就跳过
 165 </IfModule>
[root@apache-server html]# systemctl restart httpd

打开浏览器访问
在这里插入图片描述

[root@apache-server html]# rm -fr test.html    # 当默认目录不存在时,就会访问下一个目录 

打开浏览器进行访问
在这里插入图片描述

4.修改默认发布目录
[root@apache-server html]# mkdir /westos/web/html -p 
[root@apache-server html]# vim /westos/web/html/index.html 
this is /westos/web/html/’s  page 
[root@apache-server html]# vim /etc/httpd/conf/httpd.conf
120 DocumentRoot "/westos/web/html"    # 修改为发布目录为自己设定的默认发布目录
[root@apache-server html]# systemctl restart httpd

打开浏览器进行访问,发现访问不到

在这里插入图片描述

[root@apache-server html]# cat /etc/httpd/logs/error_log   # 查看httpd的报错日志,发现服务被禁止

在这里插入图片描述

[root@apache-server html]# vim /etc/httpd/conf/httpd.conf
121 <Directory "/westos/web/html">      # 授权   
122         Require all granted
123 </Directory>

在这里插入图片描述

[root@apache-server html]# systemctl restart httpd	

浏览器访问发现还是不能访问

[root@apache-server html]# cat  /etc/httpd/logs/error_log 
[root@apache-server html]# cat /etc/httpd/logs/error_log  # 查看日志,发现是selinux禁止了,安全上下文不一致

在这里插入图片描述

[root@apache-server html]# ls -Zd /westos/   # 查看安全上下文,确实不一致
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos/
[root@apache-server html]# semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?'    # 修改安全上下文
[root@apache-server html]# restorecon -FvvR /westos/   # 刷新
[root@apache-server html]# ls -Zd /westos/   # 查看
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /westos/  

再次访问
在这里插入图片描述

恢复环境(如果你忘记自己干过什么,就这样恢复)

[root@apache-server html]# rm -fr /etc/httpd/conf/httpd.conf
[root@apache-server html]# yum reinstall -y httpd
[root@apache-server html]# systemctl restart httpd
5.虚拟主机的创建

我们现在想要达到通过访问不同的域名来访问不同的页面

[root@apache-server html]# cd /var/www
[root@apache-server www]# mkdir wetos.com/news/html -p  # 建立自定义发布目录
[root@apache-server www]# mkdir wetos.com/music/html -p
[root@apache-server www]# vim wetos.com/news/html/index.html   # 编辑发布内容 
 this is news's page
[root@apache-server www]# vim wetos.com/music/html/index.html 
 this is music's page
[root@apache-server www]# cd /etc/httpd/conf.d/
[root@apache-server conf.d]# ls
autoindex.conf  manual.conf  README  userdir.conf  welcome.conf
[root@apache-server conf.d]# vim a_defult.conf    # 当访问默认端口80时,读取/var/www/html 这个发布目录里的默认发布文件
 <VirtualHost _default_:80>  
         DocumentRoot /var/www/html  
         CustomLog   logs/default.log combined  
 </VirtualHost>

在这里插入图片描述

浏览器访问测试

在这里插入图片描述

[root@apache-server conf.d]# vim news.conf     # 当访问news.westos.com 这个域名时,读取/var/www/westos.com/news/html这个目录里的默认发布文件
 <VirtualHost *:80>   # * 代指下一行的ServerName.80表示指定到80端口  
         ServerName news.westos.com    # 指定访问域名  
         DocumentRoot /var/www/westos.com/news/html  # 指定默认发布目录  
         CustomLog logs/news.log combined    # 设定日志文件,combined表示混合日志  
 </VirtualHost>  
 <Directory "/var/www/westos.com/news/html">  
         Require all granted    # 授权  
 </Directory>

在这里插入图片描述

[root@apache-server conf.d]# vim music.conf   #  当访问music.westos.com 这个域名时,读取/var/www/westos.com/musichtml这个目录里的默认发布文件
 <VirtualHost *:80>  
         ServerName music.westos.com  
         DocumentRoot /var/www/westos.com/music/html  
         CustomLog logs/music.log combined  
 </VirtualHost>  
 <Directory "/var/www/westos.com/music/html">  
         Require all granted  
 </Directory>	

在这里插入图片描述

[root@apache-server conf.d]# systemctl restart httpd 	

一定要写解析,不然域名不会被识别

 [root@foundation78 Desktop]# vim /etc/hosts   # 在客户端写解析,用谁的浏览器访问,谁就是客户端
 172.25.254.142 www.westos.com news.westos.com music.westos.com

浏览器访问测试,访问不同的域名可以查看到不同的界面
在这里插入图片描述

在这里插入图片描述

6.apache 的访问控制
(1)基于IP的控制
[root@apache-server conf.d]# vim a_defult.conf  
 <VirtualHost _default_:80>   # 访问默认80端口时,访问以下目录  
         DocumentRoot /var/www/html  # apache的默认发布目录  
         CustomLog   logs/default.log combined  # 这个文件记录着所有类的日志,即混合日志  
 </VirtualHost>  
 <Directory "/var/www/html">  
         order Deny,allow    # Deny,allow谁在前就先读谁  
         Allow from 172.25.254.242    
         Deny from all  
 </Directory>

在这里插入图片描述

[root@apache-server conf.d]# systemctl restart httpd

其他主机上测试

在这里插入图片描述

172.25.254.242 这台主机上测试
在这里插入图片描述

[root@apache-server conf.d]# vim a_defult.conf   # 不允许172.25.254.242这台主机访问
 <VirtualHost _default_:80>   
         DocumentRoot /var/www/html  
         CustomLog   logs/default.log combined  
 </VirtualHost>  
 <Directory "/var/www/html">  
         order allow,deny  
         Allow from all  
         Deny from 172.25.254.242  
 </Directory>

在这里插入图片描述

其他主机上测试

在这里插入图片描述

172.25.254.242 这台主机上测试

在这里插入图片描述

(2)基于用户的访问控制
[root@apache-server www]# cd /etc/httpd/conf.d/
[root@apache-server conf.d]# htpasswd -cm http_userlist lala  # 生成lala的基本认证的密码文件,c =create表示创建一个加密文件,m表示默认采用MD5算法对密码进行加密,http_userlist存放生成的明文密码
New password: 
Re-type new password: 
Adding password for user lala
[root@apache-server conf.d]# htpasswd -m http_userlist ting   # 当创建第二个用户时,不需要再-c
New password: 
Re-type new password: 
Adding password for user ting
[root@apache-server conf.d]# cat http_userlist 
lala:$apr1$nW9iKxL5$6MUyCAPHhbZR0I4.hTyle1
ting:$apr1$6mv/WfVQ$0jr/XfkoXFGRvy7rpO9pY.
[root@apache-server conf.d]# vim a_defult.conf  
 <VirtualHost _default_:80>  
         DocumentRoot /var/www/html  
         CustomLog   logs/default.log combined  
 </VirtualHost>  
 <Directory "/var/www/html">  
         AuthUserFile /etc/httpd/conf.d/http_userlist  # 指定密码文件的路径  
         AuthName "Please input uasename and password!!!" # 访问时的提示内容  
         AuthType basic   # 用户认证的类型是mod_auth提供的basic  

 # 注:下面这两行只能出现一行
         Require valid-user  # 允许所有apache用户进行访问 
    #    Require user lala   # 只允许lala这个用户进行访问 
 </Directory>

在这里插入图片描述

[root@apache-server conf.d]# systemctl restart httpd

打开浏览器进行访问

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

7.httpd支持的语言
(1)Php语言
[root@apache-server conf.d]# yum install -y php
[root@apache-server conf.d]# pwd
/etc/httpd/conf.d
[root@apache-server conf.d]# ls 
[root@apache-server conf.d]# vim /var/www/html/index.php  
 <?php  
         phpinfo();  
 ?>

在这里插入图片描述

[root@apache-server conf.d]# systemctl restart httpd	

浏览器测试

在这里插入图片描述

(2)Cgi # 通用网关接口
[root@apache-server conf.d]# cd /var/www/
[root@apache-server www]# ls
cgi-bin  html  westos.com
[root@apache-server www]# cd cgi-bin/
[root@apache-server cgi-bin]# ls
[root@apache-server cgi-bin]# vim index.cgi  
 #!/usr/bin/perl  
 print "Content-type: text/html\n\n";  
 print `date`";

在这里插入图片描述

[root@apache-server cgi-bin]# chmod +x index.cgi   # 给脚本加可执行权限
[root@apache-server cgi-bin]# ./index.cgi    # 执行脚本	

在这里插入图片描述

在网页进行测试

在这里插入图片描述

上面实验说明了cgi脚本可以在自己的/var/www/cgi-bin路径下执行,我们现在想要把cgi语言添加到apache的默认发布目录里

 [root@apache-server cgi-bin]# cd /var/www/html/
 [root@apache-server html]# ls
 index.html  index.php
 [root@apache-server html]# mkdir cgi
 [root@apache-server html]# vim cgi/index.cgi
 [root@apache-server html]# cp /var/www/cgi-bin/index.cgi cgi/index.cgi
 [root@apache-server html]# chmod +x cgi/index.cgi

在浏览器上测试

在这里插入图片描述

[root@apache-server html]# cd /etc/httpd/conf.d/
[root@apache-server conf.d]# vim a_defult.conf   
 <VirtualHost _default_:80>  
         DocumentRoot /var/www/html  
         CustomLog   logs/default.log combined  
 </VirtualHost>  
 <Directory "/var/www/html/cgi">  
         Options ExecCGI  
         AddHandler cgi-script .cgi  
 </Directory>

在这里插入图片描述

[root@apache-server conf.d]# systemctl restart httpd

再一次在浏览器上进行访问

在这里插入图片描述

[root@apache-server conf.d]# cd /var/www/html/
[root@apache-server html]# ls -Zd cgi/
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 cgi/
[root@apache-server html]# ls -Zd /var/www/cgi-bin/
drwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0 /var/www/cgi-bin/

在这里插入图片描述

[root@apache-server html]# semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?'            # 修改安全上下文
[root@apache-server html]# restorecon -RvvF /var/www/html/cgi/  # 刷新

浏览器测试

在这里插入图片描述

(3)wsgi
[root@apache-server cgi-bin]# vim webapp.wsgi
#!/usr/bin/env python
import time
def application (environ,start_response):
  response_body='UNIX EPOCH time is now:%s\n' % time.time()
  status = '200 ok'
  response_headers=[('Content-Type','text/plain'),
                    ('Content-Length','1'),
                    ('Content-Length',str(len(response_body)))]
  start_response(status,response_headers)
  return [response_body]

在这里插入图片描述

[root@apache-server cgi-bin]# chmod +x webapp.wsgi
[root@apache-server cgi-bin]# python webapp.wsgi   # 执行脚本之后,什么现象都看不到 

浏览器测试,发现不能访问
在这里插入图片描述

[root@apache-server cgi-bin]# yum search wsgi

在这里插入图片描述

[root@apache-server cgi-bin]# yum install -y mod_wsgi.x86_64  # 下载插件
[root@apache-server cgi-bin]# systemctl restart httpd
[root@apache-server cgi-bin]# cd /etc/httpd/conf.d/
[root@apache-server conf.d]# vim webapp.conf  1
 <VirtualHost *:80>  
          ServerName webapp.westos.com
          DocumentRoot /var/www/cgi-bin  
          CustomLog logs/webapp.log combined  
          WSGIScriptAlias / /var/www/cgi-bin/webapp.wsgi           
 </VirtualHost>

在这里插入图片描述

[root@apache-server conf.d]# systemctl restart httpd
[root@foundation78 ~]# vim /etc/hosts    # 写解析
172.25.254.142 webapp.wstos.com

浏览器再次访问
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值