1. 程式人生 > >linux搭載http伺服器

linux搭載http伺服器

1、配置IP

[[email protected]~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

BOOTPROTO=static

ONBOOT=yes

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

IPADDR=192.168.126.133

NETMASK=255.255.255.0

2、配置主機名

[[email protected]~]# cat /etc/sysconfig/network

NETWORKING=yes

NETWORKING_IPV6=yes

HOSTNAME=web.gx.com

3、修改hosts檔案

[[email protected]~]# cat /etc/hosts

127.0.0.1 localhost.localdomain localhost

::1 localhost6.localdomain6 localhost6

192.168.10.253 web.gx.com web

[[email protected]~]# service network restart

[[email protected]~]# chkconfig network on

4、軟體包的安裝

[[email protected]~]# rpm -q httpd

package httpd is not installed

[[email protected]~]# yum -y install httpd

5、啟動服務

[[email protected]~]# service httpd restart

[[email protected]~]# chkconfig httpd on

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

 Web伺服器域名:www.gx.com

 預設首頁包括:index.html、index.*

 開啟保持連線 

 網站用自己的靜態網頁musicapp測試

伺服器操作:

1、備份主配置檔案

[[email protected] ~]# cd /etc/httpd/conf

[[email protected] conf]# cp httpd.conf httpd.conf.bak

2、修改主配置檔案

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf

74 KeepAlive On

265 ServerName www.gx.com:80

391 DirectoryIndex index.html index.php

3、啟動服務

[[email protected] ~]# service httpd restart

[[email protected] ~]# chkconfig httpd on

將musicapp資料夾中的檔案全部拷貝到/var/www/html/下

4.修改客戶端hosts檔案

192.168.126.133 www.gx.com

[[email protected] ~]# vim /etc/hosts

5.經測試發現通過本機IP192.168.126.133不能訪問,網上查詢後刪除/etc/httpd/conf.d/welcome.conf

6.開啟瀏覽器訪問 http://www.gx.com

3、新建authdir站點,只允許某個IP192.168.126.135訪問www.gx.com,允許所有人訪問www.gx.com/authdir

[[email protected] ~]# mkdir /var/www/html/authdir

[[email protected] ~]# echo “http://www.gx.com/authdir/index.html” > /var/www/html/authdir/index.html

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf

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

338 Order allow,deny

339 Allow from all

340

[[email protected] ~]# service httpd restart

在不同客戶端測試

[[email protected] ~]# tail /var/log/httpd/error_log

試驗四:HTTP的使用者授權

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

1、修改主配置檔案

[[email protected] ~]# 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

2、建立賬戶密碼

[[email protected] ~]# htpasswd -c /etc/httpd/.vuser admin

New password:

Re-type new password:

Adding password for user admin

3、啟動服務測試

[[email protected] ~]# service httpd restart

http://www.gx.com/authdir

實驗五:HTTP目錄別名

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

1、建立測試站點

[[email protected] ~]# mkdir -p /var/www/html/baidu.com/bbs

[[email protected] ~]# cat /var/www/html/baidu.com/bbs/index.html

This is a test Page!!!

This is bbs.baidu.com test Page!!!

2、修改主配置檔案

[[email protected] ~]# tail -n 1 /etc/httpd/conf/httpd.conf

Alias /baidu “/var/www/html/baidu.com/bbs”

3、啟動服務測試

[[email protected] ~]# service httpd restart

http://www.gx.com/baidu

實驗六:檢視預設HTTP使用程序管理方式 更改預設程序管理方式為worker模式

[[email protected] ~]# httpd -l

Compiled in modules:

core.c

prefork.c

http_core.c

mod_so.c

[[email protected] ~]# cd /usr/sbin/

[[email protected] sbin]# mv httpd httpd.prefork

[[email protected] sbin]# mv httpd.worker httpd

[[email protected] sbin]# service httpd restart

[[email protected] sbin]# httpd -l

Compiled in modules:

core.c

worker.c

http_core.c

mod_so.c

試驗七:部署Awstats統計Http訪問日誌

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

[[email protected] ~]# cd /usr/src/

[[email protected] src]# tar -zxvf awstats-7.1.tar.gz -C /usr/local/

[[email protected] src]# cd /usr/local/

[[email protected] local]# mv awstats-7.1/ awstats

[[email protected] local]# cd awstats/tools/

[[email protected] 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.gx.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、修改主配置檔案

[[email protected] tools]# vim /etc/awstats/awstats.www.gx.com.conf

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

[[email protected] tools]# mkdir /var/lib/awstats

3、將日誌檔案匯入Awstats

[[email protected] tools]# ./awstats_updateall.pl now

[[email protected] tools]# crontab -l


輸入該命令出現錯誤提示no crontab for root

這個問題非常簡單,同樣在 root 使用者下輸入 crontab -e 按 Esc 按: wq 回車

在輸入 crontab -l 就沒有問題了

主要原因是由於這個liunx伺服器 第一次使用 crontab ,還沒有生成對應的檔案導致的,執行了 編輯(crontab -e)後 就生成了這個檔案


[[email protected] tools]# service crond restart

[[email protected] tools]# chkconfig crond on

4、驗證:

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

補充:

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

[[email protected] tools]# cat /var/www/html/awstats.html

驗證:

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

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

www.baidu.com 192.168.126.133 baidu網站

www.sohu.com 192.168.126.133 sohu網站

1、修改主配置檔案新建一個配置檔案(虛似主機配置專用)

[[email protected] ~]# vim /etc/httpd/conf.d/virt.conf

NameVirtualHost *:80

<VirtualHost *:80>

DocumentRoot /var/www/baidu

ServerName www.baidu.comE

rrorLog logs/www.baidu.com-error_log

CustomLog logs/www.baidu.com-access_log common

<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

建立網站目錄和檔案

[[email protected] ~]# mkdir /var/www/{baidu,sohu}

[[email protected] ~]# cat /var/www/baidu/index.html

www.baidu.com

[[email protected] ~]# cat /var/www/sohu/index.html

www.sohu.com

[[email protected] ~]# service httpd restart

驗證:先在客戶端修改hosts檔案

192.168.126.133 www.baidu.com

192.168.126.133 www.sohu.com

實驗九:基於埠的虛擬主機

192.168.126.133:8081 baidu網站

192.168.126.133:8082 sohu網站

2、修改主配置檔案新建一個配置檔案(虛似主機配置專用)

[[email protected] ~]# vim /etc/httpd/conf.d/virt.conf

Listen 8081 //啟用埠

Listen 8082

<VirtualHost *:8081>

DocumentRoot /var/www/baidu

ServerName 192.168.126.133

ErrorLog logs/www.baidu.com-error_log

CustomLog logs/www.baidu.com-access_log common

<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

2、啟動服務

[[email protected] ~]# service httpd restart

實驗十:配置HTTP支援php

1、安裝php

[[email protected] ~]# yum -y install php

[[email protected] ~]# cat /var/www/baidu/test.php

<?php //PHP測試頁 phpinfo(); ?>

[[email protected] ~]# service httpd restart

測試:

http://192.168.126.133:8081/test.php