Apache LINUX中搭建HTTP服務器

              LINUX中搭建HTTP服務器

 在LINUX系統中安裝Apache做爲HTTP的服務程序,通過設置配置文件,可以實現很多功能,如訪問控制,虛似主機,基於域名的虛擬主機,通過安裝Awstats我們可以更方便的統計Http訪問日誌

 

實驗拓撲:

                                    Linux Client

-----RHEL5.9(vmnet1)----------(vmnet1)

                                      Win7 Client

 

實驗一:查看默認HTTP配置

     找到默認紅帽歡迎頁面

(/etc/httpd/conf/httpd.conf ---->Include ----> /etc/httpd/conf.d  ----> welcome.conf  ----> /var/www/error/noindex.html)

前提條件:

1、配置IP

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

BOOTPROTO=static

ONBOOT=yes

HWADDR=00:0c:29:5d:a8:80

IPADDR=192.168.10.253

NETMASK=255.255.255.0

2、配置主機名

[root@localhost ~]# cat /etc/sysconfig/network

NETWORKING=yes

NETWORKING_IPV6=yes

HOSTNAME=web01.tarena.com

3、修改hosts文件

[root@localhost ~]# cat /etc/hosts

127.0.0.1               localhost.localdomain localhost

::1             localhost6.localdomain6 localhost6

192.168.10.253  web01.tarena.com        web01

[root@localhost ~]# service network restart

[root@localhost ~]# chkconfig network on

4、軟件包的安裝

[root@localhost ~]# rpm -q httpd

package httpd is not installed

[root@localhost ~]# yum -y install httpd

5、啓動服務

[root@localhost ~]# service httpd restart

[root@localhost ~]# chkconfig httpd on

 

試驗二:基本HTTP服務器的配置

     Web服務器域名:www.tarena.com

     默認首頁包括:index.html、index.php

     開啓保持連接

     確認默認httpd是否支持php    

     網站用老師提供的test_web.zip測試

 

服務器操作:

1、備份主配置文件

[root@localhost ~]# cd /etc/httpd/conf

[root@localhost conf]# cp httpd.conf httpd.conf.bak

2、修改主配置文件

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf

...

 74 KeepAlive On

...

265 ServerName www.tarena.com:80

...

391 DirectoryIndex index.html index.php

...

3、啓動服務

[root@localhost ~]# service httpd restart

[root@localhost ~]# chkconfig httpd on

[root@localhost ~]# cd /root/Desktop/

[root@localhost Desktop]# unzip test_web.zip

[root@localhost Desktop]# mv jiajutiyan/* /var/www/html/

4、編寫測試php頁面

[root@localhost ~]# cat /var/www/html/test.php

<?php

        phpinfo();

?>

測試:

1、在客戶端hosts文件指定

192.168.10.253         www.tarena.comwww

2、打開瀏覽器

http://www.tarena.com

http://www.tarena.com/test.php

 

.

3、新建authdir站點,只允許192.168.1.251訪問www.tarena.com,允許所有人訪問www.tarena.com/authdir

[root@web01 ~]# mkdir /var/www/html/authdir

[root@web01 ~]# echo "http://www.tarena.com/authdir/index.html" >  /var/www/html/authdir/index.html

[root@web01 ~]# vim /etc/httpd/conf/httpd.conf

...

337 <Directory /var/www/html/authdir>

338         Order allow,deny

339         Allow from all

340 </Directory>

[root@localhost ~]# service httpd restart

在不同客戶端測試

[root@localhost ~]# tail /var/log/httpd/error_log

 

試驗四:HTTP的用戶授權

     客戶端訪問http://www.tarena.com/authdir需要輸入用戶名密碼驗證

 

1、修改主配置文件

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf

...

337 <Directory "/var/www/html/authdir">

338         Order allow,deny

339         Allow from all

340         AuthName "Please Input Password!!"

341         AuthType Basic

342         AuthUserFile "/etc/httpd/.vuser"

343         Require valid-user

344 </Directory>

...

2、創建賬戶密碼

[root@localhost ~]# htpasswd -c /etc/httpd/.vuser admin

New password:

Re-type new password:

Adding password for user admin

3、啓動服務測試

[root@localhost ~]# service httpd restart

http://www.tarena.com/authdir

實驗五:HTTP目錄別名

     客戶端訪問http://www.tarena.com/sina時可以訪問/var/www/html/sina.com/bbs下的網頁

1、創建測試站點

[root@localhost ~]# mkdir -p /var/www/html/sina.com/bbs

[root@localhost ~]# cat /var/www/html/sina.com/bbs/index.html

<html>

<head><title>This is a test Page!!!</title></head>

<body>

<h1>This is bbs.sina.com test Page!!!</h1>

</body>

</html>

2、修改主配置文件

[root@localhost ~]# tail -n 1 /etc/httpd/conf/httpd.conf

Alias /sina     "/var/www/html/sina.com/bbs"

3、啓動服務測試

[root@ser1 ~]# service httpd restart

http://www.tarena.com/sina

