http 介绍
httpd是Apache超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会创建一个处理请求的子进程或线程的池。html
httpd 特性linux
httpd经常使用的配置
在httpd中实现虚拟主机web
所谓的虚拟主机是指在经过在配置文件中提供不一样的配置,从而能够实如今同一台物理服务器上提供多个站点的访问路径,实现方式有三种,分别是:apache
IP地址相同,监听的端口不一样,经过不一样的端口号来访问
IP地址不一样,端口能够相同,经过不一样的IP来访问
主机名不一样,端口号和IP地址能够相同,经过不一样的主机名称来访问。
使用虚拟主机的前提是关闭中心主机功能,即将主配置文件中的DocumentRoot这一指令注释。vim
Apache服务器默认在80端口监听
一台机器能够有1到65535号端口,一个端口表明2个字节windows
Netstat -an 该命令用来查询本机器有哪些端口正在被监听服务器
Netstat -anb 该命令用来查询本机器有哪些端口正在被监听及其对应的应用程序
端口中的1-1024号叫作有名端口,这些端口通常不要用,他们已经分配好了负载均衡
Apache如何配置端口:
Apache软件的端口是在httpd.conf文件中配置的,该文件在Apache目录下的conf文件下。在该文件中能够修改端口,修改后从新启动Apache,就生效。ide
控制访问法则模块化
法则 | 功能 |
---|---|
Require all granted | 容许全部主机访问 |
Require all deny | 拒绝全部主机访问 |
Require ip IPADDR | 受权指定来源地址的主机访问 |
Require not ip IPADDR | 拒绝指定来源地址的主机访问 |
Require host HOSTNAME | 受权指定来源主机名的主机访问 |
Require not host HOSTNAME | 拒绝指定来源主机名的主机访问 |
Require not ip | 拒绝指定ip主机访问 |
httpd编译安装
实验环境说明:
主机名 | IP |
---|---|
[root@yanyinglai3 ~] | 192.168.47.12.24 |
准备环境,将防火墙和selinux
[root@yanyinglai3 ~]# setenforce 0 [root@yanyinglai3 ~]# systemctl stop firewalld
安装开发环境
[root@yanyinglai3 ~]# yum groupinstall "Development Tools"
建立apache组和用户apache
[root@yanyinglai3 ~]# groupadd -r apache [root@yanyinglai3 ~]# useradd -M -s /sbin/nologin -g apache apache [root@yanyinglai3 ~]# id apache uid=1000(apache) gid=996(apache) 组=996(apache)
安装相关的软件包
[root@yanyinglai3 ~]# yum -y install openssl-devel pcre-devel expat-devel libtool
·下载并安装apr-1.4和apr-util-1.4+
[root@yanyinglai3 ~]# cd /usr/src/ [root@yanyinglai3 ~]# yum -y install wget [root@yanyinglai3 ~]# wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.3.tar.bz2 [root@yanyinglai3 ~]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
解压下载安装apr-1.4和apr-util-1.4+的压缩包
[root@yanyinglai3 ~]# tar xf apr-1.6.3.tar.bz2 [root@yanyinglai3 ~]# tar xf apr-util-1.6.1.tar.bz2 [root@yanyinglai3 ~]# ls anaconda-ks.cfg apr-1.6.3 apr-1.6.3.tar.bz2 apr-util-1.6.1 apr-util-1.6.1.tar.bz2
进入apr-1.6.3将修改configure配置文件
[root@yanyinglai3 ~]# cd apr-1.6.3/ [root@yanyinglai3 apr-1.6.3]# vim configure cfgfile=${ofile}T trap "$RM \"$cfgfile\"; exit 1" 1 2 15 #$RM "$cfgfile" //将此行加入注释,或者删除此行
编译安装
[root@yanyinglai3 apr-1.6.3]# ./configure --prefix=/usr/local/apr [root@yanyinglai3 apr-1.6.3]# make && make install [root@yanyinglai3 apr-1.6.3]# cd /usr/src/apr-util-1.6.1 [root@yanyinglai3 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [root@yanyinglai3 apr-util-1.6.1]# make && make install
编译安装httpd
[root@yanyinglai3 ~]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2 [root@yanyinglai3 ~]# ls anaconda-ks.cfg httpd-2.4.34.tar.bz2 [root@yanyinglai3 ~]# tar xf httpd-2.4.34.tar.bz2 [root@yanyinglai3 ~]# cd httpd-2.4.34/ [root@yanyinglai3 httpd-2.4.34]# ./configure --prefix=/usr/local/apache \ > --sysconfdir=/etc/httpd24 \ > --enable-so \ > --enable-ssl \ > --enable-cgi \ > --enable-rewrite \ > --with-zlib \ > --with-pcre \ > --with-apr=/usr/local/apr \ > --with-apr-util=/usr/local/apr-util/ \ > --enable-modules=most \ > --enable-mpms-shared=all \ > --with-mpm=prefork [root@yanyinglai3 httpd-2.4.34]# make && make install
当相同IP不一样端口时
[root@yanyinglai3 ~]# vim /etc/httpd24/httpd.conf //找到ServerName www.example.com:80 取消#号注释 //找到Listen 80 在下面添加不一样端口 Listen 81 //在最后面添加下面以下内容 <VirtualHost 192.168.47.12:80> DocumentRoot "/usr/local/apache/htdocs/yan" ErrorLog "logs/yan/error_log" CustomLog "logs/yan/access_log" combined <Directory "/usr/local/apache/htdocs/yan"> <RequireAll> Require all granted </RequireAll> </Directory> </VirtualHost> <VirtualHost 192.168.47.12:81> DocumentRoot "/usr/local/apache/htdocs/yyl" ErrorLog "logs/yyl/error_log" CustomLog "logs/yyl/access_log" combined <Directory "/usr/local/apache/htdocs/yyl"> <RequireAll> Require all granted </RequireAll> </Directory> </VirtualHost> [root@yanyinglai3 ~]# tail -25 /etc/httpd24/httpd.conf [root@yanyinglai3 ~]# cd /usr/local/apache/logs/ //创建与httpd主配置文件相同路径的目录 [root@yanyinglai3 logs]# mkdir yan [root@yanyinglai3 logs]# mkdir yyl [root@yanyinglai3 logs]# cd /usr/local/apache/htdocs/ //在网站存放目录下 也建立与之相同的目录 [root@yanyinglai3 htdocs]# mkdir yan [root@yanyinglai3 htdocs]# mkdir yyl [root@yanyinglai3 htdocs]# chown -R apache.apache /usr/local/apache/htdocs/ //给网站存放的目录更改属主属组为apache [root@yanyinglai3 htdocs]# echo 'hello yan' > yan/index.html [root@yanyinglai3 htdocs]# echo 'hello yyl' > yyl/index.html [root@yanyinglai3 htdocs]# cd /usr/local/apache/bin/ [root@yanyinglai3 bin]# ./apachectl start [root@yanyinglai3 bin]# ./apachectl -t
客户端验证
不一样IP相同端口
[root@yanyinglai3 bin]# vim /etc/httpd24/httpd.conf <VirtualHost 192.168.47.12:80> DocumentRoot "/usr/local/apache/htdocs/yan" ErrorLog "logs/yan/error_log" CustomLog "logs/yan/access_log" combined <Directory "/usr/local/apache/htdocs/yan"> <RequireAll> Require all granted </RequireAll> </Directory> </VirtualHost> <VirtualHost 192.168.47.13:80> DocumentRoot "/usr/local/apache/htdocs/yyl" ErrorLog "logs/yyl/error_log" CustomLog "logs/yyl/access_log" combined <Directory "/usr/local/apache/htdocs/yyl"> <RequireAll> Require all granted </RequireAll> </Directory> </VirtualHost> [root@yanyinglai3 bin]# ip addr add 192.168.47.13/24 dev ens32 创建与编辑文件对应的临时ip [root@yanyinglai3 ~]# pkill httpd [root@yanyinglai3 ~]# /usr/local/apache/bin/httpd [root@yanyinglai3 ~]# ss -antl [root@yanyinglai3 ~]# cd /usr/local/apache/bin/ [root@yanyinglai3 bin]# ./apachectl start httpd (pid 62137) already running [root@yanyinglai3 bin]# ./apachectl -t Syntax OK
客户端检测
.相同IP相同端口不一样域名
[root@yanyinglai3 ~]# vim /etc/httpd24/httpd.conf
<VirtualHost 192.168.47.12:80>
ServerName www.yanyinglai.com:80
DocumentRoot "/usr/local/apache/htdocs/yan"
ErrorLog "logs/yan/error_log"
CustomLog "logs/yan/access_log" combined
<Directory "/usr/local/apache/htdocs/yan">
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
<VirtualHost 192.168.47.12:80>
ServerName www.yyl.com:80
DocumentRoot "/usr/local/apache/htdocs/yyl"
ErrorLog "logs/yyl/error_log"
CustomLog "logs/yyl/access_log" combined
<Directory "/usr/local/apache/htdocs/yyl">
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
root@yanyinglai3 ~]# pkill httpd
[root@yanyinglai3 ~]# /usr/local/apache/bin/httpd
[root@yanyinglai3 ~]# ss -antl
[root@yanyinglai3 ~]# cd /usr/local/apache/bin/
[root@yanyinglai3 bin]# ./apachectl start
httpd (pid 62137) already running
[root@yanyinglai3 bin]# ./apachectl -t
Syntax OK
客户端检测
在windows电脑上修改, C:\Windows\System32\drivers\etc 文件