由于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 8080;
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 127.0.0.1:9000;
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 *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/etc/httpd/docs/dummy-host.example.com"
ServerName www.xwf.com
ProxyPass / http://www.xwf.com:8080/
ProxyPassReverse / http://www.xwf.com:8080/
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的,那就成功了。