nginx同一主机部署多个应用

近日有一需求,需要在一台主机上用nginx部署2个php应用,分别是wordpress和wiki,探索了半天,终于部署好了,下面把过程记录下来。

1.   在nginx下创建vhosts目录,用以放置vhost文件。

mkdir vhosts

 

2.   修改nginx.conf的配置, 在http节点增加下面内容设置,用来包含vhosts里的配置文件

# vhosts files
    include /usr/local/nginx/vhosts/*;

 

3.  在vhosts下创建testwp.com.conf和duwiki.com.conf文件, 注意:这里的配置文件以conf结尾

testwp.com.conf

server{
        listen       80;
        server_name  www.testwp.com;

        location / {
            root   /Users/jiangzhiqiang/phpwork/wordpress;
            index  index.php index.html index.htm;
        }

 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /Users/jiangzhiqiang/phpwork/wordpress;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            # $document_root指前面的root路径
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
            include        fastcgi_params;
        }
    }

 

duwiki.com.conf

server {
        listen  80;
        server_name  www.duwiki.com;

        location / {
            root   /Users/jiangzhiqiang/phpwork/dokuwiki;
            index  index.php index.html index.htm;
        }
        
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
        #  
        location ~ \.php$ {  
            root           /Users/jiangzhiqiang/phpwork/dokuwiki;  
            fastcgi_pass   127.0.0.1:9000;  
            fastcgi_index  index.php;  
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  
            include        fastcgi_params;  
        }  
       
        # deny access to files, dokuwiki settings
        location ~ /(data|conf|bin|inc)/
        {
           deny all;
        }
}

 

4.  修改hosts文件

sudo vi /etc/hosts

127.0.0.1    www.testwp.com
127.0.0.1    www.duwiki.com

 

5.   重启 nginx 

sudo nginx -s reload

 

在浏览器url栏输入 www.testwp.com, 就访问到本地部署的wordpress应用,访问 www.duwiki.com,就访问到本地部署的 dokuwiki 应用。

你可能感兴趣的:(nginx)