httpd的簡介
httpd是Apache超文本傳輸協定(HTTP)伺服器的主程式。被設計為一個獨立運作的背景程序,它會建立一個處理請求的子程序或線程的池。
通常,httpd不應該被直接調用,而應該在類Unix系統中由apachectl調用,在Windows中作為服務運作。
httpd的版本
本文主要介紹httpd的兩大版本,httpd-2.2和httpd-2.4。
- CentOS6系列的版本預設提供的是httpd-2.2版本的rpm包
- CentOS7系列的版本預設提供的是httpd-2.4版本的rpm包
httpd的特性
httpd有很多特性,下面就分别來說說httpd-2.2版本和httpd-2.4版本各自的特性。
版本 | 特性 |
---|---|
2.2 | 事先建立程序 按需維持适當的程序 子產品化設計,核心比較小,各種功能通過子產品添加(包括PHP),支援運作時配置,支援單獨編譯子產品 支援多種方式的虛拟主機配置,如基于ip的虛拟主機,基于端口的虛拟主機,基于域名的虛拟主機等 支援https協定(通過mod_ssl子產品實作) 支援使用者認證 支援基于IP或域名的ACL通路控制機制 支援每目錄的通路控制(使用者通路預設首頁時不需要提供使用者名和密碼,但是使用者通路某特定目錄時需要提供使用者名和密碼) 支援URL重寫 支援MPM(Multi Path Modules,多處理子產品)。用于定義httpd的工作模型(單程序、單程序多線程、多程序、多程序單線程、多程序多線程) |
2.4 | httpd-2.4的新特性: MPM支援運作DSO機制(Dynamic Share Object,子產品的動态裝/解除安裝機制),以子產品形式按需加載 支援event MPM,eventMPM子產品生産環境可用 支援異步讀寫 支援每個子產品及每個目錄分别使用各自的日志級别 每個請求相關的專業配置,使用來配置 增強版的表達式分析器 支援毫秒級的keepalive timeout 基于FQDN的虛拟主機不再需要NameVirtualHost指令 支援使用者自定義變量 支援新的指令(AllowOverrideList) 降低對記憶體的消耗 |
編譯安裝httpd
//配置yum源安裝編譯環境
[[email protected] ~]# cat /etc/yum.repos.d/xx.repo
[xx]
baseurl=file:///mnt
gpgcheck=0
enabled=1
name=xx
[[email protected] ~]# mount /dev/sr0 /mnt/
mount: /dev/sr0 寫保護,将以隻讀方式挂載
[[email protected] ~]# yum groups mark install "Development Tools"
[[email protected] ~]# yum -y install openssl-devel pcre-devel expat-devel libtool
//建立apache使用者
[[email protected] ~]# groupadd -r apache
[[email protected] ~]# useradd -r -g apache apache
//下載下傳apache
[[email protected] ~]# wget http://mirror.bit.edu.cn/apache/apr/apr-1.6.5.tar.gz
[[email protected] ~]# wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[[email protected] ~]# wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.38.tar.gz
//解壓apache
[[email protected] ~]# tar xf apr-1.6.5.tar.gz
[[email protected] ~]# tar xf apr-util-1.6.1.tar.gz
//注釋或删除apr-1.6.5/configure中$RM "$cfgfile"
//編譯apr1.6.5
[[email protected] apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@server apr-1.6.5]# make && make install -j 4
//編譯apr-util
[[email protected] ~]# cd 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 -j 4
//解壓httpd
[[email protected] ~]# tar xf httpd-2.4.37.tar.bz2
//編譯httpd
[[email protected] ~]# cd httpd-2.4.37
[[email protected] httpd-2.4.37]# ./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.37]# make && make install -j 4
//取消端口号的注釋
[[email protected] ~]# vim /etc/httpd24/httpd.conf
[[email protected] ~]# grep '^ServerName' /etc/httpd24/httpd.conf
ServerName www.example.com:80
//配置虛拟主機
[[email protected] ~]# vim /etc/httpd24/httpd.conf
[[email protected] ~]# tail -12 /etc/httpd24/httpd.conf
<VirtualHost 192.168.194.129>
ServerName 192.168.194.129
DocumentRoot "/var/www/html/www"
ErrorLog "/var/log/httpd/www/error_log"
CustomLog "/var/log/httpd/www/access_log" combined
<Directory /var/www/html/www>
<RequireAll>
Require all granted
Require not ip 192.168.1
</RequireAll>
</Directory>
</VirtualHost>
//建立網頁
[[email protected] var]# mkdir -p www/html
[[email protected] var]# cd www/html/
[[email protected] html]#
[[email protected] html]# mkdir www blog
[[email protected] html]# chown -R apache.apache blog
[[email protected] html]# chown -R apache.apache www
[[email protected] html]# ll
總用量 0
drwxr-xr-x. 2 apache apache 6 11月 8 13:04 blog
drwxr-xr-x. 2 apache apache 6 11月 8 13:04 www
[[email protected] html]# vim www/index.html
[[email protected] html]# cat www/index.html
hello word
//建立網頁對應的日志
[[email protected] html]# mkdir -p /var/log/httpd/{www,blog}
[[email protected] html]# ll /var/log/httpd/
總用量 0
drwxr-xr-x. 2 root root 6 11月 8 13:10 blog
drwxr-xr-x. 2 root root 6 11月 8 13:10 www
[[email protected] html]# chown -R apache.apache /var/log/httpd/
//配置環境變量
[[email protected] ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[[email protected] ~]# source /etc/profile.d/httpd.sh
[[email protected] ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[[email protected] ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
//啟動apache
[[email protected] ~]# apachectl start
[[email protected] ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
//關閉防火牆
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# setenforce 0
//測試
在ie浏覽器中輸入伺服器ip位址