apache:www.apache.org --->主页中的project项目标题中提供了apache组织发布的各类平台软件。
apache:是取自“a patchy server”的读音,意思是充满补丁的服务器。
发布了不少开源的应用程序。如大数据库的平台软件hadoop、zookeeper等。php
软件包名称:httpd
服务端口:80/tcp 443/tcp
查apache的配置文件:rpm -qc httpd
配置文件: /etc/httpd/conf/httpd.conf 主配置文件
/etc/httpd/conf.d/*.conf 子配置文件
/etc/httpd/conf.d/welcome.conf 默认配置文件
数据目录:/var/www/html .htmlhtml
练习:安装并启动httpd软件。 rpm -q httpd httpd-manual yum -y install httpd httpd-manual systemctl restart httpd && systemctl enable httpd lsof -i:80 或 netstat -atunlp |grep :80 练习:安装命令行界面的网页浏览器软件,访问网站。 yum -y install curl elinks curl 127.0.0.1 看到的网站的html源代码 elinks 127.0.0.1 看到网页内容,按q键退出浏览器软件 练习:建立一个index.html的主页。访问网站。 data > /var/www/html/index.html free >> /var/www/html/index.html lsblk >> /var/www/html/index.html
curl 127.0.0.1 或elinks 127.0.0.1node
URl:统一资源标识,定位了一切网上的资源。列子 www.qf.com/index.html
URL:统一资源定位器。列:http://www.baidu.com/xinwen/1.html
www. 万维网
www 主机名
qf.com 域名web
客户端 图形化界面:firefox 浏览器 命令: elinks links elinks 192.168.1.250 #echo qf.com > /var/www/html/index.html 当服务器没有welcome.conf和*.html文件时,web server会显示、/var/www/html这个目录中的目录结构 *//apache的主配置文件: vim /etc/httpd/conf/httpd.conf httpd.conf配置文件的组成: global全局设置: mlodules模块设置: VirtualHost虚拟主机设置: Directory目录设置(控制访问权限-基于IP、用户认证): File文件控制访问控制: .....
31 ServerRoot "/etc/httpd" 服务器的根目录(即apache软件的根目录)
41 #Listen 12.34.56.78:80
42 Listen 80 启用80端口的监听
56 Include conf.modules.d/*.conf 包含的子配置文件,此目录是存放apache的模块程序
66 User apache 程序的执行者,用lsof -i :80 或 ps aux | grep apache可查
67 Group apache 程序的执行组,用top -u apache -->按f键-->方向键选group,并空格-->Esc键
86 ServerAdmin root@localhost 网站服务器管理员的邮箱数据库
102 <Directory /> 根目录访问权限设置
103 AllowOverride none
104 Require all denied 要求所有拒绝,即拒绝访问/根目录下的任何文件
105 </Directory>apache
119 DocumentRoot "/var/www/html" 文档根目录,即默认的网站主页目录
124 <Directory "/var/www"> 对/var/www目录访问权限设置
125 AllowOverride None
126 # Allow open access:
127 Require all granted 须要全部受权,即容许任何人访问
128 </Directory>vim
131 <Directory "/var/www/html">
144 Options Indexes FollowSymLinks
151 AllowOverride None
156 Require all granted
157 </Directory>centos
163 <IfModule dir_module> 设置dir_module模块。If是Interface接口的缩写
164 DirectoryIndex index.html index.php index.jsp 指定目录的索引(即主页)文件
165 </IfModule>浏览器
171 <Files ".ht*"> 文件访问设置
172 Require all denied
173 </Files>服务器
182 ErrorLog "logs/error_log"
189 LogLevel warn
191 <IfModule log_config_module> 设置log_config_module日志配置模块
196 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"% {User-Agent}i\"" combined
197 LogFormat "%h %l %u %t \"%r\" %>s %b" common
198
199 <IfModule logio_module>
201 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \ "%{User-Agent}i\" %I %O" combinedio
202 </IfModule>
217 CustomLog "logs/access_log" combined
218 </IfModule>
220 <IfModule alias_module> 设置alias_module别名模块
231 # Example:
232 # Alias /webpath /full/filesystem/path 定义/full/.../path路径的别名为/webpath
247 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
248
249 </IfModule>
255 <Directory "/var/www/cgi-bin">
256 AllowOverride None
257 Options None
258 Require all granted
259 </Directory>
260
261 <IfModule mime_module>
266 TypesConfig /etc/mime.types
277 #AddEncoding x-compress .Z
278 #AddEncoding x-gzip .gz .tgz
283 AddType application/x-compress .Z
284 AddType application/x-gzip .gz .tgz
305 AddType text/html .shtml
306 AddOutputFilter INCLUDES .shtml
307 </IfModule>
316 AddDefaultCharset UTF-8 默认的网站字符编码为UTF-8(支持中文)
317
318 <IfModule mime_magic_module>
324 MIMEMagicFile conf/magic
325 </IfModule>
348 EnableSendfile on
353 IncludeOptional conf.d/*.conf
apache有两种运行模式(进程和线程)
稳 prefork model 基于进程(公司经常使用) 即一个apache进程启动一个线程来响应客户的请求
快 worker model 基于线程(公司少用) 即一个apache进程启动多个线程来响应客户的请求
通俗理解:将进程理解成“超市”,将线程理解成超市中的“收银台”。
基于进程:由主进程建立子进程,每个进程只有一个线程,内存是不能够共享,系统开销大,可是一个线程坏掉了不会影响其余线程
基于线程:一个进程中会产生多个线程,内存中的某个区域是能够共享的,建立线程数度快,开销小,可是一个线程出现问题时,其余线程都死掉。[以武侠小说中的分身术来理解,即发分身术招式的是进程(本尊),招式中分身是线程]
集群环境:通常都会使用进程模式,不须要某一台主机单独承担大量的访问
httpd模式切换:
centos 7的操做:
cat -n /etc/sysconfig/httpd 请看第10行内容
10 # /etc/httpd/conf.modules.d/00-mpm.conf.
vim /etc/httpd/conf.modules.d/00-mpm.conf
6 LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
12 #LoadModule mpm_worker_module modules/mod_mpm_worker.so
18 #LoadModule mpm_event_module modules/mod_mpm_event.so
说明:apache默认是采用进程模式来运行的,即以上的第6行代码。如需使用多线程模式运行,须要将第6行注释掉而且将第12行启用。而后重启httpd服务,再用httpd -V 查apache的运行模式信息。输出结果的部份内容以下:
Server MPM: worker(线程模式)
threaded: yes (fixed thread count)
forked: yes (variable process count)
---
centos 6的操做(httpd模式切换):
vim /etc/sysconfig/httpd //切换成线程模式
HTTPD=/usr/sbin/httpd.worker
/etc/init.d/httpd restart (centos 6的操做) 或 systemctl restart httpd (centos 7的操做)
<IfModule prefork.c> 进程模块
StartServers 8 初始创建的进程数(1个父进程,8个工做进程)
MinSpareServers 5 最小空闲的进程数
MaxSpareServers 20 最大空闲的进程数
ServerLimit 256 服务器最大并发链接限制(默认256)
MaxClients 256 服务器最大并发访问量
MaxRequestsPerChild 4000 每一个子进程在其生命周期内容许响应的最大请求数,达到进程会被杀死,若是为0则用不结束,一个程序结束后
</IfModule>
httpd线程查询
[root@node11 ~]# ps -ef | grep httpd
apache虚拟主机功能:在一台apache网站服务器发布多个网站站点。
网站虚拟主机三种技术:
1.基于ip地址 每一个网站有一个ip地址
2.基于端口 全部网站仅用一个ip,端口不同
3.基于主机名(域名) 全部网站仅用一个ip,可是主机名称不同
最经常使用的是基于主机名,基于ip地址不经常使用,由于申请ip须要资金
配置apache实现虚拟主机 基于ip的虚拟主机: 思路: 1.给服务器的网卡设置多个ip地址。 2.建立网站主页目录及index.html主页文件。 3.在httpd服务器软件的/etc/httpd/conf.d目录中建立虚拟主机的配置文件,给每一个ip地址绑定一个网站。 4.访问测试:访问不一样的ip地址,获得不一样网站页面。
实施参考:
1.给服务器的网卡设置多个ip地址。(临时设置多个ip,重启network服务是时ip会丢失)
ifconfig ens33:1 192.168.11.21/24 up
ifconfig ens33:2 192.168.11.22/24 up
ifconfig ens33:3 192.168.11.23/24 up
ip a 或 ifconfig
2.建立网站主页目录及index.html主页文件。
mkdir -pv /baidu/{www,map,music}
echo www.baidu.com >/baidu/www/index.html
echo map .baidu.com > /baidu/map/index.html
echo music.baidu.com > /baidu/music/index.html
要给文件加权限setfacl -m u:apache:rx /baidu/www
3.在httpd服务器软件的/etc/httpd/conf.d目录中建立虚拟主机的配置文件,给每一个ip地址绑定一个网站。
cd /etc/httpd/conf.d(子配置文件)
ls
rpm -ql httpd | grep vhosts查httpd软件的全部文件名列表,但匹配vhosts关键词的文件
cat /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf
tail /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf > /etc/httpd/conf.d/baidu.conf(建立baidu.conf配置文件)
-----基于ip的虚拟主机
vim /etc/httpd/conf.d/baidu.conf 修改后的内容以下
<VirtualHost 192.168.11.21:80> 指定虚拟主机的ip号和端口号 ServerAdmin 123@qq.com 服务器管理员邮箱 DocumentRoot "/baidu/www/" 网站主页根目录 ServerName www.baidu.com 网站的域名 ErrorLog "/var/log/httpd/www.baidu.com-error_log" CustomLog "/var/log/httpd/www.baidu.com-access_log" common
</VirtualHost>
<Directory "/baidu/www"> 目录设置 在/etc/httd/conf/httpd.conf能够找到源代码
AllowOverride None
井 Allow open access:
Require all granted 须要全部受权,即容许任何人访问
</Directory>
<VirtualHost 192.168.11.22:80>
ServerAdmin 123@qq.com
DocumentRoot "/baidu/map/"
ServerName www.map.com
ErrorLog "/var/log/httpd/www.map.com-error_log"
CustomLog "/var/log/httpd/www.map.com-access_log" common
</VirtualHost>
<Directory "/baidu/map">
AllowOverride None
井Allow open access:
Require all granted
</Directory>
4.访问测试:访问不一样的ip地址,获得不一样的页面。
systemctl restart httpd
curl 192.168.11.21 结果是www.baidu.com
curl 192.168.11.22 结果是map.baidu.com
----基于端口的虚拟主机
vim /etc/httpd/conf.d/baidu.conf修改内容以下
Listen 81 启用81端口的监听
Listen 82 启用82端口的监听
<VirtualHost 192.168.11.21:81> ip和下面的ip相同 但端口不一样
ServerAdmin 123@qq.com
DocumentRoot "/baidu/www/"
ServerName www.baidu.com
ErrorLog "/var/log/httpd/www.baidu.com-error_log"
CustomLog "/var/log/httpd/www.baidu.com-access_log" common
</VirtualHost>
<Directory "/baidu/www">
AllowOverride None
井 Allow open access:
Require all granted
</Directory>
<VirtualHost 192.168.11.21:82>
ServerAdmin 123@qq.com
DocumentRoot "/baidu/map/"
ServerName www.map.com
ErrorLog "/var/log/httpd/www.map.com-error_log"
CustomLog "/var/log/httpd/www.map.com-access_log" common
</VirtualHost>
<Directory "/baidu/map">
AllowOverride None
井 Allow open access:
Require all granted
</Directory>
访问测试:访问相同ip的不一样端口,获得不一样网页界面
systemctl restart httpd
curl 192.168.11.21:81 输出结果是www.baidu.com
curl 192.168.11.21:82 输出结果是map.baidu.com
基于域名的虚拟主机:
Listen 81
Listen 82
<VirtualHost 192.168.11.21:80>
ServerAdmin 123@qq.com
DocumentRoot "/baidu/www/"
ServerName www.baidu.com
ErrorLog "/var/log/httpd/www.baidu.com-error_log"
CustomLog "/var/log/httpd/www.baidu.com-access_log" common
</VirtualHost>
<Directory "/baidu/www">
AllowOverride None
井 Allow open access:
Require all granted
</Directory>
<VirtualHost 192.168.11.21:80>
ServerAdmin 123@qq.com
DocumentRoot "/baidu/map/"
ServerName www.map.com
ErrorLog "/var/log/httpd/www.map.com-error_log"
CustomLog "/var/log/httpd/www.map.com-access_log" common
</VirtualHost>
<Directory "/baidu/map">
AllowOverride None
井 Allow open access:
Require all granted
</Directory>
并改掉 vim /etc/hosts加入这一行192.168.11.21 www.baidu.com map.baidu.comsystemctl restart httpdcurl www.baidu.com 输出结果为www.baidu.comcurl map.baidu.com 输出结果为map.baidu.com