Linux運維筆記(三)

 

目錄

一、構建Linux下Apache主流WEB服務器

Apache三種工作模式

1、下載並解壓tar包

2、安裝apr、apr-util

3、編譯並安裝

4、安裝完成及查看端口

二、Apache構建基於域名的虛擬主機

1、配置httpd-vhosts.conf

2、httpd.conf引用httpd-vhosts.conf

 3、重啓httpd

4、訪問域名

 


一、構建Linux下Apache主流WEB服務器

Apache HTTP Server(簡稱Apache)是Apache軟件基金會的一個開放源碼的網頁服務器,可以在大多數計算機操作系統中運行,由於其多平臺和安全性被廣泛使用,是最流行的Web服務器端軟件之一。它快速、可靠並且可通過簡單的API擴展,將Perl/Python等解釋器編譯到服務器中。官網:http://httpd.apache.org/download.cgi

Apache三種工作模式

  • prefork模式(默認模式)是很古老但是非常穩定的模式。使用的是多個子進程,Apache在啓動之初,控制進程會建立若干個子進程,然後等待請求進來,並且總是視圖保持一些備用的子進程。爲了不在請求到來時再生成子進程,所以要根據需求不斷的創建新的子進程,最大可以達到每秒32個直到滿足需求爲止。之所以這樣做,是爲了減少頻繁創建和銷燬進程的開銷。每個子進程中只有一個線程,在一個時間點內,只能處理一個請求。在Unix系統中,父進程通常以root身份運行以便邦定80端口,而Apache產生的子進程通常以一個低特權的用戶運行。User和Group指令用於配置子進程的低特權用戶。運行子進程的用戶必須要對他所服務的內容有讀取的權限,但是對服務內容之外的其他資源必須擁有儘可能少的權限。
  • worker模式使用了多進程和多線程的混合模式,worker模式也同樣會先預派生一些子進程,然後每個子進程創建一些線程,同時包括一個監聽線程,每個請求過來會被分配到一個線程來服務。線程比起進程會更輕量,因爲線程是通過共享父進程的內存空間,因此,內存的佔用會減少一些,在高併發的場景下會比prefork有更多可用的線程,表現會更優秀一些。另外,如果一個線程出現了問題也會導致同一進程下的線程出現問題,如果是多個線程出現問題,也只是影響Apache的一部分,而不是全部。由於用到多進程多線程,需要考慮到線程的安全了,在使用keep-alive長連接的時候,某個線程會一直被佔用,即使中間沒有請求,需要等待到超時纔會被釋放(該問題在prefork模式下也存在)。Apache總是試圖維持一個備用(spare)或是空閒的服務線程池。這樣,客戶端無須等待新線程或新進程的建立即可得到處理。在Unix中,爲了能夠綁定80端口,父進程一般都是以root身份啓動,隨後,Apache以較低權限的用戶建立子進程和線程。User和Group指令用於配置Apache子進程的權限。雖然子進程必須對其提供的內容擁有讀權限,但應該儘可能給予他較少的特權。另外,除非使用了suexec ,否則,這些指令配置的權限將被CGI腳本所繼承。內存的佔用會減少一些,在高併發的場景下,表現得比 prefork模式好。
  • event是Apache最新的工作模式,它和worker模式很像,不同的是在於它解決了keep-alive長連接的時候佔用線程資源被浪費的問題(某些線程因爲被keep-alive,掛在那裏等待,中間幾乎沒有請求過來,一直等到超時)。在event工作模式中,會有一些專門的線程用來管理這些keep-alive類型的線程,當有真實請求過來的時候,將請求傳遞給服務器的線程,執行完畢後,又允許它釋放。這樣,一個線程就能處理幾個請求了,實現了異步非阻塞。這增強了在高併發場景下的請求處理。event工作模式在遇到某些不兼容的模塊時,會失效,將會回退到worker模式,一個工作線程處理一個請求。官方自帶的模塊,全部是支持event工作模式的。event工作模式需要Linux系統(Linux2.6+)對epoll的支持,才能啓用。需要補充的是HTTPS的連接(SSL),它的運行模式仍然是類似worker的方式,線程會被一直佔用,知道連接關閉。部分比較老的資料裏,說event MPM不支持SSL,那個說法是幾年前的說法,現在已經支持了。

安裝步驟:

1、下載並解壓tar包

官網:http://httpd.apache.org/download.cgi

rpm -qa httpd //檢查是否安裝有httpd,可以使用yum install httpd安裝(默認路徑/etc/httpd)或者如下方式

wget https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.41.tar.gz

tar xzf httpd-2.4.41.tar.gz

[root@10 opt]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4                                                                .41.tar.gz
--2020-03-22 13:16:30--  https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/http                                                                d-2.4.41.tar.gz
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8                                                                .193, 2402:f000:1:408:8100::1
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.                                                                8.193|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9267917 (8.8M) [application/octet-stream]
Saving to: ‘httpd-2.4.41.tar.gz’

100%[======================================>] 9,267,917   2.31MB/s   in 3.8s

2020-03-22 13:16:35 (2.31 MB/s) - ‘httpd-2.4.41.tar.gz’ saved [9267917/9267917]
[root@10 opt]# tar xzf httpd-2.4.41.tar.gz
[root@10 opt]# ls
httpd-2.4.41  httpd-2.4.41.tar.gz

2、安裝apr、apr-util

 yum install apr apr-util apr-devel apr-util-devel

3、編譯並安裝

1)./configure --prefix=/usr/local/apache2 [--enable-rewrite --enable-so] //可選參數

2)make

3)make install

注:錯誤解決

