天天看點

【LINUX】linux搭建web伺服器

linux httpd

假設伺服器位址為192.168.80.20/24

1.   将準備安裝的httpd軟體包共享給everyone ,

(1)在linux上mount.cifs  //真機ip位址/共享檔案夾名   /media /

ls  /meidia/ 檢視

tar    xjvf   httpd-2.4.10.tar.bz2    -c  /usr/src       解壓至/usr/src下

下面兩個插件是httpd2.4以後的版本所需要的

http://ftp.jaist.ac.jp/pub/apache/apr/apr-1.4.6.tar.gz

wgethttp://ftp.jaist.ac.jp/pub/apache/apr/apr-util-1.4.1.tar.gz

下載下傳上面兩個軟體解壓後複制到httpd的srclib解壓目錄中(不帶版本号)

tar   xzvf  apr-1.4.6.tar.gz   -c   /opt

tar   xzvf  apr-util-1.4.1.tar.gz    -c    /opt

cd   /opt

cp   -r    apr-1.4.6   /usr/src/httpd-2.4.10/srclib/apr

cp   -r   apr-util-1.4.1     /usr/src/httpd-2.4.10/srclib/apr-util

(2)安裝編譯語言工具

yum -y install \

gcc \

gcc-c++ \

make \

pcre-devel

(3)cd    /usr/src/httpd-2.4.10/

輸入以下腳本(配置需求)

./configure \

--prefix=/usr/local/apache\

--enable-so \

--enable-rewrite \

--enable-mods-shared=most\

--with-mpm=worker\

--disable-cgid \

--disable-cgi

