httpd
時間:2023-07-16 01:54:02 | 來源:網(wǎng)站運營
時間:2023-07-16 01:54:02 來源:網(wǎng)站運營
httpd:
- 1. httpd簡介
- 2. httpd版本
- 2.1 httpd的特性
- 2.2 httpd-2.4新增的模塊
- 3. httpd基礎(chǔ)
- 3.1 httpd自帶的工具程序
- 3.2 rpm包安裝的httpd程序環(huán)境
- 3.3 web相關(guān)的命令
- 3.3.1 curl命令
- 3.3.2 httpd命令
- 4. 編譯安裝httpd-2.4
- 5. httpd常用配置
- 作業(yè)
1. httpd簡介
httpd是Apache超文本傳輸協(xié)議(HTTP)服務(wù)器的主程序。被設(shè)計為一個獨立運行的后臺進(jìn)程,它會建立一個處理請求的子進(jìn)程或線程的池。
通常,httpd不應(yīng)該被直接調(diào)用,而應(yīng)該在類Unix系統(tǒng)中由apachectl調(diào)用,在Windows中作為服務(wù)運行。
2. httpd版本
本文主要介紹httpd的兩大版本,httpd-2.2和httpd-2.4。
- CentOS6系列的版本默認(rèn)提供的是httpd-2.2版本的rpm包
- CentOS7系列的版本默認(rèn)提供的是httpd-2.4版本的rpm包
2.1 httpd的特性
httpd有很多特性,下面就分別來說說httpd-2.2版本和httpd-2.4版本各自的特性。
2.2 httpd-2.4新增的模塊
httpd-2.4在之前的版本基礎(chǔ)上新增了幾大模塊,下面就幾個常用的來介紹一下。
3. httpd基礎(chǔ)
3.1 httpd自帶的工具程序
3.2 rpm包安裝的httpd程序環(huán)境
mpm:以DSO機(jī)制提供,配置文件為/etc/httpd/conf.modules.d/00-mpm.conf3.3 web相關(guān)的命令
3.3.1 curl命令
curl是基于URL語法在命令行方式下工作的文件傳輸工具,它支持FTP,F(xiàn)TPS,HTTP,HTTPS,GOPHER,TELNET,DICT,F(xiàn)ILE及LDAP等協(xié)議。
curl支持以下功能:
- https認(rèn)證
- http的POST/PUT等方法
- ftp上傳
- kerberos認(rèn)證
- http上傳
- 代理服務(wù)器
- cookies
- 用戶名/密碼認(rèn)證
- 下載文件斷點續(xù)傳
- socks5代理服務(wù)器
- 通過http代理服務(wù)器上傳文件到ftp服務(wù)器
//語法:curl [options] [URL ...]//常用的options: -A/--user-agent <string>
//設(shè)置用戶代理發(fā)送給服務(wù)器 -basic
//使用Http基本認(rèn)證 --tcp-nodelay
//使用TCP_NODELAY選項 -e/--referer <URL>
//來源網(wǎng)址 --cacert <file>
//CA證書(SSL) --compressed
//要求返回時壓縮的格式 -H/--header <line>
//自定義請求首部信息傳遞給服務(wù)器 -I/--head
//只顯示響應(yīng)報文首部信息 --limit-rate <rate>
//設(shè)置傳輸速度 -u/--user <user[:password]>
//設(shè)置服務(wù)器的用戶和密碼 -0/--http1
//使用http 1.0版本,默認(rèn)使用1.1版本。這個選項是數(shù)字0而不是字母o -o/--output
//把輸出寫到文件中 -
#/--progress-bar //進(jìn)度條顯示當(dāng)前的傳送狀態(tài)//通過curl下載文件[root@localhost ~]
# ls[root@localhost ~]
# curl -o myblog.html http://http://blog.51cto.com/itchentao % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed100 67025 0 67025 0 0 87248 0 --:--:-- --:--:-- --:--:-- 87385
[root@localhost ~]
# lsmyblog.html
3.3.2 httpd命令
//語法:httpd [options]
//常用的options:
-l //查看靜態(tài)編譯的模塊,列出核心中編譯了哪些模塊。 /
//它不會列出使用LoadModule指令動態(tài)加載的模塊
-M //輸出一個已經(jīng)啟用的模塊列表,包括靜態(tài)編譯在服務(wù) /
//器中的模塊和作為DSO動態(tài)加載的模塊
-v //顯示httpd的版本,然后退出
-V //顯示httpd和apr/apr-util的版本和編譯參數(shù),然后退出
-X //以調(diào)試模式運行httpd。僅啟動一個工作進(jìn)程,并且 /
//服務(wù)器不與控制臺脫離
-t //檢查配置文件是否有語法錯誤
[root@localhost ~]# httpd -l
Compiled in modules:
core.c
mod_so.c
http_core.c
[root@localhost ~]# httpd -M
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain.
Set the 'ServerName' directive globally
to suppress this message
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
access_compat_module (shared)
actions_module (shared)
alias_module (shared)
allowmethods_module (shared)
auth_basic_module (shared)
......
......
[root@localhost ~]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Jun 27 2018 13:48:59
[root@localhost ~]# httpd -V
AH00558: httpd: Could
not reliably determine the
server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Server version: Apache/2.4.6 (CentOS)
Server built: Jun 27 2018 13:48:59
Server's
Module Magic Number: 20120211:24
Server loaded: APR 1.4.8, APR-UTIL 1.5.2
Compiled
using: APR 1.4.8, APR-UTIL 1.5.2
Architecture: 64-bit
Server MPM: prefork
threaded:
no forked: yes (variable process
count)
Server compiled
with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/etc/httpd"
-D SUEXEC_BIN="/usr/sbin/suexec"
-D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"
4. 編譯安裝httpd-2.4
httpd依賴于apr-1.4+,apr-util-1.4+,[apr-icon]
apr:apache portable runtime
//安裝開發(fā)環(huán)境
[root@localhost ~]
# yum groups mark install "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 libtoolLoaded plugins: product-id, search-disabled-repos, subscription-manager
This system
is not registered with an entitlement server. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package openssl-devel.x86_64 1:1.0.2k-12.el7 will be installed
--> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-12.el7
for package: 1:openssl-devel-1.0.2k-12.el7.x86_64
......
......
libselinux-utils.x86_64 0:2.5-12.el7 libsepol.x86_64 0:2.5-8.1.el7
libss.x86_64 0:1.42.9-12.el7_5 openssl.x86_64 1:1.0.2k-12.el7
openssl-libs.x86_64 1:1.0.2k-12.el7
Complete!
//下載并安裝apr-1.4+和apr-util-1.4+
[root@localhost ~]
# cd /usr/src/[root@localhost src]
# wget http://mirror.bit.edu.cn/apache/apr/apr-1.6.5.tar.gz[root@localhost src]
# wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz[root@localhost src]
# lsapr-1.6.5.tar.gz apr-util-1.6.1.tar.gz debug kernels
[root@localhost src]
# tar xf apr-1.6.5.tar.gz[root@localhost src]
# tar xf apr-util-1.6.1.tar.gz[root@localhost src]
# lsapr-1.6.5 apr-1.6.5.tar.gz apr-util-1.6.1 apr-util-1.6.1.tar.gz debug kernels
[root@localhost src]
# cd apr-1.6.5[root@localhost apr-1.6.5]
# vim configure cfgfile="${ofile}T"
trap "$RM /"$cfgfile/"; exit 1" 1 2 15
# $RM "$cfgfile" //將此行加上注釋,或者刪除此行[root@localhost apr-1.6.5]
# ./configure --prefix=/usr/local/apr配置過程略...
[root@localhost apr-1.6.5]
# make && make install編譯安裝過程略...
[root@localhost apr-1.6.5]
# 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編譯安裝過程略...
//編譯安裝httpd
[root@localhost ~]
# wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.38.tar.gz[root@localhost ~]
# lshttpd-2.4.38.tar.gz
[root@localhost ~]
# tar xf httpd-2.4.38.tar.gz[root@localhost ~]
# cd httpd-2.4.38[root@localhost httpd-2.4.38]
# ./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.38]
# make && make install編譯安裝過程略...
5. httpd常用配置
切換使用MPM(編輯/etc/httpd/conf.modules.d/00-mpm.conf文件)://LoadModule mpm_NAME_module modules/mod_mpm_NAME.so//NAME有三種,分別是:
prefork
event
worker
[root@localhost conf.modules.d]
# pwd/etc/httpd/conf.modules.d
[root@localhost conf.modules.d]
# ls00-base.conf 00-lua.conf 00-proxy.conf 01-cgi.conf00-dav.conf 00-mpm.conf 00-systemd.conf
[root@localhost conf.modules.d]
# vim 00-mpm.conf# Select the MPM module which should be used by uncommenting exactly# one of the following LoadModule lines:# prefork MPM: Implements a non-threaded, pre-forking web server# See: http://httpd.apache.org/docs/2.4/mod/prefork.htmlLoadModule mpm_prefork_module modules/mod_mpm_prefork.so
訪問控制法則:注意:httpd-2.4版本默認(rèn)是拒絕所有主機(jī)訪問的,所以安裝以后必須做顯示授權(quán)訪問示例:
<Directory /var/www/html/www>
<RequireAll>
Require not ip 192.168.1.20
Require all granted
</RequireAll></Directory>
虛擬主機(jī):虛擬主機(jī)有三類:
- 相同IP不同端口
- 不同IP相同端口
- 相同IP相同端口不同域名
//設(shè)置主機(jī)名
[root@localhost ~]
# vim /etc/httpd/conf/httpd.conf......
ServerAdmin root@localhost
## ServerName gives the name and port that the server uses to identify itself.# This can often be determined automatically, but we recommend you specify# it explicitly to prevent problems during startup.## If your host doesn't have a registered DNS name, enter its IP address here.#ServerName
http://www.example.com:80 //取消此行前面的
#號......
[root@localhost ~]
# vim /etc/httpd/conf/httpd.conf//在配置文件的最后加上如下內(nèi)容
[root@localhost ~]
# tail -25 /etc/httpd/conf/httpd.conf#virtual host 1 # 虛擬主機(jī)1的配置<VirtualHost 172.16.12.128:80>
ServerName
http://www.wangqing.com 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>
# virtual host 2 # 虛擬主機(jī)2的配置<VirtualHost 172.16.12.128:80>
ServerName
http://blog.wangqing.com DocumentRoot "/var/www/html/blog"
ErrorLog "/var/log/httpd/blog/error_log"
CustomLog "/var/log/httpd/blog/access_log" combined
<Directory /var/www/html/blog>
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
//創(chuàng)建網(wǎng)頁目錄并修改屬主屬組
[root@localhost ~]
# cd /var/www/html/[root@localhost html]
# ls[root@localhost html]
# mkdir www blog[root@localhost html]
# lltotal 0
drwxr-xr-x 2 root root 6 Aug 5 16:56 blog
drwxr-xr-x 2 root root 6 Aug 5 16:56 www
[root@localhost html]
# chown -R apache.apache blog[root@localhost html]
# chown -R apache.apache www[root@localhost html]
# lltotal 0
drwxr-xr-x 2 apache apache 6 Aug 5 16:56 blog
drwxr-xr-x 2 apache apache 6 Aug 5 16:56 www
//創(chuàng)建網(wǎng)頁
[root@localhost html]
# pwd/var/www/html
[root@localhost html]
# lsblog www
[root@localhost html]
# echo 'hello welcome to visit www' > www/index.html[root@localhost html]
# echo 'hello welcome to visit blog' > blog/index.html//創(chuàng)建相應(yīng)網(wǎng)頁的日志目錄
[root@localhost ~]
# mkdir /var/log/httpd/{www,blog}[root@localhost ~]
# ll /var/log/httpd/total 0
drwxr-xr-x 2 root root 6 Aug 5 09:11 blog
drwxr-xr-x 2 root root 6 Aug 5 09:11 www
[root@localhost ~]
# chown -R apache.apache /var/log/httpd///啟動服務(wù)并查看是否有80端口
[root@localhost ~]
# systemctl start httpd[root@localhost ~]
# ss -antlState 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 :::*
//在客戶機(jī)上驗證1.修改hosts文件
~ cat /etc/hosts
### Host Database## localhost is used to configure the loopback interface# when the system is booting. Do not change this entry.##127.0.0.1 localhost255.255.255.255 broadcasthost
::1 localhost118.31.33.0 zabbix.forevercq.com0.0.0.0
http://account.jetbrains.com//添加以下2行172.16.12.128 www.wangqing.com172.16.12.128
http://blog.wangqing.com2.在瀏覽器上打開網(wǎng)頁測試
ssl:啟用模塊:編輯/etc/httpd/conf.modules.d/00-base.conf文件,添加下面這行,如果已經(jīng)有了但是注釋了,則取消注釋即可
LoadModule ssl_module modules/mod_ssl.so
配置https步驟:- 生成證書(參考博客linux運維系列第6章)
- 配置httpd.conf,取消以下內(nèi)容的注釋
LoadModule ssl_module modules/mod_ssl.so
Include /etc/httpd24/extra/httpd-vhosts.conf
Include /etc/httpd24/extra/httpd-ssl.conf
- 在httpd-vhosts.conf中配置虛擬主機(jī)
- 在httpd-ssl.conf中配置證書的位置
- 檢查配置文件是否有語法錯誤
- 啟動或重啟服務(wù)
- 設(shè)置hosts以便用域名訪問(僅學(xué)習(xí)階段,企業(yè)實際工作中無需做此步。)
作業(yè)
1.編譯安裝httpd-2.4
2.配置三種不同風(fēng)格的虛擬主機(jī)
3.寫博客,要寫明環(huán)境背景、配置步驟,要有驗證截圖
更多網(wǎng)絡(luò)知識學(xué)習(xí),微信runtime66