安裝軟件包httpd(軟件包)httpd-devel(開發工具)httpd-manual(服務手冊)
[[email protected]]# yum install httpd httpd-devel httpd-manual –y
啓動服務
[root@localhost~]# service httpd restart 停止 httpd: [失敗] 啓動 httpd: [確定]
默認端口 TCP 80
查看服務進程,是否成功啓動
[root@localhost~]# netstat -ln | grep :80 tcp 0 0 :::80 :::* LISTEN
查看所有httpd進程,一個父進程,其餘均爲此進程的子進程。pstree –p 可以查看
[root@localhost~]# ps -ef | grep http root 4959 1 0 10:10 ? 00:00:00 /usr/sbin/httpd apache 4960 4959 0 10:10 ? 00:00:00 /usr/sbin/httpd apache 4961 4959 0 10:10 ? 00:00:00 /usr/sbin/httpd apache 4962 4959 0 10:10 ? 00:00:00 /usr/sbin/httpd apache 4963 4959 0 10:10 ? 00:00:00 /usr/sbin/httpd apache 4964 4959 0 10:10 ? 00:00:00 /usr/sbin/httpd apache 4965 4959 0 10:10 ? 00:00:00 /usr/sbin/httpd apache 4967 4959 0 10:10 ? 00:00:00 /usr/sbin/httpd apache 4968 4959 0 10:10 ? 00:00:00 /usr/sbin/httpd
enlinks,命令行瀏覽器命令。--dump以文本方式顯示,--source以源碼方式顯示,-default-keys使用用戶名密碼登陸。
[root@localhost~]# elinks --dump /var/www/error/noindex.html [root@localhost~]# elinks --dump http://www.example.com [root@localhost~]# elinks -default-keys http://www.example.com/movie
編輯主配置文件。
[root@localhost ~]#vim /etc/httpd/conf/httpd.conf 265 #ServerName //本站點的FQDN名稱 281 DocumentRoot"/var/www/html" //網頁文檔的根目錄 391 DirectoryIndex index.html index.html.var //默認首頁文件 472 ErrorLog logs/error_log //錯誤日誌文件位置 501 #CustomLog logs/access_log common //訪問文件位置 134 Listen 80 //監聽端口 63 PidFile run/httpd.pid //進程文件位置 57 ServerRoot "/etc/httpd" //服務目錄 251 ServerAdmin root@localhost //管理員郵箱 231 User apache //服務所屬用戶 232 Group apache //服務所屬組 68 Timeout 120 //網絡連接超時 74 KeepAlive Off //是否保持連接 81 MaxKeepAliveRequests 100 //每次連接最多處理的請求數 87 KeepAliveTimeout 15 //保持連接的超時時限 210 Include conf.d/*.conf //可以包含其他子配置文件
網頁文檔的根目錄
[root@localhost~]# cd /var/www/html/ [root@localhosthtml]# ls Authdir 編輯主頁內容 [root@localhosthtml]# vim index.html
客戶機地址限制
root@localhost~]# vim /etc/httpd/conf/httpd.conf Order deny,allow 先拒絕後允許,默認允許所有 Allow from all Allow,deny先允許後拒絕,默認拒絕所有。 先讀前邊限制,繼續讀後一限制,若衝突,以後一限制爲準。若要限制某一IP則爲 order allow,deny deny IP地址
對目錄進行用戶限制
[root@localhost~]# mkdir /var/www/html/authdir/ [root@localhost~]# vim /var/www/html/authdir/index.html <h1>TEST</h1>
添加一訪問用戶並設密碼,-c 路徑表示新建一用戶文件。
[root@localhost~]# htpasswd -c /etc/httpd/auth.ulist admin Newpassword: Re-typenew password: Addingpassword for user admin
編輯主配置文件,添加目錄訪問控制
[root@localhost~]# vim /etc/httpd/conf/httpd.conf <Directory"/var/www/html/authdir/"> Authname "Need Auth" 認證領域名稱 Authtype basic 認證類型 Authuserfile /etc/httpd/auth.ulist 用戶數據文件的路徑 Require valid-user指定用戶或組的授權 </Directory> [root@localhost~]# service httpd restart 停止 httpd: [確定] 啓動 httpd: [確定]
訪問路徑別名設置
[root@localhostly]# mkdir -p /var/www/html/l/y/z/c/t [root@localhostly]# vim /var/www/html/l/y/z/c/t/index.html [root@localhostly]# vim /etc/httpd/conf/httpd.conf Alias/123 "/var/www/html/l/y/z/c/t" [root@localhostly]# service httpd restart 停止 httpd: [確定] 啓動 httpd:httpd: apr_sockaddr_info_get() failed for ser2.example.com httpd: Could notreliably determine the server's fully qualified domain name, using 127.0.0.1for ServerName 服務啓動成功單出現提示表示不能解析ser2.example.com
AWstats日誌分析
[root@localhost~]# cd /root/Desktop/ [root@localhostDesktop]# ls awstats-7.1.tar.gz [root@localhostDesktop]# tar zxf awstats-7.1.tar.gz -C /usr/local [root@localhostDesktop]# cd /usr/local/ [root@localhostlocal]# ls awstats-7.1 bin etc games include lib lib64 libexec sbin share src [root@localhostlocal]# mv awstats-7.1 awstats [root@localhostlocal]# ls awstats bin etc games include lib lib64 libexec sbin share src [root@localhostlocal]# cd awstats/ [root@localhostawstats]# ls docs README.TXT tools wwwroot [root@localhostawstats]# cd tools/ [root@localhosttools]# ls awstats_buildstaticpages.pl awstats_exportlib.pl geoip_generator.pl logresolvemerge.pl nginx webmin awstats_configure.pl awstats_updateall.pl httpd_conf maillogconvert.pl urlaliasbuilder.pl xslt
安裝軟件包(源碼包安裝),執行pl文件
[root@localhosttools]# ./awstats_configure.pl Enter full configfile path of your Web server. Example:/etc/httpd/httpd.conf Example:/usr/local/apache2/conf/httpd.conf Example:c:\Program files\apache group\apache\conf\httpd.conf Config file path('none' to skip web server setup): >/etc/httpd/conf/httpd.conf Do you want me tobuild a new AWStats config/profile file(required if first install) [y/N] ?y What is the nameof your web site or profile analysis ? Example:www.mysite.com Example: demo Your web site,virtual server or profile name: > www.example.com In which directorydo you plan to store your config file(s) ? Default:/etc/awstats Directory path tostore config file(s) (Enter for default): > PressENTER to continue... PressENTER to finish...
編輯awstats配置文件
[root@localhosttools]# vim /etc/awstats/awstats.www.example.com.conf LogFile="/var/log/httpd/access_log" DirData="/var/lib/awstats" [root@localhosttools]# mkdir /var/lib/awstats
運行awstats。提取日誌。
[root@localhosttools]# ./awstats_updateall.pl now Running'"/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -update-config=www.example.com -configdir="/etc/awstats"' to update config www.example.com Create/Updatedatabase for config "/etc/awstats/awstats.www.example.com.conf" byAWStats version 7.0 (build 1.975) From data in logfile "/var/log/httpd/access_log"... Phase 1 : Firstbypass old records, searching new record... Searching newrecords from beginning of log file... Phase 2 : Nowprocess new records (Flush history on disk after 20000 hosts)... Jumped lines infile: 0 Parsed lines infile: 75 Found 0 dropped records, Found 0 comments, Found 0 blank records, Found 0 corrupted records, Found 0 old records, Found 75 new qualified records
重啓服務
[root@localhosthtml]# service httpd restart 停止 httpd: [確定] 啓動 httpd: [確定]
添加計劃任務
[root@localhosthtml]# crontab -e */5 * * * */usr/local/awstats/tools/awstats_updateall.pl now [root@localhosthtml]# service crond restart 停止 crond: [確定] 啓動 crond: [確定] [root@localhosthtml]# chkconfig crond on
由於訪問路徑過於複雜,設置一個較簡單路徑自動跳轉到此頁面。
[root@localhost ~]#vim /var/www/html/aw.html <html><head> <metahttp-equiv=refresh content="0; url=http://192.168.10.1/awstats/awstats.pl?config=www.example.com"> </head> <body></body></html> ~ [root@localhost~]# service httpd restart
基於域名的虛擬主機(使用最多)
[root@localhost~]# mkdir -p /data/www/html/www [root@localhost~]# mkdir -p /data/www/html/bbs [root@localhost~]# cd /data/www/html/www/ [root@localhostwww]# vim index.html [root@localhostwww]# vim ../bbs/index.html [root@localhosthttpd]# vim conf.d/vi.conf NameVirtualHost*:80 <VirtualHost*:80> DocumentRoot /data/www/html/www 網頁文檔存放位置 ServerName www.example.com域名 ErrorLoglogs/dummy-www.example.com-error_log 錯誤日誌文件位置 CustomLoglogs/dummy-www.example.com-access_log common 訪問日誌 </VirtualHost> <VirtualHost*:80> DocumentRoot /data/www/html/bbs ServerName bbs.example.com ErrorLoglogs/dummy-bbs.example.com-error_log CustomLoglogs/dummy-bbs.example.com-access_log common </VirtualHost> [root@localhosthttpd]# service httpd restart
基於端口的虛擬主機(一般於測試時使用,開發環境)
[root@localhosthttpd]# vim conf.d/vi.conf #NameVirtualHost*:80 基於端口的不需要 Listen 81 監聽81端口,在httpd主配置文件中已listen80,只需添加增加的端口,也可以在httpd.conf中添加 <VirtualHost192.168.10.1:80> DocumentRoot /data/www/html/www ServerName www.example.com不需要域名添加也可以 ErrorLoglogs/dummy-www.example.com-error_log CustomLoglogs/dummy-www.example.com-access_log common </VirtualHost> <VirtualHost192.168.10.1:81> DocumentRoot /data/www/html/bbs ServerName bbs.example.com ErrorLoglogs/dummy-bbs.example.com-error_log CustomLoglogs/dummy-bbs.example.com-access_log common </VirtualHost> [root@localhosthttpd]# service httpd restart
基於IP的虛擬主機
[root@localhostwww]# vim /etc/httpd/conf.d/vi.conf #NameVirtualHost*:80 <VirtualHost192.168.10.1> DocumentRoot /data/www/html/www ServerName www.example.com ErrorLoglogs/dummy-www.example.com-error_log CustomLog logs/dummy-www.example.com-access_logcommon </VirtualHost> <VirtualHost192.168.10.2> DocumentRoot /data/www/html/bbs ServerName bbs.example.com ErrorLoglogs/dummy-bbs.example.com-error_log CustomLoglogs/dummy-bbs.example.com-access_log common </VirtualHost> [root@localhostwww]# yum -y install php [root@localhostwww]# vim index.php <?php phpinfo(); ?> [root@localhostwww]# service httpd restart
源碼包安裝apachectl服務,目錄路徑爲源碼包的安裝路徑。注意主配置文件中的權限設置。源碼包安裝完成後,可根據下面操作將源碼包服務添加到開機自啓服務中。
[root@localhosthttpd]# cp /usr/local/httpd/bin/apachectl /etc/init.d/ [root@localhosthttpd]# cd /etc/init.d/ [[email protected]]# mv apachectl httpd [[email protected]]# vim httpd #chkconfig: 2345 56 30 #description:web server [root@localhost init.d]#chkconfig --add httpd [[email protected]]# service httpd stop [[email protected]]# service httpd start