任何一個域名指向這臺服務器,只要是沒有對應的虛擬主機,就會由這個默認虛擬主機來處理。先編輯一下配置文件httpd.confjavascript
vim /usr/local/apache2.4/conf/httpd.conf 搜索httpd-vhost找到這行吧行首的#號刪除
保存主配置文件,而後編輯虛擬主機配置文件:php
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf // 這個配置文件就是虛擬主機配置文件了,後面的操做都是在這個裏面完成的。
這裏面每個段,都是一個主機css
<VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/usr/local/apache2.4/docs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "logs/dummy-host.example.com-error_log" CustomLog "logs/dummy-host.example.com-access_log" common </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/usr/local/apache2.4/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 webmaster@dummy-host2.example.com //定義用戶的郵箱,沒用能夠直接刪掉 DocumentRoot "/data/wwwroot/abc.com" //定義網站的根目錄 ServerName abc.com ErrorLog "logs/abc-host2.example.com-error_log" //指定錯誤日誌 CustomLog "logs/abc-host2.example.com-access_log" common //指定訪問日誌 </VirtualHost>
建立一個網站根目錄[root@aminglinux-01 ~]# mkdir /data/wwwroot/abc.comhtml
定義一個phpjava
[root@aminglinux-01 ~]# vim /data/wwwroot/abc.com/index.php <?php echo "abc.com"; ?> ~
curl -x192.168.245.128:80 abc.comlinux
[root@aminglinux-01 ~]# curl -x192.168.245.128:80 abc.com abc.com[root@aminglinux-01 ~]#
不生效的話 能夠試着從新加載配置文件/usr/local/apache2.4/bin/apachectl gracefulweb
curl -x/--proxy 在給定的端口上使用HTTP代理apache
虛擬主機配置文件一但生效,其餘配置就會失效。vim
所謂默認主機,不管任何域名解析到這個ip上,都會訪問默認虛擬主機。瀏覽器
<VirtualHost *:80> DocumentRoot "/data/wwwroot/www.123.com" ServerName www.123.com <Directory /data/wwwroot/www.123.com> //指定認證的目錄 AllowOverride AuthConfig //這個至關於打開認證的開關 AuthName "123.com user auth" //自定義認證的名字,做用不大 AuthType Basic //認證的類型,通常爲Basic,其餘類型阿銘沒用過 AuthUserFile /data/.htpasswd //指定密碼文件所在位置 require valid-user //指定須要認證的用戶爲所有可用用戶
改爲:
<VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com <Directory /data/wwwroot/abc.com> AllowOverride AuthConfig AuthName "abc.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </Directory> ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost>
[root@aminglinux-01 ~]# /usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd aming New password: Re-type new password: Adding password for user aming [root@aminglinux-01 ~]#
-c 建立
htpasswd第一次建立用戶要用到-c 參數 第2次就不能加-c了,不然會覆蓋前面已創建好的用戶
-m 指定密碼加密類型
[root@aminglinux-01 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@aminglinux-01 ~]# /usr/local/apache2.4/bin/apachectl graceful [root@aminglinux-01 ~]#
curl -x192.168.245.128:80 abc.com
[root@aminglinux-01 ~]# curl -x192.168.245.128:80 abc.com <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Unauthorized</title> </head><body> <h1>Unauthorized</h1> <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.</p> </body></html>
出現401報錯,從瀏覽器輸入網址,提示須要輸入用戶名密碼
或者curl -x192.168.245.128:80 -uaming:123456 abc.com -I 出現狀態碼 200
[root@aminglinux-01 ~]# curl -x192.168.245.128:80 -uaming:123456 abc.com -I HTTP/1.1 200 OK Date: Mon, 09 Oct 2017 10:16:45 GMT Server: Apache/2.4.27 (Unix) PHP/5.6.30 X-Powered-By: PHP/5.6.30 Content-Type: text/html; charset=UTF-8
這樣就成功了
<VirtualHost *:80> DocumentRoot "/data/wwwroot/www.123.com" ServerName www.123.com <FilesMatch admin.php> AllowOverride AuthConfig AuthName "123.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </FilesMatch> </VirtualHost>
惟一的區別就是<FilesMatch admin.php> ,指定的是單個文件纔會出現認證。而以前是用Directory指定整個目錄
FilesMatch後面的文件是相對路徑,而Files、Directory則使用絕對路徑
<VirtualHost *:80> DocumentRoot "/data/wwwroot/www.123.com" ServerName www.123.com ServerAlias 123.com <IfModule mod_rewrite.c> //須要mod_rewrite模塊支持 RewriteEngine on //打開rewrite功能 RewriteCond %{HTTP_HOST} !^www.123.com$ //定義rewrite的條件,主機名(域名)不是www.123.com知足條件 RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L] //定義rewrite規則,當知足上面的條件時,這條規則纔會執行 </IfModule> </VirtualHost> /usr/local/apache2/bin/apachectl -M|grep -i rewrite //若無該模塊,須要編輯配置文件httpd.conf,刪除rewrite_module (shared) 前面的#
實際寫成這樣子:
<VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias abc.com www.example.com 2111.com.cn #<Directory /data/wwwroot/abc.com> # AllowOverride AuthConfig # AuthName "abc.com user auth" # AuthType Basic # AuthUserFile /data/.htpasswd # require valid-user # </Directory> <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^www.abc.com$ RewriteRule ^/(.*)$ http://www.abc.com/$1 [R=301,L] </IfModule> ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost>
檢測,重新加載
[root@aminglinux-01 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@aminglinux-01 ~]# /usr/local/apache2.4/bin/apachectl graceful [root@aminglinux-01 ~]#
檢測前先查看這個模塊有沒有起來 /usr/local/apache2.4/bin/apachectl -M|grep -i rewrite
若是沒有起來須要編輯配置文件httpd.conf
[root@aminglinux-01 ~]# vi /usr/local/apache2.4/conf/httpd.conf
搜索一下rewrite
LoadModule rewrite_module modules/mod_rewrite.so 找到這一行,把#號刪掉
在檢測rewrite模塊
[root@aminglinux-01 ~]# /usr/local/apache2.4/bin/apachectl -M|grep rewrite rewrite_module (shared) [root@aminglinux-01 ~]#
檢測,從新加載
Syntax OK [root@aminglinux-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
查看結果 -I 是隻看狀態碼
[root@aminglinux-01 ~]# curl -x192.168.245.128:80 2111.com.cn <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>301 Moved Permanently</title> </head><body> <h1>Moved Permanently</h1> <p>The document has moved <a href="http://www.abc.com/">here</a>.</p> </body></html> [root@aminglinux-01 ~]# curl -x192.168.245.128:80 2111.com.cn -I HTTP/1.1 301 Moved Permanently Date: Mon, 09 Oct 2017 14:08:44 GMT Server: Apache/2.4.27 (Unix) PHP/5.6.30 Location: http://www.abc.com/ Content-Type: text/html; charset=iso-8859-1 [root@aminglinux-01 ~]#
日誌格式,默認是common
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common
"%{User-Agent}i" 用戶代理
"%{Referer}i" 瀏覽器上一次訪問的網站是什麼
ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common
修改成
ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" combined
從新加載配置文件 -t,graceful
在訪問一下:[root@aminglinux-01 ~]# curl -x192.168.245.128:80 2111.com.cn -I
[root@aminglinux-01 ~]# cat /usr/local/apache2.4/logs/abc.com-access_log
原來common
[root@aminglinux-01 ~]# cat /usr/local/apache2.4/logs/abc.com-access_log 192.168.245.128 - - [09/Oct/2017:17:07:36 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 7 192.168.245.128 - - [09/Oct/2017:17:31:25 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 7 192.168.245.128 - - [09/Oct/2017:17:53:56 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 7 192.168.245.1 - - [09/Oct/2017:17:55:37 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.1 - - [09/Oct/2017:17:55:40 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.1 - - [09/Oct/2017:17:56:03 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.1 - - [09/Oct/2017:17:57:26 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.128 - - [09/Oct/2017:17:58:10 +0800] "GET HTTP://www.abc.com/ HTTP/1.1" 200 7 192.168.245.1 - - [09/Oct/2017:17:59:16 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.128 - - [09/Oct/2017:18:01:40 +0800] "GET HTTP://www.abc.com/ HTTP/1.1" 200 7 192.168.245.1 - - [09/Oct/2017:18:03:15 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.128 - - [09/Oct/2017:18:03:18 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 7 192.168.245.128 - - [09/Oct/2017:18:10:01 +0800] "GET HTTP://abc.com/ HTTP/1.1" 401 381 192.168.245.1 - - [09/Oct/2017:18:11:03 +0800] "GET /1.php HTTP/1.1" 401 381 192.168.245.1 - - [09/Oct/2017:18:13:17 +0800] "GET /1.php HTTP/1.1" 401 381 192.168.245.1 - aming [09/Oct/2017:18:14:54 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.1 - aming [09/Oct/2017:18:14:57 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.1 - aming [09/Oct/2017:18:15:40 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.1 - aming [09/Oct/2017:18:15:43 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.128 - aming [09/Oct/2017:18:16:26 +0800] "HEAD HTTP://abc.com/ HTTP/1.1" 200 - 192.168.245.128 - aming [09/Oct/2017:18:16:39 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 7 192.168.245.128 - aming [09/Oct/2017:18:16:45 +0800] "HEAD HTTP://abc.com/ HTTP/1.1" 200 - 192.168.245.128 - - [09/Oct/2017:22:08:34 +0800] "GET HTTP://2111.com.cn/ HTTP/1.1" 301 227 192.168.245.128 - - [09/Oct/2017:22:08:44 +0800] "HEAD HTTP://2111.com.cn/ HTTP/1.1" 301 - 192.168.245.128 - - [09/Oct/2017:22:11:47 +0800] "HEAD HTTP://2111.com.cn/ HTTP/1.1" 301 -
改成combined
[root@aminglinux-01 ~]# cat /usr/local/apache2.4/logs/abc.com-access_log 192.168.245.128 - - [09/Oct/2017:17:07:36 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 7 192.168.245.128 - - [09/Oct/2017:17:31:25 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 7 192.168.245.128 - - [09/Oct/2017:17:53:56 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 7 192.168.245.1 - - [09/Oct/2017:17:55:37 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.1 - - [09/Oct/2017:17:55:40 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.1 - - [09/Oct/2017:17:56:03 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.1 - - [09/Oct/2017:17:57:26 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.128 - - [09/Oct/2017:17:58:10 +0800] "GET HTTP://www.abc.com/ HTTP/1.1" 200 7 192.168.245.1 - - [09/Oct/2017:17:59:16 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.128 - - [09/Oct/2017:18:01:40 +0800] "GET HTTP://www.abc.com/ HTTP/1.1" 200 7 192.168.245.1 - - [09/Oct/2017:18:03:15 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.128 - - [09/Oct/2017:18:03:18 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 7 192.168.245.128 - - [09/Oct/2017:18:10:01 +0800] "GET HTTP://abc.com/ HTTP/1.1" 401 381 192.168.245.1 - - [09/Oct/2017:18:11:03 +0800] "GET /1.php HTTP/1.1" 401 381 192.168.245.1 - - [09/Oct/2017:18:13:17 +0800] "GET /1.php HTTP/1.1" 401 381 192.168.245.1 - aming [09/Oct/2017:18:14:54 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.1 - aming [09/Oct/2017:18:14:57 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.1 - aming [09/Oct/2017:18:15:40 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.1 - aming [09/Oct/2017:18:15:43 +0800] "GET /1.php HTTP/1.1" 404 203 192.168.245.128 - aming [09/Oct/2017:18:16:26 +0800] "HEAD HTTP://abc.com/ HTTP/1.1" 200 - 192.168.245.128 - aming [09/Oct/2017:18:16:39 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 7 192.168.245.128 - aming [09/Oct/2017:18:16:45 +0800] "HEAD HTTP://abc.com/ HTTP/1.1" 200 - 192.168.245.128 - - [09/Oct/2017:22:08:34 +0800] "GET HTTP://2111.com.cn/ HTTP/1.1" 301 227 192.168.245.128 - - [09/Oct/2017:22:08:44 +0800] "HEAD HTTP://2111.com.cn/ HTTP/1.1" 301 - 192.168.245.128 - - [09/Oct/2017:22:11:47 +0800] "HEAD HTTP://2111.com.cn/ HTTP/1.1" 301 - 192.168.245.128 - - [09/Oct/2017:22:36:01 +0800] "HEAD HTTP://2111.com.cn/ HTTP/1.1" 301 - "-" "curl/7.29.0" 192.168.245.128 - - [09/Oct/2017:22:36:05 +0800] "HEAD HTTP://2111.com.cn/ HTTP/1.1" 301 - "-" "curl/7.29.0"
<VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias abc.com www.example.com 2111.com.cn #<Directory /data/wwwroot/abc.com> # AllowOverride AuthConfig # AuthName "abc.com user auth" # AuthType Basic # AuthUserFile /data/.htpasswd # require valid-user # </Directory> <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^www.abc.com$ RewriteRule ^/(.*)$ http://www.abc.com/$1 [R=301,L] </IfModule> ErrorLog "logs/abc.com-error_log" SetEnvIf Request_URI ".*\.gif$" img SetEnvIf Request_URI ".*\.jpg$" img SetEnvIf Request_URI ".*\.png$" img SetEnvIf Request_URI ".*\.bmp$" img SetEnvIf Request_URI ".*\.swf$" img SetEnvIf Request_URI ".*\.js$" img SetEnvIf Request_URI ".*\.css$" img CustomLog "logs/abc.com-access_log" combined env=!img //表示符合img的請求都不會記錄到日誌裏。 </VirtualHost>
<VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias abc.com www.example.com 2111.com.cn #<Directory /data/wwwroot/abc.com> # AllowOverride AuthConfig # AuthName "abc.com user auth" # AuthType Basic # AuthUserFile /data/.htpasswd # require valid-user # </Directory> <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^www.abc.com$ RewriteRule ^/(.*)$ http://www.abc.com/$1 [R=301,L] </IfModule> ErrorLog "logs/abc.com-error_log" SetEnvIf Request_URI ".*\.gif$" img SetEnvIf Request_URI ".*\.jpg$" img SetEnvIf Request_URI ".*\.png$" img SetEnvIf Request_URI ".*\.bmp$" img SetEnvIf Request_URI ".*\.swf$" img SetEnvIf Request_URI ".*\.js$" img SetEnvIf Request_URI ".*\.css$" img CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/abc.com-access_%Y%m%d.log 86400" combined env=!img </VirtualHost>
用apache自帶分割工具rotatelogs,以天天進行日誌分割。-l的意思是用cst時間,不用-l是utc時間
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^www.abc.com$ RewriteRule ^/(.*)$ http://www.abc.com/$1 [R=301,L] </IfModule> <IfModule mod_expires.c> ExpiresActive on ExpiresByType image/gif "access plus 1 days" ExpiresByType image/jpeg "access plus 24 hours" ExpiresByType image/png "access plus 24 hours" ExpiresByType text/css "now plus 2 hour" ExpiresByType application/x-javascript "now plus 2 hours" ExpiresByType application/javascript "now plus 2 hours" ExpiresByType application/x-shockwave-flash "now plus 2 hours" ExpiresDefault "now plus 0 min" </IfModule> ErrorLog "logs/abc.com-error_log" SetEnvIf Request_URI ".*\.gif$" img SetEnvIf Request_URI ".*\.jpg$" img SetEnvIf Request_URI ".*\.png$" img SetEnvIf Request_URI ".*\.bmp$" img SetEnvIf Request_URI ".*\.swf$" img SetEnvIf Request_URI ".*\.js$" img SetEnvIf Request_URI ".*\.css$" img CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/abc.com-access_%Y%m%d.log 86400" combined env=!img
先看看這個模塊有沒有打開
[root@aminglinux-01 ~]# /usr/local/apache2.4/bin/apachectl -M |grep expire [root@aminglinux-01 ~]#
明顯沒有打開
編輯主配置文件:/usr/local/apache2.4/conf/httpd.conf
找到expire哪一行,把前面#號刪掉
[root@aminglinux-01 ~]# /usr/local/apache2.4/bin/apachectl -M |grep expire expires_module (shared) [root@aminglinux-01 ~]#