1)configure: error: C compiler cannot create executables    See `config.log' for more details

config.log:./configure: line 5327: gcc: command not found
yum install gcc / yum install glibc-headers / yum install gcc-c++

2)configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

yum -y install pcre-devel

4、安裝完成及查看端口

默認路徑:/usr/local/apache2   (yum install httpd安裝默認路徑/etc/httpd)

配置文件:/usr/local/apache2/conf/httpd.conf (yum install httpd默認配置文件路徑/etc/httpd/conf/httpd.conf)

默認發佈目錄:/usr/local/apache2/htdocs (yum install httpd默認發佈路徑/var/www/html)

啓動腳本:/usr/local/apache2/bin/apachectl start

[root@10 httpd-2.4.41]# cd /usr/local/apache2/
[root@10 apache2]# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules
[root@10 apache2]# cd conf/
[root@10 conf]# ls
extra  httpd.conf  magic  mime.types  original
[root@10 conf]# cd ../htdocs/
[root@10 htdocs]# ls
index.html
[root@10 htdocs]# cd ../bin/
[root@10 bin]# ls
ab         apxs      dbmmanage  envvars-std  htcacheclean  htdigest  httpd      logresolve
apachectl  checkgid  envvars    fcgistarter  htdbm         htpasswd  httxt2dbm  rotatelogs
[root@10 bin]# /usr/local/apache2/bin/apachectl start
[root@10 bin]# ps -ef | grep http
root     20238     1  0 14:00 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
daemon   20239 20238  0 14:00 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
daemon   20240 20238  0 14:00 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
daemon   20241 20238  0 14:00 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
root     20324  1288  0 14:00 pts/0    00:00:00 grep --color=auto http
[root@10 bin]# netstat -an |grep 80
tcp6       0      0 :::80                   :::*                    LISTEN
udp6       0      0 fe80::f582:e24c:6a1:123 :::*
udp6       0      0 fe80::4f14:193b:a52:123 :::*
[root@10 bin]# netstat -ntl |grep 80
tcp6       0      0 :::80                   :::*                    LISTEN
[root@10 bin]# lsof -i :80   // yum install lsof
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd   20238   root    4u  IPv6  29064      0t0  TCP *:http (LISTEN)
httpd   21147 daemon    4u  IPv6  29064      0t0  TCP *:http (LISTEN)
httpd   21148 daemon    4u  IPv6  29064      0t0  TCP *:http (LISTEN)
httpd   21149 daemon    4u  IPv6  29064      0t0  TCP *:http (LISTEN)
httpd   21372 daemon    4u  IPv6  29064      0t0  TCP *:http (LISTEN)
[root@10 bin]# curl http://192.168.56.102
<html><body><h1>It works!</h1></body></html>

訪問(注意關閉防火牆)

二、Apache構建基於域名的虛擬主機

1、配置httpd-vhosts.conf

[root@10 extra]# pwd
/usr/local/apache2/conf/extra
[root@10 extra]# ls
httpd-autoindex.conf  httpd-languages.conf           httpd-ssl.conf
httpd-dav.conf        httpd-manual.conf              httpd-userdir.conf
httpd-default.conf    httpd-mpm.conf                 httpd-vhosts.conf
httpd-info.conf       httpd-multilang-errordoc.conf  proxy-html.conf
[root@10 extra]# cat httpd-vhosts.conf
# Virtual Hosts
#
# Required modules: mod_log_config

# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/usr/local/apache2/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/dummy-host.example.com-error_log"
    CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/usr/local/apache2/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "logs/dummy-host2.example.com-error_log"
    CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>

示例配置參考:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/data/webapps/test1"
    ServerName www.test1.com
    <Directory "/data/webapps/test1">
        AllowOverride All
        Options +FollowSymlinks
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog "logs/test1-error_log"
    CustomLog "logs/test1-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/data/webapps/test2"
    ServerName www.test2.com
    <Directory "/data/webapps/test2">
        AllowOverride All
        Options +FollowSymlinks
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog "logs/test2-error_log"
    CustomLog "logs/test2-access_log" common
</VirtualHost>

 mkdir /data/webapps/{test1,test2} -p,創建index.html

2、httpd.conf引用httpd-vhosts.conf

 vi /usr/local/apache2/conf/httpd.conf,取消註釋

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

 3、重啓httpd

[root@10 bin]# ./apachectl -t
Syntax OK
[root@10 bin]# ./apachectl restart
httpd not running, trying to start

[root@10 bin]# ./apachectl graceful   //平滑重啓,不會殺進程
[root@10 bin]#

4、訪問域名

本地主機綁定hosts,路徑:C:\Windows\System32\drivers\etc\hosts

添加:

192.168.56.102 www.test1.com
192.168.56.102 www.test2.com

如果訪問返回Forbidden You don't have permission to access this resource.

修改httpd.conf中

<Directory /> AllowOverride none Require all denied </Directory>

修改爲<Directory /> AllowOverride none Require all granted </Directory>

訪問成功後:

 

Apache基於IP虛擬主機同樣跟域名一樣,在服務器配置多個IP,然後把域名改成IP即可。

 

 

 

補充內容:

tar壓縮解壓常見參數

-c:建立一個壓縮文件的參數指令(creat)

-x:解開一個壓縮文件的參數指令

-t:查看tar file裏面文件

-z:是否同時具有gzip屬性,亦即是否需要用gzip壓縮

-j:是否同時具有bzip2的屬性,亦即是否需要用bzip2壓縮

-v:壓縮的過程中顯示文件

-f:使用檔名,注意,在f之後要立即接檔名,不要再加參數

--exclude FIFE:壓縮過程中,不要將FILE打包

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章