實驗六:

     查看默認HTTP使用進程管理方式

     更改默認進程管理方式爲worker模式

[root@localhost ~]# httpd -l

Compiled in modules:                                   

  core.c

  prefork.c

  http_core.c

  mod_so.c

[root@localhost ~]# cd /usr/sbin/

[root@localhost sbin]# mv httpd httpd.prefork

[root@localhost sbin]# mv httpd.worker httpd

[root@localhost sbin]# service httpd restart

[root@localhost sbin]# httpd -l

Compiled in modules:

  core.c

  worker.c

  http_core.c

  mod_so.c

試驗七:

     部署Awstats統計Http訪問日誌

1、安裝軟件(軟件在/usr/src下)

[root@localhost ~]# cd /usr/src/

[root@localhost src]# tar -zxvf awstats-7.1.tar.gz -C /usr/local/

[root@localhost src]# cd /usr/local/

[root@localhost local]# mv awstats-7.1/ awstats

[root@localhost local]# cd awstats/tools/

[root@localhost tools]# ./awstats_configure.pl

...

Config file path ('none' to skip web server setup):

> /etc/httpd/conf/httpd.conf    //輸入apache的主配置文件

...

-----> Need to create a new config file ?

Do you want me to build a new AWStats config/profile

file (required if first install) [y/N] ? y   //生成awstats的配置文件

...

Your web site, virtual server or profile name:

> www.tarena.com            //輸入你的web服務器名字

...

Default: /etc/awstats

Directory path to store config file(s) (Enter for default):

>

...

/usr/local/awstats/tools/awstats_updateall.pl now

Press ENTER to continue...

...

Press ENTER to finish...

2、修改主配置文件

[root@localhost tools]# vim /etc/awstats/awstats.www.tarena.com.conf

...

  51 LogFile="/var/log/httpd/access_log"

[root@localhost tools]# mkdir /var/lib/awstats

3、將日誌文件導入Awstats

[root@localhost tools]# ./awstats_updateall.pl now

[root@localhost tools]# crontab -l

*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now

[root@localhost tools]# service crond restart

[root@localhost tools]# chkconfig crond on

4、驗證:

http://www.tarena.com/awstats/awstats.pl?config=www.tarena.com

補充:

通過html代碼實現網頁跳轉功能

[root@localhost tools]# cat /var/www/html/awstats.html

<html>

<head><meta http-equiv=refresh content="0;  url=http://www.tarena.com/awstats/awstats.pl? config=www.tarena.com">

</head>

<body>

</body>

</html>

驗證:

http://www.tarena.com/awstats.html 

 

實驗八:基於域名的虛擬主機

     www.sina.com     192.168.10.253    sina網站

     www.sohu.com    192.168.10.253    sohu網站

1、修改主配置文件

新建一個配置文件(虛似主機配置專用)

[root@localhost ~]# vim /etc/httpd/conf.d/virt.conf

NameVirtualHost *:80

<VirtualHost *:80>

DocumentRoot /var/www/sina

ServerName www.sina.com

ErrorLog logs/www.sina.com-error_log

CustomLog logs/www.sina.com-access_log common

</VirtualHost>

<VirtualHost *:80>

DocumentRoot /var/www/sohu

ServerName www.sohu.com

ErrorLog logs/www.sohu.com-error_log

CustomLog logs/www.sohu.com-access_log common

</VirtualHost>

創建網站目錄和文件

[root@localhost ~]# mkdir /var/www/{sina,sohu}

[root@localhost ~]# cat /var/www/sina/index.html

<h1>www.sina.com</h1>

[root@localhost ~]# cat /var/www/sohu/index.html

<h1>www.sohu.com</h1>

[root@localhost ~]# servicehttpd restart

驗證:

先在客戶端修改hosts文件

192.168.10.253         www.sina.com

192.168.10.253         www.sohu.com

 

實驗九:基於端口的虛擬主機

     192.168.10.253:8081              sina網站

     192.168.10.253:8082              sohu網站

2、修改主配置文件

新建一個配置文件(虛似主機配置專用)

[root@localhost ~]# vim /etc/httpd/conf.d/virt.conf

Listen 8081                                             //激活端口

Listen 8082

<VirtualHost *:8081>

DocumentRoot /var/www/sina

ServerName 192.168.10.253

ErrorLog logs/www.sina.com-error_log

CustomLog logs/www.sina.com-access_log common

</VirtualHost>

<VirtualHost *:8082>

DocumentRoot /var/www/sohu

ServerName 192.168.10.253

ErrorLog logs/www.sohu.com-error_log

CustomLog logs/www.sohu.com-access_log common

</VirtualHost>

2、啓動服務

[root@localhost ~]# servicehttpd restart

 

 

實驗十:配置HTTP支持php

1、安裝php

[root@localhost ~]# yum -y install php

[root@localhost ~]# cat /var/www/sina/test.php

<?php                                              //PHP測試頁

phpinfo();

?>

[root@localhost ~]# servicehttpd restart

測試:

http://192.168.10.253:8081/test.php

 

 

 

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