Linux LAMP務器安裝
Lamp服務器安裝
wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.37.tar.bz2
tar工具解壓httpd包
yum -y install bzip2 安裝bzip2工具
tar -xjvf httpd-2.4.37.tar.bz2 解壓
yum install apr apr-devel apr-util apr-util-devel -y 安裝apr相關移植庫模塊
yum -y install pcre-devel
yum install gcc -y 安裝gcc
yum install gcc-c++ -y
安裝ARP
wget http://apache.website-solution.net//apr/apr-1.6.5.tar.bz2
yum -y install pcre-devel
預編譯Apache,啓用rewrite規則,啓用動態加載庫
./configure --prefix=/usr/local/apache2/ --enable-rewrite --enable-so
make 編譯
make install 安裝
/usr/local/apache2/bin/apachectl start 啓動Apache服務
setenforce 0 關閉防火牆
systemctl stop firewalld.service
http://10.206.35.197 測試訪問
Apache虛擬主機企業應用
1- 基於單IP多個socket端口
2- 基於多個IP地址一個端口
3- 基於單IP一個端口不一樣域名
<VirtualHost *:80> 虛擬主機配置起始
ServerAdmin webmaster@dummy-host.example.com 管理員郵件
DocumentRoot "/usr/local/apache2//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 訪問日誌路徑及文件名
1-建立www.jf1.com及www.jf2.com發佈目錄,重啓目錄,分別建立index
mkdir -p /usr/local/apache2/htdocs/{jf1,jf2}/
/usr/local/apache2/bin/apachectl restart
echo "<h1>www.jf1.com Pages</h1>" >/usr/local/apache2/htdocs/jf1/index.html
echo "<h1>www.jf2.com Pages</h1>" >/usr/local/apache2/htdocs/jf2/index.html
2-在客戶機上設置host將www.jf1com/www.jf2.com與10.206.35.197綁定
Apache rewrite規則實戰
添加rewrite功能
1- 添加rewrite模塊,基於源碼安裝,指定參數--enable-rewrite
2- 動態添加模塊,以DSO模式安娜裝,利用模塊源碼和Apach apxs工具完成rewrite模塊的添加
在httpd.conf的全局配置段或者虛擬主機配置段設置以下命令,開起rewrite
vi /usr/local/apache2/conf/httpd.conf
RewriteEngine on
1- Apache rewrite結尾標識符,用於rewrite規則末尾
R[=code](force redirect) 強制外部重定向
G(force URL to be gone) 強制URL爲gone,返回410HTTP狀態碼
P(force proxy) 強制使用代理轉發
L(last rul) 匹配當前規則爲最後一條匹配規則,中止匹配後續規則
N(next round) 從新從第一條規則開始匹配
C(chained with next rule) 與下一條規則關聯
T=MIME-type(force MIME type) 強制MIME類型
NC(no case) 不區分大小寫
2- Apache rewrite規則經常使用表達式,用於匹配參數、字符呂及過濾設置
. 匹配任何單字符
[word] 匹配字符串word
[^word] 不匹配字符串word
jfedu|jfteach 可選擇字符串jfedu|jfteach
? 匹配0到1個字符php
- 匹配0到多個字符
- 匹配1到多個字符
^ 字符串開始標誌
$ 字符串結束標誌
\n 轉義符標誌
3- Apache rewrite變量,用於匹配HTTP請求信息、瀏覽器主機名、URL
HTTP headers:HTTP_USER_AGENT,HTTP_REFERER,HTTP_COOKIE,HTTP_HOST,HTTP_ACCEPT;
connection&request:REMOTE_ADDR,QUERY_STRING
server internals:DOCUMENT_ROOT,SERVER_PORT,SERVER_PROTOCOL;
system stuff:TIME_YEAR,TIME_MON,TIME_DAY.
HTTP_USER_AGENT 用戶使用的代理,例如瀏覽器
HTTP_REFERER 告知服務器,從哪一個面來訪問的
HTTP_COOKIE 客戶端緩存,主要用於存儲用戶和密碼等信息
HTTP_HOST 匹配服務器ServerName域名
HTTP_ACCETP 客戶端的瀏覽器支持的MIME類型
REMODE_ADDR 客戶端的IP地址
QUERY_STRING URL中訪問的字符串
DOCUMENT_ROOT 服務器發佈目錄
SERVER_PORT 服務器端口
SERVER_PROTOCOL 服務器端協議R
4- Apache rewrite規則,如下配置在httpd.conf或者vhosts.conf
將jfedu.net跳轉至www.jfedu.net
RewriteEngine on 啓用rewrite引擎
RewriteCond %{HTTP_HOST} ^jfedu.net[NC] 匹配jfedu.net開頭的域名,NC忽略大小寫
RewriteRule ^/(.)$http://www.jfedu.net/$1 [L] (.)表示任意字符串,$1表示引用(.)的任意內容
將www.jf1.com\www.jf2.com\jfedu.net跳轉至www.jfedu.net.OR表示或者
RewriteEngine on
RewriteCond % {HTTP_HOST} www.jf1.com [NC,OR]
RewriteCond % {HTTP_HOST} www.jf2.com [NC,OR]
RewriteCond % {HTTP_HOST} ^jfedu.net [NC]
RewriteRule ^/(.)$ http://www.jfedu.net/$1 [L]
訪問www,jfedu.net首頁,跳轉到www.jfedu.net/newindex/,R=301 表示永久從新定向
RewriteEngine on
RewriteRule ^/$ http://www.jfedu.net/newindex/ [L,R=301]
訪問/netindex/plus/view.php?aid=71跳轉至http://www.jfedu.net/linux/
RewriteEngine on
RewriteCond % {QUERY_STRING} ^tid=(.+)$ [NC]
RewriteRule ^/forum.php$ /jfedu/thread -new - %1.html? [R=301,L]
訪問www.jfedu.net首頁,內容訪問www.jfedu.net/newindex/ 可是瀏覽器URL地址不改變
RewriteEngine on
RewriteCond % {HTTP_HOST} ^www.jfedu.net [NC]
RewriteRule ^ /s /newindex/ [L]
訪問/forun.php?tid=107258跳轉至/jfedu/thread-new-107258.html
RewriteEngine on
RewriteCond % {QUERY_STRING} ^tid = (.+$ [NC]
RewriteRule ^/form.pho$ /jfedu/thread-new-%1.html? [R=301,L]
訪問/xxx/123456跳轉至/www?id=123456
RewriteEngine on
RewriteRule ^/(.+)$1?id=$2 [L,R=301]
判斷是否使用移動端訪問網站,是則跳轉到m.jfedu.net
RewriteEngine on
RewriteCond % {HTTP_USER_AGEN} ^iPhone [NC,OR]
RewriteCond % {HTTP_USER_AGEN} ^Android [NC,OR]
RewriteCond % {HTTP_USERAGEN} ^WAP [NC]
RewriteRule ^ /$ http://m.jfedu.net/index.html [L,R=301]
RewriteRule ^/(.*)/$ http://m.jfedu.net/$1 [L,R=301]訪問/10690jfedu/123跳轉至/index.php?tid/10690/iteams=123,[0-9]表示任意一個數字,+表示多個,(.+)表示任何多個字符RewriteEngine onRewriteRule ^/([0-9]+)/jfedu/(.)$ /index.php?tid/$1/itmes=$2 [L,R=301]