由于nginx是一個輕量級的伺服器,配置相對Apache比較簡單,而且支援負載均衡、分布式、虛拟主機、反向代理等等,是以一直以來對nginx情有獨鐘。
這次搭建web開發環境是在Linux下做的,直接從軟體倉庫中安裝的php,nginx,Apache,mysql,本來安裝好nginx,mysql,php就可以用了,但是因為從軟體倉庫中安裝的緣故,每次啟動必須要root使用者權限,于是就把所有的權限都改了,這樣可以以普通使用者啟動nginx和php-fpm,但是預設的80端口必須以root身份啟動,于是我就把預設端口改成了8080,但是問題又來了,每次都要輸入8080這就比較煩了,是以就想到端口轉發,用Apache監聽80端口,然後全部轉發給8080,這樣又繞了一個彎,愛折騰,沒辦法,也算學了個新東西,下面開始介紹,其實也沒什麼,nginx+Apache端口轉發一大堆的教程,隻不過一般都是nginx做前端,Apache做後端,隻要你知道怎麼配置Apache虛拟主機,這就不是個問題。
Nginx的配置按照教程走一遍就行了,端口配成8080:
server {
#listen 80;
listen ;
server_name localhost xwf.com www.xwf.com;
root /home/xwf/program/web;
index index.html index.htm index.php ;
#charset koi8-r;
access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
#root /home/xwf/program/web;
index index.html index.htm index.php ;
# index index.php index.html index.htm ;
}
......
location ~ \.php$ {
fastcgi_pass ..:;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
......
下面我們來看Apache的配置,首先按照教程,開啟Apache的端口轉發功能,找到如下三行檔案,去掉注釋:
LoadModule proxy_module modules/mod_proxy.so #啟用轉發子產品
LoadModule proxy_http_module modules/mod_proxy.so #啟用http轉發子產品
Include conf/extra/httpd-vhosts.conf #啟用獨立配置檔案支援
去掉注釋後我們來編輯extra檔案夾下的配置檔案httpd-vhosts.conf:
<VirtualHost *:>
ServerAdmin [email protected].example.com
DocumentRoot "/etc/httpd/docs/dummy-host.example.com"
ServerName www.xwf.com
ProxyPass / http://www.xwf.com:/
ProxyPassReverse / http://www.xwf.com:/
ServerAlias xwf.com
ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
</VirtualHost>
配置完成以後,輸入域名,顯示的不是Apache的歡迎頁面而是nginx的,那就成功了。