Apache的详细配置

it2022-05-05  128

http 协议名apache 软件名 nginx httpd (http daemon;http服务)web 服务器survey.netcraft.net --此网站会有每月份的世界上网站使用的WEB服务器的使用率统计官网:www.apache.org IBM apache软件基金会 redhat oracle websphere tomcat jboss weblogic ==========================================================================================================学习环境配置:准备一台全新的虚拟机(centos7.3平台),准备好本地yum源# yum install httpd\* --安装软件包# rpm -qa |grep httpdhttpd-manual-2.4.6-45.el7.centos.noarchhttpd-2.4.6-45.el7.centos.x86_64httpd-devel-2.4.6-45.el7.centos.x86_64httpd-tools-2.4.6-45.el7.centos.x86_64主配置文件ls /etc/httpd/conf/httpd.conf启动服务默认端口:80启动前验证没有进程占用80,如果有,先停掉或kill掉占用的进程# lsof -i:80# netstat -ntlup |grep :80# systemctl start httpd# systemctl enable httpd# systemctl status httpd启动完成后,再验证是否是你要启动的进程占用了80# lsof -i:80COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEhttpd 2312 root 4u IPv6 30417 0t0 TCP *:http (LISTEN)httpd 2313 apache 4u IPv6 30417 0t0 TCP *:http (LISTEN)httpd 2314 apache 4u IPv6 30417 0t0 TCP *:http (LISTEN)httpd 2315 apache 4u IPv6 30417 0t0 TCP *:http (LISTEN)httpd 2316 apache 4u IPv6 30417 0t0 TCP *:http (LISTEN)httpd 2317 apache 4u IPv6 30417 0t0 TCP *:http (LISTEN)# netstat -ntlup |grep :80tcp6 0 0 :::80 :::* LISTEN 2312/httpd======================================================================================================例一:建立网站主页(主页文件优先于welcome欢迎页面) 在网站根目录下 建立一个主页文件 echo 'main page' > /var/www/html/index.html 主页文件的默认路径http://127.0.0.1/ --就可以看到main page的信息,也就是直接访问到主页把主页文件写成html标签的格式如下:<html><head><title>笔记站点</title></head><body><center><h1>欢迎来我的网站!@_@</h1></center></body></html>例二:修改网站根目录# vim /etc/httpd/conf/httpd.conf119 DocumentRoot "/www" --修改网站根目录为/www131 <Directory "/www"> --把这个也对应的修改为/www# mkdir /www# echo "new main page" > /www/index.html# systemctl reload httpd--注意,不要开启selinux,否则换新的家目录的话,客户端访问会被拒绝例三:修改主页类型或加多个主页类型并调整优先级# vim /etc/httpd/conf/httpd.conf164 DirectoryIndex index.php index.html --使用浏览器访问,发现没有返回主页信息,因为上面的意思为:先找网站家目录下的index.php,不存在话,再找index.html,都不存在,就表示找不到主页# echo 'php main page' > /www/index.php客户端测试会优先访问index.php例四:对php支持的简单讨论# cat /www/index.php --把例三的index.php主页文件改成下面的一段简单代码<?phpecho "hello world"?>客户端测试访问不到hello world,而是一个空白页面(说明没有php解释器来解释它)解决方法:# yum install php\* --要安装php解释器# systemctl restart httpd客户端再测试,显示的是hello world(说明php解释成功)例五:把家目录里的文件做成列表的形式条件1. Options Indexes FollowSymLinks --相应目录的参数控制里要有indexes参数条件2. 没有主页文件条件3. 符合上面两个条件,就会访问到redhat的欢迎页面 vim /etc/httpd/conf.d/welcome.conf --注释掉这个欢迎页面,或者是删除它,或者mv改名例六关于apache的标签,容器(访问控制) Directory (目录) Files(文件) Location (位置,url) DirectoryMatch (目录,匹配regex) Files(文件,匹配regex) Location (位置,url,匹配regex)<Directory "/www"> Options indexes FollowSymLinks --允许列表,符号链接 AllowOverride None --不使用.htaccess控制 Require all granted --允许所有人访问; Require all deined 拒绝所有人访问 </Directory>访问控制写法简单总结(基于apache2.4版本): Require all denied Require ip 10.1.1.1 10.1.1.2 --拒绝所有,但允许10.1.1.1和10.1.1.2 <RequireAll> Require all granted Require not ip 10.1.1.1 10.1.1.2 --允许所有,但拒绝10.1.1.1和10.1.1.2 </RequireAll>例七:对apache家目录做一个基本验证功能1,# vim /etc/httpd/conf/httpd.conf<Directory "/www"> Options indexes FollowSymLinks AllowOverride All --把None改为All(None表示此目录的验证功能不生效,All表示生效) Require all granted </Directory>2,# vim /www/.htaccess   --对哪个目录进行验证,就在哪个目录下建立此文件authname "please input your username and password! @_@"authtype basicauthuserfile /etc/httpd/userpasswdrequire valid-user3,# htpasswd -c /etc/httpd/userpasswd aaa  --创建此文件,并加入一个用户,自定义密码,注意此用户与系统普通用户无关(文件不存在,第一次创建需要加-c参数)New password: Re-type new password: Adding password for user aaa# htpasswd /etc/httpd/userpasswd bbb  --再增加一个用户New password: Re-type new password: Adding password for user bbb4,#systemctl restart httpd5,客户端使用浏览器访问测试练习:针对一个子目录(非家目录)单独做验证例八:关于Files和FilesMatch标签 --支持正则匹配,一个是文件匹配,一个是目录匹配拒绝所有目录下的1文件被访问<Files ~ "1"> Require all denied </Files>练习:网站下所有目录(包括子目录)以aaa开头的文件都不能被访问<FilesMatch ~ "^aaa"> Require all denied</FilesMatch>练习:网站下所有目录(包括子目录)以.txt结尾的文件都不能被访问<FilesMatch ~ ".txt$"> Require all denied</FilesMatch>练习:网站下所有.gif .jpeg .png的图片都被拒绝<FilesMatch ~ "\.(gif|jpeg|png)$"> Require all denied</FilesMatch>例九:关于Location和LocationMatch的讨论<Location "/aaa"> Require all denied</Location>表示elinks http://IP/aaa这个访问路径会被拒绝<LocationMatch "^/aa"> Require all denied</LocationMatch>表示elinks http://IP/aaa或http://IP/aab这样以aa开头的访问路径会被拒绝例十:apache的alias跳转# vim /etc/httpd/conf/httpd.confAlias /yum "/yum" --前面/yum是location /yum ;后面的/yum是服务器根目录下的yum子目录<Directory "/yum"> Options Indexes MultiViews FollowSymLinks AllowOverride None Require all granted</Directory># systemctl restart httpd客户端elinks http://IP/yum 就可以访问到服务器的/yum目录的文件例十一:错误页面准备一个图片放到家目录,比如一个404bg.jpg# vim /etc/httpd/conf/httpd.conf --修改主配置文件,指向你的这个图片路径ErrorDocument 404 /404bg.jpg# systemctl restart httpd客户端测试,故意访问一个不存在的页面,触发404错误,就会显示上面的图片======================================================================================================虚拟主机 --用apache或nginx就可以做 一台服务器跑多台web服务虚拟主机(用apache或nginx就可以做) --》 VPS(virtual private server虚拟专用服务器)(虚拟化) --》 云服务器 (云计算)mkdir /www/aaamkdir /www/bbbecho "aaa main page" > /www/aaa/index.htmlecho "bbb main page" > /www/bbb/index.html例十二:基于ip的虚拟主机# vim /etc/httpd/conf/httpd.conf --把下面一段直接加到最后的空白处NameVirtualHost *:80<VirtualHost *:80> DocumentRoot /www/aaa ServerName 10.1.1.2 </VirtualHost><VirtualHost *:80> DocumentRoot /www/bbb ServerName 10.1.1.5</VirtualHost># ifconfig eth0:0 10.1.1.5/24 --基于ip的虚拟主机,你这台apache服务器得有访问它们的多个ip,没有的可以虚拟出来# systemctl restart httpd客户端访问测试elinks -dump http://10.1.1.2/ --得到aaa main pageelinks -dump http://10.1.1.5/ --得到bbb main page例十三:基于port的虚拟主机# vim /etc/httpd/conf/httpd.conf listen 80 --默认有这一句,不需要再加listen 8080 --这一句没有,需要加上这一句<VirtualHost *:80> DocumentRoot /www/aaa ServerName 10.1.1.2</VirtualHost><VirtualHost *:8080> DocumentRoot /www/bbb ServerName 10.1.1.2</VirtualHost># systemctl restart httpd客户端访问测试elinks -dump http://10.1.1.2/ --得到aaa main pageelinks -dump http://10.1.1.5:8080/ --得到bbb main page例十四:基于域名的虚拟主机NameVirtualHost *:80<VirtualHost *:80> DocumentRoot /www/aaa ServerName news.web.com</VirtualHost><VirtualHost *:80> DocumentRoot /www/bbb ServerName sports.web.com</VirtualHost># systemctl restart httpddns做CNAME,也就是news.web.com和sports.web.com都指向apache服务器ip(这里过程省略),如果不想做dns,可以直接在客户端/etc/hosts里绑定就可以了客户端访问测试elinks -dump http://news.web.com/ --得到aaa main pageelinks -dump http://sports.web.com/ --得到bbb main page

转载于:https://www.cnblogs.com/masterkx/p/8308631.html


最新回复(0)