httpd服务
实验环境
主机 | IP地址 | 角色 |
---|---|---|
server-httpd | 192.168.100.60 | http服务 |
- 下载软件包
[root@server-httpd src]# ll
-rw-r--r-- 1 root root 854100 Sep 17 06:35 apr-1.6.3.tar.bz2
-rw-r--r-- 1 root root 428595 Sep 17 06:35 apr-util-1.6.1.tar.bz2
-rw-r--r-- 1 root root 6942969 Sep 17 06:35 httpd-2.4.34.tar.bz2
- 关闭防火墙,selinux
[root@server-httpd ~]# systemctl stop firewalld.service
[root@server-httpd ~]# systemctl enable firewalld.service
[root@server-httpd ~]# setenforce 0
编译安装Httpd-2.4
- 安装开发环境
[root@server-httpd ~]# yum groupinstall "Development Tools" -y
[root@server-httpd ~]# yum -y install openssl-devel pcre-devel expat-devel libtool
- 解压下载的httpd等包
[root@server-httpd ~]# cd /usr/src/
[root@server-httpd src]# tar xf apr-1.6.3.tar.bz2
[root@server-httpd src]# tar xf apr-util-1.6.1.tar.bz2
[root@server-httpd src]# tar xf httpd-2.4.34.tar.bz2
- 编译安装apr
[root@server-httpd src]# cd apr-1.6.3/
[root@server-httpd apr-1.6.3]# vim configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
$RM "$cfgfile" //删除此项
[root@server-httpd apr-1.6.3]# ./configure --prefix=/usr/local/apr
[root@server-httpd apr-1.6.3]# echo $?
0
[root@server-httpd apr-1.6.3]# make -j 4 && make install
[root@server-httpd apr-1.6.3]# echo $?
0
- 编译安装apr-util
[root@server-httpd apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr
[root@server-httpd apr-util-1.6.1]# make -j 4 && make install
- 编译安装httpd
[root@server-httpd 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@server-httpd httpd-2.4.34]# echo $?
0
[root@server-httpd httpd-2.4.34]# make -j 4 && make install //编译完成
[root@server-httpd httpd-2.4.34]# echo $?
0
创建apache用户
[root@server-httpd ~]# groupadd apache
[root@server-httpd ~]# useradd -r -M -s /sbin/nologin apache -g apache
配置第一类
\\
相同IP不同端口
- 修改配置文件
[root@server-httpd ~]# vim /etc/httpd24/httpd.conf
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName www.example.com:80 //取消这行的注释
- 添加端口
[root@server-httpd ~]# vim /etc/httpd24/httpd.conf
#Listen 12.34.56.78:80
Listen 80
Listen 8080
- 在文件尾部添加
[root@server-httpd ~]# vim /etc/httpd24/httpd.conf
#virtual host 1
<VirtualHost 192.168.100.60:80>
ServerName www.xie.com //域名
DocumentRoot "/usr/local/apache/htdocs/xie" //网站的路径
ErrorLog "/usr/local/apache/logs/xie_error_log" //错误日志
CustomLog "/usr/local/apache/logs/xie_access_log"" combined //登陆日志
<Directory /usr/local/apache/htdocs/xie>
<RequireAll>
Require all granted //允许所有人访问
Require not ip 192.168.1 //禁止此网段访问
</RequireAll>
</Directory>
</VirtualHost>
# virtual host 2
<VirtualHost 192.168.100.60:8080>
ServerName www.fei.com
DocumentRoot "/usr/local/apache/htdocs/fei"
ErrorLog "/usr/local/apache/logs/fei_error_log"
CustomLog "/usr/local/apache/logs/fei_access_log" combined
<Directory /usr/local/apache/htdocs/fei>
<RequireAll>
Require all granted
Require not ip 192.168.1
</RequireAll>
</Directory>
</VirtualHost>
- 新建网站文件,并加入到将属主和属组加入到apache
[root@server-httpd logs]# mkdir /usr/local/apache/htdocs/{xie,fei} -p
[root@server-httpd logs]# chown apache.apache /usr/local/apache/htdocs/xie/
[root@server-httpd logs]# chown apache.apache /usr/local/apache/htdocs/fei
- 新建网站内容
[root@server-httpd htdocs]# echo "xie" >> xie/index.html
[root@server-httpd htdocs]# echo "fei" >> fei/index.html
- 查看配置文件是否有误
[root@server-httpd logs]# apachectl -t
Syntax OK
- 开启服务
[root@server-httpd logs]# apachectl start
[root@server-httpd logs]# ss -lnpt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:* users:(("sshd",pid=678,fd=3))
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=831,fd=13))
LISTEN 0 128 :::8080 :::* users:(("httpd",pid=53252,fd=6),("httpd",pid=53251,fd=6),("httpd",pid=53250,fd=6),("httpd",pid=53249,fd=6),("httpd",pid=53248,fd=6),("httpd",pid=53247,fd=6))
LISTEN 0 128 :::80 :::* users:(("httpd",pid=53252,fd=4),("httpd",pid=53251,fd=4),("httpd",pid=53250,fd=4),("httpd",pid=53249,fd=4),("httpd",pid=53248,fd=4),("httpd",pid=53247,fd=4))
LISTEN 0 128 :::22 :::* users:(("sshd",pid=678,fd=4))
LISTEN 0 100 ::1:25 :::* users:(("master",pid=831,fd=14))
- 测试
配置第二类
\\
相同端口不同IP
- 修改配置
[root@server-httpd ~]# vim /etc/httpd24/httpd.conf
#Listen 12.34.56.78:80
Listen 80
Listen 8080 //删除此行
<VirtualHost 192.168.100.60:80>
ServerName www.xie.com
DocumentRoot "/usr/local/apache/htdocs/xie"
ErrorLog "/usr/local/apache/logs/xie_error_log"
CustomLog "/usr/local/apache/logs/xie_access_log" combined
<Directory /usr/local/apache/htdocs/xie>
<RequireAll>
Require not ip 192.168.1
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
<VirtualHost 192.168.100.61:80>
ServerName www.fei.com
DocumentRoot "/usr/local/apache/htdocs/fei"
ErrorLog "/usr/local/apache/logs/fei_error_log"
CustomLog "/usr/local/apache/logs/fei_access_log" combined
<Directory /usr/local/apache/htdocs/fei>
Require all granted
</Directory>
</VirtualHost>
- 增加IP地址
[root@server-httpd bin]# ip addr add 192.168.100.61/24 dev ens32
- 重启服务
[root@server-httpd logs]# apachectl restart
- 测试
配置第三类
\\
相同IP相同端口不同域名
- 修改配置文件
[root@server-httpd bin]# vim /etc/httpd24/httpd.conf
<VirtualHost 192.168.100.60:80>
ServerName www.xie.com
DocumentRoot "/usr/local/apache/htdocs/xie"
ErrorLog "/usr/local/apache/logs/xie_error_log"
CustomLog "/usr/local/apache/logs/xie_access_log" combined
<Directory /usr/local/apache/htdocs/xie>
<RequireAll>
Require not ip 192.168.1
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
<VirtualHost 192.168.100.60:80>
ServerName www.fei.com
DocumentRoot "/usr/local/apache/htdocs/fei"
ErrorLog "/usr/local/apache/logs/fei_error_log"
CustomLog "/usr/local/apache/logs/fei_access_log" combined
<Directory /usr/local/apache/htdocs/fei>
Require all granted
</Directory>
</VirtualHost>
- 重启服务
[root@server-httpd logs]# apachectl restart
- 修改windows的hosts文件
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
192.168.100.60 www.xie.com
192.168.100.60 www.fei.com
- 测试