(參數解釋:

--prefix=   //來指定安裝路徑

--enable-so  //該參數表示支援用mod_so子產品提供的功能,用loadmodule在httpd.conf檔案或包含的conf檔案中動态加載某個子產品。讓 apache 可以支援dso模式

--enable-rewrite  //支援 url 重寫

--enable-mods-shared=most  //選項:告訴編譯器将所有标準子產品都動态編譯為dso子產品。

--with-mpm=worker// 讓apache以worker方式運作

--with-mpm=worker   //該參數是配置apache将以何種模式編譯的。apache網站文檔指出不同作業系統下的不同的預設模式.

--disable-cgid //禁止用一個外部 cgi 守護程序執行cgi腳本

--disable-cgi //禁止編譯 cgi 版本的 php)

安裝如無報錯,輸入 make(編譯)

安裝如無報錯,再輸入make  install(完成安裝)

(4) grep   -v   "#"   /usr/local/apache/bin/apachectl   >   /etc/init.d/httpd

vi/etc/init.d/httpd

在檔案最前面插入下面的行,使其支援chkconfig指令:

#!/bin/sh

              #   chkconfig:   2345   85  15

              # description:   apache  is  a  world  wide web server.

第一行4個數字參數意義分别為:哪些linux級别需要啟動httpd(2,3,4,5);啟動序号(85);關閉序号(15)。

chmod   +x   /etc/init.d/httpd                      添加執行權限

chkconfig   --add   httpd                      

 注如果在/etc/init.d/apache不編輯這個# chkconfig: 2345 85 15内容的話,就沒法用chkconfig--add來添加apache這個服務的

chkconfig --listhttpd     檢視啟動項

(5) vi    /usr/local/apache/conf/httpd.conf

servername   s01.rhel.com

listen  網卡ip位址:80

ln   -s   /usr/local/apache/conf/httpd.conf    /etc/httpd.conf                   做軟連結

/etc/init.d/httpd  start/stop                      啟動/停止

配置檔案位置:  /usr/local/apache

cat   /usr/local/apache/htdocs/index.html    檢視預設文檔

2.匿名通路虛拟目錄

(1)vi   /usr/local/apache/conf/httpd.conf

在末尾加上

include    /usr/local/apache/conf/conf.d/*.conf

儲存。

(2)cd     /usr/local/apache/conf/conf.d

vi    vdir.conf

輸入以下内容

alias   /ic/ "/var/www/icons/"                    虛拟目錄對應實體路徑

<directory  "/var/www/icons">

options   indexes  multiviews   followsymlinks

allowoverride   none

order   allow,deny                      白名單

allow   from   all                      允許所有人通路

require   all   granted                   給目錄授權

</directory>

/usr/local/apache/bin/httpd  -k  start     啟動web服務

可以在浏覽器上輸入   //192.168.80.20/ic/    通路虛拟目錄

3.需要身份驗證的虛拟目錄

vi   /usr/local/apache/conf/conf.d/vdir.conf

在原有的語句末尾加入以下一段:

alias   /share/    "/usr/share/doc/"

<directory  "/usr/share/doc/">

options   indexes  multiviews   followsymlinks

order   allow,deny

allow  from  all

authname   "hello"

authtype   basic                      設定認證的類型,basic為基本的認證

authuserfile   /usr/local/apache/user       設定用于儲存使用者賬号、密碼的認證檔案路徑         require valid-user         要求隻有認證檔案中的合法使用者才能通路。valid-user表示所有合法使用者

#require  all   grante

儲存

添加有通路權限的使用者

./htpasswd  -c  /usr/local/apache/user test              增加test使用者,第一次要加-c

./htpasswd  /usr/local/apache/user  jack          增加jack使用者,不需要加-c

  /usr/local/apache/bin/httpd  -k  restart    重新開機服務

這是,通路\\192.168.80.20\share\  的時候,就需要使用test或者jack使用者來驗證。

4.虛拟主機

搭建好dns

1、全局配置檔案

 vi   /etc/named.conf

options {

        listen-on  port 53  { 192.168.80.11; };

      allow-query     { any; };

}

2、區域配置檔案

 vi   /etc/named.rfc1912.zones

zone  "benet.com"  in  {

        type  master;

        file  "benet.com.zone";

        allow-update  { none; };

};

zone  "accp.com"  in  {

        file  "accp.com.zone";

3、區域資料配置檔案

# vi  /var/named/benet.com.zone

$ttl 1d

@       in soa @  admin(

                                        0       ; serial

                                        1d     ; refresh

                                        1h      ; retry

                                        1w      ; expire

                                        3h)    ; minimum

        ns     @

        a      192.168.80.20

www     a      192.168.80.20

# vi/var/named/accp.com.zone

@       in soa @  accp(

                                        1d      ; refresh

(1)基于域名,準備搭建www.benet.com和www.accp.com兩個網址,ip均為192.168.80.20 .

cd   /usr/local/apache/conf/conf.d

vi   vhost.conf       

輸入以下腳本:

<virtualhost  192.168.80.20:80>

    serveradmin  [email protected]

    documentroot   /opt/benet/              首頁實體路徑

    servername  www.benet.com            域名

    errorlog  logs/bjxh.com-error_log      錯誤日志位址

    customlog   logs/bjxh.com-access_log common

</virtualhost>

<directory"/opt/benet/">                             授權目錄

    options  indexes  multiviews  followsymlinks

    allowoverride  none

    order  allow,deny                               白名單

    allow  from  all

    require  all  granted

<virtualhost  192.168.80.11:80>

    serveradmin  [email protected]

    documentroot  /opt/accp/

    servername  www.accp.com

    errorlog  logs/xhce.com-error_log

    customlog  logs/xhce.com-access_log common

<directory  "/opt/accp/">

    options  indexes  multiviews  followsymlinks

    order  allow,deny

(或者直接為/opt/授權

<directory  "/opt">

    options  none

</directory>)

準備兩個首頁,放在/opt/下

cd  /opt/

mkdir   benet

mkdir    accp

echo    “<h1>this  is benet</h1>   >   /opt/benet/index.html

echo    “<h1>this  is  accp</h1>   >  /opt/accp/index.html

 /usr/local/apache/bin/httpd  -k  restart    重新開機服務

這時候,在浏覽器上輸入www.benet.com 或者www.accp.com的時候就可以分别通路兩個首頁了.

(2)基于端口(比較少用)

vi   /usr/local/apache/conf/conf.d/vhost.conf

将原内容改為:

listen   192.168.80.11:8080

<directory"/opt/benet/">

    options indexes multiviews followsymlinks

    allowoverride none

    order allow,deny

    allow from all

    require all granted

<virtualhost192.168.80.20:80>

    [email protected]

    documentroot /opt/benet/

    servername www.benet.com

    errorlog logs/benet.com-error_log

    customlog logs/benet.com-access_log common

<directory"/opt/accp/">

<virtualhost192.168.80.20:8080>

    documentroot /opt/accp/

    servername www.accp.com

    errorlog logs/accp.com-error_log

    customlog logs/accp.com-access_log common

紅字為跟以上不同的地方

儲存,重新開機服務。

這時候,通路www.benet.com和www.accp.com:8080可以分别通路兩個首頁

繼續閱讀