搭建httpd服务

版权声明:勇哥出品必属精品违者必究,相信勇哥幸福一生,不信勇哥抱憾终身,盗版一时爽全家火葬场! https://blog.csdn.net/weixin_42837637/article/details/82748121

一、httpd简介

1.http简介

httpd的是Apache的超文本传输​​协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。

2.httpd-2.4新增的模块

模块 功能
mod_proxy_fcgi 反向代理时支持的Apache服务器后端协议的模块
mod_ratelimit 提供速率限制功能的模块
mod_remoteip 基于IP的访问控制机制被改变,不再支持使用秩序,拒绝,允许来做基于IP的访问控制

3.httpd自带的工具程序

工具 功能
htpasswd的 基本的认证基于文件实现时,用到的帐号密码生成工具
apachectl httpd自带的服务控制脚本,支持start,stop,restart
apxs 由httpd-devel包提供的,扩展httpd使用第三方模块的工具
rotatelogs 日志滚动工具
suexec 访问某些有特殊权限配置的资源时,临时切换至指定用户运行的工具
ab apache benchmark,httpd的压力测试工具

4.rpm包安装的httpd程序环境

文件/目录 对应的功能
/var/log/httpd/access.log 访问日志
/var/log/httpd/error_log 错误日志
/var/www/html/ 站点文档目录
/usr/lib64/httpd/modules/ 模块文件路径
/etc/httpd/conf/httpd.conf 主配置文件
/etc/httpd/conf.modules.d/*.conf 模块配置文件
/etc/httpd/conf.d/*.conf 辅助配置文件

二、编译安装http-2.4

1.安装开发环境

[root@localhost ~]# yum groupinstall "Development Tools"
[root@localhost ~]# groupadd -r apache
[root@localhost ~]# useradd -r -g apache apache
[root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel lib

2.下载安装apr-1.6.3和 tar xf apr-1.6.3.tar.bz2

[root@localhost ~]# cd /usr/src/
[root@localhost src]#  tar xf apr-1.6.3.tar.bz2
[root@localhost src]# tar xf apr-util-1.6.1.tar.bz2
[root@localhost apr-1.6.3]# vim configure
    cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    cat <<_LT_EOF >> "$cfgfile"
#RM "$cfgfile"删掉此行或者加上井号注释
[root@localhost apr-1.6.3]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.6.3]# make && make install
[root@localhost local]# cd /usr/src/apr-util-1.6.1
[root@localhost apr-util-1.6.1]#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost apr-util-1.6.1]# make && make install
[root@localhost ~]# tar xf httpd-2.4.34.tar.bz2
[root@localhost ~]# cd httpd-2.4.34
[root@localhost 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@localhost httpd-2.4.34]#make && make install
httpd安装完毕了

三、配置三种不同风格虚拟主机

创建apache用户

[root@localhost ~]#  groupadd apache
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache -g apache

第一种:相同IP不同端口

1.修改配置文件

[root@localhost httpd-2.4.34]# which httpd
/usr/sbin/httpd
[root@localhost httpd-2.4.34]# which apachectl
/usr/sbin/apachectl
[root@localhost httpd-2.4.34]# vim  /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin:$PATH
[root@localhost httpd-2.4.34]# vim /etc/httpd24/httpd.conf
ServerName www.example.com:80取消掉前面注释避免报错

2.添加端口

#Listen 12.34.56.78:80
Listen 80
Listen 8080

3.添加端口配置

#virtual host 1
<VirtualHost 192.168.100.100:80>
    ServerName www.qin.com               //域名
    DocumentRoot "/usr/local/apache/htdocs/qin"   //网站的路径
    ErrorLog "/usr/local/apache/logs/qin_error_log"        //错误日志
    CustomLog "/usr/local/apache/logs/qin_access_log""   combined            //登陆日志
    <Directory /usr/local/apache/htdocs/qin>
            <RequireAll>
            Require all granted                   //允许所有人访问
            Require not ip 192.168.1.111              //禁止此网段访问
            </RequireAll>
    </Directory>
</VirtualHost>
# virtual host 2
<VirtualHost 192.168.100.100:8080>
    ServerName www.yong.com
    DocumentRoot "/usr/local/apache/htdocs/yong"
    ErrorLog "/usr/local/apache/logs/yong_error_log"
    CustomLog "/usr/local/apache/logs/yong_access_log"   combined
    <Directory /usr/local/apache/htdocs/yong>
         <RequireAll>
            Require all granted
            Require not ip 192.168.1.111
        </RequireAll>
    </Directory>
</VirtualHost>

4.新建网站文件,并加入到将属主和属组加入到apache

[root@localhost ~]# mkdir -p /usr/local/apache/htdocs/{qin,yong}
[root@localhost ~]#  chown apache.apache /usr/local/apache/htdocs/qin/
[root@localhost ~]#  chown apache.apache /usr/local/apache/htdocs/yong/

5.新建网站内容

[root@localhost ~]# cd /usr/local/apache/htdocs/
[root@localhost htdocs]# echo "qin" >> qin/index.html
[root@localhost htdocs]# echo "yong" >> yong/index.html
[root@localhost htdocs]# apachectl -t
Syntax OK

6.启动服务

[root@localhost ~]# apachectl start
httpd (pid 1430) already running
[root@localhost ~]# ss -lnpt
State      Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
LISTEN     0      128                                                                *:111                                                                            *:*                   users:(("systemd",pid=1,fd=53))
LISTEN     0      100                                                        127.0.0.1:25                                                                             *:*                   users:(("master",pid=945,fd=13))
LISTEN     0      128                                                               :::111                                                                           :::*                   users:(("systemd",pid=1,fd=52))
LISTEN     0      128                                                               :::8080                                                                          :::*                   users:(("httpd",pid=1435,fd=6),("httpd",pid=1434,fd=6),("httpd",pid=1433,fd=6),("httpd",pid=1432,fd=6),("httpd",pid=1431,fd=6),("httpd",pid=1430,fd=6))
LISTEN     0      128                                                               :::80                                                                            :::*                   users:(("httpd",pid=1435,fd=4),("httpd",pid=1434,fd=4),("httpd",pid=1433,fd=4),("httpd",pid=1432,fd=4),("httpd",pid=1431,fd=4),("httpd",pid=1430,fd=4))
LISTEN     0      100                                                              ::1:25                                                                            :::*                   users:(("master",pid=945,fd=14))

7.验证结果

在这里插入图片描述
在这里插入图片描述

第二种: 相同端口不同IP

1.修改配置

[root@server-httpd ~]# vim /etc/httpd24/httpd.conf
#Listen 12.34.56.78:80
Listen 80
Listen 8080  //删除此行

<VirtualHost 192.168.100.100:80>
    ServerName www.qin.com
    DocumentRoot "/usr/local/apache/htdocs/xie"
    ErrorLog "/usr/local/apache/logs/qin_error_log"
    CustomLog "/usr/local/apache/logs/qin_access_log"  combined
    <Directory /usr/local/apache/htdocs/qin>
        <RequireAll>
            Require not ip 192.168.1
            Require all granted
        </RequireAll>
    </Directory>
</VirtualHost>

<VirtualHost 192.168.100.102:80>
    ServerName www.yong.com
    DocumentRoot "/usr/local/apache/htdocs/yong"
    ErrorLog "/usr/local/apache/logs/yong_error_log"
    CustomLog "/usr/local/apache/logs/yong_access_log"   combined
    <Directory /usr/local/apache/htdocs/yong>
            Require all granted
    </Directory>
</VirtualHost>

2.增加ip

[root@localhost ~]# ip addr add 192.168.100.102/24 dev eth0

3.重启服务

[root@localhost ~]# apachectl restart

4.验证结果

在这里插入图片描述
在这里插入图片描述

第三种:相同IP相同端口不同域名

1.修改配置

<VirtualHost 192.168.100.100:80>
    ServerName www.qin.com
    DocumentRoot "/usr/local/apache/htdocs/qin"
    ErrorLog "/usr/local/apache/logs/qin_error_log"
    CustomLog "/usr/local/apache/logs/qin_access_log""   combined   
    <Directory /usr/local/apache/htdocs/qin>
            <RequireAll>    
            Require all granted   
            Require not ip 192.168.1 
            </RequireAll>     
    </Directory>
</VirtualHost>
# virtual host 2
<VirtualHost 192.168.100.100:80>
    ServerName www.yong.com
    DocumentRoot "/usr/local/apache/htdocs/yong"
    ErrorLog "/usr/local/apache/logs/yong_error_log"
    CustomLog "/usr/local/apache/logs/yong_access_log"   combined
    <Directory /usr/local/apache/htdocs/yong>
         <RequireAll>
            Require all granted 
            Require not ip 192.168.1
        </RequireAll> 
    </Directory>
</VirtualHost>

2.重启服务

[root@server-httpd logs]# apachectl restart

3.修改windows配置

在自己电脑windows的C:\Windows\System32\drivers\etc找到host文件修改


# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
192.168.100.100 www.qin.com
192.168.100.100 www.yong.com

4.验证结果

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42837637/article/details/82748121