httpd简介
httpd是Apahce超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。
通常,httpd不应该被直接调用,而应该在类Unix系统中由apachectl调用,在Windows中作为服务运行。
1.编译安装httpd-2.4
httpd依赖于apr-1.4+,apr-util-1.4+,[apr-icon]
apr :apache portable runtime
(1)安装开发环境
[root@hj~]# yum groupinstall "Development Tools" -y
(2)创建系统用户,不允许登录不创建家目录
[root@hj ~]# groupadd -r apache
[root@hj ~]# useradd -r -M -s /sbin/nologin -g apache apache
(3)安装编译依赖包
[root@hj ~]# yum install openssl-devel pcre-devel expat-devel libtool -y
(4)下载并安装apr-1.4+和apr-util-1.4+
[root@hj ~]# yum install wget -y
[root@hj ~]# cd /usr/src
[root@hj src]# ls
debug kernels
//下载编译安装apr-1.4+
[root@hj src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.3.tar.bz2
[root@hj src]# tar xf apr-1.6.3.tar.bz2
[root@hj src]# ls
apr-1.6.3 apr-1.6.3.tar.bz2 debug kernels
其中需要修改apr-1.6.3 的配置文件
[root@hj apr-1.6.3]# vi configure
if test -n "${ZSH_VERSION+set}"; then
setopt NO_GLOB_SUBST
fi
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#$RM "$cfgfile" //将此行加上注释或者删除掉
[root@hj apr-1.6.3]# ./configure --prefix=/usr/local/apr
[root@hj apr-1.6.3]# make && make install
//下载编译安装apr-util-1.4+
[root@hj src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
[root@hj src]# tar xf apr-util-1.6.1.tar.bz2
[root@hj src]# ls
apr-1.6.3 apr-util-1.6.1 debug
apr-1.6.3.tar.bz2 apr-util-1.6.1.tar.bz2 kernels
[root@hj apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@hj apr-util-1.6.1]# make && make install
(5)编译安装httpd
[root@hj ~]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2
[root@hj ~]# ls
anaconda-ks.cfg httpd-2.4.34.tar.bz2
[root@hj ~]# tar xf httpd-2.4.34.tar.bz2
[root@hj ~]# ls
anaconda-ks.cfg httpd-2.4.34 httpd-2.4.34.tar.bz2
[root@hj ~]# cd httpd-2.4.34
[root@hj 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@hj httpd-2.4.34]# make && make install
(6)修改httpd的配置文件
[root@hj ~]# vi /etc/httpd24/httpd.conf
ServerName www.example.com:80 //取消此行前面的#号
三类虚拟主机:
相同ip不同端口
不同ip相同端口
相同ip相同端口不同域名
- 相同ip不同端口
//修改配置文件增加一个新端口,新端口必须是其他进程没有占用的
[root@hj ~]# vi /etc/httpd24/httpd.conf
Listen 80
Listen 8080
//在配置文件最后加上虚拟主机配置
ServerName www.rose.com
DocumentRoot "/usr/local/apache/htdocs/rose"
ErrorLog "/usr/local/apache/logs/rose/error_log"
CustomLog "/usr/local/apache/logs/rose/access_log" combined
Require all granted
ServerName www.rose.com
DocumentRoot "/usr/local/apache/htdocs/huaer"
ErrorLog "/usr/local/apache/logs/huaer/error_log"
CustomLog "/usr/local/apache/logs/huaer/access_log" combined
Require all granted
//创建网页目录并修改属主属组
[root@hj ~]# cd /usr/local/apache/htdocs/
[root@hj htdocs]# ls
index.html
[root@hj htdocs]# mkdir rose huaer
[root@hj htdocs]# ll
total 4
drwxr-xr-x. 2 root root 6 Aug 10 14:50 huaer
-rw-r--r--. 1 root root 45 Jun 12 2007 index.html
drwxr-xr-x. 2 root root 6 Aug 10 14:50 rose
[root@hj htdocs]# chown apache.apache rose -R
[root@hj htdocs]# chown apache.apache huaer -R
[root@hj htdocs]# ll
total 4
drwxr-xr-x. 2 apache apache 6 Aug 10 14:50 huaer
-rw-r--r--. 1 root root 45 Jun 12 2007 index.html
drwxr-xr-x. 2 apache apache 6 Aug 10 14:50 rose
//创建网页并写入内容
[root@hj htdocs]# echo 'you are my rose, ran' > rose/index.html
[root@hj htdocs]# echo 'zhe huangye zui hou yi duo meigui' > huaer/index.html
//创建相应的网页日志目录并更改属主属组
[root@hj ~]# mkdir /usr/local/apache/logs/{rose,huaer}
[root@hj ~]# ls /usr/local/apache/logs/
huaer rose
[root@hj ~]# chown -R apache.apache /usr/local/apache/logs/
[root@hj ~]# ll /usr/local/apache/logs/
total 0
drwxr-xr-x. 2 apache apache 6 Aug 10 15:05 huaer
drwxr-xr-x. 2 apache apache 6 Aug 10 15:05 rose
//启动服务,关闭防火墙,查看端口起来没
[root@hj ~]# /usr/local/apache/bin/httpd
[root@hj ~]# systemctl stop firewalld
[root@hj ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :
LISTEN 0 128 :::8080 :::
LISTEN 0 128 :::80 :::
LISTEN 0 128 :::22 :::
2.不同ip相同端口
//在原先的配置文件基础上去掉端口8080
//添加一个新IP
[root@hj ~]# ip addr add 192.168.56.14/24 dev ens32
[root@hj ~]# ip a
1: lo:
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32:
link/ether 00:0c:29:f5:a6:c5 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.13/24 brd 192.168.56.255 scope global ens32
valid_lft forever preferred_lft forever
inet 192.168.56.14/24 scope global secondary ens32
valid_lft forever preferred_lft forever
inet6 fe80::6c93:e6f0:10cc:2f84/64 scope link
valid_lft forever preferred_lft forever
//修改配置文件
ServerName www.rose.com
DocumentRoot "/usr/local/apache/htdocs/rose"
ErrorLog "/usr/local/apache/logs/rose/error_log"
CustomLog "/usr/local/apache/logs/rose/access_log" combined
ServerName www.rose.com
DocumentRoot "/usr/local/apache/htdocs/huaer"
ErrorLog "/usr/local/apache/logs/huaer/error_log"
CustomLog "/usr/local/apache/logs/huaer/access_log" combined
Require all granted
//重启服务
[root@hj ~]# pkill httpd
[root@hj ~]# /usr/local/apache/bin/httpd
3.相同ip相同端口不同域名
//修改配置文件
ServerName www.rose.com
DocumentRoot "/usr/local/apache/htdocs/rose"
ErrorLog "/usr/local/apache/logs/rose/error_log"
CustomLog "/usr/local/apache/logs/rose/access_log" combined
Require all granted
ServerName www.huaer.com
DocumentRoot "/usr/local/apache/htdocs/huaer"
ErrorLog "/usr/local/apache/logs/huaer/error_log"
CustomLog "/usr/local/apache/logs/huaer/access_log" combined
Require all granted
//解析域名 windos 系统 在C:\Windows\System32\drivers\etc中
苹果的在 /etc/hosts中
在C:\Windows\System32\drivers\etc\hosts文件中添加以下内容
192.168.56.13 www.rose.com
192.168.56.13 www.huaer.com
//验证结果