0543-實踐配置apahe服務並實現基於虛擬主機的實踐
阿新 • • 發佈:2017-08-28
linux
1 apache下載:
登錄官網:http://www.apache.org/ 根據下圖提示下載
下載版本為: httpd-2.2.34.tar.gz (二進制源碼)
2 在web02機器配置、編譯、安裝apache
2.1 apache 安裝
####apache httpd安裝步驟(源碼編譯安裝)####
mkdir -p /home/oldboy/tools/ cd /home/oldboy/tools/ wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.2.34.tar.gz tar xf httpd-2.2.34.tar.gz cd httpd-2.2.34 #進入源碼包裏面才可以配置、編譯、安裝。 #less INSTALL #查看安裝指導 #less README #查看說明 yum install zlib zlib-devel -y #C語言源碼編譯前配置 ./configure --prefix=/application/apache2.2.34 --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=worker --enable-rewrite #源碼編譯成二進制,是寫的過程,速度慢。 make #編譯的過程,(編譯完成後最後一行是:make[1]: Leaving directory `/home/oldboy/tools/httpd-2.2.34‘說明已經編譯完成) #二進制安裝過程,屬於拷貝過程,速度快。 make install #拷貝的過程(安裝後最後一行是make[1]: Leaving directory `/home/oldboy/tools/httpd-2.2.34‘說明已經安裝完成)
2.2 apache編譯安裝後配置(配置三個虛擬主機站點)
ln -s /application/apache2.2.34/ /application/apache ll /application/apache/ [[email protected] httpd-2.2.34]# cd /application/apache/htdocs [[email protected] htdocs]# mkdir bbs www blog [[email protected] htdocs]# cd /application/apache/conf/ [[email protected] conf]# ll 總用量 92 drwxr-xr-x 2 root root 4096 8月 27 21:57 extra -rw-r--r-- 1 root root 13956 8月 27 21:57 httpd.conf -rw-r--r-- 1 root root 12958 8月 27 21:57 magic -rw-r--r-- 1 root root 53015 8月 27 21:57 mime.types drwxr-xr-x 3 root root 4096 8月 27 21:57 original [[email protected] conf]# vim httpd.conf 把#Include conf/extra/httpd-vhosts.conf這行的#註釋去掉。把98行的#ServerName www.example.com:80修改為ServerName 127.0.0.1:80 [[email protected] conf]# vim /application/apache/conf/extra/httpd-vhosts.conf 把<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/application/apache2.2.34/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 "/application/apache2.2.34/htdocs/www" ServerName www.etiantian.org ServerAlias etiantian.org ErrorLog "logs/www-error_log" CustomLog "logs/www-access_log" common </VirtualHost> #VirtualHost全部配置如下所示: [[email protected] conf]# tail -36 /application/apache/conf/extra/httpd-vhosts.conf # # Use name-based virtual hosting. # NameVirtualHost *:80 # # 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 "/application/apache2.2.34/htdocs/www" ServerName www.etiantian.org ServerAlias etiantian.org ErrorLog "logs/www-error_log" CustomLog "logs/www-access_log" common </VirtualHost> # <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/application/apache2.2.34/htdocs/bbs" ServerName bbs.etiantian.org ErrorLog "logs/bbs-error_log" CustomLog "logs/bbs-access_log" common </VirtualHost> # <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/application/apache2.2.34/htdocs/blog" ServerName blog.etiantian.org ErrorLog "logs/blog-error_log" CustomLog "logs/blog-access_log" common </VirtualHost> 檢查語法: [[email protected] conf]# ../bin/apachectl -t Syntax OK #這個OK的標誌是檢查apache的語法沒有錯誤 啟動apache [[email protected] htdocs]# /application/apache/bin/apachectl start 如果apache已經啟動了,優雅重新apache [[email protected] htdocs]# /application/apache/bin/apachectl graceful
3 驗證配置的三個虛擬主機站點是否可以正常登陸(在web01機器上面測試)
在web01上面測試apache先添加hosts文件解析 [[email protected] ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.16.1.5 lb01 172.16.1.6 lb01 172.16.1.7 web02 www.etiantian.org etiantian.org blog.etiantian.org bbs.etiantian.org 172.16.1.8 web01 172.16.1.51 db01 db01.etiantian.org 172.16.1.31 nfs01 172.16.1.41 backup 172.16.1.61 m01 [[email protected] ~]# curl -I www.etiantian.org ##403錯誤代表沒有首頁 HTTP/1.1 403 Forbidden Date: Sun, 27 Aug 2017 14:56:47 GMT Server: Apache/2.2.34 (Unix) DAV/2 Content-Type: text/html; charset=iso-8859-1 在web02虛擬主機站點服務器給各個站點添加首頁: [[email protected] htdocs]# vi /application/apache/conf/httpd.conf 把Options Indexes FollowSymLinks修改為Options -Indexes FollowSymLinks [[email protected] htdocs]# echo ‘apache www‘ >www/index.html [[email protected] htdocs]# echo ‘apache bbs‘ >bbs/index.html [[email protected] htdocs]# echo ‘apache blog‘ >blog/index.html 在web01上面繼續測試:繼續curl虛擬主機站點 [[email protected] ~]# curl www.etiantian.org apache www #測試OK [[email protected] ~]# curl bbs.etiantian.org apache bbs #測試OK [[email protected] ~]# curl blog.etiantian.org apache blog #測試OK [[email protected] ~]# curl -I www.etiantian.org HTTP/1.1 200 OK #測試OK Date: Sun, 27 Aug 2017 16:20:55 GMT Server: Apache/2.2.34 (Unix) DAV/2 Last-Modified: Sun, 27 Aug 2017 14:59:40 GMT ETag: "204c4-b-557bd6d7606d8" Accept-Ranges: bytes Content-Length: 11 Content-Type: text/html
如上測試結果說明三個虛擬主機站點www.etiantian.org、blog.etiantian.org、bbs.etiantian.org都已配置OK了。
######
./configure
--prefix=/application/apache2.2.34
--enable-deflate
--enable-expires
--enable-headers
--enable-modules=most
--enable-so
--with-mpm=worker
--enable-rewrite
虛擬主機配置: | |
域名 | 站點目錄 |
www.etiantian.org | /application/apache/htdocs/www |
blog.etiantian.org | /application/apache/htdocs/blog |
bbs.etiantian.org | /application/apache/htdocs/bbs |
本文出自 “sandshell” 博客,請務必保留此出處http://sandshell.blog.51cto.com/9055959/1959798
0543-實踐配置apahe服務並實現基於虛擬主機的實踐