http背景
超文本傳輸協議(HypeText Transfer Protocol)互聯網上應用最廣泛的網絡協議。1960年美國人Ted Nelson構思的一種通過計算機處理文本信息的方法,稱爲'超文本',這成爲HTTP超文本傳輸協議標準架構的發展根基。
技術架構
HTTP是一個客戶端和服務器端請求和應答的標準(TCP),通常,由HTTP客戶端發起一個請求,建立一個到服務器指定端口(默認是80端口)的TCP連接。HTTP服務器則在那個端口監聽客戶端發送過來的請求。一旦收到請求,服務器(向客戶端)發回一個狀態,消息可能是請求的文件、錯誤消息,或其他信息。
軟件包
http 服務端口: 80/tcp(http)
https 服務端口: 443/tcp(https,http+ssl)
配置文件
'使用rpm包安裝的httpd程序環境'
/etc/httpd/conf/httpd.conf '主配置文件'
/etc/httpd/conf.d/*.conf '輔助配置文件'
/etc/httpd/conf.d/welcome.conf '默認測試頁面'
/var/log/httpd/access.log '訪問日誌'
/var/log/httpd/error_log '錯誤日誌'
/var/www/html/ '站點文檔目錄'
/usr/lib64/httpd/modules/ '模塊文件路徑'
/etc/httpd/conf.modules.d/*.conf '模塊配置文件'
mpm:以DSO機制提供,配置文件爲/etc/httpd/conf.modules.d/00-mpm.conf
編譯安裝httpd生成的配置文件
/usr/local/apache/logs '日誌文件'
/etc/httpd24/httpd.conf '主配置文件'
/usr/local/apache/htdocs '站點文檔目錄'
'啓動服務可以使用絕對路徑與相對路徑'
絕對路徑:
[root@yaoxiaorong apache]# /usr/local/apache/bin/httpd
httpd (pid 56281) already running
相對路徑:
[root@yaoxiaorong apache]# cd /usr/local/apache/bin/
[root@yaoxiaorong bin]# ./apachectl
httpd (pid 56281) already running
如果需要解析動態php程序,則需要安裝php
'安裝PHP'
[root@yaoxiaorong ~]# yum install -y php
'如果作爲Apache的模塊運行,並生成對應配置文件'
[root@yaoxiaorong ~]# ll /etc/httpd/modules/libphp5.so
-rwxr-xr-x. 1 root root 4588376 Apr 13 03:04 /etc/httpd/modules/libphp5.so
[root@yaoxiaorong ~]# ll /etc/httpd/conf.d/php.conf
-rw-r--r--. 1 root root 691 Apr 13 03:04 /etc/httpd/conf.d/php.conf
重啓Apache加載PHP
[root@yaoxiaorong ~]# systemctl restart httpd
'編寫php狀態頁面'
[root@yaoxiaorong ~]# cat >> /var/www/html/info.php <<EOF
> <?php
> phpinfo();
> ?>
> EOF
[root@yaoxiaorong html]# systemctl restart httpd
[root@yaoxiaorong html]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
測試訪問php狀態頁面,輸入自己的IP,後面在加info.php,在操作這些步驟之前,一定要關防火牆和selinux。
如圖:
編譯安裝httpd-2.4
'準備環境,將防火牆和selinux'
[root@yaoxiaorong ~]# setenforce 0
[root@yaoxiaorong ~]# systemctl stop firewalld
'安裝開發環境'
[root@yaoxiaorong ~]# yum groups mark install "Development Tools"
Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Determining fastest mirrors
* base: mirrors.nwsuaf.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
Marked install: Development Tools
[root@yaoxiaorong ~]# yum grouplist
Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Loading mirror speeds from cached hostfile
* base: mirrors.nwsuaf.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
Available Environment Groups:
Minimal Install
Compute Node
Infrastructure Server
File and Print Server
Basic Web Server
Virtualization Host
Server with GUI
GNOME Desktop
KDE Plasma Workspaces
Development and Creative Workstation
Installed Groups:
Development Tools '開發工具'
Available Groups:
Compatibility Libraries
Console Internet Tools
Graphical Administration Tools
Legacy UNIX Compatibility
Scientific Support
Security Tools
Smart Card Support
System Administration Tools
System Management
Done
'創建apache組和用戶apache'
[root@yaoxiaorong ~]# groupadd -r apache
[root@yaoxiaorong ~]# useradd -M -s /sbin/nologin -g apache apache
[root@yaoxiaorong ~]# id apache
uid=1000(apache) gid=996(apache) groups=996(apache)
'安裝相關的軟件包'
[root@yaoxiaorong ~]# yum -y install openssl-devel pcre-devel expat-devel libtool
·下載並安裝apr-1.4和apr-util-1.4+
'下載wget'
[root@yaoxiaorong src]# yum -y install wget
[root@yaoxiaorong ~]# cd /usr/src/
[root@yaoxiaorong src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.3.tar.bz2
[root@yaoxiaorong src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
'解壓下載安裝apr-1.4和apr-util-1.4+的壓縮包
[root@yaoxiaorong src]# tar xf apr-1.6.3.tar.bz2
[root@yaoxiaorong src]# tar xf apr-util-1.6.1.tar.bz2
[root@yaoxiaorong src]# ls
apr-1.6.3 apr-util-1.6.1 debug
apr-1.6.3.tar.bz2 apr-util-1.6.1.tar.bz2 kernels
'進入apr-1.6.3將修改configure配置文件'
[root@yaoxiaorong apr-1.6.3]# cd apr-1.6.3/
[root@yaoxiaorong apr-1.6.3]# vim configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#$RM "$cfgfile" //將此行加入註釋,或者刪除此行
'指定編譯參數,使用./configure --help 命令查看可以使用的選項,一般常用的有 --prefix=PREFIX這個選項的意思是定義軟件包安裝到哪裏'
[root@yaoxiaorong apr-1.6.3]# ./configure --prefix=/usr/local/apr
'編譯並安裝'
[root@yaoxiaorong apr-1.6.3]# make -j 2 && make install
[root@yaoxiaorong apr-1.6.3]# cd /usr/src/apr-util-1.6.1/
[root@yaoxiaorong apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@yaoxiaorong apr-util-1.6.1]# make -j 2 && make install
編譯安裝httpd
[root@yaoxiaorong apr-util-1.6.1]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2
[root@yaoxiaorong apr-util-1.6.1]# mv httpd-2.4.34.tar.bz2 /root
[root@yaoxiaorong apr-util-1.6.1]# cd
[root@yaoxiaorong ~]# ls
anaconda-ks.cfg httpd-2.4.34.tar.bz2
[root@yaoxiaorong ~]# tar xf httpd-2.4.34.tar.bz2
anaconda-ks.cfg httpd-2.4.34 httpd-2.4.34.tar.bz2
[root@yaoxiaorong ~]# cd httpd-2.4.34/
[root@yaoxiaorong 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@yaoxiaorong httpd-2.4.34]# make && make install
將虛擬主機改成相同IP相同端口不同域名
[root@yaoxiaorong httpd-2.4.34]# vim /etc/httpd24/httpd.conf
# 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 www.example.com:80 //取消此行前面的#號
在/etc/httpd24/httpd.conf最後一行添加以下內容:
#virtual host 1
<VirtualHost 192.168.228.20:80>
ServerName www.yaoxiaorong.com
DocumentRoot "/usr/local/apache/htdocs/yaoxiaorong"
ErrorLog "logs/yaoxiaorong/error_log"
CustomLog "logs/yaoxiaorong/access_log" combined
<Directory /usr/local/apache/htdocs/yaoxiaorong
<RequireAll>
Require all granted
Require not ip 192.168.1
</RequireAll>
</Directory>
</virtualHost>
/# virtual host 2 (前面的/不需要添加)
<VirtualHost 192.168.228.20:80>
ServerName blog.yxr.com
DocumentRoot "/usr/local/apache/htdocs/yxr"
ErrorLog "logs/yxr/error_log"
CustomLog "logs/yxr/access_log" combined
<Directory /usr/local/apache/htdocs/yxr>
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
'啓動httpd服務,並查看80端口是否啓動起來'
[root@yaoxiaorong httpd-2.4.34]# /usr/local/apache/bin/httpd
[root@yaoxiaorong httpd-2.4.34]# 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 :::*
創建網頁目錄
[root@yaoxiaorong apache]# cd /usr/local/apache/htdocs/
[root@yaoxiaorong htdocs]# ls
index.html
[root@yaoxiaorong htdocs]# mkdir yaoxiaorong
[root@yaoxiaorong htdocs]# ls
index.html yaoxiaorong
[root@yaoxiaorong htdocs]# mkdir yxr
創建網頁
[root@yaoxiaorong htdocs]# pwd
/usr/local/apache/htdocs
[root@yaoxiaorong htdocs]# echo 'hello yaoxiaorong' > yaoxiaorong/index.html
[root@yaoxiaorong htdocs]# echo 'hello yxr' > yxr/index.html
修改網頁目錄修改屬主屬組
[root@yaoxiaorong htdocs]# chown -R apache.apache yaoxiaorong
[root@yaoxiaorong htdocs]# chown -R apache.apache yxr
[root@yaoxiaorong htdocs]# ll
total 4
-rw-r--r--. 1 root root 45 Jun 12 2007 index.html
drwxr-xr-x. 2 apache apache 24 Aug 9 17:44 yaoxiaorong
drwxr-xr-x. 2 apache apache 24 Aug 9 17:44 yxr
創建相應網頁的日誌目錄
[root@yaoxiaorong apache]# cd logs
[root@yaoxiaorong logs]# mkdir yaoxiaorong
[root@yaoxiaorong logs]# mkdir yxr
[root@yaoxiaorong logs]# chown -R apache.apache /usr/local/apache/logs/
重新啓動服務並查看是否有80端口
[root@yaoxiaorong logs]# pkill httpd
[root@yaoxiaorong logs]# /usr/local/apache/bin/httpd
root@yaoxiaorong logs]# 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 :::*
在真實主機上驗證,修改C:\Windows\System32\drivers\etc配置文件
'最後一行添加以下兩行'
192.168.228.20 www.yaoxiaorong.com
192.168.228.20 blog.yxr.com
在瀏覽器上輸入www.yaoxiaorong.com測試,看能否訪問