天天看點

搭建httpd服務

文章目錄

    • 一、httpd簡介
      • 1.http簡介
      • 2.httpd-2.4新增的子產品
      • 3.httpd自帶的工具程式
      • 4.rpm包安裝的httpd程式環境
    • 二、編譯安裝http-2.4
    • 三、配置三種不同風格虛拟主機
      • 第一種:相同IP不同端口
        • 1.修改配置檔案
        • 2.添加端口
        • 3.添加端口配置
        • 4.建立網站檔案,并加入到将屬主和屬組加入到apache
        • 5.建立網站内容
        • 6.啟動服務
        • 7.驗證結果
      • 第二種: 相同端口不同IP
        • 1.修改配置
        • 2.增加ip
        • 3.重新開機服務
        • 4.驗證結果
      • 第三種:相同IP相同端口不同域名
        • 1.修改配置
        • 2.重新開機服務
        • 3.修改windows配置
        • 4.驗證結果

一、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.安裝開發環境

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

2.下載下傳安裝apr-1.6.3和 tar xf apr-1.6.3.tar.bz2

[[email protected] ~]# cd /usr/src/
[[email protected] src]#  tar xf apr-1.6.3.tar.bz2
[[email protected] src]# tar xf apr-util-1.6.1.tar.bz2
[[email protected] 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
[[email protected] apr-1.6.3]# make && make install
[[email protected] local]# cd /usr/src/apr-util-1.6.1
[[email protected] apr-util-1.6.1]#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[[email protected] apr-util-1.6.1]# make && make install
[[email protected] ~]# tar xf httpd-2.4.34.tar.bz2
[[email protected] ~]# cd httpd-2.4.34
[[email protected] 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
[[email protected] httpd-2.4.34]#make && make install
httpd安裝完畢了
           

三、配置三種不同風格虛拟主機

建立apache使用者

[[email protected] ~]#  groupadd apache
[roo[email protected] ~]# useradd -r -M -s /sbin/nologin apache -g apache
           

第一種:相同IP不同端口

1.修改配置檔案

[[email protected] httpd-2.4.34]# which httpd
/usr/sbin/httpd
[[email protected] httpd-2.4.34]# which apachectl
/usr/sbin/apachectl
[[email protected] httpd-2.4.34]# vim  /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin:$PATH
[[email protected] 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

[[email protected] ~]# mkdir -p /usr/local/apache/htdocs/{qin,yong}
[[email protected] ~]#  chown apache.apache /usr/local/apache/htdocs/qin/
[[email protected] ~]#  chown apache.apache /usr/local/apache/htdocs/yong/
           

5.建立網站内容

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

6.啟動服務

[[email protected] ~]# apachectl start
httpd (pid 1430) already running
[[email protected] ~]# 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.驗證結果

搭建httpd服務
搭建httpd服務

第二種: 相同端口不同IP

1.修改配置

[[email protected] ~]# 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

[[email protected] ~]# ip addr add 192.168.100.102/24 dev eth0
           

3.重新開機服務

[[email protected] ~]# apachectl restart
           

4.驗證結果

搭建httpd服務
搭建httpd服務

第三種:相同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.重新開機服務

[[email protected] 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.驗證結果

搭建httpd服務
搭建httpd服務